isl_basic_map_is_div_constraint: use isl_basic_map_offset
[isl.git] / cpp / cpp-checked.h.pre
blob5aeb79e4e180365ae3852f842756f0fc88889e07
2 #include <stdio.h>
3 #include <stdlib.h>
5 #include <functional>
6 #include <ostream>
7 #include <string>
9 namespace isl {
10 namespace checked {
12 #define ISLPP_STRINGIZE_(X) #X
13 #define ISLPP_STRINGIZE(X) ISLPP_STRINGIZE_(X)
15 #define ISLPP_ASSERT(test, message)                          \
16   do {                                                       \
17     if (test)                                                \
18       break;                                                 \
19     fputs("Assertion \"" #test "\" failed at " __FILE__      \
20       ":" ISLPP_STRINGIZE(__LINE__) "\n  " message "\n",     \
21       stderr);                                               \
22     abort();                                                 \
23   } while (0)
25 class boolean {
26 private:
27   mutable bool checked = false;
28   isl_bool val;
30   friend boolean manage(isl_bool val);
31   boolean(isl_bool val): val(val) {}
32 public:
33   boolean()
34       : val(isl_bool_error) {}
35   ~boolean() {
36     ISLPP_ASSERT(checked, "IMPLEMENTATION ERROR: Unchecked state");
37   }
39   /* implicit */ boolean(bool val)
40       : val(val ? isl_bool_true : isl_bool_false) {}
42   bool is_error() const { checked = true; return val == isl_bool_error; }
43   bool is_false() const { checked = true; return val == isl_bool_false; }
44   bool is_true() const { checked = true; return val == isl_bool_true; }
46   explicit operator bool() const {
47     ISLPP_ASSERT(checked, "IMPLEMENTATION ERROR: Unchecked error state");
48     ISLPP_ASSERT(!is_error(), "IMPLEMENTATION ERROR: Unhandled error state");
49     return is_true();
50   }
52   boolean operator!() const {
53     if (is_error())
54       return *this;
55     return !is_true();
56   }
59 inline boolean manage(isl_bool val) {
60   return boolean(val);
63 class ctx {
64   isl_ctx *ptr;
65 public:
66   /* implicit */ ctx(isl_ctx *ctx)
67       : ptr(ctx) {}
68   isl_ctx *release() {
69     auto tmp = ptr;
70     ptr = nullptr;
71     return tmp;
72   }
73   isl_ctx *get() {
74     return ptr;
75   }
78 /* Class encapsulating an isl_stat value.
79  */
80 class stat {
81 private:
82         mutable bool checked = false;
83         isl_stat val;
85         friend stat manage(isl_stat val);
86         constexpr stat(isl_stat val) : val(val) {}
87 public:
88         static stat ok() {
89                 return stat(isl_stat_ok);
90         }
91         static stat error() {
92                 return stat(isl_stat_error);
93         }
94         stat() : val(isl_stat_error) {}
95         ~stat() {
96                 ISLPP_ASSERT(checked, "IMPLEMENTATION ERROR: Unchecked state");
97         }
99         isl_stat release() {
100                 checked = true;
101                 return val;
102         }
104         bool is_error() const {
105                 checked = true;
106                 return val == isl_stat_error;
107         }
108         bool is_ok() const {
109                 checked = true;
110                 return val == isl_stat_ok;
111         }
114 inline stat manage(isl_stat val)
116         return stat(val);
120 } // namespace isl