Browse Source

Added `jessilib_debug_assert`

master
Jessica James 6 years ago
parent
commit
37323eb003
  1. 4
      src/common/timer/timer.cpp
  2. 2
      src/common/timer/timer_context.cpp
  3. 56
      src/include/assert.hpp
  4. 2
      src/include/timer.hpp

4
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) } } { : m_context{ new impl::timer_context{ in_period, std::move(in_callback) } } {
// Add timer_context to timer_manager // 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 // 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_debug_assert(in_period.count() != 0)
if jessilib_assert(!m_context->null()) { && jessilib_debug_assert(!m_context->null())) {
// Add timer // Add timer
impl::timer_manager& manager = impl::timer_manager::instance(); impl::timer_manager& manager = impl::timer_manager::instance();
{ {

2
src/common/timer/timer_context.cpp

@ -65,7 +65,7 @@ void timer_context::detach() {
impl::timer_manager& manager = impl::timer_manager::instance(); impl::timer_manager& manager = impl::timer_manager::instance();
std::list<std::shared_ptr<timer_context>>& detached_timers = manager.m_detached_timers; std::list<std::shared_ptr<timer_context>>& 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<std::mutex> lock(manager.m_detached_timers_mutex); std::lock_guard<std::mutex> lock(manager.m_detached_timers_mutex);
// We need to attach this new shared_ptr to the callback // We need to attach this new shared_ptr to the callback
m_self = detached_timers.emplace(detached_timers.end(), shared_from_this()); m_self = detached_timers.emplace(detached_timers.end(), shared_from_this());

56
src/include/assert.hpp

@ -19,6 +19,7 @@
#pragma once #pragma once
#include <stdexcept> #include <stdexcept>
#include <iostream>
/** Macros */ /** Macros */
@ -27,42 +28,59 @@
#define STRINGIFY_HELPER(line) \ #define STRINGIFY_HELPER(line) \
STRINGIFY(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 // Returns a boolean expression indicating assertion success/failure
#ifndef jessilib_assert
#define jessilib_assert(expression) \ #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 { namespace jessilib {
/** Exception type */ /** Exception type */
class assertion_failed : public std::logic_error { class assertion_failed : public std::logic_error {
public: public:
inline explicit assertion_failed(const char* expression) inline explicit assertion_failed(const char* expression)
: std::logic_error{ expression } { : std::logic_error{ expression } {
} }
}; };
namespace impl { namespace impl {
/** Macro helpers */ /** Macro helpers */
inline bool _assert_helper(bool value, [[maybe_unused]] const char* message) { inline bool _assert_helper(bool value, const char* message) {
#ifndef NDEBUG
if (!value) { if (!value) {
// TODO: wrap std::cerr
std::cerr << message << std::endl;
throw assertion_failed{message}; throw assertion_failed{message};
} }
#endif // NDEBUG
return value; return value;
} }
} // namespace impl inline bool _debug_assert_helper(bool value, const char* message) {
} // namespace jessilib if (!value) {
// TODO: wrap std::cerr
std::cerr << message << std::endl;
// Provides for disabling of assertions; will likely produce warnings #ifndef NDEBUG
#ifdef DISABLE_ASSERTIONS throw assertion_failed{message};
#endif // NDEBUG
}
// Disable jessilib_assert return value;
#undef jessilib_assert }
#define jessilib_assert(expression) \
(true)
#endif // DISABLE_ASSERTIONS } // namespace impl
} // namespace jessilib

2
src/include/timer.hpp

@ -91,7 +91,6 @@ public:
syncrhonized_timer& operator=(syncrhonized_timer&& in_timer) = default; syncrhonized_timer& operator=(syncrhonized_timer&& in_timer) = default;
}; };
/** Useful when performing actions within a timer which may destroy the timer's callback */ /** Useful when performing actions within a timer which may destroy the timer's callback */
class cancel_token { class cancel_token {
@ -115,5 +114,4 @@ private:
impl::cancel_token_context* m_context; impl::cancel_token_context* m_context;
}; };
} // namespace jessilib } // namespace jessilib
Loading…
Cancel
Save