pars 0.2.1
Loading...
Searching...
No Matches
state_machine.h
Go to the documentation of this file.
1/*
2Copyright (c) 2025 Giuseppe Roberti.
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without modification,
6are permitted provided that the following conditions are met:
7
81. Redistributions of source code must retain the above copyright notice, this
9list of conditions and the following disclaimer.
10
112. Redistributions in binary form must reproduce the above copyright notice,
12this list of conditions and the following disclaimer in the documentation and/or
13other materials provided with the distribution.
14
153. Neither the name of the copyright holder nor the names of its contributors
16may be used to endorse or promote products derived from this software without
17specific prior written permission.
18
19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*/
30#pragma once
31
32#include "pars/log.h"
33
34#include <format>
35#include <optional>
36#include <stdexcept>
37
38namespace pars::app
39{
40
41template<typename>
42struct state_machine;
43
44template<typename state_t>
46{
47 using state_type = state_t;
48
50 : sm_m{sm}
51 , done_m{false}
52 {
53 sm.next(to_state);
54 }
55
57 {
58 if (!done_m)
59 sm_m.rollback();
60 }
61
62 void commit()
63 {
64 sm_m.commit();
65
66 done_m = true;
67 }
68
69 void rollback()
70 {
71 sm_m.rollback();
72
73 done_m = true;
74 }
75
76private:
78 bool done_m;
79};
80
81template<typename state_t>
83{
84 using state_type = state_t;
85
87 : state_m{s}
88 {
89 }
90
91 void ensure(std::initializer_list<state_type> expected)
92 {
93 for (const auto& e : expected)
94 if (e == state_m)
95 return;
96
97 std::string states_str;
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 }
109
111 {
112 if (state_m != s)
113 throw std::runtime_error(
114 std::format("Wrong State \"{}\" [must be \"{}\"]", state_m, s));
115 }
116
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 }
127
128 void rollback()
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 }
138
139 void commit()
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 }
151
153 {
154 return state_tx(*this, to_state);
155 }
156
158 {
159 ensure(from_state);
160
161 return state_tx(*this, to_state);
162 }
163
164 state_tx<state_type> tx(std::initializer_list<state_type> from_states,
165 state_type to_state)
166 {
167 ensure(from_states);
168
169 return state_tx(*this, to_state);
170 }
171
172 state_type current() const { return state_m; }
173
174 void reset()
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 }
181
182private:
183 state_type state_m;
184 std::optional<state_type> next_m;
185};
186
187} // namespace pars::app
#define SL
Definition log.h:58
@ app
Definition flags.h:41
void debug(spdlog::source_loc loc, pars::lf lf, spdlog::format_string_t< args_t... > fmt, args_t &&... args)
Definition log.h:129
void ensure(std::initializer_list< state_type > expected)
void next(state_type s)
state_type current() const
state_tx< state_type > tx(state_type from_state, state_type to_state)
state_tx< state_type > tx(std::initializer_list< state_type > from_states, state_type to_state)
void ensure(state_type s)
state_tx< state_type > tx(state_type to_state)
state_machine(state_type s)
state_tx(state_machine< state_type > &sm, state_type to_state)