Skip to content

Commit

Permalink
Merge pull request #121 from NuiCpp/fix/competing-observed-ctor
Browse files Browse the repository at this point in the history
Added flag to Observed<> ctor.
  • Loading branch information
5cript authored Jul 18, 2024
2 parents 992eff2 + a25c856 commit 7034674
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 20 deletions.
51 changes: 37 additions & 14 deletions nui/include/nui/event_system/observed_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <nui/concepts.hpp>
#include <nui/frontend/event_system/range_event_context.hpp>
#include <nui/frontend/event_system/event_context.hpp>
#include <nui/utility/assert.hpp>
#include <nui/utility/meta/pick_first.hpp>

#include <memory>
Expand All @@ -26,7 +27,7 @@ namespace Nui
class ObservedBase
{
public:
explicit ObservedBase(EventContext* ctx)
explicit ObservedBase(CustomEventContextFlag_t, EventContext* ctx)
: eventContext_{ctx}
, attachedEvents_{}
, attachedOneshotEvents_{}
Expand All @@ -49,7 +50,6 @@ namespace Nui
ObservedBase& operator=(ObservedBase&& other)
{
eventContext_ = other.eventContext_;
other.eventContext_ = nullptr;
for (auto& event : other.attachedEvents_)
attachedEvents_.push_back(std::move(event));
for (auto& event : other.attachedOneshotEvents_)
Expand Down Expand Up @@ -96,7 +96,7 @@ namespace Nui

virtual void update(bool /*force*/ = false) const
{
assert(eventContext_ != nullptr);
NUI_ASSERT(eventContext_ != nullptr, "Event context must never be null.");

for (auto& event : attachedEvents_)
{
Expand All @@ -114,7 +114,7 @@ namespace Nui

void updateNow(bool force = false) const
{
assert(eventContext_ != nullptr);
NUI_ASSERT(eventContext_ != nullptr, "Event context must never be null.");

update(force);
eventContext_->executeActiveEventsImmediately();
Expand Down Expand Up @@ -182,7 +182,7 @@ namespace Nui

public:
ModifiableObserved()
: ObservedBase{&globalEventContext}
: ObservedBase{CustomEventContextFlag, &globalEventContext}
, contained_{}
{}
ModifiableObserved(const ModifiableObserved&) = delete;
Expand Down Expand Up @@ -219,15 +219,21 @@ namespace Nui

template <typename T = ContainedT>
explicit ModifiableObserved(T&& t)
: ObservedBase{&globalEventContext}
: ObservedBase{CustomEventContextFlag, &globalEventContext}
, contained_{std::forward<T>(t)}
{}

explicit ModifiableObserved(EventContext* ctx)
: ObservedBase{ctx}
explicit ModifiableObserved(CustomEventContextFlag_t, EventContext* ctx)
: ObservedBase{CustomEventContextFlag, ctx}
, contained_{}
{}

template <typename T = ContainedT>
explicit ModifiableObserved(CustomEventContextFlag_t, EventContext* ctx, T&& t)
: ObservedBase{CustomEventContextFlag, ctx}
, contained_{std::forward<T>(t)}
{}

/**
* @brief Assign a completely new value.
*
Expand Down Expand Up @@ -584,8 +590,8 @@ namespace Nui
using ModifiableObserved<ContainerT>::update;

public:
explicit ObservedContainer(EventContext* ctx)
: ModifiableObserved<ContainerT>{ctx}
explicit ObservedContainer(CustomEventContextFlag_t, EventContext* ctx)
: ModifiableObserved<ContainerT>{CustomEventContextFlag, ctx}
, rangeContext_{0}
, afterEffectId_{registerAfterEffect()}
{}
Expand All @@ -595,6 +601,12 @@ namespace Nui
, afterEffectId_{registerAfterEffect()}
{}
template <typename T = ContainerT>
explicit ObservedContainer(CustomEventContextFlag_t, EventContext* ctx, T&& t)
: ModifiableObserved<ContainerT>{CustomEventContextFlag, ctx, std::forward<T>(t)}
, rangeContext_{static_cast<long>(contained_.size())}
, afterEffectId_{registerAfterEffect()}
{}
template <typename T = ContainerT>
explicit ObservedContainer(T&& t)
: ModifiableObserved<ContainerT>{std::forward<T>(t)}
, rangeContext_{static_cast<long>(contained_.size())}
Expand All @@ -605,12 +617,23 @@ namespace Nui
, rangeContext_{std::move(rangeContext)}
, afterEffectId_{registerAfterEffect()}
{}
explicit ObservedContainer(CustomEventContextFlag_t, EventContext* ctx, RangeEventContext&& rangeContext)
: ModifiableObserved<ContainerT>{CustomEventContextFlag, ctx}
, rangeContext_{std::move(rangeContext)}
, afterEffectId_{registerAfterEffect()}
{}
template <typename T = ContainerT>
ObservedContainer(T&& t, RangeEventContext&& rangeContext)
: ModifiableObserved<ContainerT>{std::forward<T>(t)}
, rangeContext_{std::move(rangeContext)}
, afterEffectId_{registerAfterEffect()}
{}
template <typename T = ContainerT>
ObservedContainer(CustomEventContextFlag_t, EventContext* ctx, T&& t, RangeEventContext&& rangeContext)
: ModifiableObserved<ContainerT>{CustomEventContextFlag, ctx, std::forward<T>(t)}
, rangeContext_{std::move(rangeContext)}
, afterEffectId_{registerAfterEffect()}
{}

ObservedContainer(const ObservedContainer&) = delete;
ObservedContainer(ObservedContainer&&) = default;
Expand Down Expand Up @@ -785,7 +808,7 @@ namespace Nui
decltype(std::declval<U>().insert(std::declval<const value_type&>()))>
insert(const value_type& value)
{
assert(ObservedBase::eventContext_ != nullptr);
NUI_ASSERT(ObservedBase::eventContext_ != nullptr, "Event context must never be null.");

const auto result = contained_.insert(value);
rangeContext_.performFullRangeUpdate();
Expand All @@ -799,7 +822,7 @@ namespace Nui
decltype(std::declval<U>().insert(std::declval<value_type&&>()))>
insert(value_type&& value)
{
assert(ObservedBase::eventContext_ != nullptr);
NUI_ASSERT(ObservedBase::eventContext_ != nullptr, "Event context must never be null.");

const auto result = contained_.insert(std::move(value));
rangeContext_.performFullRangeUpdate();
Expand Down Expand Up @@ -1014,7 +1037,7 @@ namespace Nui
{
std::function<void(int)> doInsert;
doInsert = [&](int retries) {
assert(ObservedBase::eventContext_ != nullptr);
NUI_ASSERT(ObservedBase::eventContext_ != nullptr, "Event context must never be null.");

const auto result = rangeContext_.insertModificationRange(contained_.size(), low, high, type);
if (result == RangeEventContext::InsertResult::Final)
Expand Down Expand Up @@ -1043,7 +1066,7 @@ namespace Nui

auto registerAfterEffect()
{
assert(ObservedBase::eventContext_ != nullptr);
NUI_ASSERT(ObservedBase::eventContext_ != nullptr, "Event context must never be null.");
return ObservedBase::eventContext_->registerAfterEffect(Event{[this](EventContext::EventIdType) {
rangeContext_.reset(static_cast<long>(contained_.size()), true);
return true;
Expand Down
26 changes: 26 additions & 0 deletions nui/include/nui/utility/assert.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <nui/frontend/val.hpp>

#include <stdexcept>

namespace Nui
{
inline void assertImpl(bool condition, const char* message)
{
if (!condition)
{
#ifdef __cpp_exceptions
throw std::runtime_error(message);
#else
Nui::val::global("console").call<void>("error", message);
#endif
}
}

#ifdef NDEBUG
# define NUI_ASSERT(condition, message)
#else
# define NUI_ASSERT(condition, message) assertImpl(condition, message)
#endif
}
12 changes: 6 additions & 6 deletions nui/test/nui/test_events.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ namespace Nui::Tests
{
EventContext eventContext;

Observed<int> obs{&eventContext};
Observed<int> obs{CustomEventContextFlag, &eventContext};

int calledWith = 77;
listen(eventContext, obs, [&calledWith](int const& value) -> bool {
Expand All @@ -162,7 +162,7 @@ namespace Nui::Tests
{
EventContext eventContext;

Observed<int> obs{&eventContext};
Observed<int> obs{CustomEventContextFlag, &eventContext};

int calledWith = 77;
listen(eventContext, obs, [&calledWith](int const& value) -> void {
Expand All @@ -179,7 +179,7 @@ namespace Nui::Tests
{
EventContext eventContext;

std::shared_ptr<Observed<int>> obs = std::make_shared<Observed<int>>(&eventContext);
std::shared_ptr<Observed<int>> obs = std::make_shared<Observed<int>>(CustomEventContextFlag, &eventContext);

int calledWith = 77;
listen(eventContext, obs, [&calledWith](int const& value) -> void {
Expand All @@ -196,7 +196,7 @@ namespace Nui::Tests
{
EventContext eventContext;

std::shared_ptr<Observed<int>> obs = std::make_shared<Observed<int>>(&eventContext);
std::shared_ptr<Observed<int>> obs = std::make_shared<Observed<int>>(CustomEventContextFlag, &eventContext);

int calledWith = 77;
listen(eventContext, obs, [&calledWith](int const& value) -> bool {
Expand All @@ -217,7 +217,7 @@ namespace Nui::Tests
{
EventContext eventContext;

std::shared_ptr<Observed<int>> obs = std::make_shared<Observed<int>>(&eventContext);
std::shared_ptr<Observed<int>> obs = std::make_shared<Observed<int>>(CustomEventContextFlag, &eventContext);

int calledWith = 77;
listen(eventContext, obs, [&calledWith](int const& value) -> bool {
Expand Down Expand Up @@ -254,7 +254,7 @@ namespace Nui::Tests
{
EventContext eventContext;

Observed<int> obs{&eventContext};
Observed<int> obs{CustomEventContextFlag, &eventContext};

int calledWith = 77;
listen(eventContext, obs, [&calledWith](int const& value) -> bool {
Expand Down

0 comments on commit 7034674

Please sign in to comment.