siplasplas
A library for C++ reflection and introspection
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...
 

Detailed Description

Assertion utilities.

Macro Definition Documentation

#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.
1 SIPLASPLAS_ASSERT("The answer to the universe and everything must be...", answer == 42);
Examples:
examples/reflection/static/serialization.cpp.
#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.
1 int a = -3141592;
2 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&)

#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.
1 int a = -3141592;
2 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&)

#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:

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

So for an example assertion like this:

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

returns a diagnostic like the following:

1 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&)

#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.
1 int a = 3141592;
2 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/static/serialization.cpp.
#define SIPLASPLAS_ASSERT_FALSE (   ...)

Defines a false assertion.

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

Parameters
...Assertion boolean expression
1 SIPLASPLAS_ASSERT_FALSE(std::is_integral<float>()); // "Expected 'std::is_integral<float>()' to be false. Got true instead"
#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.
1 int a = 3141592;
2 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&)

#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.
1 int a = 3141592;
2 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&)

#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.
1 int a = 42;
2 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&)

#define SIPLASPLAS_ASSERT_TRUE (   ...)

Defines a true assertion.

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

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