cpp-checked.h.pre
2.56 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
#include <stdio.h>
#include <stdlib.h>
#include <functional>
#include <string>
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 boolean {
private:
mutable bool checked = false;
isl_bool val;
friend boolean manage(isl_bool val);
boolean(isl_bool val): val(val) {}
public:
boolean()
: val(isl_bool_error) {}
~boolean() {
ISLPP_ASSERT(checked, "IMPLEMENTATION ERROR: Unchecked state");
}
/* implicit */ boolean(bool val)
: val(val ? isl_bool_true : isl_bool_false) {}
bool is_error() const { checked = true; return val == isl_bool_error; }
bool is_false() const { checked = true; return val == isl_bool_false; }
bool is_true() const { checked = true; return val == isl_bool_true; }
explicit operator bool() const {
ISLPP_ASSERT(checked, "IMPLEMENTATION ERROR: Unchecked error state");
ISLPP_ASSERT(!is_error(), "IMPLEMENTATION ERROR: Unhandled error state");
return is_true();
}
boolean operator!() const {
if (is_error())
return *this;
return !is_true();
}
};
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 bool checked = false;
isl_stat val;
friend stat manage(isl_stat val);
constexpr 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) {}
~stat() {
ISLPP_ASSERT(checked, "IMPLEMENTATION ERROR: Unchecked state");
}
isl_stat release() {
checked = true;
return val;
}
bool is_error() const {
checked = true;
return val == isl_stat_error;
}
bool is_ok() const {
checked = true;
return val == isl_stat_ok;
}
};
inline stat manage(isl_stat val)
{
return stat(val);
}
}
} // namespace isl