1 // Copyright 2008 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)
11 #ifndef BOOST_MSM_BACK_HISTORY_POLICIES_H
12 #define BOOST_MSM_BACK_HISTORY_POLICIES_H
14 #include <boost/mpl/contains.hpp>
16 namespace boost { namespace msm { namespace back
21 // Default: no history used
22 template <int NumberOfRegions>
28 void set_initial_states(int* const initial_states)
30 for (int i=0;i<NumberOfRegions;++i)
31 m_initialStates[i] = initial_states[i];
33 void history_exit(int* const )
37 // returns the state where the state machine should be at start
38 template <class Event>
39 const int* history_entry(Event const& )
41 // always come back to the original state
42 return m_initialStates;
44 NoHistoryImpl<NumberOfRegions>& operator=(NoHistoryImpl<NumberOfRegions> const& rhs)
46 for (int i=0; i<NumberOfRegions;++i)
48 m_initialStates[i] = rhs.m_initialStates[i];
52 // this policy deletes all waiting deferred events
53 template <class Event>
54 bool process_deferred_events(Event const&)const
58 template<class Archive>
59 void serialize(Archive & ar, const unsigned int)
64 int m_initialStates[NumberOfRegions];
67 // not UML standard. Always activates history, no matter which event generated the transition
68 template <int NumberOfRegions>
69 class AlwaysHistoryImpl
73 ~AlwaysHistoryImpl(){}
74 void set_initial_states(int* const initial_states)
76 for (int i=0;i<NumberOfRegions;++i)
77 m_initialStates[i] = initial_states[i];
79 void history_exit(int* const current_states)
81 for (int i=0;i<NumberOfRegions;++i)
82 m_initialStates[i] = current_states[i];
84 // returns the state where the state machine should be at start
85 template <class Event>
86 const int* history_entry(Event const& )
88 // always load back the last active state
89 return m_initialStates;
91 AlwaysHistoryImpl<NumberOfRegions>& operator=(AlwaysHistoryImpl<NumberOfRegions> const& rhs)
93 for (int i=0; i<NumberOfRegions;++i)
95 m_initialStates[i] = rhs.m_initialStates[i];
99 // the history policy keeps all deferred events until next reentry
100 template <class Event>
101 bool process_deferred_events(Event const&)const
106 template<class Archive>
107 void serialize(Archive & ar, const unsigned int)
109 ar & m_initialStates;
112 int m_initialStates[NumberOfRegions];
115 // UML Shallow history. For deep history, just use this policy for all the contained state machines
116 template <class Events,int NumberOfRegions>
117 class ShallowHistoryImpl
120 ShallowHistoryImpl(){}
121 ~ShallowHistoryImpl(){}
122 void set_initial_states(int* const initial_states)
124 for (int i=0;i<NumberOfRegions;++i)
126 m_currentStates[i] = initial_states[i];
127 m_initialStates[i] = initial_states[i];
130 void history_exit(int* const current_states)
132 for (int i=0;i<NumberOfRegions;++i)
133 m_currentStates[i] = current_states[i];
135 // returns the state where the state machine should be at start
136 template <class Event>
137 const int* history_entry(Event const&)
139 if ( ::boost::mpl::contains<Events,Event>::value)
141 return m_currentStates;
143 // not one of our events, no history
144 return m_initialStates;
146 ShallowHistoryImpl<Events,NumberOfRegions>& operator=(ShallowHistoryImpl<Events,NumberOfRegions> const& rhs)
148 for (int i=0; i<NumberOfRegions;++i)
150 m_initialStates[i] = rhs.m_initialStates[i];
151 m_currentStates[i] = rhs.m_currentStates[i];
155 // the history policy keeps deferred events until next reentry if coming from our history event
156 template <class Event>
157 bool process_deferred_events(Event const&)const
159 return ::boost::mpl::contains<Events,Event>::value;
161 template<class Archive>
162 void serialize(Archive & ar, const unsigned int)
164 ar & m_initialStates;
165 ar & m_currentStates;
168 int m_initialStates[NumberOfRegions];
169 int m_currentStates[NumberOfRegions];
174 typedef int history_policy;
175 template <int NumberOfRegions>
178 typedef NoHistoryImpl<NumberOfRegions> type;
183 typedef int history_policy;
184 template <int NumberOfRegions>
187 typedef AlwaysHistoryImpl<NumberOfRegions> type;
190 template <class Events>
191 struct ShallowHistory
193 typedef int history_policy;
194 template <int NumberOfRegions>
197 typedef ShallowHistoryImpl<Events,NumberOfRegions> type;
200 } } }//boost::msm::back
201 #endif //BOOST_MSM_BACK_HISTORY_POLICIES_H