ADS
Algorithms and Data Structures
Toggle main menu visibility
Loading...
Searching...
No Matches
Contract.h
Go to the documentation of this file.
1
/*--------------------------------------------------------------------------*/
2
/*---------------------------- File Contract.h -----------------------------*/
3
/*--------------------------------------------------------------------------*/
41
/*--------------------------------------------------------------------------*/
42
/*----------------------------- DEFINITIONS --------------------------------*/
43
/*--------------------------------------------------------------------------*/
44
45
#ifndef __ADS_Contract
46
#define __ADS_Contract
47
/* self-identification: #endif constant */
48
49
/*--------------------------------------------------------------------------*/
50
/*------------------------------ INCLUDES ----------------------------------*/
51
/*--------------------------------------------------------------------------*/
52
53
#include <concepts>
54
#include <optional>
55
#include <stdexcept>
56
#include <string>
57
58
/*--------------------------------------------------------------------------*/
59
/*--------------------------- NAMESPACE ------------------------------------*/
60
/*--------------------------------------------------------------------------*/
61
62
namespace
ADS
63
{
64
67
#ifndef ADS_CONTRACTS
68
#ifdef NDEBUG
69
#define ADS_CONTRACTS 0
70
#else
71
#define ADS_CONTRACTS 1
72
#endif
73
#endif
74
76
class
contract_violation
:
public
std::logic_error
77
{
78
public
:
79
using
std::logic_error::logic_error;
80
};
81
82
namespace
detail
83
{
84
85
[[noreturn]]
inline
void
violated(
const
char
* kind ,
const
char
* cond ,
86
const
char
* func ,
const
char
* file ,
87
int
line )
88
{
89
throw
(
contract_violation
( std::string(
"ADS: "
) + kind +
" ( "
+ cond +
90
" ) violated in "
+ func +
", "
+ file +
":"
+
91
std::to_string( line ) ) );
92
}
93
94
}
// end( namespace detail )
95
112
#define ADS_CONTRACT_CHECK( level, kind, cond ) \
113
do { \
114
if constexpr( ADS_CONTRACTS >= ( level ) ) \
115
if( ! ( cond ) ) \
116
::ADS::detail::violated( kind, #cond, __func__, __FILE__, __LINE__ ); \
117
} while( false )
118
119
121
#define ADS_PRE( cond ) ADS_CONTRACT_CHECK( 1, "precondition", cond )
122
124
#define ADS_POST( cond ) ADS_CONTRACT_CHECK( 2, "postcondition", cond )
125
127
#define ADS_INVARIANT( cond ) ADS_CONTRACT_CHECK( 3, "invariant", cond )
128
131
#define ADS_AUDIT( cond ) ADS_CONTRACT_CHECK( 3, "audit", cond )
132
133
/*--------------------------------------------------------------------------*/
134
/*----------------------- HELPERS OF THE CONDITIONS ------------------------*/
135
/*--------------------------------------------------------------------------*/
136
137
namespace
detail
138
{
139
144
template
<
class
T >
145
bool
equal(
const
T & a ,
const
T & b )
146
{
147
if
constexpr
( std::equality_comparable< T > )
148
return
( ( a == b ) || ( ! ( a == a ) && ! ( b == b ) ) );
149
else
150
return
(
true
);
151
}
152
156
template
<
class
X >
157
std::optional< X > snapshot(
const
X & x )
158
{
159
if
constexpr
(
ADS_CONTRACTS
>= 3 )
160
return
( x );
161
else
162
return
( std::nullopt );
163
}
164
165
}
// end( namespace detail )
166
167
/*--------------------------------------------------------------------------*/
168
169
}
// end( namespace ADS )
170
171
/*--------------------------------------------------------------------------*/
172
173
#endif
/* Contract.h included */
174
175
/*--------------------------------------------------------------------------*/
176
/*-------------------------- End File Contract.h ---------------------------*/
177
/*--------------------------------------------------------------------------*/
ADS::contract_violation
a contract violated: a bug of the caller (a precondition) or of ADS
Definition
Contract.h:77
ADS_CONTRACTS
#define ADS_CONTRACTS
the level of the contracts checked: 0 none, 1 preconditions, 2 postconditions, 3 invariants and audit...
Definition
Contract.h:71
ADS
Definition
ADT.h:96
ADS
Contract.h
Generated by
1.17.0