Skip to content

Commit

Permalink
Added support for non const ranges.
Browse files Browse the repository at this point in the history
  • Loading branch information
5cript committed Aug 31, 2023
1 parent a0626fd commit 38d167b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
8 changes: 4 additions & 4 deletions nui/include/nui/frontend/elements/impl/html_element.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -341,9 +341,9 @@ namespace Nui
// Children functions:
template <typename... ElementT>
requires requires(ElementT&&... elements) {
std::vector<std::function<std::shared_ptr<Dom::Element>(Dom::Element&, Renderer const&)>>{
std::forward<ElementT>(elements)...};
}
std::vector<std::function<std::shared_ptr<Dom::Element>(Dom::Element&, Renderer const&)>>{
std::forward<ElementT>(elements)...};
}
constexpr auto operator()(ElementT&&... elements) &&
{
return std::function<std::shared_ptr<Dom::Element>(Dom::Element&, Renderer const&)>{
Expand Down
9 changes: 9 additions & 0 deletions nui/include/nui/frontend/event_system/observed_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ namespace Nui
~ObservedContainer() = default;

constexpr auto map(auto&& function) const;
constexpr auto map(auto&& function);

template <typename T = ContainerT>
ObservedContainer& operator=(T&& t)
Expand Down Expand Up @@ -1048,6 +1049,14 @@ namespace Nui
std::forward<std::decay_t<decltype(function)>>(function),
};
}
template <typename ContainerT>
constexpr auto ObservedContainer<ContainerT>::map(auto&& function)
{
return std::pair<ObservedRange<Observed<ContainerT>>, std::decay_t<decltype(function)>>{
ObservedRange<Observed<ContainerT>>{static_cast<Observed<ContainerT>&>(*this)},
std::forward<std::decay_t<decltype(function)>>(function),
};
}

namespace Detail
{
Expand Down
17 changes: 14 additions & 3 deletions nui/include/nui/frontend/event_system/range.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,33 @@ namespace Nui
class ObservedRange
{
public:
constexpr ObservedRange(ObservedValue const& observedValues)
constexpr ObservedRange(ObservedValue& observedValues)
: observedValue_{observedValues}
{}

ObservedValue const& observedValue() const
{
return observedValue_;
}
ObservedValue& observedValue()
requires(!std::is_const_v<ObservedValue>)
{
return observedValue_;
}

private:
ObservedValue const& observedValue_;
ObservedValue& observedValue_;
};

template <typename ObservedValue>
ObservedRange<ObservedValue> range(ObservedValue const& observedValues)
ObservedRange<ObservedValue> range(ObservedValue& observedValues)
{
return ObservedRange<ObservedValue>{observedValues};
}

template <typename ObservedValue>
ObservedRange<const ObservedValue> range(ObservedValue const& observedValues)
{
return ObservedRange<const ObservedValue>{observedValues};
}
}

0 comments on commit 38d167b

Please sign in to comment.