Skip to content

Commit

Permalink
Merge pull request #74 from NuiCpp/devel
Browse files Browse the repository at this point in the history
Important fix to make Observed<> ctor explicit
  • Loading branch information
5cript authored Sep 15, 2023
2 parents 5283fc0 + 8138bdc commit 1e1ee1a
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 96 deletions.
7 changes: 4 additions & 3 deletions nui/include/nui/frontend/attributes/impl/attribute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace Nui
std::string data;
};

Attribute() = default;
Attribute(
std::function<void(Dom::ChildlessElement&)> setter,
std::function<EventContext::EventIdType(std::weak_ptr<Dom::ChildlessElement>&& element)> createEvent = {},
Expand Down Expand Up @@ -67,8 +68,8 @@ namespace Nui
bool isStringData() const;

private:
std::variant<RegularAttribute, StringDataAttribute> attributeImpl_;
std::function<EventContext::EventIdType(std::weak_ptr<Dom::ChildlessElement>&& element)> createEvent_;
std::function<void(EventContext::EventIdType const&)> clearEvent_;
std::variant<std::monostate, RegularAttribute, StringDataAttribute> attributeImpl_{};
std::function<EventContext::EventIdType(std::weak_ptr<Dom::ChildlessElement>&& element)> createEvent_{};
std::function<void(EventContext::EventIdType const&)> clearEvent_{};
};
}
17 changes: 11 additions & 6 deletions nui/include/nui/frontend/components/dialog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ namespace Nui::Components

struct ConstructionArgs
{
Observed<std::optional<std::string>> className = std::nullopt;
Observed<std::string> title = "";
Observed<std::string> body = "";
Observed<std::string> buttonClassName = "";
Observed<ButtonConfiguration> buttonConfiguration = ButtonConfiguration::Ok;
std::optional<std::string> className{std::nullopt};
std::string title{""};
std::string body{""};
std::string buttonClassName{""};
ButtonConfiguration buttonConfiguration{ButtonConfiguration::Ok};
std::function<void(Button)> onButtonClicked = [](Button) {};
};

