siplasplas
A library for C++ reflection and introspection
All Classes Functions Variables Typedefs Enumerations Friends Modules Pages
Assert

Assertion utilities. More...

Classes

class  cpp::AssertException
 Represents an assertion error. More...
 
class  cpp::AssertExpression
 Implements a siplasplas assertion. More...
 
class  cpp::DummyAssertExpression
 Represents an assert expression when SIPLASPLAS_ASSERT() macros are disabled. More...
 

Macros

#define SIPLASPLAS_ASSERT(...)
 Defines an assertion expression. More...
 
#define SIPLASPLAS_ASSERT_COMP_IMPL(a, b, op, wording)
 Implements a comparison assertion. More...
 
#define SIPLASPLAS_ASSERT_EQ(a, b)
 Defines an equal to assertion. More...
 
#define SIPLASPLAS_ASSERT_NE(a, b)
 Defines a not equal to assertion. More...
 
#define SIPLASPLAS_ASSERT_BT(a, b)
 Defines a bigger than assertion. More...
 
#define SIPLASPLAS_ASSERT_LT(a, b)
 Defines a less than assertion. More...
 
#define SIPLASPLAS_ASSERT_BE(a, b)
 Defines a bigger or equal assertion. More...
 
#define SIPLASPLAS_ASSERT_LE(a, b)
 Defines a less or equal assertion. More...
 
#define SIPLASPLAS_ASSERT_TRUE(...)
 Defines a true assertion. More...
 
#define SIPLASPLAS_ASSERT_FALSE(...)
 Defines a false assertion. More...
 
#define SIPLASPLAS_STATICASSERT_EQ(expr, expect)
 Defines an static equal to assertion. More...
 
#define SIPLASPLAS_STATICASSERT_NE(expr, expect)
 Defines an static not equal to assertion. More...
 
#define SIPLASPLAS_STATICASSERT_BT(expr, expect)
 Defines an static bigger than assertion. More...
 
#define SIPLASPLAS_STATICASSERT_LT(expr, expect)
 Defines an static less than assertion. More...
 
#define SIPLASPLAS_STATICASSERT_BE(expr, expect)
 Defines an static bigger or equal to assertion. More...
 
#define SIPLASPLAS_STATICASSERT_LE(expr, expect)
 Defines an static less or equal to assertion. More...
 
#define SIPLASPLAS_STATICASSERT_TRUE(expression)
 Defines an static true assertion. More...
 
#define SIPLASPLAS_STATICASSERT_FALSE(expression)
 Defines an static false assertion. More...
 

Detailed Description

Assertion utilities.

Macro Definition Documentation

§ SIPLASPLAS_ASSERT

#define SIPLASPLAS_ASSERT (   ...)

Defines an assertion expression.

When NDEBUG is not defined, this macro is defined to instance a cpp::AssertExpression object. Else it instances a cpp::DummyAssertExpression object which does nothing.

Parameters
__VA_ARGS__Parameters for the underlying assertion expression constructor. See cpp::AssertExpression and cpp::DummyAssertExpression.
SIPLASPLAS_ASSERT("The answer to the universe and everything must be...", answer == 42);
Examples:
examples/reflection/static/serialization.cpp.

§ SIPLASPLAS_ASSERT_BE

#define SIPLASPLAS_ASSERT_BE (   a,
 
)

Defines a bigger or equal assertion.

The assertion success if a is bigger or equal to b. Fails otherwise. The comparison is done by means of operator>=(a, b).

Parameters
aFirst value to compare.
bSecond value to compare.
int a = -3141592;
SIPLASPLAS_ASSERT_BT(a, 42); // "Expected 'a' (-3141592) bigger or equal to '42' (42)"

Compared values must be printable by the fmt library. This can be achieved by implementing operator<<(std::ostream&, const T&)

§ SIPLASPLAS_ASSERT_BT

#define SIPLASPLAS_ASSERT_BT (   a,
 
)

Defines a bigger than assertion.

The assertion success if a is bigger than b. Fails otherwise. The comparison is done by means of operator>(a, b).

Parameters
aFirst value to compare.
bSecond value to compare.
int a = -3141592;
SIPLASPLAS_ASSERT_BT(a, 42); // "Expected 'a' (-3141592) bigger than '42' (42)"

Compared values must be printable by the fmt library. This can be achieved by implementing operator<<(std::ostream&, const T&)

§ SIPLASPLAS_ASSERT_COMP_IMPL

#define SIPLASPLAS_ASSERT_COMP_IMPL (   a,
  b,
  op,
  wording 
)

Implements a comparison assertion.

This macro builds a detailed assertion expression for expressions that compare two values using an specific comparison operator. The resulting diagnostic message is of the form:

Expected '<left expression>' (<left expression value>) <operator wording> '<right expression>' (<right expression value>)

So for an example assertion like this:

void bad_memcpy(void* to, void* from, int bytes)
{
constexpr int MIN_BYTES = 0;
SIPLASPLAS_ASSERT_COMP_IMPL(bytes, MIN_BYTES, >=, "bigger or equal to");
}
char source[1024];
char dest[1024];
bad_memcpy(buffer, dest, -8);

returns a diagnostic like the following:

Expected 'bytes' (-8) bigger or equal to 'MIN_BYTES' (0)

This macro is used by siplasplas to implement comparison assertion macros for different comparison operators.

Compared values must be printable by the fmt library. This can be achieved by implementing operator<<(std::ostream&, const T&)

