Skip to content

Commit

Permalink
Make it possible to use own assertion framework
Browse files Browse the repository at this point in the history
  • Loading branch information
dziegel committed Dec 19, 2024
1 parent aa66c8d commit 044bad9
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 36 deletions.
26 changes: 25 additions & 1 deletion include/cpp_event_framework/Concepts.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,51 @@

#pragma once

#include <cassert>
#include <cstddef>
#include <memory_resource>
#include <type_traits>

namespace cpp_event_framework
{
/**
* @brief Concept for DefaultConstructible, DefaultDesctructible, BasicLockable
* @brief Concept for Mutex: DefaultConstructible, DefaultDesctructible, BasicLockable
*/
template <typename T>
concept Mutex = std::is_constructible_v<T> && std::is_destructible_v<T> && requires(T a) {
{ a.lock() };
{ a.unlock() };
};

/**
* @brief Concept for semaphore: DefaultConstructible, DefaultDesctructible
*/
template <typename T>
concept Semaphore = std::is_constructible_v<T, std::ptrdiff_t> && std::is_destructible_v<T> && requires(T a) {
{ a.acquire() };
{ a.release() };
};

/**
* @brief Concept for assertion function
*/
template <typename T>
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)
*/
Expand Down
7 changes: 4 additions & 3 deletions include/cpp_event_framework/Pool.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#pragma once

#include <cassert>
#include <cstddef>
#include <cstdint>
#include <memory>
Expand All @@ -32,7 +31,8 @@ namespace cpp_event_framework
* NamedRequirements: DefaultConstructible, Destructible, BasicLockable
* @tparam Alignment Alignment requirement
*/
template <Mutex MutexType = std::mutex, size_t Alignment = sizeof(uint64_t)>
template <Mutex MutexType = std::mutex, size_t Alignment = sizeof(uint64_t),
AssertionProvider AssertionProviderType = DefaultAssertionProvider>
class Pool : public std::pmr::memory_resource
{
public:
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 10 additions & 10 deletions include/cpp_event_framework/Signal.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#pragma once

#include <cassert>
#include <cstdint>
#include <memory>
#include <memory_resource>
Expand Down Expand Up @@ -107,7 +106,7 @@ concept SignalSubclass = std::is_base_of_v<Signal, T>;
*
* @tparam T Name of inhering class
*/
template <typename T>
template <typename T, AssertionProvider AssertionProviderType = DefaultAssertionProvider>
class CustomAllocator
{
public:
Expand All @@ -116,7 +115,7 @@ public:
*/
static void SetAllocator(std::pmr::memory_resource* alloc)
{
assert(allocator == nullptr);
AssertionProviderType::Assert(allocator == nullptr);
allocator = alloc;
}

Expand All @@ -125,7 +124,7 @@ public:
*/
static void SetAllocator(std::shared_ptr<std::pmr::memory_resource> alloc)
{
assert(allocator == nullptr);
AssertionProviderType::Assert(allocator == nullptr);
shared_allocator = std::move(alloc);
allocator = shared_allocator.get();
}
Expand All @@ -142,11 +141,11 @@ private:
static std::pmr::memory_resource* allocator;
static std::shared_ptr<std::pmr::memory_resource> shared_allocator;
};
template <typename T>
std::pmr::memory_resource* CustomAllocator<T>::allocator = nullptr;
template <typename T, AssertionProvider AssertionProviderType>
std::pmr::memory_resource* CustomAllocator<T, AssertionProviderType>::allocator = nullptr;

template <typename T>
std::shared_ptr<std::pmr::memory_resource> CustomAllocator<T>::shared_allocator = nullptr;
template <typename T, AssertionProvider AssertionProviderType>
std::shared_ptr<std::pmr::memory_resource> CustomAllocator<T, AssertionProviderType>::shared_allocator = nullptr;

/**
* @brief Signal event template
Expand All @@ -157,7 +156,8 @@ std::shared_ptr<std::pmr::memory_resource> CustomAllocator<T>::shared_allocator
* @tparam BaseType Base class to inherit from
*/
template <typename T, Signal::IdType id, SignalSubclass BaseType = Signal,
PolymorphicAllocatorProvider AllocatorType = HeapAllocator>
PolymorphicAllocatorProvider AllocatorType = HeapAllocator,
AssertionProvider AssertionProviderType = DefaultAssertionProvider>
class SignalBase : public BaseType
{
public:
Expand Down Expand Up @@ -193,7 +193,7 @@ public:
*/
static SPtr FromSignal(const Signal::SPtr& event)
{
assert(Check(event));
AssertionProviderType::Assert(Check(event));
return std::static_pointer_cast<T>(event);
}

Expand Down
40 changes: 23 additions & 17 deletions include/cpp_event_framework/Statemachine.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#pragma once

#include <cassert>
#include <functional>
#include <map>
#include <memory>
Expand Down Expand Up @@ -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 <typename ImplType, typename EventType, PolymorphicAllocatorProvider HistoryMapAllocator = HeapAllocator>
template <typename ImplType, typename EventType, PolymorphicAllocatorProvider HistoryMapAllocator = HeapAllocator,
AssertionProvider AssertionProviderType = DefaultAssertionProvider>
class Statemachine
{
public:
Expand Down Expand Up @@ -435,7 +435,7 @@ public:
*/
void Init(ImplPtr impl, const char* name)
{
assert(impl != nullptr);
AssertionProviderType::Assert(impl != nullptr);
name_ = name;
impl_ = impl;
}
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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_);
}

Expand Down Expand Up @@ -809,13 +809,19 @@ private:
}
};

