pars 0.2.1.99
Loading...
Searching...
No Matches
pars::app::state_machine< state_t > Struct Template Reference

#include <state_machine.h>

Public Types

using state_type = state_t
 

Public Member Functions

 state_machine (state_type s)
 
void ensure (std::initializer_list< state_type > expected)
 
void ensure (state_type s)
 
void next (state_type s)
 
void rollback ()
 
void commit ()
 
state_tx< state_typetx (state_type to_state)
 
state_tx< state_typetx (state_type from_state, state_type to_state)
 
state_tx< state_typetx (std::initializer_list< state_type > from_states, state_type to_state)
 
state_type current () const
 
void reset ()
 

Detailed Description

template<typename state_t>
struct pars::app::state_machine< state_t >

Definition at line 82 of file state_machine.h.

Member Typedef Documentation

◆ state_type

template<typename state_t>
using pars::app::state_machine< state_t >::state_type = state_t

Definition at line 84 of file state_machine.h.

Constructor & Destructor Documentation

◆ state_machine()

template<typename state_t>
pars::app::state_machine< state_t >::state_machine ( state_type s)
inline

Definition at line 86 of file state_machine.h.

87 : state_m{s}
88 {
89 }

Member Function Documentation

◆ commit()

template<typename state_t>
void pars::app::state_machine< state_t >::commit ( )
inline

Definition at line 139 of file state_machine.h.

140 {
141 if (!next_m)
142 throw std::runtime_error("Commit while no transition!");
143
144 pars::debug(SL, lf::app, "Commit transition from \"{}\" to \"{}\"", state_m,
145 *next_m);
146
147 state_m = *next_m;
148
149 next_m.reset();
150 }
void debug(spdlog::source_loc loc, pars::lf lf, spdlog::format_string_t< args_t... > fmt, args_t &&... args)
Definition log.h:129

References pars::app, pars::debug(), and SL.

Here is the call graph for this function:

◆ current()

template<typename state_t>
state_type pars::app::state_machine< state_t >::current ( ) const
inline

Definition at line 172 of file state_machine.h.

172{ return state_m; }

◆ ensure() [1/2]

template<typename state_t>
void pars::app::state_machine< state_t >::ensure ( state_type s)
inline

Definition at line 110 of file state_machine.h.

111 {
112 if (state_m != s)
113 throw std::runtime_error(
114 std::format("Wrong State \"{}\" [must be \"{}\"]", state_m, s));
115 }

◆ ensure() [2/2]

template<typename state_t>
void pars::app::state_machine< state_t >::ensure ( std::initializer_list< state_type > expected)
inline

Definition at line 91 of file state_machine.h.

92 {
93 for (const auto& e : expected)
94 if (e == state_m)
95 return;
96
98
99 for (const auto& e : expected)
100 if (e != *expected.end())
101 states_str += std::format("{},", e);
102 else
103 states_str += std::format("{}", e);
104
105 throw std::runtime_error(
106 std::format("Wrong State {} [must be in \"{}\"]", state_m,
107 std::string_view{states_str.begin(), states_str.end() - 1}));
108 }

Referenced by tx(), and tx().

Here is the caller graph for this function:

◆ next()

template<typename state_t>
void pars::app::state_machine< state_t >::next ( state_type s)
inline

Definition at line 117 of file state_machine.h.

118 {
119 if (next_m)
120 throw std::runtime_error("Another transition in progress!");
121
122 pars::debug(SL, lf::app, "Start transition from \"{}\" to \"{}\"", state_m,
123 s);
124
125 next_m = s;
126 }

References pars::app, pars::debug(), and SL.

Referenced by pars::app::state_tx< state_t >::state_tx().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reset()

template<typename state_t>
void pars::app::state_machine< state_t >::reset ( )
inline

Definition at line 174 of file state_machine.h.

175 {
176 pars::debug(SL, lf::app, "Reset State to \"{}\" [was \"{}\"]",
177 state_type::INITIALIZING, state_m);
178
179 state_m = state_type::INITIALIZING;
180 }

References pars::app, pars::debug(), and SL.

Here is the call graph for this function:

◆ rollback()

template<typename state_t>
void pars::app::state_machine< state_t >::rollback ( )
inline

Definition at line 128 of file state_machine.h.

129 {
130 if (!next_m)
131 throw std::runtime_error("Rollback while no transition!");
132
133 pars::debug(SL, lf::app, "Rollback transition from \"{}\" to \"{}\"",
134 state_m, *next_m);
135
136 next_m.reset();
137 }

References pars::app, pars::debug(), and SL.

Here is the call graph for this function:

◆ tx() [1/3]

template<typename state_t>
state_tx< state_type > pars::app::state_machine< state_t >::tx ( state_type from_state,
state_type to_state )
inline

Definition at line 157 of file state_machine.h.

158 {
160
161 return state_tx(*this, to_state);
162 }
void ensure(std::initializer_list< state_type > expected)

References ensure().

Here is the call graph for this function:

◆ tx() [2/3]

template<typename state_t>
state_tx< state_type > pars::app::state_machine< state_t >::tx ( state_type to_state)
inline

Definition at line 152 of file state_machine.h.

153 {
154 return state_tx(*this, to_state);
155 }

◆ tx() [3/3]

template<typename state_t>
state_tx< state_type > pars::app::state_machine< state_t >::tx ( std::initializer_list< state_type > from_states,
state_type to_state )
inline

Definition at line 164 of file state_machine.h.

166 {
168
169 return state_tx(*this, to_state);
170 }

References ensure().

Here is the call graph for this function:

The documentation for this struct was generated from the following file: