C/C++ Library, mos@ua 1.0
cpplib-mos
dbc.h
Go to the documentation of this file.
1
37#ifndef DBC_H
38#define DBC_H
39
40#include <stdio.h>
41#include <stdlib.h>
42#include <assert.h>
43#include "hdp.h"
44#include "utils.h"
45
46#ifdef NDEBUG
47#define NDEBUG_CHECKS
48#define NDEBUG_PRECONDITIONS
49#define NDEBUG_POSTCONDITIONS
50#define NDEBUG_INVARIANTS
51#endif
52
53#ifdef DEBUG_ONLY_PRECONDITIONS
54#ifdef NDEBUG_PRECONDITIONS
55#undef NDEBUG_PRECONDITIONS
56#endif
57#ifndef NDEBUG_CHECKS
58#define NDEBUG_CHECKS
59#endif
60#ifndef NDEBUG_POSTCONDITIONS
61#define NDEBUG_POSTCONDITIONS
62#endif
63#ifndef NDEBUG_INVARIANTS
64#define NDEBUG_INVARIANTS
65#endif
66#endif
67
68// check:
69#ifdef NDEBUG_CHECKS
70#define check(condition, message)
71#else
72#if defined __cplusplus && defined EXCEPTION_POLICY
73#define check(condition, message) \
74 if (!(condition)) \
75 throw (char*)Nu(strdup(concat_12str((char*)"Assertion fail", message[0] ? (char*)": " : (char*)"", message, (char*)", assertion: \"", #condition, (char*)"\", function: \"", __FUNCTION__, (char*)"\":", int2str(__LINE__), (char*)", file: \"", __FILE__, (char*)"\"\n")))
76#else // EXIT_POLICY (default)
86#define check(condition, message) \
87 if (!(condition)) \
88 do { \
89 fprintf (stderr, "Assertion fail%s%s, assertion: \"%s\", function: \"%s\":%d, file: \"%s\"\n", \
90 message[0] ? ": " : "", message, #condition, __FUNCTION__, __LINE__ , __FILE__); \
91 *((int*)0) = 0; \
92 abort (); \
93 } while (0)
94#endif
95#endif
96
97
98// precondition:
99#ifdef NDEBUG_PRECONDITIONS
100#define require(condition, message)
101#else
102#if defined __cplusplus && defined EXCEPTION_POLICY
103#define require(condition, message) \
104 if (!(condition)) \
105 throw (char*)Nu(strdup(concat_12str((char*)"Precondition fail", message[0] ? (char*)": " : (char*)"", message, (char*)", assertion: \"", #condition, (char*)"\", function: \"", __FUNCTION__, (char*)"\":", int2str(__LINE__), (char*)", file: \"", __FILE__, (char*)"\"\n")))
106#else // EXIT_POLICY (default)
116#define require(condition, message) \
117 if (!(condition)) \
118 do { \
119 fprintf (stderr, "Precondition fail%s%s, assertion: \"%s\", function: \"%s\":%d, file: \"%s\"\n", \
120 message[0] ? ": " : "", message, #condition, __FUNCTION__, __LINE__ , __FILE__); \
121 *((int*)0) = 0; \
122 abort (); \
123 } while (0)
124#endif
125#endif
126
127
128// postcondition:
129#ifdef NDEBUG_POSTCONDITIONS
130#define ensure(condition, message)
131#else
132#if defined __cplusplus && defined EXCEPTION_POLICY
133#define ensure(condition, message) \
134 if (!(condition)) \
135 throw (char*)Nu(strdup(concat_12str((char*)"Postcondition fail", message[0] ? (char*)": " : (char*)"", message, (char*)", assertion: \"", #condition, (char*)"\", function: \"", __FUNCTION__, (char*)"\":", int2str(__LINE__), (char*)", file: \"", __FILE__, (char*)"\"\n")))
136#else // EXIT_POLICY (default)
146#define ensure(condition, message) \
147 if (!(condition)) \
148 do { \
149 fprintf (stderr, "Postcondition fail%s%s, assertion: \"%s\", function: \"%s\":%d, file: \"%s\"\n", \
150 message[0] ? ": " : "", message, #condition, __FUNCTION__, __LINE__ , __FILE__); \
151 *((int*)0) = 0; \
152 abort (); \
153 } while (0)
154#endif
155#endif
156
157//
158// invariant:
159#ifdef NDEBUG_INVARIANTS
160#define invariant(condition, message)
161#else
162#if defined __cplusplus && defined EXCEPTION_POLICY
163#define invariant(condition, message) \
164 if (!(condition)) \
165 throw (char*)Nu(strdup(concat_12str((char*)"Invariant fail", message[0] ? (char*)": " : (char*)"", message, (char*)", assertion: \"", #condition, (char*)"\", function: \"", __FUNCTION__, (char*)"\":", int2str(__LINE__), (char*)", file: \"", __FILE__, (char*)"\"\n")))
166#else // EXIT_POLICY (default)
176#define invariant(condition, message) \
177 if (!(condition)) \
178 do { \
179 fprintf (stderr, "Invariant fail%s%s, assertion: \"%s\", function: \"%s\":%d, file: \"%s\"\n", \
180 message[0] ? ": " : "", message, #condition, __FUNCTION__, __LINE__ , __FILE__); \
181 *((int*)0) = 0; \
182 abort (); \
183 } while (0)
184#endif
185#endif
186
187#endif
188
189/* ************************************************** */
193/* ************************************************** */
194
Handle Defensive Programming module.
Useful common functions and macros.