Expand Down Expand Up @@ -107,6 +107,11 @@ namespace Nui::Components
private:
bool isOpen_;
std::weak_ptr<Dom::Element> element_;
ConstructionArgs args_;
Observed<std::optional<std::string>> className_;
Observed<std::string> title_;
Observed<std::string> body_;
Observed<std::string> buttonClassName_;
Observed<ButtonConfiguration> buttonConfiguration_;
std::function<void(Button)> onButtonClicked_;
};
}
2 changes: 1 addition & 1 deletion nui/include/nui/frontend/dom/basic_element.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Nui::Dom
class BasicElement : public std::enable_shared_from_this<BasicElement>
{
public:
BasicElement(Nui::val val)
explicit BasicElement(Nui::val val)
: element_{std::move(val)}
{}
virtual ~BasicElement() = default;
Expand Down
8 changes: 6 additions & 2 deletions nui/include/nui/frontend/dom/childless_element.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#include <nui/frontend/elements/impl/html_element.hpp>
#include <nui/frontend/utility/functions.hpp>

#include <optional>
#include <string>
#include <string_view>

namespace Nui::Dom
{
/**
Expand All @@ -13,10 +17,10 @@ namespace Nui::Dom
class ChildlessElement : public BasicElement
{
public:
ChildlessElement(HtmlElement const& elem)
explicit ChildlessElement(HtmlElement const& elem)
: BasicElement{ChildlessElement::createElement(elem)}
{}
ChildlessElement(Nui::val val)
explicit ChildlessElement(Nui::val val)
: BasicElement{std::move(val)}
{}

Expand Down
4 changes: 2 additions & 2 deletions nui/include/nui/frontend/dom/element.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ namespace Nui::Dom
using const_iterator = collection_type::const_iterator;
using value_type = collection_type::value_type;

Element(HtmlElement const& elem)
explicit Element(HtmlElement const& elem)
: ChildlessElement{elem}
, children_{}
, unsetup_{}
{}

Element(Nui::val val)
explicit Element(Nui::val val)
: ChildlessElement{std::move(val)}
, children_{}
, unsetup_{}
Expand Down
2 changes: 1 addition & 1 deletion nui/include/nui/frontend/dom/reference.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Nui::Dom
struct ReferencePasser
{
public:
ReferencePasser(T&& func)
explicit ReferencePasser(T&& func)
: func_{std::move(func)}
{}
void operator()(std::weak_ptr<BasicElement>&& weakElement) const
Expand Down
89 changes: 82 additions & 7 deletions nui/include/nui/frontend/event_system/observed_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,22 @@ namespace Nui
}
return *this;
};
ModifiableObserved& operator=(ContainedT const& contained)
{
contained_ = contained;
update();
return *this;
}
ModifiableObserved& operator=(ContainedT&& contained)
{
contained_ = std::move(contained);
update();
return *this;
}
~ModifiableObserved() = default;

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

Expand Down Expand Up @@ -316,9 +328,14 @@ namespace Nui
{
return ref_;
}
void operator=(T&& ref)
void operator=(T&& val)
{
ref_ = std::move(val);
owner_->insertRangeChecked(pos_, pos_, RangeStateType::Modify);
}
void operator=(T const& val)
{
ref_ = std::forward<T>(ref);
ref_ = val;
owner_->insertRangeChecked(pos_, pos_, RangeStateType::Modify);
}

Expand Down Expand Up @@ -527,12 +544,12 @@ namespace Nui
, afterEffectId_{registerAfterEffect()}
{}
template <typename T = ContainerT>
ObservedContainer(T&& t)
explicit ObservedContainer(T&& t)
: ModifiableObserved<ContainerT>{std::forward<T>(t)}
, rangeContext_{static_cast<long>(contained_.size())}
, afterEffectId_{registerAfterEffect()}
{}
ObservedContainer(RangeEventContext&& rangeContext)
explicit ObservedContainer(RangeEventContext&& rangeContext)
: ModifiableObserved<ContainerT>{}
, rangeContext_{std::move(rangeContext)}
, afterEffectId_{registerAfterEffect()}
Expand Down Expand Up @@ -987,6 +1004,17 @@ namespace Nui
using ModifiableObserved<T>::ModifiableObserved;
using ModifiableObserved<T>::operator=;
using ModifiableObserved<T>::operator->;

Observed& operator=(T const& contained)
{
ModifiableObserved<T>::operator=(contained);
return *this;
}
Observed& operator=(T&& contained)
{
ModifiableObserved<T>::operator=(std::move(contained));
return *this;
}
};
template <typename... Parameters>
class Observed<std::vector<Parameters...>> : public ObservedContainer<std::vector<Parameters...>>
Expand All @@ -996,6 +1024,17 @@ namespace Nui
using ObservedContainer<std::vector<Parameters...>>::operator=;
using ObservedContainer<std::vector<Parameters...>>::operator->;
static constexpr auto isRandomAccess = true;

Observed<std::vector<Parameters...>>& operator=(std::vector<Parameters...> const& contained)
{
ObservedContainer<std::vector<Parameters...>>::operator=(contained);
return *this;
}
Observed<std::vector<Parameters...>>& operator=(std::vector<Parameters...>&& contained)
{
ObservedContainer<std::vector<Parameters...>>::operator=(std::move(contained));
return *this;
}
};
template <typename... Parameters>
class Observed<std::deque<Parameters...>> : public ObservedContainer<std::deque<Parameters...>>
Expand All @@ -1005,6 +1044,17 @@ namespace Nui
using ObservedContainer<std::deque<Parameters...>>::operator=;
using ObservedContainer<std::deque<Parameters...>>::operator->;
static constexpr auto isRandomAccess = true;

Observed<std::deque<Parameters...>>& operator=(std::deque<Parameters...> const& contained)
{
ObservedContainer<std::deque<Parameters...>>::operator=(contained);
return *this;
}
Observed<std::deque<Parameters...>>& operator=(std::deque<Parameters...>&& contained)
{
ObservedContainer<std::deque<Parameters...>>::operator=(std::move(contained));
return *this;
}
};
template <typename... Parameters>
class Observed<std::basic_string<Parameters...>> : public ObservedContainer<std::basic_string<Parameters...>>
Expand All @@ -1015,6 +1065,17 @@ namespace Nui
using ObservedContainer<std::basic_string<Parameters...>>::operator->;
static constexpr auto isRandomAccess = true;

Observed<std::basic_string<Parameters...>>& operator=(std::basic_string<Parameters...> const& contained)
{
ObservedContainer<std::basic_string<Parameters...>>::operator=(contained);
return *this;
}
Observed<std::basic_string<Parameters...>>& operator=(std::basic_string<Parameters...>&& contained)
{
ObservedContainer<std::basic_string<Parameters...>>::operator=(std::move(contained));
return *this;
}

Observed<std::basic_string<Parameters...>>& erase(std::size_t index = 0, std::size_t count = std::string::npos)
{
const auto sizeBefore = this->contained_.size();
Expand All @@ -1027,18 +1088,32 @@ namespace Nui
class Observed<std::set<Parameters...>> : public ObservedContainer<std::set<Parameters...>>
{
public:
using ObservedContainer<std::set<Parameters...>>::ObservedContainer;
using ObservedContainer<std::set<Parameters...>>::operator=;
using ObservedContainer<std::set<Parameters...>>::operator->;
static constexpr auto isRandomAccess = false;

public:
Observed()
: ObservedContainer<std::set<Parameters...>>{RangeEventContext{0, true}}
{}
template <typename T = std::set<Parameters...>>
Observed(T&& t)
explicit Observed(T&& t)
: ObservedContainer<std::set<Parameters...>>{
std::forward<T>(t),
RangeEventContext{static_cast<long>(t.size()), true}}
{}

Observed<std::set<Parameters...>>& operator=(std::set<Parameters...> const& contained)
{
ObservedContainer<std::set<Parameters...>>::operator=(contained);
return *this;
}
Observed<std::set<Parameters...>>& operator=(std::set<Parameters...>&& contained)
{
ObservedContainer<std::set<Parameters...>>::operator=(std::move(contained));
return *this;
}
};

template <typename ContainerT>
Expand Down Expand Up @@ -1118,7 +1193,7 @@ namespace Nui
struct CopiableObservedWrap // minimal wrapper to make Observed<T> copiable
{
public:
constexpr CopiableObservedWrap(Observed<T> const& observed)
explicit constexpr CopiableObservedWrap(Observed<T> const& observed)
: observed_{&observed}
{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ namespace Nui
class ObservedValueCombinatorBase
{
public:
constexpr ObservedValueCombinatorBase(ObservedValues const&... observedValues)
explicit constexpr ObservedValueCombinatorBase(ObservedValues const&... observedValues)
: observedValues_{observedValues...}
{}
constexpr ObservedValueCombinatorBase(std::tuple<ObservedValues const&...> observedValues)
explicit constexpr ObservedValueCombinatorBase(std::tuple<ObservedValues const&...> observedValues)
: observedValues_{std::move(observedValues)}
{}

Expand Down
2 changes: 1 addition & 1 deletion nui/include/nui/frontend/event_system/range.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Nui
class ObservedRange
{
public:
constexpr ObservedRange(ObservedValue& observedValues)
explicit constexpr ObservedRange(ObservedValue& observedValues)
: observedValue_{observedValues}
{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ namespace Nui
class RangeEventContext
{
public:
RangeEventContext(long dataSize)
explicit RangeEventContext(long dataSize)
: modificationRanges_{}
, fullRangeUpdate_{true}
, disableOptimizations_{false}
Expand Down
Loading

0 comments on commit 1e1ee1a

Please sign in to comment.