Skip to content
Snippets Groups Projects
Commit 9119cf97 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Added little assert error class to deal with assert like exceptions

parent e73c51aa
No related branches found
No related tags found
No related merge requests found
...@@ -10,17 +10,55 @@ ...@@ -10,17 +10,55 @@
#else // NDEBUG #else // NDEBUG
#include <rang.hpp> #include <rang.hpp>
#include <iostream>
#include <string>
class AssertError
{
private:
const std::string m_file;
const int m_line;
const std::string m_function;
const std::string m_message;
public:
friend
std::ostream& operator<<(std::ostream& os,
const AssertError& assert_error)
{
os << '\n'
<< rang::style::bold
<< "*** Assertion error ***\n"
<< " at " << assert_error.m_file << ':' << assert_error.m_line << '\n'
<< " in " << assert_error.m_function << '\n'
<< "*** " << rang::fgB::red << assert_error.m_message << rang::fg::reset
<< rang::style::reset << '\n';
return os;
}
AssertError(const AssertError&) = default;
AssertError(std::string file,
int line,
std::string function,
std::string message)
: m_file(file),
m_line(line),
m_function(function),
m_message(message)
{
;
}
~AssertError() = default;
};
#define Assert(assertion) \ #define Assert(assertion) \
if (not (assertion)) { \ if (not (assertion)) { \
std::cerr << rang::style::bold \ throw AssertError(__FILE__, \
<< "Assertion \"" \ __LINE__, \
<< rang::fg::red \ __PRETTY_FUNCTION__, \
<< #assertion \ (#assertion)); \
<< rang::fg::reset \
<< "\" failed\n" \
<< rang::style::reset; \
std::exit(1); \
} }
#endif // NDEBUG #endif // NDEBUG
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment