From 9119cf979d48ede3daeeda1fa2fbfc682a0d379a Mon Sep 17 00:00:00 2001 From: Stephane Del Pino <stephane.delpino44@gmail.com> Date: Fri, 8 Jun 2018 19:17:11 +0200 Subject: [PATCH] Added little assert error class to deal with assert like exceptions --- src/utils/PastisAssert.hpp | 58 +++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 10 deletions(-) diff --git a/src/utils/PastisAssert.hpp b/src/utils/PastisAssert.hpp index 7acded2a5..a6342a41e 100644 --- a/src/utils/PastisAssert.hpp +++ b/src/utils/PastisAssert.hpp @@ -10,17 +10,55 @@ #else // NDEBUG #include <rang.hpp> +#include <iostream> +#include <string> -#define Assert(assertion) \ - if (not (assertion)) { \ - std::cerr << rang::style::bold \ - << "Assertion \"" \ - << rang::fg::red \ - << #assertion \ - << rang::fg::reset \ - << "\" failed\n" \ - << rang::style::reset; \ - std::exit(1); \ +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) \ + if (not (assertion)) { \ + throw AssertError(__FILE__, \ + __LINE__, \ + __PRETTY_FUNCTION__, \ + (#assertion)); \ } #endif // NDEBUG -- GitLab