Nui
Loading...
Searching...
No Matches
event_registry.hpp
Go to the documentation of this file.
1#pragma once
2
6
7#include <limits>
8
9namespace Nui
10{
12 {
13 public:
16 constexpr static EventIdType invalidEventId = std::numeric_limits<EventIdType>::max();
17
18 public:
19 EventRegistry() = default;
20 EventRegistry(const EventRegistry&) = delete;
24 ~EventRegistry() = default;
25
27 {
28 return registry_.append(std::move(event));
29 }
30
38 RegistryType::SelectionResult activateEvent(EventIdType id)
39 {
40 return registry_.select(id);
41 }
42
49 RegistryType::SelectionResult activateAfterEffect(EventIdType id)
50 {
51 return afterEffects_.select(id);
52 }
53
62 {
63 return afterEffects_.append(std::move(event));
64 }
65
67 {
68 registry_.deselect(id, [](SelectablesRegistry<Event>::ItemWithId const& itemWithId) -> bool {
69 if (!itemWithId.item)
70 return false;
71 return itemWithId.item.value()(itemWithId.id);
72 });
73 }
74
76 {
77 registry_.deselectAll([](SelectablesRegistry<Event>::ItemWithId const& itemWithId) -> bool {
78 if (!itemWithId.item)
79 return false;
80 return itemWithId.item.value()(itemWithId.id);
81 });
82 afterEffects_.deselectAll([](SelectablesRegistry<Event>::ItemWithId const& itemWithId) -> bool {
83 if (!itemWithId.item)
84 return false;
85 return itemWithId.item.value()(itemWithId.id);
86 });
87 }
88
90 {
91 std::vector<EventIdType> invalidIds;
92 for (auto const& itemWithId : registry_.rawRange())
93 {
94 if (!itemWithId.item)
95 continue;
96 if (!static_cast<bool>(itemWithId.item.value()))
97 invalidIds.push_back(itemWithId.id);
98 }
99
100 for (auto const& id : invalidIds)
101 registry_.erase(id);
102 }
103
105 {
106 afterEffects_.erase(id);
107 }
108
109 void clear()
110 {
111 registry_.clear();
112 afterEffects_.clear();
113 }
114
115 private:
116 RegistryType registry_;
117 RegistryType afterEffects_;
118 };
119}
Definition event_registry.hpp:12
~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:61
EventRegistry()=default
void executeEvent(EventIdType id)
Definition event_registry.hpp:66
void clear()
Definition event_registry.hpp:109
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:38
SelectablesRegistry< Event >::IdType EventIdType
Definition event_registry.hpp:15
SelectablesRegistry< Event > RegistryType
Definition event_registry.hpp:14
EventRegistry & operator=(const EventRegistry &)=delete
EventRegistry & operator=(EventRegistry &&)=default
void cleanInvalidEvents()
Definition event_registry.hpp:89
void removeAfterEffect(EventIdType id)
Definition event_registry.hpp:104
void executeActiveEvents()
Definition event_registry.hpp:75
EventRegistry(const EventRegistry &)=delete
static constexpr EventIdType invalidEventId
Definition event_registry.hpp:16
RegistryType::SelectionResult activateAfterEffect(EventIdType id)
Activate an after effect.
Definition event_registry.hpp:49
EventIdType registerEvent(Event event)
Definition event_registry.hpp:26
Definition event.hpp:46
IteratorType erase(IdType id)
Erase/Remove an item from the container.
Definition selectables_registry.hpp:312
std::size_t IdType
Id type used to identify items.
Definition selectables_registry.hpp:34
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:423
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:642
IdType append(T const &element)
Append an item to the container.
Definition selectables_registry.hpp:259
void clear()
Definition selectables_registry.hpp:445
Definition file_dialog.hpp:6
Wrapper around items that associates them with an id.
Definition selectables_registry.hpp:40
std::optional< T > item
The item.
Definition selectables_registry.hpp:49
IdType id
Id of the item.
Definition selectables_registry.hpp:42