Design Patterns
-
class Memento
Base class for implementing the Memento design pattern.
The
Mementoclass enables derived classes to save and restore their state usingSnapshotobjects. It achieves this by exposingsave()andload()methods that internally call the private_saveToSnapshot()and_loadFromSnapshot()methods of the derived class. These private methods are accessed using thefriendmechanism, allowingMementoto interact directly with the internals of derived classes.
Usage:
Derive your class from
Memento.Implement the private
_saveToSnapshot()and_loadFromSnapshot()methods to define what state should be saved and restored.
Example:
class TestClass : public Memento { friend class Memento; public: int x_ = 0; std::string y_; private: void _saveToSnapshot(Snapshot& snapshot) const override { snapshot << x_ << y_; } void _loadFromSnapshot(Snapshot& snapshot) override { snapshot >> x_ >> y_; } };
Public Types
-
using Snapshot = DataBuffer
Public Functions
-
Snapshot save()
Saves the current state of the object into a
Snapshot.This method creates a new
Snapshotand invokes the_saveToSnapshot()method of the derived class to populate it with the object’s state.- Returns:
A
Snapshotcontaining the serialized state of the object.
-
void load(const Snapshot &snapshot)
Restores the object’s state from a given
Snapshot.This method takes a
Snapshotcontaining a previously saved state and invokes the_loadFromSnapshot()method of the derived class to restore the object’s state.- Parameters:
snapshot – The
Snapshotcontaining the serialized state to restore.
Protected Functions
-
virtual ~Memento() = default
Virtual destructor for proper cleanup of derived classes.
-
template<typename TType>
class Singleton A template class that ensures only one instance of
TTypeexists.The
Singletonclass provides a thread-safe way to create and access a single instance of a templated classTType. It guarantees thatTTypewill be instantiated only once and provides access to that instance through a static method.The
Singletonclass also prevents re-instantiation by throwing an exception if an attempt is made to instantiate the class after it has been created.
Thread Safety:
The
Singletonclass is designed to be thread-safe, using a mutex to ensure that the instance is only created once even in multithreaded scenarios.- Template Parameters:
TType – The class for which the singleton pattern is implemented.
Public Functions
Public Static Functions
-
static inline TType *instance()
Returns the managed instance of TType.
If the instance is not yet created, it throws an exception.
Protected, thread safe by using
std::lock_guard.- Throws:
std::runtime_error – If the instance is not yet created.
- Returns:
Pointer to the unique instance of TType.
-
template<typename ...TArgs>
static inline void instantiate(TArgs&&... p_args) Instantiates the managed instance of TType with arguments.
This function will only instantiate the instance once. If an attempt is made to instantiate it after it has already been created, it throws an exception.
- Template Parameters:
TArgs – The types of the arguments used to instantiate the instance.
- Parameters:
p_args – The arguments for instantiating the instance.
- Throws:
std::runtime_error – If the instance is already created.
-
template<typename TState>
class StateMachine A template class for managing states and transitions.
The
StateMachineclass enables handling of different states and their transitions through a state machine pattern. It allows users to define actions that execute when the state machine enters a specific state, and it also allows defining transitions between states.- Template Parameters:
TState – The type of the state, typically an enum representing the states.
Public Functions
-
inline StateMachine()
Default constructor for
StateMachine.Note
initialized_state_is false until any state is available.
-
inline void addState(const TState &state)
Adds a state to the state machine.
This method registers a state that the state machine can transition into.
- Parameters:
state – The state to be added to the state machine.
-
inline void addTransition(const TState &startState, const TState &finalState, const std::function<void()> &lambda)
Adds a transition between two states.
This method registers a transition between a start state and a final state. It associates a lambda function to execute during the transition.
- Parameters:
startState – The starting state of the transition.
finalState – The final state after the transition.
lambda – The lambda function to execute during the transition.
-
inline void addAction(const TState &state, const std::function<void()> &lambda)
Adds an action to be executed when the state machine enters a specific state.
This method registers an action to be executed when the state machine is in a specific state.
- Parameters:
state – The state for which the action is to be registered.
lambda – The lambda function to execute when the state machine enters the state.
-
inline void transitionTo(const TState &state)
Transition the state machine to a specific state.
This method changes the state of the state machine and executes any transition action associated with it. If no transition is defined, it throws an exception.
- Parameters:
state – The state to transition to.
- Throws:
std::runtime_error – If current state has not been initialized.
std::invalid_argument – If the transition is not defined.
-
inline void update()
Updates the state machine, executing the action for the current state.
This method checks if an action is defined for the current state and executes it. If no action is defined, throws an exception.
- Throws:
std::runtime_error – If current state has not been initialized.
std::invalid_argument – If the transition is not defined.
Private Members
-
std::unordered_map<TState, std::function<void()>> actions_
Actions for each state (one action per state).
-
std::unordered_map<TState, std::unordered_map<TState, std::function<void()>>> transitions_
Transitions between states.
-
bool initialized_state_
Member to know if the machine has been initialized (any state is known)
-
template<typename TEvent, typename ...TArgs>
class Observer A templated Observer class for subscribing to and notifying events.
This class implements the Observer design pattern. It allows users to subscribe to specific events with lambda functions, and it notifies all subscribers when an event is triggered.
- Template Parameters:
TEvent – The type of event that will be observed.
TArgs – Variadic template for the types of arguments passed to the callbacks.
Public Functions
-
inline void subscribe(const TEvent &event, const std::function<void(TArgs...)> &lambda)
Subscribe a lambda function to an event.
This method allows a user to subscribe a lambda function to a specific event. The lambda will be called when the event is triggered via the
notifymethod.- Parameters:
event – The event to subscribe to.
lambda – The lambda function to be called when the event is triggered.
-
template<typename T>
class ObservableValue Encapsulates a value and notifies subscribers when it changes.
- Template Parameters:
T – The type of the value being observed.
Public Functions
-
inline ObservableValue(const T &initial_value)
Constructs an ObservableValue with an initial value.
- Parameters:
initial_value – The initial value.