Initial commit, version 0.1.0.
[libfsafe.git] / libfsafe / assert.hxx
blob85f828668c2e29626247451b4090256c4eecf586
1 /**
2 * @file
4 * Contains declarations of assertion macros.
5 */
7 #ifndef LIBFSAFE_ASSERT_HXX
8 #define LIBFSAFE_ASSERT_HXX
10 #include <libsex/utility.hxx>
11 #include <libfsafe/AssertionFailure.hxx>
12 #include <libfsafe/PreconditionViolation.hxx>
13 #include <libfsafe/PostconditionViolation.hxx>
15 /**
16 * General assertion macro.
18 * There are assertion macros for specific purposes.
19 * This one should only be used for internal checks.
21 * @param expr Condition to be asserted.
22 * @throws libfsafe::AssertionFailure if @a expr is false.
23 * @see ASSERT_PRE
24 * @see ASSERT_POST
26 #define ASSERT(expr) \
27 if (!(expr)) { \
28 throw libsex::formatted<libfsafe::AssertionFailure>(__FILE__, __LINE__, #expr); \
31 /**
32 * Macro to assert a precondition.
34 * To be used to ensure that a method/function is called
35 * the right way (e.g. parameter values, internal state).
37 * In other words, assert that the callee fulfilled its
38 * part of the contract.
40 * @param expr Condition to be asserted.
41 * @throws libfsafe::PreconditionViolation if @a expr is false.
42 * @see ASSERT
43 * @see ASSERT_POST
45 #define ASSERT_PRE(expr) \
46 if (!(expr)) { \
47 throw libsex::formatted<libfsafe::PreconditionViolation>(__FILE__, __LINE__, #expr); \
50 /**
51 * Macro to assert a postcondition.
53 * To be used to ensure that the result of a
54 * function/method is correct, i.e. the callee fulfilled
55 * its part of the contract.
57 * @param expr Condition to be asserted.
58 * @throws libfsafe::PostconditionViolation if @a expr is false.
59 * @see ASSERT_PRE
60 * @see ASSERT
62 #define ASSERT_POST(expr) \
63 if (!(expr)) { \
64 throw libsex::formatted<libfsafe::PostconditionViolation>(__FILE__, __LINE__, #expr); \
67 #endif