Skip to content

Commit 044bad9

Browse files
committed
Make it possible to use own assertion framework
1 parent aa66c8d commit 044bad9

5 files changed

Lines changed: 67 additions & 36 deletions

File tree

include/cpp_event_framework/Concepts.hxx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,51 @@
1010

1111
#pragma once
1212

13+
#include <cassert>
1314
#include <cstddef>
1415
#include <memory_resource>
1516
#include <type_traits>
1617

1718
namespace cpp_event_framework
1819
{
1920
/**
20-
* @brief Concept for DefaultConstructible, DefaultDesctructible, BasicLockable
21+
* @brief Concept for Mutex: DefaultConstructible, DefaultDesctructible, BasicLockable
2122
*/
2223
template <typename T>
2324
concept Mutex = std::is_constructible_v<T> && std::is_destructible_v<T> && requires(T a) {
2425
{ a.lock() };
2526
{ a.unlock() };
2627
};
2728

29+
/**
30+
* @brief Concept for semaphore: DefaultConstructible, DefaultDesctructible
31+
*/
2832
template <typename T>
2933
concept Semaphore = std::is_constructible_v<T, std::ptrdiff_t> && std::is_destructible_v<T> && requires(T a) {
3034
{ a.acquire() };
3135
{ a.release() };
3236
};
3337

38+
/**
39+
* @brief Concept for assertion function
40+
*/
41+
template <typename T>
42+
concept AssertionProvider = requires(T) {
43+
{ T::Assert(bool()) };
44+
};
45+
46+
/**
47+
* @brief Default assertion provider that uses C-library assert()
48+
*/
49+
class DefaultAssertionProvider
50+
{
51+
public:
52+
static void Assert(bool condition)
53+
{
54+
assert(condition);
55+
}
56+
};
57+
3458
/**
3559
* @brief Concept for a provider of a polymorphic allocator (std::pmr::memory_resource)
3660
*/

include/cpp_event_framework/Pool.hxx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
#pragma once
1212

13-
#include <cassert>
1413
#include <cstddef>
1514
#include <cstdint>
1615
#include <memory>
@@ -32,7 +31,8 @@ namespace cpp_event_framework
3231
* NamedRequirements: DefaultConstructible, Destructible, BasicLockable
3332
* @tparam Alignment Alignment requirement
3433
*/
35-
template <Mutex MutexType = std::mutex, size_t Alignment = sizeof(uint64_t)>
34+
template <Mutex MutexType = std::mutex, size_t Alignment = sizeof(uint64_t),
35+
AssertionProvider AssertionProviderType = DefaultAssertionProvider>
3636
class Pool : public std::pmr::memory_resource
3737
{
3838
public:
@@ -78,9 +78,10 @@ public:
7878
*/
7979
void* do_allocate(size_t bytes, size_t /*alignment*/) override
8080
{
81-
assert(bytes <= element_size_);
81+
AssertionProviderType::Assert(bytes <= element_size_);
8282

8383
std::scoped_lock lock(mutex_);
84+
AssertionProviderType::Assert(!pool_.empty());
8485
auto* result = pool_.front();
8586
pool_.pop();
8687
return result;

include/cpp_event_framework/Signal.hxx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
#pragma once
1212

13-
#include <cassert>
1413
#include <cstdint>
1514
#include <memory>
1615
#include <memory_resource>
@@ -107,7 +106,7 @@ concept SignalSubclass = std::is_base_of_v<Signal, T>;
107106
*
108107
* @tparam T Name of inhering class
109108
*/
110-
template <typename T>
109+
template <typename T, AssertionProvider AssertionProviderType = DefaultAssertionProvider>
111110
class CustomAllocator
112111
{
113112
public:
@@ -116,7 +115,7 @@ public:
116115
*/
117116
static void SetAllocator(std::pmr::memory_resource* alloc)
118117
{
119-
assert(allocator == nullptr);
118+
AssertionProviderType::Assert(allocator == nullptr);
120119
allocator = alloc;
121120
}
122121

@@ -125,7 +124,7 @@ public:
125124
*/
126125
static void SetAllocator(std::shared_ptr<std::pmr::memory_resource> alloc)
127126
{
128-
assert(allocator == nullptr);
127+
AssertionProviderType::Assert(allocator == nullptr);
129128
shared_allocator = std::move(alloc);
130129
allocator = shared_allocator.get();
131130
}
@@ -142,11 +141,11 @@ private:
142141
static std::pmr::memory_resource* allocator;
143142
static std::shared_ptr<std::pmr::memory_resource> shared_allocator;
144143
};
145-
template <typename T>
146-
std::pmr::memory_resource* CustomAllocator<T>::allocator = nullptr;
144+
template <typename T, AssertionProvider AssertionProviderType>
145+
std::pmr::memory_resource* CustomAllocator<T, AssertionProviderType>::allocator = nullptr;
147146

148-
template <typename T>
149-
std::shared_ptr<std::pmr::memory_resource> CustomAllocator<T>::shared_allocator = nullptr;
147+
template <typename T, AssertionProvider AssertionProviderType>
148+
std::shared_ptr<std::pmr::memory_resource> CustomAllocator<T, AssertionProviderType>::shared_allocator = nullptr;
150149

151150
/**
152151
* @brief Signal event template
@@ -157,7 +156,8 @@ std::shared_ptr<std::pmr::memory_resource> CustomAllocator<T>::shared_allocator
157156
* @tparam BaseType Base class to inherit from
158157
*/
159158
template <typename T, Signal::IdType id, SignalSubclass BaseType = Signal,
160-
PolymorphicAllocatorProvider AllocatorType = HeapAllocator>
159+
PolymorphicAllocatorProvider AllocatorType = HeapAllocator,
160+
AssertionProvider AssertionProviderType = DefaultAssertionProvider>
161161
class SignalBase : public BaseType
162162
{
163163
public:
@@ -193,7 +193,7 @@ public:
193193
*/
194194
static SPtr FromSignal(const Signal::SPtr& event)
195195
{
196-
assert(Check(event));
196+
AssertionProviderType::Assert(Check(event));
197197
return std::static_pointer_cast<T>(event);
198198
}
199199

include/cpp_event_framework/Statemachine.hxx

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#pragma once
1111

12-
#include <cassert>
1312
#include <functional>
1413
#include <map>
1514
#include <memory>
@@ -84,7 +83,8 @@ inline constexpr EStateFlags& operator&=(EStateFlags& lhs, EStateFlags rhs)
8483
* @tparam EventType Event type
8584
* @tparam HistoryMapAllocator Allocator for history map (only when history states are used)
8685
*/
87-
template <typename ImplType, typename EventType, PolymorphicAllocatorProvider HistoryMapAllocator = HeapAllocator>
86+
template <typename ImplType, typename EventType, PolymorphicAllocatorProvider HistoryMapAllocator = HeapAllocator,
87+
AssertionProvider AssertionProviderType = DefaultAssertionProvider>
8888
class Statemachine
8989
{
9090
public:
@@ -435,7 +435,7 @@ public:
435435
*/
436436
void Init(ImplPtr impl, const char* name)
437437
{
438-
assert(impl != nullptr);
438+
AssertionProviderType::Assert(impl != nullptr);
439439
name_ = name;
440440
impl_ = impl;
441441
}
@@ -446,7 +446,7 @@ public:
446446
*/
447447
void Start(const State* initial)
448448
{
449-
assert(impl_ != nullptr); // Most probably you forgot to call Init()
449+
AssertionProviderType::Assert(impl_ != nullptr); // Most probably you forgot to call Init()
450450
current_state_ = &kInTransition;
451451
initial_.clear();
452452
EnterStatesFromDownTo(nullptr, initial);
@@ -459,8 +459,8 @@ public:
459459
*/
460460
void React(Event event)
461461
{
462-
assert(current_state_ != nullptr); // Most probably you forgot to call Start()
463-
assert(!working_); // Most probably you are recursively calling React()
462+
AssertionProviderType::Assert(current_state_ != nullptr); // Most probably you forgot to call Start()
463+
AssertionProviderType::Assert(!working_); // Most probably you are recursively calling React()
464464
working_ = true;
465465

466466
Transition transition(kInTransition);
@@ -477,7 +477,7 @@ public:
477477

478478
if (transition.target_ == &kDeferEvent)
479479
{
480-
assert(on_defer_event_ != nullptr);
480+
AssertionProviderType::Assert(on_defer_event_ != nullptr);
481481
on_defer_event_(*s, event);
482482
working_ = false;
483483
return;
@@ -527,7 +527,7 @@ public:
527527
*/
528528
void RecallEvents()
529529
{
530-
assert(on_recall_deferred_events_ != nullptr);
530+
AssertionProviderType::Assert(on_recall_deferred_events_ != nullptr);
531531
on_recall_deferred_events_(*current_state_);
532532
}
533533

@@ -809,13 +809,19 @@ private:
809809
}
810810
};
811811

812-
template <typename Impl, typename Event, PolymorphicAllocatorProvider Allocator>
813-
const typename Statemachine<Impl, Event, Allocator>::State Statemachine<Impl, Event, Allocator>::kNone =
814-
typename Statemachine<Impl, Event, Allocator>::State("None", nullptr);
815-
template <typename Impl, typename Event, PolymorphicAllocatorProvider Allocator>
816-
const typename Statemachine<Impl, Event, Allocator>::State Statemachine<Impl, Event, Allocator>::kInTransition =
817-
typename Statemachine<Impl, Event, Allocator>::State("InTransition", nullptr);
818-
template <typename Impl, typename Event, PolymorphicAllocatorProvider Allocator>
819-
const typename Statemachine<Impl, Event, Allocator>::State Statemachine<Impl, Event, Allocator>::kDeferEvent =
820-
typename Statemachine<Impl, Event, Allocator>::State("Defer", nullptr);
812+
template <typename Impl, typename Event, PolymorphicAllocatorProvider Allocator,
813+
AssertionProvider AssertionProviderType>
814+
const typename Statemachine<Impl, Event, Allocator, AssertionProviderType>::State
815+
Statemachine<Impl, Event, Allocator, AssertionProviderType>::kNone =
816+
typename Statemachine<Impl, Event, Allocator, AssertionProviderType>::State("None", nullptr);
817+
template <typename Impl, typename Event, PolymorphicAllocatorProvider Allocator,
818+
AssertionProvider AssertionProviderType>
819+
const typename Statemachine<Impl, Event, Allocator, AssertionProviderType>::State
820+
Statemachine<Impl, Event, Allocator, AssertionProviderType>::kInTransition =
821+
typename Statemachine<Impl, Event, Allocator, AssertionProviderType>::State("InTransition", nullptr);
822+
template <typename Impl, typename Event, PolymorphicAllocatorProvider Allocator,
823+
AssertionProvider AssertionProviderType>
824+
const typename Statemachine<Impl, Event, Allocator, AssertionProviderType>::State
825+
Statemachine<Impl, Event, Allocator, AssertionProviderType>::kDeferEvent =
826+
typename Statemachine<Impl, Event, Allocator, AssertionProviderType>::State("Defer", nullptr);
821827
} // namespace cpp_event_framework

include/cpp_event_framework/StaticPool.hxx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#pragma once
1212

1313
#include <atomic>
14-
#include <cassert>
1514
#include <cstddef>
1615
#include <cstdint>
1716
#include <cstring>
@@ -32,7 +31,8 @@ namespace cpp_event_framework
3231
* NamedRequirements: DefaultConstructible, Destructible, BasicLockable
3332
* @tparam Alignment Alignment requirement
3433
*/
35-
template <uint32_t NumElements, size_t ElemSize, Mutex MutexType = std::mutex, size_t Alignment = sizeof(uint64_t)>
34+
template <uint32_t NumElements, size_t ElemSize, Mutex MutexType = std::mutex,
35+
AssertionProvider AssertionProviderType = DefaultAssertionProvider, size_t Alignment = sizeof(uint64_t)>
3636
class StaticPool final : public std::pmr::memory_resource
3737
{
3838
private:
@@ -80,10 +80,10 @@ public:
8080
*/
8181
void* do_allocate(size_t bytes, size_t /*alignment*/) override
8282
{
83-
assert(bytes <= kAlignedElementSize);
83+
AssertionProviderType::Assert(bytes <= kAlignedElementSize);
8484

8585
std::scoped_lock lock(mutex_);
86-
assert(FillLevel() != 0);
86+
AssertionProviderType::Assert(FillLevel() != 0);
8787

8888
auto* result = first_;
8989
first_ = result->next;
@@ -102,7 +102,7 @@ public:
102102
ptr->next = first_;
103103
first_ = ptr;
104104
fill_level_++;
105-
assert(FillLevel() <= NumElements);
105+
AssertionProviderType::Assert(FillLevel() <= NumElements);
106106
}
107107

108108
/**

0 commit comments

Comments
 (0)