Nui
listen.hpp
Go to the documentation of this file.
1 #pragma once
2 
5 
6 #include <utility>
7 #include <memory>
8 #include <functional>
9 
10 namespace Nui
11 {
12  template <typename ValueT>
13  void listen(EventContext& eventContext, Observed<ValueT> const& obs, std::function<bool(ValueT const&)> onEvent)
14  {
15  const auto eventId = eventContext.registerEvent(Event{
16  [obs = Detail::CopyableObservedWrap{obs}, onEvent = std::move(onEvent)](auto) {
17  return onEvent(obs.value());
18  },
19  []() {
20  return true;
21  }});
22  obs.attachEvent(eventId);
23  }
24 
25  template <typename ValueT>
26  void listen(EventContext& eventContext, Observed<ValueT> const& obs, std::function<void(ValueT const&)> onEvent)
27  {
28  return listen(eventContext, obs, [onEvent = std::move(onEvent)](ValueT const& value) {
29  onEvent(value);
30  return true;
31  });
32  }
33 
34  template <typename ValueT, typename FunctionT>
35  void listen(EventContext& eventContext, Observed<ValueT> const& obs, FunctionT onEvent)
36  {
37  return listen(eventContext, obs, std::function(std::move(onEvent)));
38  }
39 
40  template <typename ValueT>
41  void listen(
42  EventContext& eventContext,
43  std::shared_ptr<Observed<ValueT>> const& obs,
44  std::function<bool(ValueT const&)> onEvent)
45  {
46  const auto eventId = eventContext.registerEvent(Event{
47  [weak = std::weak_ptr<Observed<ValueT>>{obs}, onEvent = std::move(onEvent)](auto) {
48  if (auto obs = weak.lock(); obs)
49  return onEvent(obs->value());
50  return false;
51  },
52  [weak = std::weak_ptr<Observed<ValueT>>{obs}]() {
53  return !weak.expired();
54  }});
55  obs->attachEvent(eventId);
56  }
57 
58  template <typename ValueT>
59  void listen(
60  EventContext& eventContext,
61  std::shared_ptr<Observed<ValueT>> const& obs,
62  std::function<void(ValueT const&)> onEvent)
63  {
64  return listen(eventContext, obs, [onEvent = std::move(onEvent)](ValueT const& value) {
65  onEvent(value);
66  return true;
67  });
68  }
69 
70  template <typename ValueT, typename FunctionT>
71  void listen(EventContext& eventContext, std::shared_ptr<Observed<ValueT>> const& obs, FunctionT onEvent)
72  {
73  return listen(eventContext, obs, std::function(std::move(onEvent)));
74  }
75 
76  template <typename ValueT, typename FunctionT>
77  void listen(std::shared_ptr<Observed<ValueT>> const& obs, FunctionT onEvent)
78  {
79  return listen(globalEventContext, obs, std::function(std::move(onEvent)));
80  }
81 
82  template <typename ValueT, typename FunctionT>
83  void listen(Observed<ValueT> const& obs, FunctionT onEvent)
84  {
85  return listen(globalEventContext, obs, std::function(std::move(onEvent)));
86  }
87 }
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:40
ContainedT const & value() const
Definition: observed_value.hpp:313
void attachEvent(EventContext::EventIdType eventId) const
Definition: observed_value.hpp:62
Definition: observed_value.hpp:1202
Definition: file_dialog.hpp:6
void listen(EventContext &eventContext, Observed< ValueT > const &obs, std::function< bool(ValueT const &)> onEvent)
Definition: listen.hpp:13
thread_local EventContext globalEventContext
Definition: event_context.cpp:5
Definition: observed_value.hpp:1463