From 044bad923be339786982a571373067bc306dce3d Mon Sep 17 00:00:00 2001 From: Dirk Ziegelmeier Date: Thu, 19 Dec 2024 07:28:33 +0100 Subject: [PATCH] Make it possible to use own assertion framework --- include/cpp_event_framework/Concepts.hxx | 26 ++++++++++++- include/cpp_event_framework/Pool.hxx | 7 ++-- include/cpp_event_framework/Signal.hxx | 20 +++++----- include/cpp_event_framework/Statemachine.hxx | 40 +++++++++++--------- include/cpp_event_framework/StaticPool.hxx | 10 ++--- 5 files changed, 67 insertions(+), 36 deletions(-) diff --git a/include/cpp_event_framework/Concepts.hxx b/include/cpp_event_framework/Concepts.hxx index bfb8302..f4ceff8 100644 --- a/include/cpp_event_framework/Concepts.hxx +++ b/include/cpp_event_framework/Concepts.hxx @@ -10,6 +10,7 @@ #pragma once +#include #include #include #include @@ -17,7 +18,7 @@ namespace cpp_event_framework { /** - * @brief Concept for DefaultConstructible, DefaultDesctructible, BasicLockable + * @brief Concept for Mutex: DefaultConstructible, DefaultDesctructible, BasicLockable */ template concept Mutex = std::is_constructible_v && std::is_destructible_v && requires(T a) { @@ -25,12 +26,35 @@ concept Mutex = std::is_constructible_v && std::is_destructible_v && requi { a.unlock() }; }; +/** + * @brief Concept for semaphore: DefaultConstructible, DefaultDesctructible + */ template concept Semaphore = std::is_constructible_v && std::is_destructible_v && requires(T a) { { a.acquire() }; { a.release() }; }; +/** + * @brief Concept for assertion function + */ +template +concept AssertionProvider = requires(T) { + { T::Assert(bool()) }; +}; + +/** + * @brief Default assertion provider that uses C-library assert() + */ +class DefaultAssertionProvider +{ +public: + static void Assert(bool condition) + { + assert(condition); + } +}; + /** * @brief Concept for a provider of a polymorphic allocator (std::pmr::memory_resource) */ diff --git a/include/cpp_event_framework/Pool.hxx b/include/cpp_event_framework/Pool.hxx index 1ee051d..e00fd28 100644 --- a/include/cpp_event_framework/Pool.hxx +++ b/include/cpp_event_framework/Pool.hxx @@ -10,7 +10,6 @@ #pragma once -#include #include #include #include @@ -32,7 +31,8 @@ namespace cpp_event_framework * NamedRequirements: DefaultConstructible, Destructible, BasicLockable * @tparam Alignment Alignment requirement */ -template +template class Pool : public std::pmr::memory_resource { public: @@ -78,9 +78,10 @@ public: */ void* do_allocate(size_t bytes, size_t /*alignment*/) override { - assert(bytes <= element_size_); + AssertionProviderType::Assert(bytes <= element_size_); std::scoped_lock lock(mutex_); + AssertionProviderType::Assert(!pool_.empty()); auto* result = pool_.front(); pool_.pop(); return result; diff --git a/include/cpp_event_framework/Signal.hxx b/include/cpp_event_framework/Signal.hxx index 4ae23ee..6a5122f 100644 --- a/include/cpp_event_framework/Signal.hxx +++ b/include/cpp_event_framework/Signal.hxx @@ -10,7 +10,6 @@ #pragma once -#include #include #include #include @@ -107,7 +106,7 @@ concept SignalSubclass = std::is_base_of_v; * * @tparam T Name of inhering class */ -template +template class CustomAllocator { public: @@ -116,7 +115,7 @@ public: */ static void SetAllocator(std::pmr::memory_resource* alloc) { - assert(allocator == nullptr); + AssertionProviderType::Assert(allocator == nullptr); allocator = alloc; } @@ -125,7 +124,7 @@ public: */ static void SetAllocator(std::shared_ptr alloc) { - assert(allocator == nullptr); + AssertionProviderType::Assert(allocator == nullptr); shared_allocator = std::move(alloc); allocator = shared_allocator.get(); } @@ -142,11 +141,11 @@ private: static std::pmr::memory_resource* allocator; static std::shared_ptr shared_allocator; }; -template -std::pmr::memory_resource* CustomAllocator::allocator = nullptr; +template +std::pmr::memory_resource* CustomAllocator::allocator = nullptr; -template -std::shared_ptr CustomAllocator::shared_allocator = nullptr; +template +std::shared_ptr CustomAllocator::shared_allocator = nullptr; /** * @brief Signal event template @@ -157,7 +156,8 @@ std::shared_ptr CustomAllocator::shared_allocator * @tparam BaseType Base class to inherit from */ template + PolymorphicAllocatorProvider AllocatorType = HeapAllocator, + AssertionProvider AssertionProviderType = DefaultAssertionProvider> class SignalBase : public BaseType { public: @@ -193,7 +193,7 @@ public: */ static SPtr FromSignal(const Signal::SPtr& event) { - assert(Check(event)); + AssertionProviderType::Assert(Check(event)); return std::static_pointer_cast(event); } diff --git a/include/cpp_event_framework/Statemachine.hxx b/include/cpp_event_framework/Statemachine.hxx index 73163fe..4805560 100644 --- a/include/cpp_event_framework/Statemachine.hxx +++ b/include/cpp_event_framework/Statemachine.hxx @@ -9,7 +9,6 @@ #pragma once -#include #include #include #include @@ -84,7 +83,8 @@ inline constexpr EStateFlags& operator&=(EStateFlags& lhs, EStateFlags rhs) * @tparam EventType Event type * @tparam HistoryMapAllocator Allocator for history map (only when history states are used) */ -template +template class Statemachine { public: @@ -435,7 +435,7 @@ public: */ void Init(ImplPtr impl, const char* name) { - assert(impl != nullptr); + AssertionProviderType::Assert(impl != nullptr); name_ = name; impl_ = impl; } @@ -446,7 +446,7 @@ public: */ void Start(const State* initial) { - assert(impl_ != nullptr); // Most probably you forgot to call Init() + AssertionProviderType::Assert(impl_ != nullptr); // Most probably you forgot to call Init() current_state_ = &kInTransition; initial_.clear(); EnterStatesFromDownTo(nullptr, initial); @@ -459,8 +459,8 @@ public: */ void React(Event event) { - assert(current_state_ != nullptr); // Most probably you forgot to call Start() - assert(!working_); // Most probably you are recursively calling React() + AssertionProviderType::Assert(current_state_ != nullptr); // Most probably you forgot to call Start() + AssertionProviderType::Assert(!working_); // Most probably you are recursively calling React() working_ = true; Transition transition(kInTransition); @@ -477,7 +477,7 @@ public: if (transition.target_ == &kDeferEvent) { - assert(on_defer_event_ != nullptr); + AssertionProviderType::Assert(on_defer_event_ != nullptr); on_defer_event_(*s, event); working_ = false; return; @@ -527,7 +527,7 @@ public: */ void RecallEvents() { - assert(on_recall_deferred_events_ != nullptr); + AssertionProviderType::Assert(on_recall_deferred_events_ != nullptr); on_recall_deferred_events_(*current_state_); } @@ -809,13 +809,19 @@ private: } }; -template -const typename Statemachine::State Statemachine::kNone = - typename Statemachine::State("None", nullptr); -template -const typename Statemachine::State Statemachine::kInTransition = - typename Statemachine::State("InTransition", nullptr); -template -const typename Statemachine::State Statemachine::kDeferEvent = - typename Statemachine::State("Defer", nullptr); +template +const typename Statemachine::State + Statemachine::kNone = + typename Statemachine::State("None", nullptr); +template +const typename Statemachine::State + Statemachine::kInTransition = + typename Statemachine::State("InTransition", nullptr); +template +const typename Statemachine::State + Statemachine::kDeferEvent = + typename Statemachine::State("Defer", nullptr); } // namespace cpp_event_framework diff --git a/include/cpp_event_framework/StaticPool.hxx b/include/cpp_event_framework/StaticPool.hxx index b8d3ef9..285aad6 100644 --- a/include/cpp_event_framework/StaticPool.hxx +++ b/include/cpp_event_framework/StaticPool.hxx @@ -11,7 +11,6 @@ #pragma once #include -#include #include #include #include @@ -32,7 +31,8 @@ namespace cpp_event_framework * NamedRequirements: DefaultConstructible, Destructible, BasicLockable * @tparam Alignment Alignment requirement */ -template +template class StaticPool final : public std::pmr::memory_resource { private: @@ -80,10 +80,10 @@ public: */ void* do_allocate(size_t bytes, size_t /*alignment*/) override { - assert(bytes <= kAlignedElementSize); + AssertionProviderType::Assert(bytes <= kAlignedElementSize); std::scoped_lock lock(mutex_); - assert(FillLevel() != 0); + AssertionProviderType::Assert(FillLevel() != 0); auto* result = first_; first_ = result->next; @@ -102,7 +102,7 @@ public: ptr->next = first_; first_ = ptr; fill_level_++; - assert(FillLevel() <= NumElements); + AssertionProviderType::Assert(FillLevel() <= NumElements); } /**