From 38d167b14694726b9a6f4082fd92f7be444ea2de Mon Sep 17 00:00:00 2001 From: "Tim.Ebbeke" Date: Thu, 31 Aug 2023 13:47:20 +0200 Subject: [PATCH] Added support for non const ranges. --- .../nui/frontend/elements/impl/html_element.hpp | 8 ++++---- .../frontend/event_system/observed_value.hpp | 9 +++++++++ nui/include/nui/frontend/event_system/range.hpp | 17 ++++++++++++++--- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/nui/include/nui/frontend/elements/impl/html_element.hpp b/nui/include/nui/frontend/elements/impl/html_element.hpp index 569ab60..ecae098 100644 --- a/nui/include/nui/frontend/elements/impl/html_element.hpp +++ b/nui/include/nui/frontend/elements/impl/html_element.hpp @@ -269,7 +269,7 @@ namespace Nui { parent->clearChildren(); long counter = 0; - for (auto const& element : observedValue.value()) + for (auto& element : observedValue.value()) ElementRenderer(counter++, element)(*parent, Renderer{.type = RendererType::Append}); return; } @@ -341,9 +341,9 @@ namespace Nui // Children functions: template requires requires(ElementT&&... elements) { - std::vector(Dom::Element&, Renderer const&)>>{ - std::forward(elements)...}; - } + std::vector(Dom::Element&, Renderer const&)>>{ + std::forward(elements)...}; + } constexpr auto operator()(ElementT&&... elements) && { return std::function(Dom::Element&, Renderer const&)>{ diff --git a/nui/include/nui/frontend/event_system/observed_value.hpp b/nui/include/nui/frontend/event_system/observed_value.hpp index d3d3f48..fb6ca92 100644 --- a/nui/include/nui/frontend/event_system/observed_value.hpp +++ b/nui/include/nui/frontend/event_system/observed_value.hpp @@ -551,6 +551,7 @@ namespace Nui ~ObservedContainer() = default; constexpr auto map(auto&& function) const; + constexpr auto map(auto&& function); template ObservedContainer& operator=(T&& t) @@ -1048,6 +1049,14 @@ namespace Nui std::forward>(function), }; } + template + constexpr auto ObservedContainer::map(auto&& function) + { + return std::pair>, std::decay_t>{ + ObservedRange>{static_cast&>(*this)}, + std::forward>(function), + }; + } namespace Detail { diff --git a/nui/include/nui/frontend/event_system/range.hpp b/nui/include/nui/frontend/event_system/range.hpp index 140e6b3..bbba76d 100644 --- a/nui/include/nui/frontend/event_system/range.hpp +++ b/nui/include/nui/frontend/event_system/range.hpp @@ -6,7 +6,7 @@ namespace Nui class ObservedRange { public: - constexpr ObservedRange(ObservedValue const& observedValues) + constexpr ObservedRange(ObservedValue& observedValues) : observedValue_{observedValues} {} @@ -14,14 +14,25 @@ namespace Nui { return observedValue_; } + ObservedValue& observedValue() + requires(!std::is_const_v) + { + return observedValue_; + } private: - ObservedValue const& observedValue_; + ObservedValue& observedValue_; }; template - ObservedRange range(ObservedValue const& observedValues) + ObservedRange range(ObservedValue& observedValues) { return ObservedRange{observedValues}; } + + template + ObservedRange range(ObservedValue const& observedValues) + { + return ObservedRange{observedValues}; + } } \ No newline at end of file