- Types
- ConfigParser
- CallbackRegistry
- VariableManager
- EventDispatcher
- StateMachine
- State
- StateObserver
Enum defining the types of variables that can be stored.
enum class VariableType {
INT, ///< Integer type
FLOAT, ///< Floating point type
STRING, ///< String type
BOOL ///< Boolean type
};Structure for storing values with support for multiple types. Implements the Rule of Five with proper move semantics.
struct VariableValue {
VariableType type;
union {
int int_value;
float float_value;
std::string string_value;
bool bool_value;
};
VariableValue();
VariableValue(int v);
VariableValue(float v);
VariableValue(const std::string& v);
VariableValue(bool v);
~VariableValue();
VariableValue(const VariableValue& other);
VariableValue& operator=(const VariableValue& other);
VariableValue(VariableValue&& other) noexcept;
VariableValue& operator=(VariableValue&& other) noexcept;
int asInt() const;
float asFloat() const;
std::string asString() const;
bool asBool() const;
std::string toString() const;
};Move Constructor
VariableValue(VariableValue&& other) noexcept;Moves the internal value from other. After the move, other is left in a valid but unspecified state. For STRING type, performs a non-throwing move of the std::string.
Move Assignment Operator
VariableValue& operator=(VariableValue&& other) noexcept;Moves the internal value from other. After the move, other is left in a valid but unspecified state. Properly handles self-assignment and cleans up existing resources before moving.
Structure representing a transition between states.
struct TransitionEvent {
std::string event_name;
std::string from_state;
std::string to_state;
std::map<std::string, VariableValue> data;
std::chrono::system_clock::time_point timestamp;
TransitionEvent();
};Structure containing information about a state.
struct StateInfo {
std::string name;
std::map<std::string, VariableValue> variables;
std::string on_enter_callback;
std::string on_exit_callback;
std::vector<std::string> actions;
StateInfo();
explicit StateInfo(const std::string& name);
};Structure containing information about a transition.
struct TransitionInfo {
std::string from_state;
std::string to_state;
std::string event_name;
std::string guard_callback;
std::string transition_callback;
std::vector<std::string> actions;
TransitionInfo();
};ConfigParser();void loadFromFile(const std::string& file_path);Load configuration from a YAML file.
Parameters:
file_path- Path to the YAML configuration file
Throws:
ConfigExceptionif the file cannot be loaded or parsed
void loadFromString(const std::string& yaml_content);Load configuration from a YAML string.
Parameters:
yaml_content- YAML configuration as a string
Throws:
ConfigExceptionif the content cannot be parsed
const std::map<std::string, VariableValue>& getGlobalVariables() const;Get all global variables.
Returns: Reference to the map of global variables
const std::map<std::string, StateInfo>& getStates() const;Get all state definitions.
Returns: Reference to the map of state definitions
const std::vector<TransitionInfo>& getTransitions() const;Get all transition definitions.
Returns: Reference to the vector of transition definitions
bool hasState(const std::string& state_name) const;Check if a state exists.
Parameters:
state_name- Name of the state to check
Returns: true if the state exists, false otherwise
const StateInfo& getState(const std::string& state_name) const;Get information about a specific state.
Parameters:
state_name- Name of the state
Returns: Reference to the state information
Throws:
ConfigExceptionif the state does not exist
std::vector<TransitionInfo> getTransitionsFrom(const std::string& state_name) const;Get all transitions from a specific state.
Parameters:
state_name- Name of the source state
Returns: Vector of transitions from the specified state
const TransitionInfo* findTransition(const std::string& from_state, const std::string& event_name) const;Find a transition by event name.
Parameters:
from_state- Name of the source stateevent_name- Name of the event
Returns: Pointer to the transition, or nullptr if not found
void clear();Clear all loaded configuration.
CallbackRegistry();void registerStateCallback(
const std::string& state_name,
const std::string& callback_type,
StateCallback callback
);Register a state callback.
Parameters:
state_name- Name of the statecallback_type- Type of callback ("on_enter" or "on_exit")callback- Callback function
void registerTransitionCallback(
const std::string& from_state,
const std::string& to_state,
TransitionCallback callback
);Register a transition callback.
Parameters:
from_state- Name of the source stateto_state- Name of the target statecallback- Callback function
void registerGuard(
const std::string& from_state,
const std::string& to_state,
const std::string& event_name,
GuardCallback callback
);Register a guard callback.
Parameters:
from_state- Name of the source stateto_state- Name of the target stateevent_name- Name of the eventcallback- Guard callback function
void registerAction(
const std::string& action_name,
ActionCallback callback
);Register an action callback.
Parameters:
action_name- Name of the actioncallback- Action callback function
void callStateCallback(
const std::string& state_name,
const std::string& callback_type
) const;Call a state callback.
Parameters:
state_name- Name of the statecallback_type- Type of callback ("on_enter" or "on_exit")
void callTransitionCallback(
const std::string& from_state,
const std::string& to_state,
const TransitionEvent& event
) const;Call a transition callback.
Parameters:
from_state- Name of the source stateto_state- Name of the target stateevent- Transition event data
bool callGuard(
const std::string& from_state,
const std::string& to_state,
const std::string& event_name
) const;Call a guard callback.
Parameters:
from_state- Name of the source stateto_state- Name of the target stateevent_name- Name of the event
Returns: true if guard allows transition, false otherwise (or if guard not found)
void callAction(const std::string& action_name) const;Call an action callback.
Parameters:
action_name- Name of the action
bool hasStateCallback(
const std::string& state_name,
const std::string& callback_type
) const;Check if a state callback exists.
Parameters:
state_name- Name of the statecallback_type- Type of callback
Returns: true if callback exists, false otherwise
bool hasTransitionCallback(
const std::string& from_state,
const std::string& to_state
) const;Check if a transition callback exists.
Parameters:
from_state- Name of the source stateto_state- Name of the target state
Returns: true if callback exists, false otherwise
bool hasGuard(
const std::string& from_state,
const std::string& to_state,
const std::string& event_name
) const;Check if a guard callback exists.
Parameters:
from_state- Name of the source stateto_state- Name of the target stateevent_name- Name of the event
Returns: true if guard exists, false otherwise
bool hasAction(const std::string& action_name) const;Check if an action callback exists.
Parameters:
action_name- Name of the action
Returns: true if action exists, false otherwise
void clear();Clear all registered callbacks.
VariableManager();void setGlobalVariable(const std::string& name, const VariableValue& value);Set a global variable.
Parameters:
name- Variable namevalue- Variable value
void setStateVariable(
const std::string& state_name,
const std::string& name,
const VariableValue& value
);Set a state-local variable.
Parameters:
state_name- Name of the statename- Variable namevalue- Variable value
std::optional<VariableValue> getVariable(
const std::string& state_name,
const std::string& name
) const;Get a variable (checks state-local first, then global).
Parameters:
state_name- Name of the statename- Variable name
Returns: Optional variable value
std::optional<VariableValue> getGlobalVariable(const std::string& name) const;Get a global variable.
Parameters:
name- Variable name
Returns: Optional variable value
std::optional<VariableValue> getStateVariable(
const std::string& state_name,
const std::string& name
) const;Get a state-local variable.
Parameters:
state_name- Name of the statename- Variable name
Returns: Optional variable value
bool hasVariable(
const std::string& state_name,
const std::string& name
) const;Check if a variable exists (checks state-local first, then global).
Parameters:
state_name- Name of the statename- Variable name
Returns: true if variable exists, false otherwise
bool hasGlobalVariable(const std::string& name) const;Check if a global variable exists.
Parameters:
name- Variable name
Returns: true if variable exists, false otherwise
bool hasStateVariable(
const std::string& state_name,
const std::string& name
) const;Check if a state-local variable exists.
Parameters:
state_name- Name of the statename- Variable name
Returns: true if variable exists, false otherwise
bool removeVariable(
const std::string& state_name,
const std::string& name
);Remove a variable (checks state-local first, then global).
Parameters:
state_name- Name of the statename- Variable name
Returns: true if variable was removed, false if not found
bool removeGlobalVariable(const std::string& name);Remove a global variable.
Parameters:
name- Variable name
Returns: true if variable was removed, false if not found
bool removeStateVariable(
const std::string& state_name,
const std::string& name
);Remove a state-local variable.
Parameters:
state_name- Name of the statename- Variable name
Returns: true if variable was removed, false if not found
std::map<std::string, VariableValue> getGlobalVariables() const;Get all global variables.
Returns: Copy of the global variables map
Note: Returns a copy to ensure thread safety. The returned map is a snapshot of the variables at the time of the call and will not reflect subsequent changes.
std::map<std::string, VariableValue> getStateVariables(
const std::string& state_name
) const;Get all state-local variables for a state.
Parameters:
state_name- Name of the state
Returns: Copy of the state-local variables map
Note: Returns a copy to ensure thread safety. The returned map is a snapshot of the variables at the time of the call and will not reflect subsequent changes.
void clear();Clear all variables.
void clearStateVariables(const std::string& state_name);Clear all state-local variables for a state.
Parameters:
state_name- Name of the state
void clearGlobalVariables();Clear all global variables.
void copyStateVariables(
const std::string& from_state,
const std::string& to_state
);Copy variables from one state to another.
Parameters:
from_state- Source state nameto_state- Target state name
EventDispatcher();void dispatchEvent(
const std::string& event_name,
const TransitionEvent& event
);Dispatch an event for processing.
Parameters:
event_name- Name of the eventevent- Transition event data
void processEvents();Process all events in the queue (synchronously).
bool processOneEvent();Process one event from the queue.
Returns: true if an event was processed, false if queue was empty
size_t getEventQueueSize() const;Get the number of events in the queue.
Returns: Number of events in the queue
void clearEventQueue();Clear the event queue.
bool hasPendingEvents() const;Check if there are pending events in the queue.
Returns: true if there are pending events, false otherwise
void setEventHandler(EventHandler handler);Set the event handler function.
Parameters:
handler- Event handler function
bool hasEventHandler() const;Check if an event handler is set.
Returns: true if handler is set, false otherwise
void start();Start the dispatcher (for future async support).
void stop();Stop the dispatcher.
bool isRunning() const;Check if the dispatcher is running.
Returns: true if running, false otherwise
void waitForEmptyQueue() const;Wait for the event queue to become empty.
explicit StateMachine(const std::string& config_path);
explicit StateMachine(const std::string& yaml_content, bool is_content);Create a state machine from a YAML configuration file or string.
Parameters:
config_path- Path to the YAML configuration fileyaml_content- YAML configuration as a stringis_content- Set totrueifyaml_contentis YAML content
Throws:
ConfigExceptionif the configuration cannot be loaded or parsed
~StateMachine();void start();Start the state machine (transition to initial state).
Throws:
StateExceptionif the machine is already started or if initial state not found
void stop();Stop the state machine.
void reset();Reset the state machine to initial state.
std::string getCurrentState() const;Get the current state name.
Returns: Current state name
bool hasState(const std::string& state_name) const;Check if a state exists.
Parameters:
state_name- Name of the state to check
Returns: true if the state exists, false otherwise
std::vector<std::string> getAllStates() const;Get all state names.
Returns: Vector of all state names
void triggerEvent(const std::string& event_name);
void triggerEvent(const std::string& event_name, const std::map<std::string, VariableValue>& data);Trigger an event.
Parameters:
event_name- Name of the eventdata- Optional event data
Throws:
StateExceptionif the machine is not started or if transition is not valid
template<typename T>
void registerStateCallback(
const std::string& state_name,
const std::string& callback_type,
void (T::*callback)(),
T* instance
);Register a state callback.
Parameters:
state_name- Name of the statecallback_type- Type of callback ("on_enter" or "on_exit")callback- Member function pointerinstance- Pointer to the object instance
template<typename T>
void registerTransitionCallback(
const std::string& from_state,
const std::string& to_state,
void (T::*callback)(const TransitionEvent&),
T* instance
);Register a transition callback.
Parameters:
from_state- Name of the source stateto_state- Name of the target statecallback- Member function pointerinstance- Pointer to the object instance
template<typename T>
void registerGuard(
const std::string& from_state,
const std::string& to_state,
const std::string& event_name,
bool (T::*callback)(),
T* instance
);Register a guard callback.
Parameters:
from_state- Name of the source stateto_state- Name of the target stateevent_name- Name of the eventcallback- Member function pointerinstance- Pointer to the object instance
template<typename T>
void registerAction(
const std::string& action_name,
void (T::*callback)(),
T* instance
);Register an action callback.
Parameters:
action_name- Name of the actioncallback- Member function pointerinstance- Pointer to the object instance
void setVariable(const std::string& name, const VariableValue& value);Set a variable (global or state-local).
Parameters:
name- Variable namevalue- Variable value
VariableValue getVariable(const std::string& name) const;Get a variable (checks state-local first, then global).
Parameters:
name- Variable name
Returns: Variable value
Throws:
StateExceptionif the variable is not found
bool hasVariable(const std::string& name) const;Check if a variable exists (checks state-local first, then global).
Parameters:
name- Variable name
Returns: true if variable exists, false otherwise
void registerStateObserver(const std::shared_ptr<StateObserver>& observer);Register a state observer.
Parameters:
observer- Shared pointer to the observer
Note: The observer is stored as a std::weak_ptr internally to prevent dangling pointers. If the observer is destroyed, it will be automatically removed from the notification list on the next notification cycle. This ensures safe lifetime management and prevents memory leaks.
Example:
auto observer = std::make_shared<MyObserver>();
fsm.registerStateObserver(observer);
// No manual cleanup needed - automatic when observer goes out of scopevoid unregisterStateObserver(const std::shared_ptr<StateObserver>& observer);Unregister a state observer.
Parameters:
observer- Shared pointer to the observer
Note: Removes the observer from the notification list.
void setErrorHandler(ErrorHandler handler);Set the error handler function.
Parameters:
handler- Error handler function
explicit State(const StateInfo& info);Create a state from state information.
Parameters:
info- State information
const std::string& getName() const;Get the state name.
Returns: Reference to the state name
const std::map<std::string, VariableValue>& getVariables() const;Get the state variables.
Returns: Reference to the state variables map
const std::string& getOnEnterCallback() const;Get the on_enter callback name.
Returns: Reference to the callback name
const std::string& getOnExitCallback() const;Get the on_exit callback name.
Returns: Reference to the callback name
const std::vector<std::string>& getActions() const;Get the state actions.
Returns: Reference to the actions vector
bool hasVariable(const std::string& name) const;Check if a variable exists in the state.
Parameters:
name- Variable name
Returns: true if variable exists, false otherwise
VariableValue getVariable(const std::string& name) const;Get a variable from the state.
Parameters:
name- Variable name
Returns: Variable value
Throws:
StateExceptionif the variable is not found
void setVariable(const std::string& name, const VariableValue& value);Set a variable in the state.
Parameters:
name- Variable namevalue- Variable value
const std::map<std::string, VariableValue>& getAllVariables() const;Get all variables in the state.
Returns: Reference to the variables map
virtual void onStateEnter(const std::string& state_name) = 0;
virtual void onStateExit(const std::string& state_name) = 0;
virtual void onTransition(const TransitionEvent& event) = 0;
virtual void onError(const std::string& error_message) = 0;Observer methods that must be implemented by derived classes.
Parameters:
state_name- Name of the stateevent- Transition event dataerror_message- Error message
using StateCallback = std::function<void()>;Type for state callbacks (on_enter, on_exit).
using TransitionCallback = std::function<void(const TransitionEvent&)>;Type for transition callbacks.
using GuardCallback = std::function<bool()>;Type for guard callbacks.
using ActionCallback = std::function<void()>;Type for action callbacks.
using EventHandler = std::function<void(const std::string& event_name, const TransitionEvent& event)>;Type for event handlers.
using ErrorHandler = std::function<void(const std::string&)>;Type for error handlers.
class ConfigException : public std::runtime_error {
public:
explicit ConfigException(const std::string& message);
};Exception thrown for configuration errors.
class StateException : public std::runtime_error {
public:
explicit StateException(const std::string& message);
};Exception thrown for state machine errors.