siplasplas
A library for C++ reflection and introspection
|
Implements a siplasplas assertion. More...
#include <siplasplas/utility/assert.hpp>
Public Member Functions | |
AssertExpression (bool expressionResult, const std::string &message, const std::string &file, std::size_t line) | |
Initializes an assertion expression. More... | |
~AssertExpression () noexcept(false) | |
Assertion destructor. More... | |
AssertExpression & | onFailure (const std::function< void()> &callback) |
Specifies an action to execute when the assertion fails. More... | |
Implements a siplasplas assertion.
Siplasplas implements assertions as instances of AssertExpression class. An assert expression object takes the result (true or false) of the assertion, its source location (file, line) and a detailed message. The assertion is evaluated during the destruction of the assertion object. If the assertion failed (i.e. the value of the result was false) the AssertExpression destructor throws an AssertException exception.
cpp::AssertExpression::AssertExpression | ( | bool | expressionResult, |
const std::string & | message, | ||
const std::string & | file, | ||
std::size_t | line | ||
) |
Initializes an assertion expression.
expressionResult | Result of the assertion expression evaluation. True if the assertion passed, false if the assertion failed. |
message | Asseertion error message. |
file | Line where the assertion is defined. |
line | File where the assertion is defined. |
|
noexcept |
Assertion destructor.
The assertion result value is checked in the destructor body. If the result was true the assertion is considered passed and the program continues. If the result is false, the onFailure() callback is called (if any) and then an AssertExpression is thrown with detailed information about the assertion failure (Assertion message, location, etc). The assertion stack trace is logged to the error log (See cpp::exception()).
AssertExpression& cpp::AssertExpression::onFailure | ( | const std::function< void()> & | callback | ) |
Specifies an action to execute when the assertion fails.
This function allows to install a callback on the assertion so an special action can be taken when the assertion fails. The callback is unique, i.e. two consecutive calls to onFailure() overwrite the assigned callback.
callback | Function to execute when the assertion fails. |