NAP
Public Member Functions | List of all members
ErrorState Class Reference

#include </opt/build/repo/nap/utility/src/utility/errorstate.h>

Public Member Functions

template<typename T >
bool check (bool successCondition, T &&errorMessage)
 
template<typename... Args>
bool check (bool successCondition, const char *format, Args &&... args)
 
template<typename T >
void fail (T &&errorMessage)
 
template<typename... Args>
void fail (const char *format, Args &&... args)
 
const std::string toString () const
 
bool hasErrors () const
 

Description

ErrorState is a class that can be used to maintain an error state over (nested) function calls. It's useful to be able to return detailed error information from somewhere deep down the stack. It's kind of a poor-man's exception handling in that regard.

Member Function Documentation

◆ check() [1/2]

bool check ( bool  successCondition,
const char *  format,
Args &&...  args 
)

Same as non-templated check(), but allows for easy formatting of error messages.

if (!errorState.check(file != nullptr, "Unable to load file: %s", file.c_str()))
return false;
Parameters
successConditionThe condition to check (i.e. true/false)
formatThe error message that that is formatted
argsthe formatting arguments
Returns
Whether the condition evaluated to true (i.e. success) or not

◆ check() [2/2]

bool check ( bool  successCondition,
T &&  errorMessage 
)

Check whether the specified condition evaluates to true and adds an error message to the state if not. Often checks are performed on initialization to ensure the resource is valid, for example:

if (!errorState.check(scene != nullptr, "Unable to load scene"))
return false;
Parameters
successConditionThe condition to check (i.e. true/false)
errorMessageThe error message that belongs to the 'fail' state
Returns
Whether the condition evaluated to true (i.e. success) or not

◆ fail() [1/2]

void fail ( const char *  format,
Args &&...  args 
)

Same as non-templated fail(), but allows for easy formatting of error messages

error.fail("unable to read file: %s", file.c_str());
return false;
Parameters
formatthe error message that is formatted
argsthe format arguments

◆ fail() [2/2]

void fail ( T &&  errorMessage)

Add a failure message to the stack. Useful in situations where you already know that you've failed, in which case the check() function is redundant.

error.fail("missing data");
return false;
Parameters
errorMessageThe error message, can be a string literal, std::string etc.

◆ hasErrors()

bool hasErrors ( ) const
Returns
True if there are any errors, false when everything is okay.

◆ toString()

const std::string toString ( ) const

Format the error state of this object into a human-readable message

Returns
The full error message
nap::icon::file
constexpr const char * file
Definition: imguiservice.h:47
nap::icon::error
constexpr const char * error
Definition: imguiservice.h:55