template <typename Impl, typename Event, PolymorphicAllocatorProvider Allocator>
const typename Statemachine<Impl, Event, Allocator>::State Statemachine<Impl, Event, Allocator>::kNone =
typename Statemachine<Impl, Event, Allocator>::State("None", nullptr);
template <typename Impl, typename Event, PolymorphicAllocatorProvider Allocator>
const typename Statemachine<Impl, Event, Allocator>::State Statemachine<Impl, Event, Allocator>::kInTransition =
typename Statemachine<Impl, Event, Allocator>::State("InTransition", nullptr);
template <typename Impl, typename Event, PolymorphicAllocatorProvider Allocator>
const typename Statemachine<Impl, Event, Allocator>::State Statemachine<Impl, Event, Allocator>::kDeferEvent =
typename Statemachine<Impl, Event, Allocator>::State("Defer", nullptr);
template <typename Impl, typename Event, PolymorphicAllocatorProvider Allocator,
AssertionProvider AssertionProviderType>
const typename Statemachine<Impl, Event, Allocator, AssertionProviderType>::State
Statemachine<Impl, Event, Allocator, AssertionProviderType>::kNone =
typename Statemachine<Impl, Event, Allocator, AssertionProviderType>::State("None", nullptr);
template <typename Impl, typename Event, PolymorphicAllocatorProvider Allocator,
AssertionProvider AssertionProviderType>
const typename Statemachine<Impl, Event, Allocator, AssertionProviderType>::State
Statemachine<Impl, Event, Allocator, AssertionProviderType>::kInTransition =
typename Statemachine<Impl, Event, Allocator, AssertionProviderType>::State("InTransition", nullptr);
template <typename Impl, typename Event, PolymorphicAllocatorProvider Allocator,
AssertionProvider AssertionProviderType>
const typename Statemachine<Impl, Event, Allocator, AssertionProviderType>::State
Statemachine<Impl, Event, Allocator, AssertionProviderType>::kDeferEvent =
typename Statemachine<Impl, Event, Allocator, AssertionProviderType>::State("Defer", nullptr);
} // namespace cpp_event_framework
10 changes: 5 additions & 5 deletions include/cpp_event_framework/StaticPool.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#pragma once

#include <atomic>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstring>
Expand All @@ -32,7 +31,8 @@ namespace cpp_event_framework
* NamedRequirements: DefaultConstructible, Destructible, BasicLockable
* @tparam Alignment Alignment requirement
*/
template <uint32_t NumElements, size_t ElemSize, Mutex MutexType = std::mutex, size_t Alignment = sizeof(uint64_t)>
template <uint32_t NumElements, size_t ElemSize, Mutex MutexType = std::mutex,
AssertionProvider AssertionProviderType = DefaultAssertionProvider, size_t Alignment = sizeof(uint64_t)>
class StaticPool final : public std::pmr::memory_resource
{
private:
Expand Down Expand Up @@ -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;
Expand All @@ -102,7 +102,7 @@ public:
ptr->next = first_;
first_ = ptr;
fill_level_++;
assert(FillLevel() <= NumElements);
AssertionProviderType::Assert(FillLevel() <= NumElements);
}

/**
Expand Down

0 comments on commit 044bad9

Please sign in to comment.