From 37323eb003e2e5785488a3ced01e347daff403e3 Mon Sep 17 00:00:00 2001 From: Jessica James Date: Wed, 1 Aug 2018 20:39:44 -0500 Subject: [PATCH] Added `jessilib_debug_assert` --- src/common/timer/timer.cpp | 4 +-- src/common/timer/timer_context.cpp | 2 +- src/include/assert.hpp | 56 ++++++++++++++++++++---------- src/include/timer.hpp | 2 -- 4 files changed, 40 insertions(+), 24 deletions(-) diff --git a/src/common/timer/timer.cpp b/src/common/timer/timer.cpp index 1ee73e8..14c07ef 100644 --- a/src/common/timer/timer.cpp +++ b/src/common/timer/timer.cpp @@ -84,8 +84,8 @@ timer::timer(duration_t in_period, function_t in_callback) : m_context{ new impl::timer_context{ in_period, std::move(in_callback) } } { // Add timer_context to timer_manager // Note: this logic must be here (rather than timer_context) to ensure timer_context is wrapped in a shared_ptr - if jessilib_assert(in_period.count() != 0) - if jessilib_assert(!m_context->null()) { + if (jessilib_debug_assert(in_period.count() != 0) + && jessilib_debug_assert(!m_context->null())) { // Add timer impl::timer_manager& manager = impl::timer_manager::instance(); { diff --git a/src/common/timer/timer_context.cpp b/src/common/timer/timer_context.cpp index 65247bf..b19c383 100644 --- a/src/common/timer/timer_context.cpp +++ b/src/common/timer/timer_context.cpp @@ -65,7 +65,7 @@ void timer_context::detach() { impl::timer_manager& manager = impl::timer_manager::instance(); std::list>& detached_timers = manager.m_detached_timers; - jessilib_assert(!detached()); // you cannot detach a timer that is already detached + jessilib_debug_assert(!detached()); // you cannot detach a timer that is already detached std::lock_guard lock(manager.m_detached_timers_mutex); // We need to attach this new shared_ptr to the callback m_self = detached_timers.emplace(detached_timers.end(), shared_from_this()); diff --git a/src/include/assert.hpp b/src/include/assert.hpp index 14e6621..773c322 100644 --- a/src/include/assert.hpp +++ b/src/include/assert.hpp @@ -19,6 +19,7 @@ #pragma once #include +#include /** Macros */ @@ -27,42 +28,59 @@ #define STRINGIFY_HELPER(line) \ STRINGIFY(line) +#ifndef JESSILIB_ASSERT_MESSAGE +#define JESSILIB_ASSERT_MESSAGE(expression) \ + "Failed assertion: '" #expression "' at " __FILE__ ":" STRINGIFY_HELPER(__LINE__) +#endif // JESSILIB_ASSERT_MESSAGE + // Returns a boolean expression indicating assertion success/failure +#ifndef jessilib_assert #define jessilib_assert(expression) \ - (::jessilib::impl::_assert_helper( expression, "Failed assertion: '" #expression "' at " __FILE__ ":" STRINGIFY_HELPER(__LINE__) )) + (::jessilib::impl::_assert_helper( expression, JESSILIB_ASSERT_MESSAGE(expression) )) +#endif // jessilib_assert + +// Similar to jessilib_assert, except exceptions are thrown only when NDEBUG is undefined +#ifndef jessilib_debug_assert +#define jessilib_debug_assert(expression) \ + (::jessilib::impl::_debug_assert_helper( expression, JESSILIB_ASSERT_MESSAGE(expression) )) +#endif // jessilib_debug_assert namespace jessilib { /** Exception type */ - class assertion_failed : public std::logic_error { - public: - inline explicit assertion_failed(const char* expression) - : std::logic_error{ expression } { - } - }; +class assertion_failed : public std::logic_error { +public: + inline explicit assertion_failed(const char* expression) + : std::logic_error{ expression } { + } +}; namespace impl { /** Macro helpers */ -inline bool _assert_helper(bool value, [[maybe_unused]] const char* message) { -#ifndef NDEBUG +inline bool _assert_helper(bool value, const char* message) { if (!value) { + // TODO: wrap std::cerr + std::cerr << message << std::endl; + throw assertion_failed{message}; } -#endif // NDEBUG return value; } -} // namespace impl -} // namespace jessilib +inline bool _debug_assert_helper(bool value, const char* message) { + if (!value) { + // TODO: wrap std::cerr + std::cerr << message << std::endl; -// Provides for disabling of assertions; will likely produce warnings -#ifdef DISABLE_ASSERTIONS +#ifndef NDEBUG + throw assertion_failed{message}; +#endif // NDEBUG + } -// Disable jessilib_assert -#undef jessilib_assert -#define jessilib_assert(expression) \ - (true) + return value; +} -#endif // DISABLE_ASSERTIONS +} // namespace impl +} // namespace jessilib diff --git a/src/include/timer.hpp b/src/include/timer.hpp index e33e0f1..2613da6 100644 --- a/src/include/timer.hpp +++ b/src/include/timer.hpp @@ -91,7 +91,6 @@ public: syncrhonized_timer& operator=(syncrhonized_timer&& in_timer) = default; }; - /** Useful when performing actions within a timer which may destroy the timer's callback */ class cancel_token { @@ -115,5 +114,4 @@ private: impl::cancel_token_context* m_context; }; - } // namespace jessilib \ No newline at end of file