Leonardus
dbc.h
Go to the documentation of this file.
1
17#pragma once
18
19#include <boost/assert.hpp>
20
21
25class DbC {
26
27#ifndef BOOST_ASSERT_IS_VOID
28protected:
31 virtual bool invariant() const noexcept { /* LCOV_EXCL_START */
32 return true;
33 } /* LCOV_EXCL_STOP */
34#endif
35};
36
37
40template <typename T>
43 using assert_t = bool (T::*)() const noexcept;
44
47
49 const T * obj_;
50
51public:
53 DbCRAIIassert( assert_t p_fun, T * p_obj ) : fun_(p_fun), obj_(p_obj) {}
54
59 BOOST_ASSERT_MSG( (obj_->*fun_)(), "invariant violated" );
60 }
61};
62
64#define DBC_IS_VOID
65
69#ifndef BOOST_ASSERT_IS_VOID
70# define DBC_INV_RAII(TT) const DbCRAIIassert<TT> _destructmeatreturn( &TT::invariant, this )
71# undef DBC_IS_VOID
72#else
73# define DBC_INV_RAII(TT) ((void)0)
74#endif
75
77#define DBC_PRE(XXX) BOOST_ASSERT(XXX)
78
80#define DBC_POST(XXX) BOOST_ASSERT(XXX)
81
83#define DBC_INV BOOST_ASSERT(invariant())
84
88#define DBC_INV_CTOR(T) BOOST_ASSERT(T::invariant())
Helper class to call a check always at function return.
Definition: dbc.h:41
~DbCRAIIassert()
Dtor.
Definition: dbc.h:58
bool(T::*)() const noexcept assert_t
A shortcut for the invariant() function pointers.
Definition: dbc.h:43
DbCRAIIassert(assert_t p_fun, T *p_obj)
Ctor.
Definition: dbc.h:53
const T * obj_
The this of the invariant function.
Definition: dbc.h:49
const assert_t fun_
The invariant() of the current class to be called at function return.
Definition: dbc.h:46
Design by contract interface class.
Definition: dbc.h:25
virtual bool invariant() const noexcept
Checks the invariants of the class in which it is defined.
Definition: dbc.h:31