C++ Library, mos@ua  0.9
cpplib-mos
dbc.h
Go to the documentation of this file.
1 
34 #ifndef DBC_H
35 #define DBC_H
36 
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <assert.h>
40 #include "utils.h"
41 
42 #ifdef NDEBUG
43 #define NDEBUG_CHECKS
44 #define NDEBUG_PRECONDITIONS
45 #define NDEBUG_POSTCONDITIONS
46 #define NDEBUG_INVARIANTS
47 #endif
48 
49 #ifdef DEBUG_ONLY_PRECONDITIONS
50 #ifdef NDEBUG_PRECONDITIONS
51 #undef NDEBUG_PRECONDITIONS
52 #endif
53 #ifndef NDEBUG_CHECKS
54 #define NDEBUG_CHECKS
55 #endif
56 #ifndef NDEBUG_POSTCONDITIONS
57 #define NDEBUG_POSTCONDITIONS
58 #endif
59 #ifndef NDEBUG_INVARIANTS
60 #define NDEBUG_INVARIANTS
61 #endif
62 #endif
63 
64 // check:
65 #ifdef NDEBUG_CHECKS
66 #define check(condition, message)
67 #else
68 #ifdef EXCEPTION_POLICY
69 #define check(condition, message) \
70  if (!(condition)) \
71  throw string_concat(NULL, 0, (char*)"Assertion fail", message[0] ? (char*)": " : (char*)"", message, (char*)", assertion: \"", #condition, (char*)"\", function: \"", __FUNCTION__, (char*)"\":", int2str(__LINE__), (char*)", file: \"", __FILE__, (char*)"\"\n", NULL)
72 #else // EXIT_POLICY (default)
73 
81 #define check(condition, message) \
82  if (!(condition)) \
83  do { \
84  fprintf (stderr, "Assertion fail%s%s, assertion: \"%s\", function: \"%s\":%d, file: \"%s\"\n", \
85  message[0] ? ": " : "", message, #condition, __FUNCTION__, __LINE__ , __FILE__); \
86  *((int*)0) = 0; \
87  abort (); \
88  } while (0)
89 #endif
90 #endif
91 
92 
93 // precondition:
94 #ifdef NDEBUG_PRECONDITIONS
95 #define require(condition, message)
96 #else
97 #ifdef EXCEPTION_POLICY
98 #define require(condition, message) \
99  if (!(condition)) \
100  throw string_concat(NULL, 0, (char*)"Precondition fail", message[0] ? (char*)": " : (char*)"", message, (char*)", assertion: \"", #condition, (char*)"\", function: \"", __FUNCTION__, (char*)"\":", int2str(__LINE__), (char*)", file: \"", __FILE__, (char*)"\"\n", NULL)
101 #else // EXIT_POLICY (default)
102 
110 #define require(condition, message) \
111  if (!(condition)) \
112  do { \
113  fprintf (stderr, "Precondition fail%s%s, assertion: \"%s\", function: \"%s\":%d, file: \"%s\"\n", \
114  message[0] ? ": " : "", message, #condition, __FUNCTION__, __LINE__ , __FILE__); \
115  *((int*)0) = 0; \
116  abort (); \
117  } while (0)
118 #endif
119 #endif
120 
121 
122 // postcondition:
123 #ifdef NDEBUG_POSTCONDITIONS
124 #define ensure(condition, message)
125 #else
126 #ifdef EXCEPTION_POLICY
127 #define ensure(condition, message) \
128  if (!(condition)) \
129  throw string_concat(NULL, 0, (char*)"Postcondition fail", message[0] ? (char*)": " : (char*)"", message, (char*)", assertion: \"", #condition, (char*)"\", function: \"", __FUNCTION__, (char*)"\":", int2str(__LINE__), (char*)", file: \"", __FILE__, (char*)"\"\n", NULL)
130 #else // EXIT_POLICY (default)
131 
139 #define ensure(condition, message) \
140  if (!(condition)) \
141  do { \
142  fprintf (stderr, "Postcondition fail%s%s, assertion: \"%s\", function: \"%s\":%d, file: \"%s\"\n", \
143  message[0] ? ": " : "", message, #condition, __FUNCTION__, __LINE__ , __FILE__); \
144  *((int*)0) = 0; \
145  abort (); \
146  } while (0)
147 #endif
148 #endif
149 
150 //
151 // invariant:
152 #ifdef NDEBUG_INVARIANTS
153 #define invariant(condition, message)
154 #else
155 #ifdef EXCEPTION_POLICY
156 #define invariant(condition, message) \
157  if (!(condition)) \
158  throw string_concat(NULL, 0, (char*)"Invariant fail", message[0] ? (char*)": " : (char*)"", message, (char*)", assertion: \"", #condition, (char*)"\", function: \"", __FUNCTION__, (char*)"\":", int2str(__LINE__), (char*)", file: \"", __FILE__, (char*)"\"\n", NULL)
159 #else // EXIT_POLICY (default)
160 
168 #define invariant(condition, message) \
169  if (!(condition)) \
170  do { \
171  fprintf (stderr, "Invariant fail%s%s, assertion: \"%s\", function: \"%s\":%d, file: \"%s\"\n", \
172  message[0] ? ": " : "", message, #condition, __FUNCTION__, __LINE__ , __FILE__); \
173  *((int*)0) = 0; \
174  abort (); \
175  } while (0)
176 #endif
177 #endif
178 
179 #endif
180 
181 /* ************************************************** */
185 /* ************************************************** */
186 
Useful common functions and macros.