Nui
Loading...
Searching...
No Matches
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
12namespace Nui
13{
15 {
16 public:
18 {
19 std::function<void(Dom::ChildlessElement&)> setter;
20 };
22 {
23 std::string data;
24 };
25
26 Attribute() = default;
27 explicit Attribute(
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;
60 ~Attribute() = default;
61
62 void setOn(Dom::ChildlessElement& element) const;
63 EventContext::EventIdType createEvent(std::weak_ptr<Dom::ChildlessElement>&& element) const;
64 std::function<void(EventContext::EventIdType const&)> getEventClear() const;
65
66 std::string const& stringData() const;
67
68 bool isRegular() const;
69 bool isStringData() const;
70 bool defer() const;
71 void defer(bool doDefer) &;
72 Attribute&& defer(bool doDefer) &&
73 {
74 defer(doDefer);
75 return std::move(*this);
76 }
77
78 private:
79 std::variant<std::monostate, RegularAttribute, StringDataAttribute> attributeImpl_{};
80 std::function<EventContext::EventIdType(std::weak_ptr<Dom::ChildlessElement>&& element)> createEvent_{};
81 std::function<void(EventContext::EventIdType const&)> clearEvent_{};
82 bool defer_{false};
83 };
84
85 inline Attribute&& operator!(Attribute&& attribute)
86 {
87 attribute.defer(!attribute.defer());
88 return std::move(attribute);
89 }
90}
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
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
Attribute & operator=(Attribute &&)=default
bool defer() const
Definition attribute.cpp:41
Attribute && defer(bool doDefer) &&
Definition attribute.hpp:72
~Attribute()=default
Attribute & operator=(Attribute const &)=default
std::string const & stringData() const
Definition attribute.cpp:26
EventContext::EventIdType createEvent(std::weak_ptr< Dom::ChildlessElement > &&element) const
Definition attribute.cpp:14
std::function< void(EventContext::EventIdType const &)> getEventClear() const
Definition attribute.cpp:21
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:85
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