§ SIPLASPLAS_ASSERT_EQ

#define SIPLASPLAS_ASSERT_EQ (   a,
 
)

Defines an equal to assertion.

The assertion success if a and b are equal. Fails otherwise. Equality is tested using operator==(a, b).

Parameters
aFirst value to compare.
bSecond value to compare.
int a = 3141592;
SIPLASPLAS_ASSERT_EQ(a, 42); // "Expected 'a' (3141592) equal to '42' (42)"

Compared values must be printable by the fmt library. This can be achieved by implementing operator<<(std::ostream&, const T&)

Examples:
examples/reflection/dynamic/reflection.cpp, and examples/reflection/static/serialization.cpp.

§ SIPLASPLAS_ASSERT_FALSE

#define SIPLASPLAS_ASSERT_FALSE (   ...)

Defines a false assertion.

The assertion success if the expression evaluates to false. Fails otherwise.

Parameters
...Assertion boolean expression
SIPLASPLAS_ASSERT_FALSE(std::is_integral<float>()); // "Expected 'std::is_integral<float>()' to be false. Got true instead"

§ SIPLASPLAS_ASSERT_LE

#define SIPLASPLAS_ASSERT_LE (   a,
 
)

Defines a less or equal assertion.

The assertion success if a is less or equal to b. Fails otherwise. Comparison is done by means of operator<=(a, b).

Parameters
aFirst value to compare.
bSecond value to compare.
int a = 3141592;
SIPLASPLAS_ASSERT_EQ(a, 42); // "Expected 'a' (3141592) less or equal to '42' (42)"

Compared values must be printable by the fmt library. This can be achieved by implementing operator<<(std::ostream&, const T&)

§ SIPLASPLAS_ASSERT_LT

#define SIPLASPLAS_ASSERT_LT (   a,
 
)

Defines a less than assertion.

The assertion success if a is less than b. Fails otherwise. Comparison is done by means of operator<(a, b).

Parameters
aFirst value to compare.
bSecond value to compare.
int a = 3141592;
SIPLASPLAS_ASSERT_EQ(a, 42); // "Expected 'a' (3141592) less than '42' (42)"

Compared values must be printable by the fmt library. This can be achieved by implementing operator<<(std::ostream&, const T&)

§ SIPLASPLAS_ASSERT_NE

#define SIPLASPLAS_ASSERT_NE (   a,
 
)

Defines a not equal to assertion.

The assertion success if a and b are not equal. Fails otherwise. Inequality is tested using operator!=(a, b).

Parameters
aFirst value to compare.
bSecond value to compare.
int a = 42;
SIPLASPLAS_ASSERT_NE(a, 42); // "Expected 'a' (42) not equal to '42' (42)"

Compared values must be printable by the fmt library. This can be achieved by implementing operator<<(std::ostream&, const T&)

§ SIPLASPLAS_ASSERT_TRUE

#define SIPLASPLAS_ASSERT_TRUE (   ...)

Defines a true assertion.

The assertion success if the expression evaluates to true. Fails otherwise.

Parameters
...Assertion boolean expression
SIPLASPLAS_ASSERT_TRUE(std::is_integral<std::string>()); // "Expected 'std::is_integral<std::string>()' to be true. Got false instead"

§ SIPLASPLAS_STATICASSERT_BE

#define SIPLASPLAS_STATICASSERT_BE (   expr,
  expect 
)

Defines an static bigger or equal to assertion.

Compilation fails if the first operand (expression) is not bigger or equal to the second (expected). The comparison (operator>=) must be a constant expression.

§ SIPLASPLAS_STATICASSERT_BT

#define SIPLASPLAS_STATICASSERT_BT (   expr,
  expect 
)

Defines an static bigger than assertion.

Compilation fails if the first operand (expression) is not bigger than the second (expected). The bigger than comparison (operator>) must be a constant expression.

§ SIPLASPLAS_STATICASSERT_EQ

#define SIPLASPLAS_STATICASSERT_EQ (   expr,
  expect 
)

Defines an static equal to assertion.

Compilation fails if the first operand (expression) is not equal to the second (expected). The equality comparison (operator==) must be a constant expression.

§ SIPLASPLAS_STATICASSERT_FALSE

#define SIPLASPLAS_STATICASSERT_FALSE (   expression)

Defines an static false assertion.

Compilation fails if the expression does not evauate to false. The expression must be a constant expression.

§ SIPLASPLAS_STATICASSERT_LE

#define SIPLASPLAS_STATICASSERT_LE (   expr,
  expect 
)

Defines an static less or equal to assertion.

Compilation fails if the first operand (expression) is not less or equal to the second (expected). The comparison (operator<=) must be a constant expression.

§ SIPLASPLAS_STATICASSERT_LT

#define SIPLASPLAS_STATICASSERT_LT (   expr,
  expect 
)

Defines an static less than assertion.

Compilation fails if the first operand (expression) is not less than the second (expected). The less than comparison (operator<) must be a constant expression.

§ SIPLASPLAS_STATICASSERT_NE

#define SIPLASPLAS_STATICASSERT_NE (   expr,
  expect 
)

Defines an static not equal to assertion.

Compilation fails if the first operand (expression) is equal to the second (expected). The inequality comparison (operator!=) must be a constant expression.

§ SIPLASPLAS_STATICASSERT_TRUE

#define SIPLASPLAS_STATICASSERT_TRUE (   expression)

Defines an static true assertion.

Compilation fails if the expression does not evauate to true. The expression must be a constant expression.