forked from jamboree/CxxFunctionBenchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssert.h
212 lines (173 loc) · 5.92 KB
/
Assert.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//
// Copyright 2013 (C). Alex Robenko. All rights reserved.
//
// This library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
/// @file embxx/util/Assert.h
/// This file contains classes required for generic custom assertion
/// functionality
#pragma once
#include <cassert>
#include <type_traits>
#include <utility>
namespace embxx
{
namespace util
{
/// @addtogroup util
/// @{
/// @brief Base class for any custom assertion behaviour.
/// @details In order to implement custom assertion failure behaviour it
/// is necessary to inherit from this class and override
/// fail() virtual member function.
/// @headerfile embxx/util/Assert.h
class Assert
{
public:
/// @brief Destructor
/// @note Thread safety: Safe
/// @note Exception guarantee: No throw.
virtual ~Assert() {}
/// @brief Pure virtual function to be called when assertion fails.
/// @param[in] expr Assertion condition/expression
/// @param[in] file File name
/// @param[in] line Line number of the assert statement.
/// @param[in] function Function name.
/// @note Thread safety: Safe.
/// @note Exception guarantee: No throw.
virtual void fail(
const char* expr,
const char* file,
unsigned int line,
const char* function) = 0;
private:
};
/// @cond DOCUMENT_ASSERT_MANAGER
class AssertManager
{
public:
static AssertManager& instance()
{
static AssertManager mgr;
return mgr;
}
AssertManager(const AssertManager&) = delete;
AssertManager& operator=(const AssertManager&) = delete;
Assert* reset(Assert* newAssert = nullptr)
{
auto prevAssert = assert_;
assert_ = newAssert;
return prevAssert;
}
Assert* getAssert()
{
return assert_;
}
bool hasAssertRegistered() const
{
return (assert_ != nullptr);
}
static void infiniteLoop()
{
while (true) {};
}
private:
AssertManager() : assert_(nullptr) {}
Assert* assert_;
};
/// @endcond
/// @brief Enable new assertion behaviour.
/// @details Instantiate object of this class to enable new behaviour of
/// assertion failure.
/// @tparam TAssert Class derived from Assert that implements new custom
/// behaviour of the assertion failure.
/// @pre TAssert class must be derived from util::Assert.
/// @note Thread safety: Unsafe. If there are multiple custom assertion failures
/// defined in single binary, all of them must be done in the main thread
/// and preferable before any other threads are created.
/// @headerfile embxx/util/Assert.h
template < typename TAssert>
class EnableAssert
{
static_assert(std::is_base_of<Assert, TAssert>::value,
"TAssert class must be derived class of Assert");
public:
/// Type of assert object.
typedef TAssert AssertType;
/// @brief Constructor
/// @details Registers new assertion failure behaviour. It forwards
/// all the provided parameters to the constructor of embedded
/// assertion object of type TAssert.
/// @note Thread safety: Unsafe
/// @note Exception guarantee: Depends on exception guarantee of the
/// TAssert's constructor.
template<typename... TParams>
EnableAssert(TParams&&... args)
: assert_(std::forward<TParams>(args)...),
prevAssert_(AssertManager::instance().reset(&assert_))
{
}
/// @brief Destructor
/// @details Restores the assertion behaviour that was recorded during
/// the instantiation of this object.
/// @note Thread safety: Unsafe
/// @note Exception guarantee: Depends on exception guarantee of the
/// TAssert's destructor.
~EnableAssert()
{
AssertManager::instance().reset(prevAssert_);
}
/// @brief Provides reference to internal Assert object
/// @return Reference to object of type TAssert.
/// @note Thread safety: Safe
/// @note Exception guarantee: No throw.
AssertType& getAssert()
{
return assert_;
}
private:
AssertType assert_;
Assert* prevAssert_;
};
#ifndef NDEBUG
/// @cond DOCUCMENT_AM_ASSERT_FUNCTION
#ifndef __ASSERT_FUNCTION
#define GASSERT_FUNCTION_STR __FUNCTION__
#else // #ifndef __ASSERT_FUNCTION
#define GASSERT_FUNCTION_STR __ASSERT_FUNCTION
#endif // #ifndef __ASSERT_FUNCTION
#ifndef NOSTDLIB
#define GASSERT_FAIL_FUNC(expr) assert(expr)
#else // #ifndef NOSTDLIB
#define GASSERT_FAIL_FUNC(expr) embxx::util::AssertManager::instance().infiniteLoop()
#endif // #ifndef NOSTDLIB
/// @endcond
/// @brief Generic assert macro
/// @details Will use custom assertion failure behaviour if such is defined,
/// otherwise it will use standard "assert()" macro.
/// In case NOSTDLIB is defined and no custom assertion failure was
/// enabled, infinite loop will be executed.
/// @param expr Boolean expression
#define GASSERT(expr) \
((expr) \
? static_cast<void>(0) \
: (embxx::util::AssertManager::instance().hasAssertRegistered() \
? embxx::util::AssertManager::instance().getAssert()->fail( \
#expr, __FILE__, __LINE__, GASSERT_FUNCTION_STR) \
: GASSERT_FAIL_FUNC(expr)))
#else // #ifndef NDEBUG
#define GASSERT(expr) static_cast<void>(0)
#endif // #ifndef NDEBUG
/// @}
} // namespace util
} // namespace embxx