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:
33
35 ~DbC() = default;
36
37#ifndef BOOST_ASSERT_IS_VOID
40 virtual bool invariant() const noexcept { /* LCOV_EXCL_START */
41 return true;
42 } /* LCOV_EXCL_STOP */
43#endif
44
45};
46
47
52template <typename T>
54
56 using assert_t = bool (T::*)() const noexcept;
57
58
61
62
64 const T * obj_;
65
66public:
67
72 DbCRAIIassert( assert_t p_fun, T * p_obj ) : fun_(p_fun), obj_(p_obj) {}
73
74
78 BOOST_ASSERT_MSG( (obj_->*fun_)(), "invariant violated" );
79 }
80
81};
82
83
85#define DBC_IS_VOID
86
87
93#ifndef BOOST_ASSERT_IS_VOID
94# define DBC_INV_RAII(TT) const DbCRAIIassert<TT> _destructmeatreturn( &TT::invariant, this )
95# undef DBC_IS_VOID
96#else
97# define DBC_INV_RAII(TT) ((void)0)
98#endif
99
100
104#define DBC_PRE(XXX) BOOST_ASSERT(XXX)
105
106
110#define DBC_POST(XXX) BOOST_ASSERT(XXX)
111
112
114#define DBC_INV BOOST_ASSERT(invariant())
115
116
122#define DBC_INV_CTOR(T) BOOST_ASSERT(T::invariant())
123
124
125#pragma GCC diagnostic pop
Helper class to call a check always at function return.
Definition dbc.h:53
~DbCRAIIassert()
Dtor.
Definition dbc.h:77
bool(T::*)() const noexcept assert_t
A shortcut for the invariant() function pointers.
Definition dbc.h:56
DbCRAIIassert(assert_t p_fun, T *p_obj)
Ctor.
Definition dbc.h:72
const T * obj_
The this of the invariant function.
Definition dbc.h:64
const assert_t fun_
The invariant() of the current class to be called at function return.
Definition dbc.h:60
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:40