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