1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| #ifndef __CONTRACT_HPP
#define __CONTRACT_HPP
#include <Uncopyable.hpp>
#include <type_traits>
#include <cassert>
/*!
* \macro require
* \brief Vérifie la validité d'une précondition.
*/
#if defined(CONTRACT_NO_PRECONDITION) || defined(CONTRACT_NO_CHECK)
#define require(contract, text)
#else
#define require(contract, text) assert(contract && text)
#endif
/*!
* \macro ensure
* \brief Vérifie la validité d'une postcondition.
*/
#if defined(CONTRACT_NO_POSTCONDITION) || defined(CONTRACT_NO_CHECK)
#define ensure(contract, text)
#else
#define ensure(contract, text) assert(contract && text)
#endif
/*!
* \macro invariant
* \brief Vérifie la validité d'un invariant.
*/
#if defined(CONTRACT_NO_INVARIANT) || defined(CONTRACT_NO_CHECK)
#define invariant(contract, text)
#else
#define invariant(contract, text) assert(contract && text)
#endif
/*!
* \macro invariants
* \brief Débute un bloc d'invariants de classe.
*/
#define invariants(classname) friend class InvariantsChecker<classname>; void _contract_check_invariants() const
/*!
* \macro check_invariants
* \brief Vérifie la validité des invariants de classe.
*/
#if defined(CONTRACT_NO_INVARIANT) || defined(CONTRACT_NO_CHECK)
#define check_invariants()
#else
#define check_invariants() _contract_check_invariants()
#endif
/*!
* \macro static_invariants
* \brief Débute un bloc d'invariants statiques de classe.
*/
#define static_invariants(classname) static void _contract_check_static_invariants() const
/*!
* \macro check_static_invariants
* \brief Vérifie la validité des invariants statiques de classe.
*/
#if defined(CONTRACT_NO_INVARIANT) || defined(CONTRACT_NO_CHECK)
#define check_static_invariants()
#else
#define check_static_invariants() _contract_check_static_invariants()
#endif
/*!
* \class InvariantsChecker
* \brief Vérifie la validité des invariants en début et en fin de scope.
*/
template <typename T>
class InvariantsChecker : private Uncopyable
{
private:
T *instance;
public:
InvariantsChecker(T *instance) :
instance(instance)
{
#if !defined(CONTRACT_NO_INVARIANT) && !defined(CONTRACT_NO_CHECK)
instance->_contract_check_invariants();
#endif
}
~InvariantsChecker()
{
#if !defined(CONTRACT_NO_INVARIANT) && !defined(CONTRACT_NO_CHECK)
instance->_contract_check_invariants();
#endif
}
};
#endif // __CONTRACT_HPP |
Partager