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. 44
      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) } } {
// 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();
{

2
src/common/timer/timer_context.cpp

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

44
src/include/assert.hpp

@ -19,6 +19,7 @@
#pragma once
#include <stdexcept>
#include <iostream>
/** Macros */
@ -27,9 +28,22 @@
#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 {
@ -44,25 +58,29 @@ namespace jessilib {
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

2
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
Loading…
Cancel
Save