FSMgine
High-performance finite state machine library for C++17 with single-threaded and multi-threaded variants
Loading...
Searching...
No Matches
FSMBuilder.hpp
Go to the documentation of this file.
1
4
5#pragma once
6
7#include <string>
8#include <string_view>
10#include "FSMgine/FSM.hpp"
12
15
16namespace fsmgine {
17
36template<typename TEvent>
38public:
41
43 using Action = typename FSM<TEvent>::Action;
44
48 explicit TransitionBuilder(FSM<TEvent>& fsm, std::string_view from_state);
49
50 TransitionBuilder(const TransitionBuilder&) = delete;
51 TransitionBuilder& operator=(const TransitionBuilder&) = delete;
52
55
58
64
70
74 void to(const std::string& state);
75
76private:
77 FSM<TEvent>& fsm_;
78 std::string_view from_state_;
79 Transition<TEvent> transition_;
80};
81
111template<typename TEvent>
113public:
115 using Action = typename FSM<TEvent>::Action;
116
119 explicit FSMBuilder(FSM<TEvent>& fsm);
120
121 FSMBuilder(const FSMBuilder&) = delete;
122 FSMBuilder& operator=(const FSMBuilder&) = delete;
123
125 FSMBuilder(FSMBuilder&&) = default;
126
129
133 TransitionBuilder<TEvent> from(const std::string& state);
134
139 FSMBuilder& onEnter(const std::string& state, Action action);
140
145 FSMBuilder& onExit(const std::string& state, Action action);
146
147private:
148 FSM<TEvent>& fsm_;
149};
150
151// --- Implementation ---
152
153// TransitionBuilder
154template<typename TEvent>
156 : fsm_(fsm), from_state_(from_state) {
157}
158
159template<typename TEvent>
161 transition_.addPredicate(std::move(pred));
162 return *this;
163}
164
165template<typename TEvent>
167 transition_.addAction(std::move(action));
168 return *this;
169}
170
171template<typename TEvent>
172void TransitionBuilder<TEvent>::to(const std::string& state) {
173 // Optimization: Cache StringInterner reference
174 auto& interner = StringInterner::instance();
175 auto interned_state = interner.intern(state);
176 transition_.setTargetState(interned_state);
177 fsm_.addTransition(from_state_, std::move(transition_));
178}
179
180// FSMBuilder
181template<typename TEvent>
184
185template<typename TEvent>
187 // Optimization: Cache StringInterner reference
188 auto& interner = StringInterner::instance();
189 auto interned_state = interner.intern(state);
190 return TransitionBuilder<TEvent>(fsm_, interned_state);
191}
192
193template<typename TEvent>
194FSMBuilder<TEvent>& FSMBuilder<TEvent>::onEnter(const std::string& state, Action action) {
195 // Optimization: Cache StringInterner reference
196 auto& interner = StringInterner::instance();
197 auto interned_state = interner.intern(state);
198 fsm_.addOnEnterAction(interned_state, std::move(action));
199 return *this;
200}
201
202template<typename TEvent>
203FSMBuilder<TEvent>& FSMBuilder<TEvent>::onExit(const std::string& state, Action action) {
204 // Optimization: Cache StringInterner reference
205 auto& interner = StringInterner::instance();
206 auto interned_state = interner.intern(state);
207 fsm_.addOnExitAction(interned_state, std::move(action));
208 return *this;
209}
210
211} // namespace fsmgine
Core finite state machine implementation.
String interning utility for memory-efficient state name storage.
State transition representation with guards and actions.
Main builder class for constructing FSMs with a fluent interface.
Definition FSMBuilder.hpp:112
FSMBuilder & onEnter(const std::string &state, Action action)
Adds an action to execute when entering a state.
Definition FSMBuilder.hpp:194
FSMBuilder & operator=(FSMBuilder &&)=default
Move assignment operator (defaulted)
FSMBuilder & onExit(const std::string &state, Action action)
Adds an action to execute when exiting a state.
Definition FSMBuilder.hpp:203
FSMBuilder(FSMBuilder &&)=default
Move constructor (defaulted)
typename FSM< TEvent >::Action Action
Type alias for state actions.
Definition FSMBuilder.hpp:115
TransitionBuilder< TEvent > from(const std::string &state)
Starts building a transition from the specified state.
Definition FSMBuilder.hpp:186
FSMBuilder(FSM< TEvent > &fsm)
Constructs a builder for the given FSM.
Definition FSMBuilder.hpp:182
A high-performance finite state machine implementation.
Definition FSM.hpp:94
std::function< void(const TEvent &)> Action
Type alias for transition actions.
Definition FSM.hpp:102
std::function< bool(const TEvent &)> Predicate
Type alias for transition predicates.
Definition FSM.hpp:98
static StringInterner & instance()
Gets the singleton instance of StringInterner.
Builder for creating transitions with a fluent interface.
Definition FSMBuilder.hpp:37
TransitionBuilder(FSM< TEvent > &fsm, std::string_view from_state)
Constructs a transition builder for a specific source state.
Definition FSMBuilder.hpp:155
TransitionBuilder & action(Action action)
Adds an action to execute during the transition.
Definition FSMBuilder.hpp:166
TransitionBuilder(TransitionBuilder &&)=default
Move constructor (defaulted)
typename FSM< TEvent >::Action Action
Type alias for transition actions.
Definition FSMBuilder.hpp:43
TransitionBuilder & predicate(Predicate pred)
Adds a predicate (guard condition) to the transition.
Definition FSMBuilder.hpp:160
TransitionBuilder & operator=(TransitionBuilder &&)=default
Move assignment operator (defaulted)
void to(const std::string &state)
Completes the transition by specifying the target state.
Definition FSMBuilder.hpp:172
typename FSM< TEvent >::Predicate Predicate
Type alias for transition guard predicates.
Definition FSMBuilder.hpp:40
Represents a transition between states in a finite state machine.
Definition Transition.hpp:41
Main namespace for the FSMgine library.
Definition FSM.hpp:23