Nui
event_registry.hpp
Go to the documentation of this file.
1 #pragma once
2 
6 
7 #include <functional>
8 #include <limits>
9 #include <map>
10 
11 namespace Nui
12 {
14  {
15  public:
18  constexpr static EventIdType invalidEventId = std::numeric_limits<EventIdType>::max();
19 
20  public:
21  EventRegistry() = default;
22  EventRegistry(const EventRegistry&) = delete;
26  ~EventRegistry() = default;
27 
29  {
30  return registry_.append(std::move(event));
31  }
32 
40  RegistryType::SelectionResult activateEvent(EventIdType id)
41  {
42  return registry_.select(id);
43  }
44 
51  RegistryType::SelectionResult activateAfterEffect(EventIdType id)
52  {
53  return afterEffects_.select(id);
54  }
55 
64  {
65  return afterEffects_.append(std::move(event));
66  }
67 
69  {
70  registry_.deselect(id, [](SelectablesRegistry<Event>::ItemWithId const& itemWithId) -> bool {
71  if (!itemWithId.item)
72  return false;
73  return itemWithId.item.value()(itemWithId.id);
74  });
75  }
76 
78  {
79  registry_.deselectAll([](SelectablesRegistry<Event>::ItemWithId const& itemWithId) -> bool {
80  if (!itemWithId.item)
81  return false;
82  return itemWithId.item.value()(itemWithId.id);
83  });
84  afterEffects_.deselectAll([](SelectablesRegistry<Event>::ItemWithId const& itemWithId) -> bool {
85  if (!itemWithId.item)
86  return false;
87  return itemWithId.item.value()(itemWithId.id);
88  });
89  }
90 
92  {
93  std::vector<EventIdType> invalidIds;
94  for (auto const& itemWithId : registry_.rawRange())
95  {
96  if (!itemWithId.item)
97  continue;
98  if (!static_cast<bool>(itemWithId.item.value()))
99  invalidIds.push_back(itemWithId.id);
100  }
101 
102  for (auto const& id : invalidIds)
103  registry_.erase(id);
104  }
105 
107  {
108  afterEffects_.erase(id);
109  }
110 
111  void clear()
112  {
113  registry_.clear();
114  afterEffects_.clear();
115  }
116 
117  private:
118  RegistryType registry_;
119  RegistryType afterEffects_;
120  };
121 }
Definition: event_registry.hpp:14
~EventRegistry()=default
EventRegistry(EventRegistry &&)=default
EventIdType registerAfterEffect(Event event)
After effects are used to cause something to happen after all other events have been processed.
Definition: event_registry.hpp:63
EventRegistry()=default
EventRegistry & operator=(EventRegistry &&)=default
void executeEvent(EventIdType id)
Definition: event_registry.hpp:68
void clear()
Definition: event_registry.hpp:111
constexpr static EventIdType invalidEventId
Definition: event_registry.hpp:18
RegistryType::SelectionResult activateEvent(EventIdType id)
Returns a pointer to the selected event (only valid until the next activation or event execution).
Definition: event_registry.hpp:40
SelectablesRegistry< Event >::IdType EventIdType
Definition: event_registry.hpp:17
SelectablesRegistry< Event > RegistryType
Definition: event_registry.hpp:16
EventRegistry & operator=(const EventRegistry &)=delete
void cleanInvalidEvents()
Definition: event_registry.hpp:91
void removeAfterEffect(EventIdType id)
Definition: event_registry.hpp:106
void executeActiveEvents()
Definition: event_registry.hpp:77
EventRegistry(const EventRegistry &)=delete
RegistryType::SelectionResult activateAfterEffect(EventIdType id)
Activate an after effect.
Definition: event_registry.hpp:51
EventIdType registerEvent(Event event)
Definition: event_registry.hpp:28
Definition: event.hpp:40
IteratorType erase(IdType id)
Erase/Remove an item from the container.
Definition: selectables_registry.hpp:312
std::size_t deselectAll(std::invocable< ItemWithId const & > auto const &callback)
Deselects all items.
Definition: selectables_registry.hpp:390
bool deselect(IdType id, std::invocable< ItemWithId const & > auto const &callback)
Deselects item with id.
Definition: selectables_registry.hpp:420
SelectionResult select(IdType id)
Select an item.
Definition: selectables_registry.hpp:363
RawRangeWrap< SelectablesRegistry< T > * > rawRange()
Helper for range based for loops.
Definition: selectables_registry.hpp:639
IdType append(T const &element)
Append an item to the container.
Definition: selectables_registry.hpp:259
void clear()
Definition: selectables_registry.hpp:442
Definition: file_dialog.hpp:6