4#include <unordered_map>
6#include "structure/design_pattern/spk_contract_provider.hpp"
8#include "utils/spk_debug_macro.hpp"
17 template <
typename TType>
28 std::unordered_map<TType, ContractProvider> _callbacks;
29 mutable std::mutex _mutex;
37 _state(p_initialState)
49 _state(std::move(p_other._state)),
50 _callbacks(std::move(p_other._callbacks))
63 std::scoped_lock lock(_mutex, p_other._mutex);
65 _state = std::move(p_other._state);
66 _callbacks = std::move(p_other._callbacks);
79 std::scoped_lock lock(_mutex);
81 if (_state != p_newState)
84 _callbacks[_state].trigger();
94 std::scoped_lock lock(_mutex);
106 std::scoped_lock lock(_mutex);
107 return (std::move(_callbacks[p_state].subscribe(p_callback)));
Tracks a state value and triggers callbacks per-state.
Definition spk_stateful_object.hpp:19
ContractProvider::Job Job
Definition spk_stateful_object.hpp:24
TType state() const
Returns the current state.
Definition spk_stateful_object.hpp:92
void setState(const TType &p_newState)
Sets a new state and triggers callbacks registered for it.
Definition spk_stateful_object.hpp:77
Contract addCallback(const TType &p_state, const Job &p_callback)
Subscribes a callback for a specific state.
Definition spk_stateful_object.hpp:104
StatefulObject(StatefulObject &&p_other) noexcept
Move-constructs, transferring state and callbacks.
Definition spk_stateful_object.hpp:48
StatefulObject & operator=(StatefulObject &&p_other) noexcept
Move-assigns, transferring state and callbacks.
Definition spk_stateful_object.hpp:59
StatefulObject(const TType &p_initialState)
Builds with an initial state.
Definition spk_stateful_object.hpp:36
ContractProvider::Contract Contract
Definition spk_stateful_object.hpp:22
typename Contract::Job Job
Definition spk_contract_provider.hpp:267