Nui
Loading...
Searching...
No Matches
listen.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <utility>
7#include <memory>
8#include <functional>
9#include <type_traits>
10
11namespace Nui
12{
13 namespace Detail
14 {
15 template <typename>
16 struct IsStdFunction : std::false_type
17 {};
18 template <typename RetT, typename... ArgsT>
19 struct IsStdFunction<std::function<RetT(ArgsT...)>> : std::true_type
20 {};
21 }
22
23 template <typename ValueT>
24 void listen(EventContext& eventContext, Observed<ValueT> const& obs, std::function<bool(ValueT const&)> onEvent)
25 {
26 const auto eventId = eventContext.registerEvent(Event{
27 [obs = Detail::CopyableObservedWrap{obs}, onEvent = std::move(onEvent)](auto) {
28 return onEvent(obs.value());
29 },
30 []() {
31 return true;
32 }});
33 obs.attachEvent(eventId);
34 }
35
36 template <typename ValueT>
37 requires std::is_scalar_v<ValueT>
38 void listen(EventContext& eventContext, Observed<ValueT> const& obs, std::function<bool(ValueT)> onEvent)
39 {
40 const auto eventId = eventContext.registerEvent(Event{
41 [obs = Detail::CopyableObservedWrap{obs}, onEvent = std::move(onEvent)](auto) {
42 return onEvent(obs.value());
43 },
44 []() {
45 return true;
46 }});
47 obs.attachEvent(eventId);
48 }
49
50 template <typename ValueT>
51 void listen(EventContext& eventContext, Observed<ValueT> const& obs, std::function<void(ValueT const&)> onEvent)
52 {
53 return listen(eventContext, obs, [onEvent = std::move(onEvent)](ValueT const& value) {
54 onEvent(value);
55 return true;
56 });
57 }
58
59 template <typename ValueT>
60 requires std::is_scalar_v<ValueT>
61 void listen(EventContext& eventContext, Observed<ValueT> const& obs, std::function<void(ValueT)> onEvent)
62 {
63 return listen(eventContext, obs, [onEvent = std::move(onEvent)](ValueT value) {
64 onEvent(value);
65 return true;
66 });
67 }
68
69 template <typename ValueT, typename FunctionT>
70 requires(
71 (std::invocable<FunctionT, ValueT const&> || std::invocable<FunctionT, ValueT>) &&
72 !Detail::IsStdFunction<FunctionT>::value)
73 void listen(EventContext& eventContext, Observed<ValueT> const& obs, FunctionT&& onEvent)
74 {
75 return listen(eventContext, obs, std::function(std::forward<FunctionT>(onEvent)));
76 }
77
78 template <typename ValueT>
79 void listen(
80 EventContext& eventContext,
81 std::shared_ptr<Observed<ValueT>> const& obs,
82 std::function<bool(ValueT const&)> onEvent)
83 {
84 const auto eventId = eventContext.registerEvent(Event{
85 [weak = std::weak_ptr<Observed<ValueT>>{obs}, onEvent = std::move(onEvent)](auto) {
86 if (auto obs = weak.lock(); obs)
87 return onEvent(obs->value());
88 return false;
89 },
90 [weak = std::weak_ptr<Observed<ValueT>>{obs}]() {
91 return !weak.expired();
92 }});
93 obs->attachEvent(eventId);
94 }
95
96 template <typename ValueT>
97 requires std::is_scalar_v<ValueT>
98 void listen(
99 EventContext& eventContext,
100 std::shared_ptr<Observed<ValueT>> const& obs,
101 std::function<bool(ValueT)> onEvent)
102 {
103 const auto eventId = eventContext.registerEvent(Event{
104 [weak = std::weak_ptr<Observed<ValueT>>{obs}, onEvent = std::move(onEvent)](auto) {
105 if (auto obs = weak.lock(); obs)
106 return onEvent(obs->value());
107 return false;
108 },
109 [weak = std::weak_ptr<Observed<ValueT>>{obs}]() {
110 return !weak.expired();
111 }});
112 obs->attachEvent(eventId);
113 }
114
115 template <typename ValueT>
116 void listen(
117 EventContext& eventContext,
118 std::shared_ptr<Observed<ValueT>> const& obs,
119 std::function<void(ValueT const&)> onEvent)
120 {
121 return listen(eventContext, obs, [onEvent = std::move(onEvent)](ValueT const& value) {
122 onEvent(value);
123 return true;
124 });
125 }
126
127 template <typename ValueT>
128 requires std::is_scalar_v<ValueT>
129 void listen(
130 EventContext& eventContext,
131 std::shared_ptr<Observed<ValueT>> const& obs,
132 std::function<void(ValueT)> onEvent)
133 {
134 return listen(eventContext, obs, [onEvent = std::move(onEvent)](ValueT value) {
135 onEvent(value);
136 return true;
137 });
138 }
139
140 template <typename ValueT, typename FunctionT>
141 requires(
142 (std::invocable<FunctionT, ValueT const&> || std::invocable<FunctionT, ValueT>) &&
143 !Detail::IsStdFunction<FunctionT>::value)
144 void listen(EventContext& eventContext, std::shared_ptr<Observed<ValueT>> const& obs, FunctionT&& onEvent)
145 {
146 return listen(eventContext, obs, std::function(std::forward<FunctionT>(onEvent)));
147 }
148
149 template <typename ValueT, typename FunctionT>
150 requires(
151 (std::invocable<FunctionT, ValueT const&> || std::invocable<FunctionT, ValueT>) &&
152 !Detail::IsStdFunction<FunctionT>::value)
153 void listen(std::shared_ptr<Observed<ValueT>> const& obs, FunctionT&& onEvent)
154 {
155 return listen(globalEventContext, obs, std::function(std::forward<FunctionT>(onEvent)));
156 }
157
158 template <typename ValueT, typename FunctionT>
159 requires(
160 (std::invocable<FunctionT, ValueT const&> || std::invocable<FunctionT, ValueT>) &&
161 !Detail::IsStdFunction<FunctionT>::value)
162 void listen(Observed<ValueT> const& obs, FunctionT&& onEvent)
163 {
164 return listen(globalEventContext, obs, std::function(std::forward<FunctionT>(onEvent)));
165 }
166}
This object can be copied with low cost.
Definition event_context.hpp:38
EventIdType registerEvent(Event event)
Definition event_context.hpp:52
Definition event.hpp:46
ContainedT & value()
Definition observed_value.hpp:372
void attachEvent(EventContext::EventIdType eventId) const
Definition observed_value.hpp:100
Definition observed_value.hpp:1283
static constexpr auto extractJsonMember(nlohmann::json const &json) -> decltype(auto)
Definition rpc_hub.hpp:29
Definition file_dialog.hpp:6
void listen(EventContext &eventContext, Observed< ValueT > const &obs, std::function< bool(ValueT const &)> onEvent)
Definition listen.hpp:24
thread_local EventContext globalEventContext
Definition event_context.cpp:5
Definition observed_value.hpp:1546
Definition listen.hpp:17