libultraship
Reimplementations of libultra (N64 SDK) functions for modern hardware
Loading...
Searching...
No Matches
Events.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include <cstdint>
5#include <cstddef>
6#include <unordered_map>
7#include <string>
8
12
13namespace Ship {
14
24 const char* Name;
30 std::unordered_map<std::string, EventMetadata> Callers;
32 std::unordered_map<ListenerID, std::shared_ptr<ListenerAction>> Listeners;
33};
34
74class Events : public TickableComponent {
75 public:
84 EventID ID = -1;
86 IEvent* Event = nullptr;
88 const char* File = nullptr;
90 int Line = 0;
92 const char* Key = nullptr;
93 };
94
101 Events() : TickableComponent("Events", nullptr) {
103 Start();
104 }
105
111 EventID RegisterEvent(const char* name = nullptr);
112
128 const char* file = nullptr, int line = 0);
129
140
146
159 void CallEvent(EventID id, IEvent* event, const char* file = nullptr, int line = 0, const char* key = nullptr);
160
167 if (mEventRegistry.contains(id)) {
168 return &mEventRegistry.at(id);
169 }
170 return nullptr;
171 }
172
178 std::unordered_map<EventID, EventRegistration>& GetEventRegistrations() {
179 return this->mEventRegistry;
180 }
181
187
188 private:
194 EventRegistration* GetOrCreateEventRegistration(EventID id);
195
197 std::unordered_map<EventID, EventRegistration> mEventRegistry;
199 EventID mInternalEventID = -2;
201 std::vector<DispatchContext> mDispatchStack;
202};
203
204} // namespace Ship
EventPriority
Priority levels that control listener dispatch order within an event.
Definition EventTypes.h:19
@ EVENT_PRIORITY_NORMAL
Default priority for most listeners.
Definition EventTypes.h:21
int32_t EventID
Numeric identifier for an event. -1 is uninitialized, IDs < -1 are internally registered,...
Definition EventTypes.h:8
void(* EventCallback)(IEvent *)
Callback signature for event listeners. The argument is the event payload cast to IEvent*.
Definition EventTypes.h:39
int64_t ListenerID
Numeric identifier for a registered listener; used to unregister later.
Definition EventTypes.h:10
void MarkInitialized()
Marks this component as initialized without going through Init().
Tickable event bus that dispatches listeners through Tick(EventID).
Definition Events.h:74
void UnregisterListener(EventID id, ListenerID listenerId)
Removes a previously registered listener from an event.
EventID RegisterEvent(const char *name=nullptr)
Allocates a new internally managed EventID.
ListenerID RegisterListener(EventID id, EventCallback callback, EventPriority priority=EVENT_PRIORITY_NORMAL, const char *file=nullptr, int line=0)
Registers a callback listener for an event.
EventRegistration * GetEventRegistration(EventID id)
Returns the registration record for an EventID, if present.
Definition Events.h:166
std::unordered_map< EventID, EventRegistration > & GetEventRegistrations()
Returns the complete event registry map.
Definition Events.h:178
void CallEvent(EventID id, IEvent *event, const char *file=nullptr, int line=0, const char *key=nullptr)
Dispatches an event by pushing dispatch state and ticking its EventID.
void RemoveEvent(EventID id)
Removes an event and all listeners currently registered to it.
const DispatchContext * GetActiveDispatchContext() const
Returns the current top-of-stack dispatch context.
Events()
Constructs and self-initializes the global event bus component.
Definition Events.h:101
Combines Tickable and Component, auto-registering with a Context.
Definition TickableComponent.h:42
Core namespace for the libultraship engine framework.
Definition gfx_direct3d_common.h:14
Base event payload struct.
Definition EventTypes.h:33
Tracks registration state for a single EventID.
Definition Events.h:22
uint64_t NextListenerSequence
Registration-order sequence used to preserve stable listener ordering.
Definition Events.h:28
const char * Name
Optional human-readable name for this event.
Definition Events.h:24
std::unordered_map< ListenerID, std::shared_ptr< ListenerAction > > Listeners
Active listeners for this event keyed by ListenerID.
Definition Events.h:32
ListenerID NextListenerID
Monotonically increasing listener identifier counter for this event.
Definition Events.h:26
std::unordered_map< std::string, EventMetadata > Callers
Diagnostic map of event call sites keyed by file:line string.
Definition Events.h:30
Active payload and call-site data for a single in-flight dispatch.
Definition Events.h:82
const char * Key
Optional deduplication key used for caller diagnostics.
Definition Events.h:92
IEvent * Event
Event payload supplied by the caller.
Definition Events.h:86
EventID ID
EventID currently being dispatched.
Definition Events.h:84
int Line
Source line that initiated the dispatch, when provided.
Definition Events.h:90
Raw file buffer as read from an archive or the filesystem.
Definition File.h:56