CppELib 1.7.0
Loading...
Searching...
No Matches
Assertion.h
Go to the documentation of this file.
1#ifndef ASSERTION_ASSERTION_H_INCLUDED
2#define ASSERTION_ASSERTION_H_INCLUDED
3
4#include <cstddef>
5
6#ifndef CPPELIB_ASSERTION_FAILURE_BUFSIZE
15#define CPPELIB_ASSERTION_FAILURE_BUFSIZE (512)
16#endif
17
28#define CHECK_ASSERT(x)\
29 ((x) ? (void)0 : Assertion::Failure::assertFail(__FILE__, __LINE__, #x))
30
31
32#ifdef NDEBUG
33#define DEBUG_ASSERT(x) ((void)0)
34#else
39#define DEBUG_ASSERT(x) CHECK_ASSERT(x)
40#endif
41
42
43namespace Assertion {
44
51public:
52 virtual ~AssertHandler() {}
66 virtual void handle(const char* msg) = 0;
67};
68
74class Failure {
75public:
77 Failure(const char* file, int line, const char* expr)
78 : m_buf()
79 {
80 std::size_t remain = sizeof m_buf;
81 char* pBuf = m_buf;
82 pBuf = concatCString(pBuf, remain, file);
83 pBuf = concatCString(pBuf, remain, "(");
84 char lineBuf[16];
85 toCString(lineBuf, sizeof lineBuf, static_cast<unsigned int>(line));
86 pBuf = concatCString(pBuf, remain, lineBuf);
87 pBuf = concatCString(pBuf, remain, "): Assertion failed (");
88 pBuf = concatCString(pBuf, remain, expr);
89 concatCString(pBuf, remain, ")");
90 }
92
97 const char* message() const
98 {
99 return m_buf;
100 }
101
103 static void assertFail(const char* file, int line, const char* expr)
104 {
105 Failure failure(file, line, expr);
106 AssertHandler* handler = getHandler();
107 if (handler != 0) {
108 handler->handle(failure.message());
109 }
110#ifndef CPPELIB_NO_EXCEPTIONS
111 throw failure;
112#endif
113 }
114
115 static void setHandler(AssertHandler* handler)
116 {
117 getHandler() = handler;
118 }
119
120private:
122
123 static AssertHandler*& getHandler()
124 {
125 static AssertHandler* handler;
126 return handler;
127 }
128
129 static void toCString(char* buf, std::size_t size, unsigned int val)
130 {
131 if (size == 0U) {
132 return;
133 }
134 const std::size_t TMP_SIZE = 16U;
135 char tmp[TMP_SIZE];
136 const char* const numStr = "0123456789";
137 std::size_t i;
138 for (i = 0U; i < TMP_SIZE - 1U; i++) {
139 if (val == 0U) {
140 break;
141 }
142 tmp[i] = numStr[val % 10U];
143 val /= 10U;
144 }
145 const std::size_t tmpLen = i;
146 for (i = 0U; i < tmpLen; i++) {
147 if (i < size) {
148 buf[i] = tmp[tmpLen - i - 1U];
149 } else {
150 buf[size - 1U] = '\0';
151 return;
152 }
153 }
154 buf[tmpLen] = '\0';
155 }
156
157 static char* concatCString(char* buf, std::size_t& remain, const char* str)
158 {
159 if (remain <= 1U) {
160 return buf;
161 }
162 std::size_t len = copyCString(buf, remain, str);
163 remain -= len;
164 return buf + len;
165 }
166
167 static std::size_t copyCString(char* to, std::size_t size, const char* from)
168 {
169 if (size == 0U) {
170 return 0U;
171 }
172 const char* p = from;
173 char* q = to;
174 for (std::size_t i = 0U; i < size; i++) {
175 if (*p == '\0') {
176 *q = '\0';
177 return i;
178 }
179 *q = *p;
180 p++;
181 q++;
182 }
183 to[size - 1U] = '\0';
184 return size - 1U;
185 }
187};
188
193inline void setHandler(AssertHandler* handler)
194{
195 Failure::setHandler(handler);
196}
197
198}
199
200#endif // ASSERTION_ASSERTION_H_INCLUDED
#define CPPELIB_ASSERTION_FAILURE_BUFSIZE
Buffer size of Assertion::Failure object.
Definition Assertion.h:15
Interface for handling assertion failure by CHECK_ASSERT() macro.
Definition Assertion.h:50
virtual void handle(const char *msg)=0
Handle an assertion failure.
virtual ~AssertHandler()
Definition Assertion.h:52
Class used when CHECK_ASSERT() macro fails.
Definition Assertion.h:74
const char * message() const
Get the message when assertion fails.
Definition Assertion.h:97
Definition Assertion.h:43
void setHandler(AssertHandler *handler)
Set AssertHandler.
Definition Assertion.h:193