Nui
attribute.hpp
Go to the documentation of this file.
1 #pragma once
2 
5 
6 #include <functional>
7 #include <memory>
8 #include <string>
9 #include <string_view>
10 #include <variant>
11 
12 namespace Nui
13 {
14  class Attribute
15  {
16  public:
18  {
19  std::function<void(Dom::ChildlessElement&)> setter;
20  };
22  {
23  std::string data;
24  };
25 
26  Attribute() = default;
28  std::function<void(Dom::ChildlessElement&)> setter,
29  std::function<EventContext::EventIdType(std::weak_ptr<Dom::ChildlessElement>&& element)> createEvent = {},
30  std::function<void(EventContext::EventIdType const&)> clearEvent = {})
31  : attributeImpl_{RegularAttribute{.setter = std::move(setter)}}
32  , createEvent_{std::move(createEvent)}
33  , clearEvent_{std::move(clearEvent)}
34  {}
36  std::string data,
37  std::function<EventContext::EventIdType(std::weak_ptr<Dom::ChildlessElement>&& element)> createEvent,
38  std::function<void(EventContext::EventIdType const&)> clearEvent)
39  : attributeImpl_{StringDataAttribute{
40  .data = std::move(data),
41  }}
42  , createEvent_{std::move(createEvent)}
43  , clearEvent_{std::move(clearEvent)}
44  {}
46  std::string_view data,
47  std::function<EventContext::EventIdType(std::weak_ptr<Dom::ChildlessElement>&& element)> createEvent,
48  std::function<void(EventContext::EventIdType const&)> clearEvent)
49  : attributeImpl_{StringDataAttribute{
50  .data = std::string{data},
51  }}
52  , createEvent_{std::move(createEvent)}
53  , clearEvent_{std::move(clearEvent)}
54  {}
55 
56  Attribute(Attribute const&) = default;
57  Attribute(Attribute&&) = default;
58  Attribute& operator=(Attribute const&) = default;
59  Attribute& operator=(Attribute&&) = default;
60 
61  void setOn(Dom::ChildlessElement& element) const;
62  EventContext::EventIdType createEvent(std::weak_ptr<Dom::ChildlessElement>&& element) const;
63  std::function<void(EventContext::EventIdType const&)> getEventClear() const;
64 
65  std::string const& stringData() const;
66 
67  bool isRegular() const;
68  bool isStringData() const;
69  bool defer() const;
70  void defer(bool doDefer) &;
71  Attribute&& defer(bool doDefer) &&
72  {
73  defer(doDefer);
74  return std::move(*this);
75  }
76 
77  private:
78  std::variant<std::monostate, RegularAttribute, StringDataAttribute> attributeImpl_{};
79  std::function<EventContext::EventIdType(std::weak_ptr<Dom::ChildlessElement>&& element)> createEvent_{};
80  std::function<void(EventContext::EventIdType const&)> clearEvent_{};
81  bool defer_{false};
82  };
83 
84  inline Attribute&& operator!(Attribute&& attribute)
85  {
86  attribute.defer(!attribute.defer());
87  return std::move(attribute);
88  }
89 }
Definition: attribute.hpp:15
Attribute(std::function< void(Dom::ChildlessElement &)> setter, std::function< EventContext::EventIdType(std::weak_ptr< Dom::ChildlessElement > &&element)> createEvent={}, std::function< void(EventContext::EventIdType const &)> clearEvent={})
Definition: attribute.hpp:27
void setOn(Dom::ChildlessElement &element) const
Definition: attribute.cpp:9
Attribute()=default
Attribute(Attribute &&)=default
std::function< void(EventContext::EventIdType const &)> getEventClear() const
Definition: attribute.cpp:21
Attribute(std::string data, std::function< EventContext::EventIdType(std::weak_ptr< Dom::ChildlessElement > &&element)> createEvent, std::function< void(EventContext::EventIdType const &)> clearEvent)
Definition: attribute.hpp:35
bool defer() const
Definition: attribute.cpp:41
Attribute & operator=(Attribute &&)=default
Attribute && defer(bool doDefer) &&
Definition: attribute.hpp:71
std::string const & stringData() const
Definition: attribute.cpp:26
Attribute & operator=(Attribute const &)=default
EventContext::EventIdType createEvent(std::weak_ptr< Dom::ChildlessElement > &&element) const
Definition: attribute.cpp:14
Attribute(Attribute const &)=default
Attribute(std::string_view data, std::function< EventContext::EventIdType(std::weak_ptr< Dom::ChildlessElement > &&element)> createEvent, std::function< void(EventContext::EventIdType const &)> clearEvent)
Definition: attribute.hpp:45
bool isStringData() const
Definition: attribute.cpp:36
bool isRegular() const
Definition: attribute.cpp:31
The basic element cannot have children and does not hold explicit ownership of them.
Definition: childless_element.hpp:19
EventRegistry::EventIdType EventIdType
Definition: event_context.hpp:40
Definition: file_dialog.hpp:6
Attribute && operator!(Attribute &&attribute)
Definition: attribute.hpp:84
Definition: attribute.hpp:18
std::function< void(Dom::ChildlessElement &)> setter
Definition: attribute.hpp:19
Definition: attribute.hpp:22
std::string data
Definition: attribute.hpp:23