5#include <unordered_map>
6#include <unordered_set>
29 using ActionList::ActionList;
46 std::shared_ptr<std::vector<std::shared_ptr<Action>>>
Get(
EventID eventId)
const;
53 std::shared_ptr<std::vector<std::shared_ptr<Action>>>
Get(
const std::vector<EventID>& eventIds)
const;
61 void Added(std::shared_ptr<Action> action,
const bool forced)
override;
68 void Removed(std::shared_ptr<Action> action,
const bool forced)
override;
71 static bool ShouldInsertBefore(
const std::shared_ptr<Action>& existing,
const std::shared_ptr<Action>& incoming);
72 void IndexEventAction(
const std::shared_ptr<Action>& action);
73 void UnindexEventAction(
const std::shared_ptr<Action>& action);
75 std::unordered_map<EventID, std::vector<std::shared_ptr<Action>>> mEventActions;
79 auto it = mEventActions.find(eventId);
80 return it != mEventActions.end() && !it->second.empty();
84 auto result = std::make_shared<std::vector<std::shared_ptr<Action>>>();
85 auto it = mEventActions.find(eventId);
86 if (it != mEventActions.end()) {
87 result->insert(result->end(), it->second.begin(), it->second.end());
92inline std::shared_ptr<std::vector<std::shared_ptr<Action>>>
94 auto result = std::make_shared<std::vector<std::shared_ptr<Action>>>();
95 std::unordered_set<EventID> seen;
96 for (
const auto eventId : eventIds) {
97 if (!seen.insert(eventId).second) {
100 auto it = mEventActions.find(eventId);
101 if (it != mEventActions.end()) {
102 result->insert(result->end(), it->second.begin(), it->second.end());
110 IndexEventAction(action);
115 std::stable_sort(list.begin(), list.end(), [](
const std::shared_ptr<Action>& a,
const std::shared_ptr<Action>& b) {
116 const auto* ea = dynamic_cast<const EventAction*>(a.get());
117 const auto* eb = dynamic_cast<const EventAction*>(b.get());
127 return ea->GetEventId() < eb->GetEventId();
131inline void EventActionList::Removed(std::shared_ptr<Action> action,
const bool forced) {
132 UnindexEventAction(action);
133 ActionList::Removed(action, forced);
136inline bool EventActionList::ShouldInsertBefore(
const std::shared_ptr<Action>& existing,
137 const std::shared_ptr<Action>& incoming) {
138 auto* existingListener =
dynamic_cast<ListenerAction*
>(existing.get());
139 auto* incomingListener =
dynamic_cast<ListenerAction*
>(incoming.get());
140 if (existingListener ==
nullptr || incomingListener ==
nullptr) {
144 if (incomingListener->GetPriority() != existingListener->GetPriority()) {
145 return incomingListener->
GetPriority() < existingListener->GetPriority();
148 return incomingListener->GetSequence() < existingListener->GetSequence();
151inline void EventActionList::IndexEventAction(
const std::shared_ptr<Action>& action) {
152 auto* eventAction =
dynamic_cast<EventAction*
>(action.get());
153 if (eventAction ==
nullptr) {
157 auto& bucket = mEventActions[eventAction->GetEventId()];
158 auto insertIt = std::find_if(bucket.begin(), bucket.end(), [&action](
const std::shared_ptr<Action>& existing) {
159 return ShouldInsertBefore(existing, action);
161 bucket.insert(insertIt, action);
164inline void EventActionList::UnindexEventAction(
const std::shared_ptr<Action>& action) {
165 auto* eventAction =
dynamic_cast<EventAction*
>(action.get());
166 if (eventAction ==
nullptr) {
170 auto it = mEventActions.find(eventAction->GetEventId());
171 if (it == mEventActions.end()) {
175 auto& bucket = it->second;
176 bucket.erase(std::remove_if(bucket.begin(), bucket.end(),
177 [&action](
const std::shared_ptr<Action>& existing) {
178 return existing && action && existing->GetId() == action->GetId();
182 if (bucket.empty()) {
183 mEventActions.erase(it);
int32_t EventID
Numeric identifier for an event. -1 is uninitialized, IDs < -1 are internally registered,...
Definition EventTypes.h:8
Generic ordered container of Action instances.
Definition ActionList.h:17
void Added(std::shared_ptr< Action > action, const bool forced) override
Starts the action after insertion.
Definition ActionList.h:40
ActionList specialization with indexed EventAction lookup by EventID.
Definition EventActionList.h:27
void Removed(std::shared_ptr< Action > action, const bool forced) override
Stops the action and removes it from the EventID index when applicable.
Definition EventActionList.h:131
void Added(std::shared_ptr< Action > action, const bool forced) override
Starts the action and updates the EventID index when applicable.
Definition EventActionList.h:108
Event-bus listener action executed by Events::Tick(EventID).
Definition ListenerAction.h:22
EventPriority GetPriority() const
Returns the dispatch priority used for ordering listeners.
bool Has() const
Checks whether any Part of type T is in the list.
Definition PartList.h:777
std::shared_ptr< std::vector< std::shared_ptr< Action > > > Get() const
Returns a snapshot (copy) of all currently live Parts in the list.
Definition PartList.h:825
std::enable_if< std::is_same< P, std::shared_ptr< Action > >::value, std::vector< P > & >::type GetList()
Direct access to the underlying vector for strong-storage lists.
Definition PartList.h:733
std::shared_ptr< T > GetFirst() const
Returns the first Part that can be dynamic_cast to T.
Definition PartList.h:864
Core namespace for the libultraship engine framework.
Definition gfx_direct3d_common.h:14