KASSERT  0.0.1
Karlsruhe Assertion Library
kassert.hpp File Reference

Macros for asserting runtime checks. More...

#include <iostream>
#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>
#include "kassert/internal/assertion_macros.hpp"
#include "kassert/internal/expression_decomposition.hpp"
#include "kassert/internal/logger.hpp"

Go to the source code of this file.

Classes

struct  kassert::internal::SourceLocation
 Describes a source code location. More...
 
class  kassert::KassertException
 The default exception type used together with THROWING_KASSERT. Reports the erroneous expression together with a custom error message. More...
 

Namespaces

namespace  kassert::assert
 Assertion levels.
 

Macros

#define KASSERT_ASSERTION_LEVEL_KTHROW   10
 Assertion level for exceptions if exception mode is disabled.
 
#define KASSERT_ASSERTION_LEVEL_NORMAL   30
 Default assertion level. This level is used if no assertion level is specified.
 
#define KASSERT_ASSERTION_LEVEL   KASSERT_ASSERTION_LEVEL_NORMAL
 Default assertion level to kassert::assert::normal if not set explicitly.
 
#define KASSERT(...)
 Assertion macro. Accepts between one and three parameters. More...
 
#define THROWING_KASSERT(...)
 Macro for throwing exceptions. Accepts between one and three parameters. More...
 
#define THROWING_KASSERT_SPECIFIED(expression, message, exception_type, ...)    KASSERT_KASSERT_HPP_THROWING_KASSERT_CUSTOM_IMPL(expression, exception_type, message, ##__VA_ARGS__)
 Macro for throwing custom exception. More...
 
#define KASSERT_ENABLED(level)   level <= KASSERT_ASSERTION_LEVEL
 Checks if a assertion of the given level is enabled. This is controlled by the CMake option KASSERT_ASSERTION_LEVEL. This is the macro version of assertion_enabled for use in the preprocessor. More...
 

Functions

std::string kassert::internal::build_what (std::string const &expression, SourceLocation const where, std::string const &message)
 Builds the description for an exception. More...
 
constexpr bool kassert::internal::assertion_enabled (int level)
 Checks if a assertion of the given level is enabled. This is controlled by the CMake option KASSERT_ASSERTION_LEVEL. More...
 
bool kassert::internal::evaluate_and_print_assertion (char const *type, bool result, SourceLocation const &where, char const *expr_str)
 Evaluates an assertion that could not be decomposed (i.e., expressions that use && or ||). If the assertion fails, prints an error describing the failed assertion. More...
 
bool kassert::internal::evaluate_and_print_assertion (char const *type, Expression &&expr, SourceLocation const &where, char const *expr_str)
 Evaluates an assertion expression. If the assertion fails, prints an error describing the failed assertion. More...
 

Variables

constexpr int kassert::assert::kthrow = KASSERT_ASSERTION_LEVEL_KTHROW
 Assertion level for exceptions if exception mode is disabled.
 
constexpr int kassert::assert::normal = KASSERT_ASSERTION_LEVEL_NORMAL
 Default assertion level. This level is used if no assertion level is specified.
 

Detailed Description

Macros for asserting runtime checks.

Macro Definition Documentation

◆ KASSERT_ENABLED

#define KASSERT_ENABLED (   level)    level <= KASSERT_ASSERTION_LEVEL

Checks if a assertion of the given level is enabled. This is controlled by the CMake option KASSERT_ASSERTION_LEVEL. This is the macro version of assertion_enabled for use in the preprocessor.

Parameters
levelThe level of the assertion.
Returns
Whether the assertion is enabled.

Function Documentation

◆ assertion_enabled()

constexpr bool kassert::internal::assertion_enabled ( int  level)
constexpr

Checks if a assertion of the given level is enabled. This is controlled by the CMake option KASSERT_ASSERTION_LEVEL.

Parameters
levelThe level of the assertion.
Returns
Whether the assertion is enabled.

◆ build_what()

std::string kassert::internal::build_what ( std::string const &  expression,
SourceLocation const  where,
std::string const &  message 
)
inline

Builds the description for an exception.

Parameters
expressionExpression that caused this exception to be thrown.
whereSource code location where the exception was thrown.
messageUser message describing this exception.
Returns
The description of this exception.

◆ evaluate_and_print_assertion() [1/2]

bool kassert::internal::evaluate_and_print_assertion ( char const *  type,
bool  result,
SourceLocation const &  where,
char const *  expr_str 
)
inline

Evaluates an assertion that could not be decomposed (i.e., expressions that use && or ||). If the assertion fails, prints an error describing the failed assertion.

Parameters
typeActual type of this check. In exception mode, this parameter has always value ASSERTION, otherwise it names the type of the exception that would have been thrown.
resultAssertion expression result to be checked.
whereSource code location of the assertion.
expr_strStringified assertion expression.
Returns
Result of the assertion. If true, the assertion was triggered and the program should be halted.

◆ evaluate_and_print_assertion() [2/2]

bool kassert::internal::evaluate_and_print_assertion ( char const *  type,
Expression &&  expr,
SourceLocation const &  where,
char const *  expr_str 
)
inline

Evaluates an assertion expression. If the assertion fails, prints an error describing the failed assertion.

Parameters
typeActual type of this check. In exception mode, this parameter has always value ASSERTION, otherwise it names the type of the exception that would have been thrown.
exprAssertion expression to be checked.
whereSource code location of the assertion.
expr_strStringified assertion expression.
Returns
Result of the assertion. If true, the assertion was triggered and the program should be halted.