Assert.hpp
Go to the documentation of this file.
1 // @formatter:off
2 //
3 // Balau core C++ library
4 //
5 // Copyright (C) 2008 Bora Software (contact@borasoftware.com)
6 //
7 // Licensed under the Boost Software License - Version 1.0 - August 17th, 2003.
8 // See the LICENSE file for the full license text.
9 //
10 
16 
17 #ifndef COM_BORA_SOFTWARE__BALAU_DEV__ASSERT
18 #define COM_BORA_SOFTWARE__BALAU_DEV__ASSERT
19 
20 #include <boost/core/ignore_unused.hpp>
21 #include <string>
22 
23 #ifdef BALAU_ENABLE_STACKTRACES
24  #include <boost/stacktrace.hpp>
25 #endif
26 
27 #include <iostream>
28 #include <sstream>
29 
30 namespace Balau {
31 
35 class Assert {
39  public: static void fail(const char * fatalMessage) {
40  performAssertion(false, fatalMessage, "Fail called");
41  }
42 
44 
48  public: template <typename StringFunctionT>
49  static void assertion(bool test, StringFunctionT function) {
50  performAssertion(test, function, "Bug found");
51  }
52 
56  public: static void assertion(bool test, const char * fatalMessage) {
57  performAssertion(test, fatalMessage, "Bug found");
58  }
59 
63  public: static void assertion(bool test) {
64  performAssertion(test, "", "Bug found");
65  }
66 
70  public: template <typename TestFunctionT, typename StringFunctionT>
71  static void assertion(TestFunctionT test, StringFunctionT function) {
72  performAssertion(test, function, "Bug found");
73  }
74 
78  public: template <typename TestFunctionT>
79  static void assertion(TestFunctionT test, const char * fatalMessage) {
80  performAssertion(test, fatalMessage, "Bug found");
81  }
82 
86  public: template <typename TestFunctionT>
87  static void assertion(TestFunctionT test) {
88  performAssertion(test, "", "Bug found");
89  }
90 
92 
93  private: template <typename FunctionT>
94  static void performAssertion(bool test, FunctionT function, const char * testType) {
95  boost::ignore_unused(test);
96  boost::ignore_unused(function);
97  boost::ignore_unused(testType);
98 
99  #ifdef BALAU_DEBUG
100  if (!test) {
101  const std::string message = function();
102  abortProcess(message.c_str(), testType);
103  }
104  #endif
105  }
106 
107  private: template <typename TestT, typename FunctionT>
108  static void performAssertion(TestT test, FunctionT function, const char * testType) {
109  boost::ignore_unused(test);
110  boost::ignore_unused(function);
111  boost::ignore_unused(testType);
112 
113  #ifdef BALAU_DEBUG
114  if (!test()) {
115  const std::string message = function();
116  abortProcess(message.c_str(), testType);
117  }
118  #endif
119  }
120 
121  private: static void performAssertion(bool test, const char * fatalMessage, const char * testType) {
122  boost::ignore_unused(test);
123  boost::ignore_unused(fatalMessage);
124  boost::ignore_unused(testType);
125 
126  #ifdef BALAU_DEBUG
127  if (!test) {
128  abortProcess(fatalMessage, testType);
129  }
130  #endif
131  }
132 
133  private: template <typename TestT>
134  static void performAssertion(TestT test, const char * fatalMessage, const char * testType) {
135  boost::ignore_unused(test);
136  boost::ignore_unused(fatalMessage);
137  boost::ignore_unused(testType);
138 
139  #ifdef BALAU_DEBUG
140  if (!test()) {
141  abortProcess(fatalMessage, testType);
142  }
143  #endif
144  }
145 
146  private: static void abortProcess(const char * fatalMessage, const char * testType) {
147  std::ostringstream str;
148 
149  if (fatalMessage != nullptr) {
150  str << testType << ": " << fatalMessage << "\n";
151  } else {
152  str << testType << "\n";
153  }
154 
155  #ifdef BALAU_ENABLE_STACKTRACES
156  str << "Stack trace: " << boost::stacktrace::stacktrace() << "\n";
157  #endif
158 
159  str << "Program will abort now\n";
160  std::cerr << str.str();
161  abort();
162  }
163 };
164 
165 } // namespace Balau
166 
167 #endif // COM_BORA_SOFTWARE__BALAU_DEV__ASSERT
static void assertion(TestFunctionT test, StringFunctionT function)
If the bug test assertion predicate function fails, abort after logging the message supplied by the f...
Definition: Assert.hpp:71
The root Balau namespace.
Definition: ApplicationConfiguration.hpp:23
static void fail(const char *fatalMessage)
Abort after logging the message.
Definition: Assert.hpp:39
static void assertion(TestFunctionT test, const char *fatalMessage)
If the bug test assertion predicate function fails, abort after logging the message.
Definition: Assert.hpp:79
static void assertion(bool test, const char *fatalMessage)
If the bug test assertion fails, abort after logging the message.
Definition: Assert.hpp:56
static void assertion(bool test)
Abort if the bug test fails.
Definition: Assert.hpp:63
static void assertion(TestFunctionT test)
Abort if the bug test assertion predicate function fails.
Definition: Assert.hpp:87
An assertion class for development purposes.
Definition: Assert.hpp:35
static void assertion(bool test, StringFunctionT function)
If the bug test assertion fails, abort after logging the message supplied by the function.
Definition: Assert.hpp:49