libfsafe  0.1.1
eases writing fail-safe code (C++)
/home/ptr/dev/dev-libs/libfsafe/libfsafe/assert.hxx
Go to the documentation of this file.
00001 /**
00002  * @file
00003  *
00004  * Contains declarations of assertion macros.
00005  */
00006 
00007 #ifndef LIBFSAFE_ASSERT_HXX
00008 #define LIBFSAFE_ASSERT_HXX
00009 
00010 #include <libsex/utility.hxx>
00011 #include <libfsafe/AssertionFailure.hxx>
00012 #include <libfsafe/PreconditionViolation.hxx>
00013 #include <libfsafe/PostconditionViolation.hxx>
00014 
00015 /**
00016  * General assertion macro.
00017  *
00018  * There are assertion macros for specific purposes.
00019  * This one should only be used for internal checks.
00020  *
00021  * @param expr Condition to be asserted.
00022  * @throws libfsafe::AssertionFailure if @a expr is false.
00023  * @see ASSERT_PRE
00024  * @see ASSERT_POST
00025  */
00026 #define ASSERT(expr) \
00027 if (!(expr)) { \
00028         throw libsex::formatted<libfsafe::AssertionFailure>(__FILE__, __LINE__, #expr); \
00029 }
00030 
00031 /**
00032  * Macro to assert a precondition.
00033  *
00034  * To be used to ensure that a method/function is called
00035  * the right way (e.g. parameter values, internal state).
00036  *
00037  * In other words, assert that the callee fulfilled its
00038  * part of the contract.
00039  *
00040  * @param expr Condition to be asserted.
00041  * @throws libfsafe::PreconditionViolation if @a expr is false.
00042  * @see ASSERT
00043  * @see ASSERT_POST
00044  */
00045 #define ASSERT_PRE(expr) \
00046 if (!(expr)) { \
00047         throw libsex::formatted<libfsafe::PreconditionViolation>(__FILE__, __LINE__, #expr); \
00048 }
00049 
00050 /**
00051  * Macro to assert a postcondition.
00052  *
00053  * To be used to ensure that the result of a
00054  * function/method is correct, i.e. the callee fulfilled
00055  * its part of the contract.
00056  *
00057  * @param expr Condition to be asserted.
00058  * @throws libfsafe::PostconditionViolation if @a expr is false.
00059  * @see ASSERT_PRE
00060  * @see ASSERT
00061  */
00062 #define ASSERT_POST(expr) \
00063 if (!(expr)) { \
00064         throw libsex::formatted<libfsafe::PostconditionViolation>(__FILE__, __LINE__, #expr); \
00065 }
00066 
00067 #endif
 All Classes Files Defines