Skip to content

Commit

Permalink
feat: add NotifyingSharedOptional::OnAllocatable overload (#667)
Browse files Browse the repository at this point in the history
* feat: add NotifyingSharedOptional::OnAllocatable overload

New overload allows for immediately notifying when the SharedOptional is already allocatable

* feat: add unit test for coverage
  • Loading branch information
daantimmer authored Jul 8, 2024
1 parent 3653875 commit cb37f67
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions infra/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ target_sources(infra.util PRIVATE
SharedObjectAllocator.hpp
SharedObjectAllocatorFixedSize.hpp
SharedObjectAllocatorHeap.hpp
SharedOptional.cpp
SharedOptional.hpp
SharedOwnedObserver.hpp
SharedPtr.cpp
Expand Down
6 changes: 6 additions & 0 deletions infra/util/SharedOptional.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "infra/util/SharedOptional.hpp"

namespace infra
{
const AlsoWhenAlreadyAllocatable alsoWhenAlreadyAllocatable;
}
17 changes: 16 additions & 1 deletion infra/util/SharedOptional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
#include "infra/util/Function.hpp"
#include "infra/util/Optional.hpp"
#include "infra/util/SharedObjectAllocator.hpp"
#include <cstddef>

namespace infra
{
// clang-format off
extern const struct AlsoWhenAlreadyAllocatable{} alsoWhenAlreadyAllocatable;

// clang-format on

template<class T>
class SharedOptional
: protected SharedObjectDeleter
Expand Down Expand Up @@ -54,6 +60,7 @@ namespace infra
explicit NotifyingSharedOptionalWithSize(const infra::Function<void(), AllocatableSize>& onAllocatable);

void OnAllocatable(const infra::Function<void(), AllocatableSize>& newOnAllocatable);
void OnAllocatable(const infra::Function<void(), AllocatableSize>& newOnAllocatable, AlsoWhenAlreadyAllocatable);

protected:
void Deallocate(void* control) override;
Expand Down Expand Up @@ -159,11 +166,19 @@ namespace infra
onAllocatable = newOnAllocatable;
}

template<class T, std::size_t AllocatableSize>
void NotifyingSharedOptionalWithSize<T, AllocatableSize>::OnAllocatable(const infra::Function<void(), AllocatableSize>& newOnAllocatable, AlsoWhenAlreadyAllocatable)
{
onAllocatable = newOnAllocatable;
if (SharedOptional<T>::Allocatable() && onAllocatable != nullptr)
onAllocatable();
}

template<class T, std::size_t AllocatableSize>
void NotifyingSharedOptionalWithSize<T, AllocatableSize>::Deallocate(void* control)
{
SharedOptional<T>::Deallocate(control);
if (onAllocatable)
if (onAllocatable != nullptr)
onAllocatable();
}
}
Expand Down
14 changes: 14 additions & 0 deletions infra/util/test/TestSharedOptional.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ TEST(SharedOptionalTest, NotifyingSharedPtr_notifies_after_becoming_allocatable)
wp = nullptr;
}

TEST(SharedOptionalTest, NotifyingSharedPtr_notifies_immediately_when_already_allocatable)
{
infra::NotifyingSharedOptional<int> s;

s.OnAllocatable(infra::MockFunction<void()>(), infra::alsoWhenAlreadyAllocatable);
}

TEST(SharedOptionalTest, NotifyingSharedPtr_does_not_notify_empty_function)
{
infra::NotifyingSharedOptional<int> s;

s.OnAllocatable(nullptr, infra::alsoWhenAlreadyAllocatable);
}

TEST(SharedOptionalTest, change_callback_on_NotifyingSharedPtr)
{
infra::MockCallback<void()> callback;
Expand Down

0 comments on commit cb37f67

Please sign in to comment.