11 const auto id = action.get_id();
26 info.
action = std::move(action);
27 if(scope_policy.scope.empty())
29 info.
scopes.insert(info.
scopes.end(), scopes_.begin(), scopes_.end());
35 info.
scopes.emplace_back(scope_policy.scope);
39 info.
scopes.insert(info.
scopes.end(), scopes_.begin(), scopes_.end());
40 info.
scopes.emplace_back(scope_policy.scope);
45 auto iter = actions_.find(
id);
46 if(iter == actions_.end())
48 auto& info = actions_[id];
49 fill_info(info, std::move(action));
56 auto& info = pending_actions_[id];
57 fill_info(info, std::move(action));
65 auto iter = actions_.find(
id);
66 if(iter != std::end(actions_))
71 auto pending_iter = pending_actions_.find(
id);
72 if(pending_iter != std::end(pending_actions_))
74 pending_actions_.erase(pending_iter);
80 for(
auto& info : actions_)
82 if(std::find(info.second.scopes.begin(), info.second.scopes.end(), scope) != info.second.scopes.end())
91 auto iter = actions_.find(
id);
92 if(iter != std::end(actions_))
100 paused_scopes_.insert(std::make_pair(scope, key));
102 for(
auto& info : actions_)
104 if(std::find(info.second.scopes.begin(), info.second.scopes.end(), scope) != info.second.scopes.end())
113 auto iter = actions_.find(action);
114 if(iter != std::end(actions_))
122 paused_scopes_.erase(std::make_pair(scope, key));
124 for(
auto& info : actions_)
126 if(std::find(info.second.scopes.begin(), info.second.scopes.end(), scope) != info.second.scopes.end())
135 auto iter = actions_.find(
id);
136 if(iter != std::end(actions_))
144 for(
auto& info : actions_)
146 if(std::find(info.second.scopes.begin(), info.second.scopes.end(), scope) != info.second.scopes.end())
155 auto iter = actions_.find(
id);
156 if(iter == std::end(actions_))
160 auto& action = iter->second.action;
166 auto& depth = iter->second.depth;
171 throw std::runtime_error(
"Cannot call stop_and_finish from callbacks.");
178 const bool force =
true;
194 for(
auto& info : actions_)
196 if(std::find(info.second.scopes.begin(), info.second.scopes.end(), scope) != info.second.scopes.end())
205 auto iter = actions_.find(
id);
206 if(iter != std::end(actions_))
215 auto iter = actions_.find(
id);
216 if(iter != std::end(actions_))
225 auto iter = actions_.find(
id);
226 if(iter != std::end(actions_))
235 auto iter = actions_.find(
id);
236 if(iter != std::end(actions_))
245 auto iter = actions_.find(
id);
246 if(iter != std::end(actions_))
254 auto iter = actions_.find(
id);
255 if(iter != std::end(actions_))
264 auto iter = actions_.find(
id);
265 if(iter != std::end(actions_))
274 auto iter = actions_.find(
id);
275 if(iter != std::end(actions_))
284 auto iter = actions_.find(
id);
285 if(iter != std::end(actions_))
294 auto iter = actions_.find(
id);
295 if(iter != std::end(actions_))
303 auto iter = actions_.find(
id);
304 if(iter == std::end(actions_))
314 if(delta < duration_t::zero())
316 delta = duration_t::zero();
319 std::vector<seq_id_t> actions_to_start;
320 actions_to_start.reserve(pending_actions_.size());
322 for(
auto& kvp : pending_actions_)
324 actions_to_start.emplace_back(kvp.first);
326 actions_[kvp.first] = std::move(kvp.second);
328 pending_actions_.clear();
330 for(
auto id : actions_to_start)
332 start_action(actions_.at(
id));
335 for(
auto id : get_ids())
337 auto& action = actions_[id];
352 actions_.erase(action.action.get_id());
364 auto iter = std::find(scopes_.begin(), scopes_.end(), scope);
365 if(iter != scopes_.end())
367 throw std::logic_error(
"push_scope that is already pushed.");
370 scopes_.emplace_back(scope);
371 current_scope_idx_ =
static_cast<int32_t
>(scopes_.size()) - 1;
376 if(current_scope_idx_ < int32_t(scopes_.size()))
384 auto iter = std::find(scopes_.begin(), scopes_.end(), scope);
385 scopes_.erase(iter, scopes_.end());
387 current_scope_idx_ =
static_cast<int32_t
>(scopes_.size()) - 1;
393 current_scope_idx_ = (-1);
396void seq_manager::start_action(seq_info& info)
398 auto& action = info.action;
400 bool paused_by_scope =
false;
402 for(
const auto& scope : info.scopes)
404 auto it = std::find_if(paused_scopes_.begin(),
405 paused_scopes_.end(),
408 return sc.first == scope;
410 if(it != paused_scopes_.end())
412 const auto& scope_pause_key = it->second;
414 paused_by_scope =
true;
429 static const std::string default_scope{};
432 return default_scope;
434 return scopes_.at(current_scope_idx_);
447auto seq_manager::get_ids() const ->
std::vector<
seq_id_t>
449 std::vector<seq_id_t> result;
450 result.reserve(actions_.size());
451 for(
const auto& kvp : actions_)
453 result.emplace_back(kvp.first);
460 for(
const auto& kvp : actions_)
462 if(std::find(kvp.second.scopes.begin(), kvp.second.scopes.end(), scope_id) != kvp.second.scopes.end())
Provides a sequence-based action management system for controlling and scheduling actions.
size_t seq_id_t
Represents a unique identifier for sequence actions.
@ finished
The action has finished.
auto is_paused(seq_id_t id) -> bool
Checks if the action is paused.
void resume(const seq_id_t id)
Resumes the action associated with the given ID.
auto is_running(seq_id_t id) -> bool
Checks if the action is running.
std::chrono::nanoseconds duration_t
Represents a duration in nanoseconds.
Represents an action within the sequence management system. Contains lifecycle events and management ...
Stores information about an action, including its state, depth, and associated scopes.
seq_action action
The action being managed.
std::vector< std::string > scopes
The scopes associated with the action.
void resume(seq_id_t id)
Resumes the action associated with the specified ID.
void close_scope(const std::string &scope)
Closes a scope, removing it from the stack.
auto get_elapsed(seq_id_t id) const -> duration_t
Gets the elapsed time of an action.
auto get_duration(seq_id_t id) const -> duration_t
Gets the total duration of an action.
void stop_and_finish(seq_id_t id, duration_t finish_after=0ms)
Stops an action and ensures it completes after the specified duration.
void resume_all(const std::string &scope={}, const std::string &key={})
Resumes all actions within the specified scope and key.
auto is_stopping(seq_id_t id) const -> bool
Checks if an action is stopping.
void push_scope(const std::string &scope)
Pushes a scope onto the scope stack.
auto is_finished(seq_id_t id) const -> bool
Checks if an action has finished.
auto get_overflow(seq_id_t id) const -> duration_t
Gets the overflow time of an action.
void stop_and_finish_all(const std::string &scope={})
Stops all actions in the specified scope and ensures they complete.
auto get_current_scope() const -> const std::string &
Gets the current scope from the scope stack.
void clear_scopes()
Clears all scopes from the scope stack.
void stop_all(const std::string &scope={})
Stops all actions within the specified scope.
auto get_speed_multiplier(seq_id_t id) -> float
Gets the speed multiplier of an action.
auto get_scopes() const -> const std::vector< std::string > &
Gets the list of all active scopes.
auto is_paused(seq_id_t id) const -> bool
Checks if an action is paused.
void set_elapsed(seq_id_t id, duration_t elapsed)
Sets the elapsed time of an action (use with caution).
void set_speed_multiplier(seq_id_t id, float speed_multiplier=1.0f)
Sets the speed multiplier for an action.
void pause_all(const std::string &scope={}, const std::string &key={})
Pauses all actions within the specified scope and key.
void pause(seq_id_t id)
Pauses the action associated with the specified ID.
void update(seq_id_t id, duration_t delta)
Updates the elapsed time of a specific action.
void stop_when_finished_all(const std::string &scope={})
Marks all actions in the specified scope to stop when they finish.
auto get_actions() const -> const action_collection_t &
Gets the collection of all managed actions.
seq_id_t start(seq_action action, const seq_scope_policy &scope_policy={})
Starts a new action and associates it with the specified scope policy.
std::map< seq_id_t, seq_info > action_collection_t
Alias for the collection of actions managed by the seq_manager.
void stop_when_finished(seq_id_t id)
Marks an action to stop when it finishes.
bool has_action_with_scope(const std::string &scope_id)
Checks if there is any action associated with the specified scope ID.
void pop_scope()
Pops the current scope from the scope stack.
void stop(seq_id_t id)
Stops the action associated with the specified ID.
auto is_running(seq_id_t id) const -> bool
Checks if an action is running.
static void pause_forced(seq_action &action)
Forcibly pauses a given action.
static auto get_duration(const seq_action &self) -> duration_t
Gets the total duration of a given action.
static void pause(seq_action &action)
Pauses a given action.
static void start(seq_action &action)
Starts a given action.
static void stop(seq_action &action)
Stops a given action.
static auto update(seq_action &action, duration_t delta) -> state_t
Updates a given action with a time delta.
static auto is_running(const seq_action &action) -> bool
Checks if the action is currently running.
static void set_elapsed(seq_action &self, duration_t elapsed)
Sets the elapsed time of a given action.
static void stop_and_finished(seq_action &action)
Marks a given action as stopped and finished.
static void stop_when_finished(seq_action &action)
Marks a given action to stop when finished.
static void set_speed_multiplier(seq_action &action, float speed_multiplier)
Sets the speed multiplier for a given action.
static auto get_speed_multiplier(const seq_action &action) -> float
Gets the speed multiplier of a given action.
static auto is_finished(const seq_action &action) -> bool
Checks if the action is finished.
static auto is_paused(const seq_action &action) -> bool
Checks if the action is currently paused.
static auto get_overflow(const seq_action &self) -> duration_t
Computes the overflow duration of a given action if it exceeds its total duration.
static auto get_state(const seq_action &action) -> state_t
Gets the current state of a given action.
static auto is_stop_when_finished_requested(const seq_action &action) -> bool
Checks if the action is requested to stop when finished.
static void resume(seq_action &action, bool force=false)
Resumes a given action, optionally forcing the resume.
static auto get_elapsed(const seq_action &self) -> duration_t
Gets the elapsed time of a given action.
Defines policies for scoping actions in a sequence.
@ stacked
Actions share the same scope and stack behavior.
@ independent
Actions operate independently within their scope.