cpp-checked.h.pre
3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <stdio.h>
#include <stdlib.h>
#include <functional>
#include <memory>
#include <ostream>
#include <string>
#include <type_traits>
namespace isl {
namespace checked {
#define ISLPP_STRINGIZE_(X) #X
#define ISLPP_STRINGIZE(X) ISLPP_STRINGIZE_(X)
#define ISLPP_ASSERT(test, message) \
do { \
if (test) \
break; \
fputs("Assertion \"" #test "\" failed at " __FILE__ \
":" ISLPP_STRINGIZE(__LINE__) "\n " message "\n", \
stderr); \
abort(); \
} while (0)
/* Class used to check that isl::checked::boolean,
* isl::checked::stat and isl::checked::size values are checked for errors.
*/
struct checker {
bool checked = false;
~checker() {
ISLPP_ASSERT(checked, "IMPLEMENTATION ERROR: Unchecked state");
}
};
class boolean {
private:
mutable std::shared_ptr<checker> check = std::make_shared<checker>();
isl_bool val;
friend boolean manage(isl_bool val);
boolean(isl_bool val): val(val) {}
public:
static boolean error() {
return boolean(isl_bool_error);
}
boolean()
: val(isl_bool_error) {}
/* implicit */ boolean(bool val)
: val(val ? isl_bool_true : isl_bool_false) {}
isl_bool release() {
auto tmp = val;
val = isl_bool_error;
check->checked = true;
return tmp;
}
bool is_error() const { check->checked = true; return val == isl_bool_error; }
bool is_false() const { check->checked = true; return val == isl_bool_false; }
bool is_true() const { check->checked = true; return val == isl_bool_true; }
explicit operator bool() const {
ISLPP_ASSERT(check->checked, "IMPLEMENTATION ERROR: Unchecked error state");
ISLPP_ASSERT(!is_error(), "IMPLEMENTATION ERROR: Unhandled error state");
return is_true();
}
boolean negate() {
if (val == isl_bool_true)
val = isl_bool_false;
else if (val == isl_bool_false)
val = isl_bool_true;
return *this;
}
boolean operator!() const {
return boolean(*this).negate();
}
};
inline boolean manage(isl_bool val) {
return boolean(val);
}
class ctx {
isl_ctx *ptr;
public:
/* implicit */ ctx(isl_ctx *ctx)
: ptr(ctx) {}
isl_ctx *release() {
auto tmp = ptr;
ptr = nullptr;
return tmp;
}
isl_ctx *get() {
return ptr;
}
};
/* Class encapsulating an isl_stat value.
*/
class stat {
private:
mutable std::shared_ptr<checker> check = std::make_shared<checker>();
isl_stat val;
friend stat manage(isl_stat val);
stat(isl_stat val) : val(val) {}
public:
static stat ok() {
return stat(isl_stat_ok);
}
static stat error() {
return stat(isl_stat_error);
}
stat() : val(isl_stat_error) {}
isl_stat release() {
check->checked = true;
return val;
}
bool is_error() const {
check->checked = true;
return val == isl_stat_error;
}
bool is_ok() const {
check->checked = true;
return val == isl_stat_ok;
}
};
inline stat manage(isl_stat val)
{
return stat(val);
}
/* Class encapsulating an isl_size value.
*/
class size {
private:
mutable std::shared_ptr<checker> check = std::make_shared<checker>();
isl_size val;
friend size manage(isl_size val);
size(isl_size val) : val(val) {}
public:
size() : val(isl_size_error) {}
isl_size release() {
auto tmp = val;
val = isl_size_error;
check->checked = true;
return tmp;
}
bool is_error() const {
check->checked = true;
return val == isl_size_error;
}
explicit operator unsigned() const {
ISLPP_ASSERT(check->checked,
"IMPLEMENTATION ERROR: Unchecked error state");
ISLPP_ASSERT(!is_error(),
"IMPLEMENTATION ERROR: Unhandled error state");
return val;
}
};
inline size manage(isl_size val)
{
return size(val);
}
}
} // namespace isl