libultraship
Reimplementations of libultra (N64 SDK) functions for modern hardware
Loading...
Searching...
No Matches
Tickable.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <mutex>
5#include <vector>
6#include <stdint.h>
7#include <stddef.h>
8#ifdef COMPONENT_THREAD_SAFE
9#include <atomic>
10#endif
11
12#include "ship/core/Action.h"
16
17namespace Ship {
18
19class Component;
20
29class Tickable : public std::enable_shared_from_this<Tickable> {
30 public:
35 Tickable(const bool isTicking = true);
36
42 Tickable(const bool isTicking, const std::vector<std::shared_ptr<Action>>& actions);
43 virtual ~Tickable();
44
46 bool IsTicking() const;
47
53 bool Start(const bool force = false);
54
60 bool Stop(const bool force = false);
61
67 bool Tick(EventID eventId);
68
74 bool Tick(const std::vector<EventID>& eventIds);
75
82 template <typename T> bool Tick(const std::vector<EventID>& eventIds);
83
90 template <typename T> bool Tick(EventID eventId);
91
96
101
102#ifdef INCLUDE_PROFILING
108 double GetTime(const ClockType clockType) const;
109#endif
110
111 protected:
113 virtual bool CanStart();
114
116 virtual bool CanStop();
117
122 virtual void Started(const bool forced);
123
128 virtual void Stopped(const bool forced);
129
132#ifdef COMPONENT_THREAD_SAFE
133 std::mutex& GetMutex();
134#endif
135
136 private:
137#ifdef INCLUDE_PROFILING
138 Tickable& SetClock(const ClockType clockType, std::chrono::time_point<std::chrono::steady_clock> clockValue);
139 std::chrono::time_point<std::chrono::steady_clock> GetClock(const ClockType clockType) const;
140#endif
141
142#ifdef COMPONENT_THREAD_SAFE
143 std::atomic<bool> mIsTicking;
144#else
145 bool mIsTicking;
146#endif
147 EventActionList mActions;
148#ifdef COMPONENT_THREAD_SAFE
149 mutable std::mutex mMutex;
150#endif
151#ifdef INCLUDE_PROFILING
152 std::chrono::time_point<std::chrono::steady_clock> mClocks[static_cast<size_t>(ClockType::ClockMax)];
153#endif
154};
155
156// ---- Template method implementations ----
157
158template <typename T> bool Tickable::Tick(const std::vector<EventID>& eventIds) {
159#ifdef COMPONENT_THREAD_SAFE
160 if (!mIsTicking.load(std::memory_order_acquire)) {
161#else
162 if (!mIsTicking) {
163#endif
164 return false;
165 }
166#ifdef INCLUDE_PROFILING
167 const auto start = std::chrono::steady_clock::now();
168#endif
169 bool ran = false;
170 auto allActions = mActions.Get(eventIds);
171 for (const auto& action : *allActions) {
172 if (std::dynamic_pointer_cast<T>(action)) {
173 ran = action->Run() || ran;
174 }
175 }
176#ifdef INCLUDE_PROFILING
177 const auto end = std::chrono::steady_clock::now();
178 (void)end;
179 (void)start;
180#endif
181 return ran;
182}
183
184template <typename T> bool Tickable::Tick(EventID eventId) {
185#ifdef COMPONENT_THREAD_SAFE
186 if (!mIsTicking.load(std::memory_order_acquire)) {
187#else
188 if (!mIsTicking) {
189#endif
190 return false;
191 }
192#ifdef INCLUDE_PROFILING
193 const auto start = std::chrono::steady_clock::now();
194#endif
195 bool ran = false;
196 auto allActions = mActions.Get(eventId);
197 for (const auto& action : *allActions) {
198 if (std::dynamic_pointer_cast<T>(action)) {
199 ran = action->Run() || ran;
200 }
201 }
202#ifdef INCLUDE_PROFILING
203 const auto end = std::chrono::steady_clock::now();
204 (void)end;
205 (void)start;
206#endif
207 return ran;
208}
209
210} // namespace Ship
int32_t EventID
Numeric identifier for an event. -1 is uninitialized, IDs < -1 are internally registered,...
Definition EventTypes.h:8
ActionList specialization with indexed EventAction lookup by EventID.
Definition EventActionList.h:27
std::shared_ptr< std::vector< std::shared_ptr< Action > > > Get(EventID eventId) const
Returns the indexed EventActions for a single EventID.
Definition EventActionList.h:83
Manages a collection of Actions and indexed EventActions.
Definition Tickable.h:29
virtual bool CanStop()
Permission hook; override to prevent stopping. Defaults to true.
const EventActionList & GetActionList() const
Returns a const reference to the internal EventActionList.
bool Start(const bool force=false)
Starts ticking, enabling EventID-targeted Tick() calls on Actions.
bool Stop(const bool force=false)
Stops ticking; subsequent Tick() calls become no-ops.
double GetTime(const ClockType clockType) const
Returns the wall-clock time for a profiling checkpoint.
bool Tick(const std::vector< EventID > &eventIds)
Ticks only indexed EventActions whose EventID matches one of the given IDs.
virtual ~Tickable()
bool Tick(EventID eventId)
Ticks only indexed EventActions whose EventID matches the given ID.
Tickable(const bool isTicking, const std::vector< std::shared_ptr< Action > > &actions)
Constructs a Tickable with an initial set of Actions.
EventActionList & GetActionList()
Returns a mutable reference to the internal EventActionList.
virtual void Stopped(const bool forced)
Notification hook called after the Tickable has been stopped.
Tickable(const bool isTicking=true)
Constructs a Tickable.
virtual bool CanStart()
Permission hook; override to prevent starting. Defaults to true.
bool IsTicking() const
Returns true if this Tickable is currently ticking.
std::mutex & GetMutex()
Returns a reference to the internal mutex for synchronization. Only available when COMPONENT_THREAD_S...
virtual void Started(const bool forced)
Notification hook called after the Tickable has been started.
Core namespace for the libultraship engine framework.
Definition gfx_direct3d_common.h:14
ClockType
Identifies a profiling time-point within an Action or Tickable.
Definition Action.h:22