File: SetStates.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (166 lines) | stat: -rw-r--r-- 5,327 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2010 Christophe Henry
// henry UNDERSCORE christophe AT hotmail DOT com
// This is an extended version of the state machine available in the boost::mpl library
// Distributed under the same license as the original.
// Copyright for the original version:
// Copyright 2005 David Abrahams and Aleksey Gurtovoy. Distributed
// under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

// back-end
// set_states API is not supported by backmp11
#define BOOST_MSM_TEST_SKIP_BACKMP11
#include "BackCommon.hpp"
//front-end
#include <boost/msm/front/state_machine_def.hpp>
#include <boost/msm/front/functor_row.hpp>

#ifndef BOOST_MSM_NONSTANDALONE_TEST
#define BOOST_TEST_MODULE set_states_test
#endif
#include <boost/test/unit_test.hpp>

namespace msm = boost::msm;
namespace mpl = boost::mpl;
using namespace msm::front;

namespace
{
    struct InputEvent {};
    struct ExitEvent
    {
        ExitEvent() = default;
        template <typename T>
        ExitEvent(const T&) {};
    };

    struct State : public boost::msm::front::state<>
    {
        template <class Event, class FSM>
        void on_entry(Event const&, FSM&)
        {
            std::cout << "State - on_entry" << "\n";
        };

        template <class Event, class FSM>
        void on_exit(Event const&, FSM&)
        {
            std::cout << "State - on_exit" << "\n";
        }
    };

    struct TerminateState : public boost::msm::front::terminate_state<>
    {
        template <class Event, class FSM>
        void on_entry(Event const&, FSM&)
        {
            std::cout << "TerminateState - on_entry" << "\n";
        };

        template <class Event, class FSM>
        void on_exit(Event const&, FSM&)
        {
            std::cout << "TerminateState - on_exit" << "\n";
        }
    };

    struct PseudoExitState : public boost::msm::front::exit_pseudo_state<ExitEvent>
    {
    };

    template<template <typename...> class Back, typename Policy = void>
    struct hierarchical_state_machine
    {
    struct SubFsm_ : public msm::front::state_machine_def<SubFsm_>
    {
        BOOST_MSM_TEST_DEFINE_DEPENDENT_TEMPLATES(SubFsm_)

        template <class Event, class FSM>
        void on_entry(Event const&, FSM&)
        {
            std::cout << "SubFsm - on_entry" << "\n";
        };

        template <class Event, class FSM>
        void on_exit(Event const&, FSM&)
        {
            std::cout << "SubFsm - on_exit" << "\n";
        }

        typedef State initial_state;

        struct transition_table : mpl::vector<
            Row< State, InputEvent, PseudoExitState, none, none >
        > {};

        template <class FSM, class Event>
        void no_transition(Event const& e, FSM&, int state)
        {
            std::cout << "SubFsm: no transition from state " << state
                << " on event " << typeid(e).name() << std::endl;
        }
    };
    typedef Back<SubFsm_, Policy> SubFsm;

    struct Fsm_ : public msm::front::state_machine_def<Fsm_>
    {
        BOOST_MSM_TEST_DEFINE_DEPENDENT_TEMPLATES(Fsm_)

        template <class Event, class FSM>
        void on_entry(Event const&, FSM&)
        {
            std::cout << "Fsm - on_entry" << "\n";
        };

        template <class Event, class FSM>
        void on_exit(Event const&, FSM&)
        {
            std::cout << "Fsm - on_exit" << "\n";
        }

        typedef SubFsm initial_state;

        struct transition_table : mpl::vector<
            Row< typename SubFsm::template exit_pt<PseudoExitState>, ExitEvent, TerminateState, none, none >
        > {};

        template <class FSM, class Event>
        void no_transition(Event const& e, FSM&, int state)
        {
            std::cout << "Fsm: no transition from state " << state
                << " on event " << typeid(e).name() << std::endl;
        }
    };
    typedef Back<Fsm_, Policy> Fsm;
    };
    // typedef msm::back11::state_machine<Fsm_> Fsm;
    typedef get_hierarchical_test_machines<hierarchical_state_machine> test_machines;


    BOOST_AUTO_TEST_CASE_TEMPLATE(set_states_test, test_machine, test_machines)
    {     
        InputEvent input_event;
        typedef typename test_machine::Fsm Fsm;
        typedef typename test_machine::SubFsm SubFsm;

        std::cout << "\nSubFsms created in constructor\n";
        Fsm fsm_subfsms_created_in_constructor(boost::msm::back::states_
            << SubFsm());
        fsm_subfsms_created_in_constructor.start();
        fsm_subfsms_created_in_constructor.process_event(input_event);

        std::cout << "\nSubFsms created through set_states method\n";
        Fsm fsm_subfsms_created_thorugh_constructor;
        fsm_subfsms_created_thorugh_constructor.set_states(boost::msm::back::states_
            << SubFsm());
        fsm_subfsms_created_thorugh_constructor.start();
        fsm_subfsms_created_thorugh_constructor.process_event(input_event);
    }
}

#if !defined(BOOST_MSM_TEST_ONLY_BACKMP11)
using back0 = hierarchical_state_machine<boost::msm::back::state_machine, boost::msm::back::favor_compile_time>::Fsm;
using back1 = hierarchical_state_machine<boost::msm::back::state_machine, boost::msm::back::favor_compile_time>::SubFsm;
BOOST_MSM_BACK_GENERATE_PROCESS_EVENT(back1);
#endif