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#pragma GCC diagnostic push
23#pragma GCC diagnostic ignored "-Weffc++"
24
25
30class DbC {
31
32protected:
34 ~DbC() = default;
35
36#ifndef BOOST_ASSERT_IS_VOID
39 virtual bool invariant() const noexcept { /* LCOV_EXCL_START */
40 return true;
41 } /* LCOV_EXCL_STOP */
42#endif
43};
44
45
50template <typename T>
52
54 using assert_t = bool (T::*)() const noexcept;
55
56
59
60
62 const T * obj_;
63
64
65public:
70 DbCRAIIassert( assert_t p_fun, T * p_obj ) : fun_(p_fun), obj_(p_obj) {}
71
72
77 BOOST_ASSERT_MSG( (obj_->*fun_)(), "invariant violated" );
78 }
79
80};
81
82
84#define DBC_IS_VOID
85
86
92#ifndef BOOST_ASSERT_IS_VOID
93# define DBC_INV_RAII(TT) const DbCRAIIassert<TT> _destructmeatreturn( &TT::invariant, this )
94# undef DBC_IS_VOID
95#else
96# define DBC_INV_RAII(TT) ((void)0)
97#endif
98
99
103#define DBC_PRE(XXX) BOOST_ASSERT(XXX)
104
105
109#define DBC_POST(XXX) BOOST_ASSERT(XXX)
110
111
113#define DBC_INV BOOST_ASSERT(invariant())
114
115
121#define DBC_INV_CTOR(T) BOOST_ASSERT(T::invariant())
122
123
124#pragma GCC diagnostic pop
Helper class to call a check always at function return.
Definition dbc.h:51
~DbCRAIIassert()
Dtor.
Definition dbc.h:76
bool(T::*)() const noexcept assert_t
A shortcut for the invariant() function pointers.
Definition dbc.h:54
DbCRAIIassert(assert_t p_fun, T *p_obj)
Ctor.
Definition dbc.h:70
const T * obj_
The this of the invariant function.
Definition dbc.h:62
const assert_t fun_
The invariant() of the current class to be called at function return.
Definition dbc.h:58
Design by contract interface class.
Definition dbc.h:30
~DbC()=default
Protected dtor.
virtual bool invariant() const noexcept
Checks the invariants of the class in which it is defined.
Definition dbc.h:39