00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef PPL_ppl_c_implementation_common_defs_hh
00025 #define PPL_ppl_c_implementation_common_defs_hh 1
00026
00027 #define PPL_NO_AUTOMATIC_INITIALIZATION
00028 #include "ppl.hh"
00029 #ifdef PPL_WATCHDOG_LIBRARY_ENABLED
00030 #include "pwl.hh"
00031 #endif
00032 #include "ppl_c.h"
00033 #include <stdexcept>
00034
00035 namespace Parma_Polyhedra_Library {
00036
00037 namespace Interfaces {
00038
00039 namespace C {
00040
00041 extern "C" typedef void
00042 (*error_handler_type)(enum ppl_enum_error_code code, const char* description);
00043
00044 extern error_handler_type user_error_handler;
00045
00046 void notify_error(enum ppl_enum_error_code code, const char* description);
00047
00048 Relation_Symbol relation_symbol(enum ppl_enum_Constraint_Type t);
00049
00050 Bounded_Integer_Type_Width
00051 bounded_integer_type_width(enum ppl_enum_Bounded_Integer_Type_Width w);
00052
00053 Bounded_Integer_Type_Representation
00054 bounded_integer_type_representation(enum ppl_enum_Bounded_Integer_Type_Representation r);
00055
00060 class Array_Partial_Function_Wrapper {
00061 public:
00066 Array_Partial_Function_Wrapper(dimension_type* v, size_t n);
00067
00072 bool has_empty_codomain() const;
00073
00078 dimension_type max_in_codomain() const;
00079
00089 bool maps(dimension_type i, dimension_type& j) const;
00090
00091 private:
00093 dimension_type* vec;
00094
00096 size_t vec_size;
00097
00099 mutable dimension_type max_in_codomain_;
00100
00103 mutable int empty;
00104 };
00105
00106 class timeout_exception : public Parma_Polyhedra_Library::Throwable {
00107 public:
00108 void throw_me() const {
00109 throw *this;
00110 }
00111 int priority() const {
00112 return 0;
00113 }
00114 };
00115
00116 void reset_timeout();
00117
00118 class deterministic_timeout_exception
00119 : public Parma_Polyhedra_Library::Throwable {
00120 public:
00121 void throw_me() const {
00122 throw *this;
00123 }
00124 int priority() const {
00125 return 0;
00126 }
00127 };
00128
00129 void reset_deterministic_timeout();
00130
00131 }
00132
00133 }
00134
00135 }
00136
00137
00138 #define CATCH_STD_EXCEPTION(exception, code) \
00139 catch (const std::exception& e) { \
00140 notify_error(code, e.what()); \
00141 return code; \
00142 }
00143
00144 #define CATCH_ALL \
00145 CATCH_STD_EXCEPTION(bad_alloc, PPL_ERROR_OUT_OF_MEMORY) \
00146 CATCH_STD_EXCEPTION(invalid_argument, PPL_ERROR_INVALID_ARGUMENT) \
00147 CATCH_STD_EXCEPTION(domain_error, PPL_ERROR_DOMAIN_ERROR) \
00148 CATCH_STD_EXCEPTION(length_error, PPL_ERROR_LENGTH_ERROR) \
00149 CATCH_STD_EXCEPTION(logic_error, PPL_ERROR_LOGIC_ERROR) \
00150 CATCH_STD_EXCEPTION(overflow_error, PPL_ARITHMETIC_OVERFLOW) \
00151 CATCH_STD_EXCEPTION(runtime_error, PPL_ERROR_INTERNAL_ERROR) \
00152 CATCH_STD_EXCEPTION(exception, PPL_ERROR_UNKNOWN_STANDARD_EXCEPTION) \
00153 catch (timeout_exception&) { \
00154 reset_timeout(); \
00155 notify_error(PPL_TIMEOUT_EXCEPTION, "PPL timeout expired"); \
00156 return PPL_TIMEOUT_EXCEPTION; \
00157 } \
00158 catch (deterministic_timeout_exception&) { \
00159 reset_deterministic_timeout(); \
00160 notify_error(PPL_TIMEOUT_EXCEPTION, "PPL deterministic timeout expired"); \
00161 return PPL_TIMEOUT_EXCEPTION; \
00162 } \
00163 catch (...) { \
00164 notify_error(PPL_ERROR_UNEXPECTED_ERROR, \
00165 "completely unexpected error: a bug in the PPL"); \
00166 return PPL_ERROR_UNEXPECTED_ERROR; \
00167 }
00168
00169 #define DECLARE_CONVERSIONS(Type, CPP_Type) \
00170 inline const CPP_Type* \
00171 to_const(ppl_const_##Type##_t x) { \
00172 return reinterpret_cast<const CPP_Type*>(x); \
00173 } \
00174 \
00175 inline CPP_Type* \
00176 to_nonconst(ppl_##Type##_t x) { \
00177 return reinterpret_cast<CPP_Type*>(x); \
00178 } \
00179 \
00180 inline ppl_const_##Type##_t \
00181 to_const(const CPP_Type* x) { \
00182 return reinterpret_cast<ppl_const_##Type##_t>(x); \
00183 } \
00184 \
00185 inline ppl_##Type##_t \
00186 to_nonconst(CPP_Type* x) { \
00187 return reinterpret_cast<ppl_##Type##_t>(x); \
00188 }
00189
00190 #define DEFINE_PRINT_FUNCTIONS(Type) \
00191 int \
00192 ppl_io_print_##Type(ppl_const_##Type##_t x) try { \
00193 using namespace IO_Operators; \
00194 stdiobuf sb(stdout); \
00195 std::ostream os(&sb); \
00196 os << *to_const(x); \
00197 if (!os) \
00198 return PPL_STDIO_ERROR; \
00199 return 0; \
00200 } \
00201 CATCH_ALL \
00202 \
00203 int \
00204 ppl_io_fprint_##Type(FILE* file, ppl_const_##Type##_t x) try { \
00205 using namespace IO_Operators; \
00206 stdiobuf sb(file); \
00207 std::ostream os(&sb); \
00208 os << *to_const(x); \
00209 if (!os) \
00210 return PPL_STDIO_ERROR; \
00211 return 0; \
00212 } \
00213 CATCH_ALL \
00214 \
00215 int \
00216 ppl_io_asprint_##Type(char** strp, ppl_const_##Type##_t x) try { \
00217 using namespace IO_Operators; \
00218 std::ostringstream os; \
00219 os << *to_const(x); \
00220 if (!os) \
00221 return PPL_STDIO_ERROR; \
00222 *strp = strdup(os.str().c_str()); \
00223 if (*strp == 0) \
00224 return PPL_ERROR_OUT_OF_MEMORY; \
00225 return 0; \
00226 } \
00227 CATCH_ALL
00228
00229 #define DEFINE_ASCII_DUMP_FUNCTIONS(Type) \
00230 int \
00231 ppl_##Type##_ascii_dump(ppl_const_##Type##_t x, FILE* file) try { \
00232 stdiobuf sb(file); \
00233 std::ostream os(&sb); \
00234 to_const(x)->ascii_dump(os); \
00235 if (!os) \
00236 return PPL_STDIO_ERROR; \
00237 return 0; \
00238 } \
00239 CATCH_ALL
00240
00241 #define DEFINE_ASCII_LOAD_FUNCTIONS(Type) \
00242 int \
00243 ppl_##Type##_ascii_load(ppl_##Type##_t x, FILE* file) try { \
00244 stdiobuf sb(file); \
00245 std::istream is(&sb); \
00246 if (!to_nonconst(x)->ascii_load(is)) \
00247 return PPL_STDIO_ERROR; \
00248 return 0; \
00249 } \
00250 CATCH_ALL
00251
00252 #define DEFINE_ASCII_DUMP_LOAD_FUNCTIONS(Type) \
00253 DEFINE_ASCII_DUMP_FUNCTIONS(Type) \
00254 DEFINE_ASCII_LOAD_FUNCTIONS(Type)
00255
00256 #define DEFINE_OUTPUT_FUNCTIONS(Type) \
00257 DEFINE_PRINT_FUNCTIONS(Type) \
00258 DEFINE_ASCII_DUMP_LOAD_FUNCTIONS(Type)
00259
00260 #include "ppl_c_implementation_common.inlines.hh"
00261
00262 #endif // !defined(PPL_ppl_c_implementation_common_defs_hh)