1// Copyright 2024 Christophe Henry
2// henry UNDERSCORE christophe AT hotmail DOT com
3// This is an extended version of the state machine available in the boost::mpl library
4// Distributed under the same license as the original.
5// Copyright for the original version:
6// Copyright 2005 David Abrahams and Aleksey Gurtovoy. Distributed
7// under the Boost Software License, Version 1.0. (See accompanying
8// file LICENSE_1_0.txt or copy at
9// http://www.boost.org/LICENSE_1_0.txt)
10
11// back-end
12#include <boost/msm/back11/state_machine.hpp>
13//front-end
14#include <boost/msm/front/state_machine_def.hpp>
15#include <boost/msm/front/functor_row.hpp>
16#include <string>
17#include <iostream>
18
19#ifndef BOOST_MSM_NONSTANDALONE_TEST
20#define BOOST_TEST_MODULE Back11ThrowingTest
21#endif
22#include <boost/test/unit_test.hpp>
23
24namespace msm = boost::msm;
25namespace mpl = boost::mpl;
26using namespace boost::msm::front;
27
28namespace
29{
30 template <typename T>
31 void print(T t)
32 {
33 std::cout << t << "\n";
34 }
35
36 template <typename... Ts>
37 using Row = boost::msm::front::Row<Ts...>;
38
39 using None = boost::msm::front::none;
40
41 // events
42 struct ExceptionRaised {};
43
44 // states
45 struct Init : boost::msm::front::state<>
46 {
47 template <class Event, class FSM>
48 void on_entry(Event const&, FSM&) { ++entry_counter;}
49 template <class Event, class FSM>
50 void on_exit(Event const&, FSM&) { ++exit_counter;}
51 int entry_counter = 0;
52 int exit_counter = 0;
53 };
54 struct WaitingForException : boost::msm::front::state<>
55 {
56 template <class Event, class FSM>
57 void on_entry(Event const&, FSM&) { ++entry_counter; }
58 template <class Event, class FSM>
59 void on_exit(Event const&, FSM&) { ++exit_counter;}
60 int entry_counter = 0;
61 int exit_counter = 0;
62 };
63 struct Intermediate : boost::msm::front::state<>
64 {
65 template <class Event, class FSM>
66 void on_entry(Event const&, FSM&) { ++entry_counter;}
67 template <class Event, class FSM>
68 void on_exit(Event const&, FSM&) { ++exit_counter;}
69 int entry_counter = 0;
70 int exit_counter = 0;
71 };
72 struct End : boost::msm::front::terminate_state<>
73 {
74 template <class Event, class FSM>
75 void on_entry(Event const&, FSM&) { ++entry_counter;}
76 template <class Event, class FSM>
77 void on_exit(Event const&, FSM&) { ++exit_counter;}
78 int entry_counter = 0;
79 int exit_counter = 0;
80 };
81
82 // actions
83 struct ThrowingAction
84 {
85 template <class Event, class StateMachine, class SourceState, class TargetState>
86 void operator()(const Event&, StateMachine&, SourceState&, TargetState&)
87 {
88 throw std::runtime_error("foo");
89 }
90 };
91 struct ExceptionHandler
92 {
93 template <class Event, class StateMachine, class SourceState, class TargetState>
94 void operator()(const Event&, StateMachine&, SourceState&, TargetState&)
95 {
96 }
97 };
98
99 struct MyMachineFrontend : public boost::msm::front::state_machine_def<MyMachineFrontend>
100 {
101 using initial_state = boost::mpl::vector<Init,WaitingForException>;
102
103 using transition_table = boost::mpl::vector<
104 Row<Init, None, Intermediate, ThrowingAction>,
105
106 Row<WaitingForException, ExceptionRaised, End, ExceptionHandler>
107 >;
108
109 template <class StateMachine, class Event>
110 void exception_caught(const Event&, StateMachine& machine, std::exception&)
111 {
112 machine.process_event(ExceptionRaised{});
113 }
114 };
115
116 using MyMachine = boost::msm::back11::state_machine<MyMachineFrontend>;
117
118
119 BOOST_AUTO_TEST_CASE(Back11ThrowingTest)
120 {
121 MyMachine m;
122 m.start();
123
124 BOOST_CHECK_MESSAGE(m.get_state<Init&>().entry_counter == 1, "Init entry not called correctly");
125 BOOST_CHECK_MESSAGE(m.get_state<Init&>().exit_counter == 1, "Init exit not called correctly");
126 BOOST_CHECK_MESSAGE(m.get_state<Intermediate&>().entry_counter == 0, "Intermediate entry not called correctly");
127 BOOST_CHECK_MESSAGE(m.get_state<Intermediate&>().exit_counter == 0, "Intermediate exit not called correctly");
128
129 BOOST_CHECK_MESSAGE(m.get_state<WaitingForException&>().entry_counter == 1, "WaitingForException entry not called correctly");
130 BOOST_CHECK_MESSAGE(m.get_state<WaitingForException&>().exit_counter == 1, "WaitingForException exit not called correctly");
131 BOOST_CHECK_MESSAGE(m.get_state<End&>().entry_counter == 1, "End entry not called correctly");
132 BOOST_CHECK_MESSAGE(m.get_state<End&>().exit_counter == 0, "End exit not called correctly");
133
134
135 BOOST_CHECK_MESSAGE(m.current_state()[0] == 0, "Init should be active");
136 BOOST_CHECK_MESSAGE(m.current_state()[1] == 3, "End should be active");
137 }
138}
139
140
141

source code of boost/libs/msm/test/Back11Throwing.cpp