mirror of
https://github.com/boostorg/mpl.git
synced 2026-05-05 20:24:14 +02:00
examples, initial check in
[SVN r16255]
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
// boost fsm/handler.hpp header file
|
||||
// See http://www.boost.org for updates, documentation, and revision history.
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2002
|
||||
// Aleksey Gurtovoy
|
||||
//
|
||||
// Permission to use, copy, modify, distribute and sell this software
|
||||
// and its documentation for any purpose is hereby granted without fee,
|
||||
// provided that the above copyright notice appears in all copies and
|
||||
// that both the copyright notice and this permission notice appear in
|
||||
// supporting documentation. No representations are made about the
|
||||
// suitability of this software for any purpose. It is provided "as is"
|
||||
// without express or implied warranty.
|
||||
|
||||
#ifndef BOOST_FSM_HANDLER_INCLUDED
|
||||
#define BOOST_FSM_HANDLER_INCLUDED
|
||||
|
||||
#include "boost/mpl/if.hpp"
|
||||
#include "boost/mpl/fold.hpp"
|
||||
#include "boost/mpl/front.hpp"
|
||||
#include "boost/type_traits/is_same.hpp"
|
||||
|
||||
#include <typeinfo>
|
||||
#include <cassert>
|
||||
|
||||
namespace fsm {
|
||||
namespace aux {
|
||||
|
||||
namespace mpl = boost::mpl;
|
||||
using namespace mpl::placeholder;
|
||||
|
||||
template< typename Transition >
|
||||
struct STT_void_row_impl
|
||||
{
|
||||
typedef typename Transition::from_state_t state_t;
|
||||
typedef typename Transition::fsm_t fsm_t;
|
||||
typedef typename Transition::base_event_t base_event_t;
|
||||
|
||||
static long do_process_event(fsm_t&, long state, base_event_t const&)
|
||||
{
|
||||
assert(false);
|
||||
return state;
|
||||
}
|
||||
|
||||
static long do_transition(fsm_t&, long state, base_event_t const&)
|
||||
{
|
||||
assert(false);
|
||||
return state;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<
|
||||
typename PrevRowImpl
|
||||
, typename Transition
|
||||
>
|
||||
struct STT_event_row_impl
|
||||
: PrevRowImpl
|
||||
{
|
||||
typedef typename Transition::from_state_t state_t;
|
||||
typedef typename Transition::fsm_t fsm_t;
|
||||
typedef typename Transition::base_event_t base_event_t;
|
||||
|
||||
static long do_process_event(fsm_t& fsm, long state, base_event_t const& evt)
|
||||
{
|
||||
if (typeid(typename Transition::event_t) == typeid(evt))
|
||||
{
|
||||
// typedefs are here to make GCC happy
|
||||
typedef typename Transition::to_state_t to_state_;
|
||||
typedef typename Transition::from_state_t from_state_;
|
||||
|
||||
return Transition::do_transition(fsm, evt)
|
||||
? to_state_::do_check_invariant(fsm)
|
||||
: from_state_::do_check_invariant(fsm)
|
||||
;
|
||||
}
|
||||
|
||||
return PrevRowImpl::do_process_event(fsm, state, evt);
|
||||
}
|
||||
};
|
||||
|
||||
template<
|
||||
typename PrevRowImpl
|
||||
, typename StateType
|
||||
>
|
||||
struct STT_state_row_impl
|
||||
: PrevRowImpl
|
||||
{
|
||||
typedef typename PrevRowImpl::fsm_t fsm_t;
|
||||
typedef typename PrevRowImpl::base_event_t base_event_t;
|
||||
|
||||
static long do_transition(fsm_t& fsm, long state, base_event_t const& evt)
|
||||
{
|
||||
return state == StateType::value
|
||||
? PrevRowImpl::do_process_event(fsm, state, evt)
|
||||
: PrevRowImpl::do_transition(fsm, state, evt)
|
||||
;
|
||||
}
|
||||
|
||||
static long do_process_event(fsm_t&, long state, base_event_t const&)
|
||||
{
|
||||
assert(false);
|
||||
return state;
|
||||
}
|
||||
};
|
||||
|
||||
template<
|
||||
typename PrevRowImpl
|
||||
, typename Transition
|
||||
>
|
||||
struct STT_row_impl
|
||||
{
|
||||
typedef typename mpl::if_<
|
||||
boost::is_same<
|
||||
typename PrevRowImpl::state_t
|
||||
, typename Transition::from_state_t
|
||||
>
|
||||
, STT_event_row_impl< PrevRowImpl,Transition >
|
||||
, STT_event_row_impl<
|
||||
STT_state_row_impl< PrevRowImpl,typename PrevRowImpl::state_t >
|
||||
, Transition
|
||||
>
|
||||
>::type type;
|
||||
};
|
||||
|
||||
|
||||
template< typename Transitions >
|
||||
struct STT_impl_gen
|
||||
{
|
||||
private:
|
||||
typedef typename mpl::front<Transitions>::type first_;
|
||||
typedef typename mpl::fold<
|
||||
Transitions
|
||||
, STT_void_row_impl<first_>
|
||||
, STT_row_impl<_,_>
|
||||
>::type STT_impl_;
|
||||
|
||||
public:
|
||||
typedef STT_state_row_impl<
|
||||
STT_impl_
|
||||
, typename STT_impl_::state_t
|
||||
> type;
|
||||
};
|
||||
|
||||
} // namespace aux
|
||||
} // namespace fsm
|
||||
|
||||
#endif // BOOST_FSM_HANDLER_INCLUDED
|
||||
@@ -0,0 +1,44 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
// boost fsm/base_event.hpp header file
|
||||
// See http://www.boost.org for updates, documentation, and revision history.
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2002
|
||||
// Aleksey Gurtovoy
|
||||
//
|
||||
// Permission to use, copy, modify, distribute and sell this software
|
||||
// and its documentation for any purpose is hereby granted without fee,
|
||||
// provided that the above copyright notice appears in all copies and
|
||||
// that both the copyright notice and this permission notice appear in
|
||||
// supporting documentation. No representations are made about the
|
||||
// suitability of this software for any purpose. It is provided "as is"
|
||||
// without express or implied warranty.
|
||||
|
||||
#ifndef BOOST_FSM_BASE_EVENT_INCLUDED
|
||||
#define BOOST_FSM_BASE_EVENT_INCLUDED
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace fsm {
|
||||
namespace aux {
|
||||
|
||||
// represent an abstract base for FSM events
|
||||
|
||||
struct base_event
|
||||
{
|
||||
public:
|
||||
virtual ~base_event() {};
|
||||
|
||||
std::auto_ptr<base_event> clone() const
|
||||
{
|
||||
return do_clone();
|
||||
}
|
||||
|
||||
private:
|
||||
virtual std::auto_ptr<base_event> do_clone() const = 0;
|
||||
};
|
||||
|
||||
} // namespace aux
|
||||
} // namespace fsm
|
||||
|
||||
#endif // BOOST_FSM_BASE_EVENT_INCLUDED
|
||||
@@ -0,0 +1,44 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
// boost fsm/event.hpp header file
|
||||
// See http://www.boost.org for updates, documentation, and revision history.
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2002
|
||||
// Aleksey Gurtovoy
|
||||
//
|
||||
// Permission to use, copy, modify, distribute and sell this software
|
||||
// and its documentation for any purpose is hereby granted without fee,
|
||||
// provided that the above copyright notice appears in all copies and
|
||||
// that both the copyright notice and this permission notice appear in
|
||||
// supporting documentation. No representations are made about the
|
||||
// suitability of this software for any purpose. It is provided "as is"
|
||||
// without express or implied warranty.
|
||||
|
||||
#ifndef BOOST_FSM_EVENT_INCLUDED
|
||||
#define BOOST_FSM_EVENT_INCLUDED
|
||||
|
||||
#include "base_event.hpp"
|
||||
|
||||
namespace fsm {
|
||||
namespace aux {
|
||||
|
||||
template< typename Derived >
|
||||
struct event
|
||||
: base_event
|
||||
{
|
||||
public:
|
||||
typedef base_event base_t;
|
||||
|
||||
private:
|
||||
virtual std::auto_ptr<base_event> do_clone() const
|
||||
{
|
||||
return std::auto_ptr<base_event>(
|
||||
new Derived(static_cast<Derived const&>(*this))
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace aux
|
||||
} // namespace fsm
|
||||
|
||||
#endif // BOOST_FSM_EVENT_INCLUDED
|
||||
@@ -0,0 +1,47 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
// boost fsm/state.hpp header file
|
||||
// See http://www.boost.org for updates, documentation, and revision history.
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2002
|
||||
// Aleksey Gurtovoy
|
||||
//
|
||||
// Permission to use, copy, modify, distribute and sell this software
|
||||
// and its documentation for any purpose is hereby granted without fee,
|
||||
// provided that the above copyright notice appears in all copies and
|
||||
// that both the copyright notice and this permission notice appear in
|
||||
// supporting documentation. No representations are made about the
|
||||
// suitability of this software for any purpose. It is provided "as is"
|
||||
// without express or implied warranty.
|
||||
|
||||
#ifndef BOOST_FSM_STATE_INCLUDED
|
||||
#define BOOST_FSM_STATE_INCLUDED
|
||||
|
||||
#include "boost/mpl/integral_c.hpp"
|
||||
|
||||
namespace fsm {
|
||||
namespace aux {
|
||||
|
||||
namespace mpl = boost::mpl;
|
||||
|
||||
// represent a FSM state
|
||||
|
||||
template<
|
||||
typename T
|
||||
, long State
|
||||
, void (T::* invariant_func)() const
|
||||
>
|
||||
struct state
|
||||
: mpl::integral_c<long,State>
|
||||
{
|
||||
static long do_check_invariant(T const& x)
|
||||
{
|
||||
if (invariant_func) (x.*invariant_func)();
|
||||
return State;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace aux
|
||||
} // namespace fsm
|
||||
|
||||
#endif // BOOST_FSM_STATE_INCLUDED
|
||||
@@ -0,0 +1,52 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
// boost fsm/transition.hpp header file
|
||||
// See http://www.boost.org for updates, documentation, and revision history.
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2002
|
||||
// Aleksey Gurtovoy
|
||||
//
|
||||
// Permission to use, copy, modify, distribute and sell this software
|
||||
// and its documentation for any purpose is hereby granted without fee,
|
||||
// provided that the above copyright notice appears in all copies and
|
||||
// that both the copyright notice and this permission notice appear in
|
||||
// supporting documentation. No representations are made about the
|
||||
// suitability of this software for any purpose. It is provided "as is"
|
||||
// without express or implied warranty.
|
||||
|
||||
#ifndef BOOST_FSM_TRANSITION_INCLUDED
|
||||
#define BOOST_FSM_TRANSITION_INCLUDED
|
||||
|
||||
#include <cassert>
|
||||
|
||||
namespace fsm {
|
||||
namespace aux {
|
||||
|
||||
// represent a signle transition between states |From| and |To|
|
||||
|
||||
template<
|
||||
typename T
|
||||
, typename From
|
||||
, typename Event
|
||||
, typename To
|
||||
, bool (T::* transition_func)(Event const&)
|
||||
>
|
||||
struct transition
|
||||
{
|
||||
typedef T fsm_t;
|
||||
typedef From from_state_t;
|
||||
typedef Event event_t;
|
||||
typedef To to_state_t;
|
||||
|
||||
typedef typename Event::base_t base_event_t;
|
||||
static bool do_transition(T& x, base_event_t const& e)
|
||||
{
|
||||
assert(dynamic_cast<event_t const*>(&e) == &e);
|
||||
return (x.*transition_func)(static_cast<event_t const &>(e));
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace aux
|
||||
} // namespace fsm
|
||||
|
||||
#endif // BOOST_FSM_TRANSITION_INCLUDED
|
||||
@@ -0,0 +1,79 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
// boost fsm/example/player.cpp source file
|
||||
// See http://www.boost.org for updates, documentation, and revision history.
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2002
|
||||
// Aleksey Gurtovoy
|
||||
//
|
||||
// Permission to use, copy, modify, distribute and sell this software
|
||||
// and its documentation for any purpose is hereby granted without fee,
|
||||
// provided that the above copyright notice appears in all copies and
|
||||
// that both the copyright notice and this permission notice appear in
|
||||
// supporting documentation. No representations are made about the
|
||||
// suitability of this software for any purpose. It is provided "as is"
|
||||
// without express or implied warranty.
|
||||
|
||||
#include "state_machine.hpp"
|
||||
#include "boost/mpl/list.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace mpl = boost::mpl;
|
||||
|
||||
class player
|
||||
: public fsm::state_machine<player>
|
||||
{
|
||||
public:
|
||||
player() {}
|
||||
|
||||
// events
|
||||
struct play_event : event<play_event> {};
|
||||
struct stop_event : event<stop_event> {};
|
||||
struct pause_event : event<pause_event> {};
|
||||
|
||||
// MWCW 8.1 is too eager in inforcing access for non-type template parameters
|
||||
// private:
|
||||
typedef player self_t;
|
||||
|
||||
// state invariants
|
||||
void stopped_state_invariant() const {}
|
||||
void playing_state_invariant() const {}
|
||||
void paused_state_invariant() const {}
|
||||
|
||||
// states (invariants are passed as non-type template arguments)
|
||||
typedef state<0, &self_t::stopped_state_invariant> stopped;
|
||||
typedef state<1, &self_t::playing_state_invariant> playing;
|
||||
typedef state<2, &self_t::paused_state_invariant> paused;
|
||||
|
||||
// private:
|
||||
|
||||
// transition functions
|
||||
bool do_play(play_event const&) { std::cout << "player::do_play\n"; return true; }
|
||||
bool do_stop(stop_event const&) { std::cout << "player::do_stop\n"; return true; }
|
||||
bool do_pause(pause_event const&) { std::cout << "player::do_pause\n"; return true; }
|
||||
bool do_resume(play_event const&) { std::cout << "player::do_resume\n"; return true; }
|
||||
|
||||
// transitions, in the following format:
|
||||
// | current state | event | next state | transition function |
|
||||
friend class fsm::state_machine<player>;
|
||||
typedef mpl::list<
|
||||
transition<stopped, play_event, playing, &player::do_play>
|
||||
, transition<playing, stop_event, stopped, &player::do_stop>
|
||||
, transition<playing, pause_event, paused, &player::do_pause>
|
||||
, transition<paused, play_event, playing, &player::do_resume>
|
||||
, transition<paused, stop_event, stopped, &player::do_stop>
|
||||
>::type transition_table;
|
||||
|
||||
typedef stopped initial_state;
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
player p;
|
||||
p.process_event(player::play_event());
|
||||
p.process_event(player::pause_event());
|
||||
p.process_event(player::play_event());
|
||||
p.process_event(player::stop_event());
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
// boost fsm/state_machine.hpp header file
|
||||
// See http://www.boost.org for updates, documentation, and revision history.
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2002
|
||||
// Aleksey Gurtovoy
|
||||
//
|
||||
// Permission to use, copy, modify, distribute and sell this software
|
||||
// and its documentation for any purpose is hereby granted without fee,
|
||||
// provided that the above copyright notice appears in all copies and
|
||||
// that both the copyright notice and this permission notice appear in
|
||||
// supporting documentation. No representations are made about the
|
||||
// suitability of this software for any purpose. It is provided "as is"
|
||||
// without express or implied warranty.
|
||||
|
||||
#ifndef BOOST_FSM_STATE_MACHINE_INCLUDED
|
||||
#define BOOST_FSM_STATE_MACHINE_INCLUDED
|
||||
|
||||
#include "aux_/event.hpp"
|
||||
#include "aux_/state.hpp"
|
||||
#include "aux_/transition.hpp"
|
||||
#include "aux_/STT_impl_gen.hpp"
|
||||
#include "boost/shared_ptr.hpp"
|
||||
|
||||
#include <queue>
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
namespace fsm {
|
||||
|
||||
template< typename Derived >
|
||||
class state_machine
|
||||
{
|
||||
private:
|
||||
typedef state_machine self_t;
|
||||
typedef aux::base_event base_event_t;
|
||||
typedef boost::shared_ptr<base_event_t const> base_event_ptr_t;
|
||||
|
||||
public:
|
||||
typedef long state_t;
|
||||
|
||||
template< typename DerivedEvent >
|
||||
struct event
|
||||
: aux::event<DerivedEvent>
|
||||
{
|
||||
};
|
||||
|
||||
void process_event(base_event_t const& evt)
|
||||
{
|
||||
// all internal events should be handled at this point
|
||||
assert(!m_events_queue.size());
|
||||
|
||||
// process the external event passed
|
||||
do_transition(evt);
|
||||
|
||||
// if the previous transition generated any internal events,
|
||||
// process those
|
||||
while (m_events_queue.size())
|
||||
{
|
||||
do_transition(*m_events_queue.front());
|
||||
m_events_queue.pop();
|
||||
}
|
||||
}
|
||||
|
||||
state_t current_state() const
|
||||
{
|
||||
return m_state;
|
||||
}
|
||||
|
||||
protected:
|
||||
// interface for the derived class
|
||||
|
||||
state_machine(state_t const& initial_state)
|
||||
: m_state(initial_state)
|
||||
{
|
||||
}
|
||||
|
||||
state_machine()
|
||||
: m_state(typename Derived::initial_state())
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~state_machine()
|
||||
{
|
||||
}
|
||||
|
||||
void post_event(std::auto_ptr<base_event_t const> evt)
|
||||
{
|
||||
m_events_queue.push(base_event_ptr_t(evt.release()));
|
||||
}
|
||||
|
||||
template<
|
||||
long State
|
||||
, void (Derived::* invariant_func)() const = 0
|
||||
>
|
||||
struct state
|
||||
: fsm::aux::state<Derived,State,invariant_func>
|
||||
{
|
||||
};
|
||||
|
||||
template<
|
||||
typename From
|
||||
, typename Event
|
||||
, typename To
|
||||
, bool (Derived::* transition_func)(Event const&)
|
||||
>
|
||||
struct transition
|
||||
: aux::transition< Derived,From,Event,To,transition_func >
|
||||
{
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
void do_transition(base_event_t const& evt)
|
||||
{
|
||||
typedef typename Derived::transition_table STT_;
|
||||
typedef typename aux::STT_impl_gen< STT_ >::type STT_impl_;
|
||||
|
||||
m_state = STT_impl_::do_transition(
|
||||
static_cast<Derived&>(*this)
|
||||
, m_state
|
||||
, evt
|
||||
);
|
||||
}
|
||||
|
||||
state_t m_state;
|
||||
std::queue< base_event_ptr_t > m_events_queue;
|
||||
};
|
||||
|
||||
} // namespace fsm
|
||||
|
||||
#endif // BOOST_FSM_STATE_MACHINE_INCLUDED
|
||||
Reference in New Issue
Block a user