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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
//////////////////////////////////////////////////////////////////////////////
// Copyright 2002-2006 Andreas Huber Doenni
// Distributed under the Boost Software License, Version 1.0. (See accompany-
// ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// The following example program demonstrates the use of orthogonal states and
// state_downcast to query the state of orthogonal regions.
// Moreover, the use of the state type information interface is also shown.
//////////////////////////////////////////////////////////////////////////////
// #define BOOST_STATECHART_USE_NATIVE_RTTI
#include <boost/statechart/event.hpp>
#include <boost/statechart/state_machine.hpp>
#include <boost/statechart/simple_state.hpp>
#include <boost/statechart/transition.hpp>
#include <boost/statechart/custom_reaction.hpp>
#include <boost/mpl/list.hpp>
#include <boost/config.hpp>
#include <iostream>
#include <iomanip>
#ifdef BOOST_INTEL
# pragma warning( disable: 304 ) // access control not specified
# pragma warning( disable: 981 ) // operands are evaluated in unspecified order
#endif
namespace sc = boost::statechart;
namespace mpl = boost::mpl;
//////////////////////////////////////////////////////////////////////////////
struct EvNumLockPressed : sc::event< EvNumLockPressed > {};
struct EvCapsLockPressed : sc::event< EvCapsLockPressed > {};
struct EvScrollLockPressed : sc::event< EvScrollLockPressed > {};
struct EvRequestShutdown : sc::event< EvRequestShutdown > {};
struct Active;
struct Keyboard : sc::state_machine< Keyboard, Active > {};
struct NumLockOff;
struct CapsLockOff;
struct ScrollLockOff;
struct Active: sc::simple_state<
Active, Keyboard, mpl::list< NumLockOff, CapsLockOff, ScrollLockOff > >
{
typedef sc::custom_reaction< EvRequestShutdown > reactions;
sc::result react( const EvRequestShutdown & );
};
struct NumLockOn : sc::simple_state< NumLockOn, Active::orthogonal< 0 > >
{
typedef sc::transition< EvNumLockPressed, NumLockOff > reactions;
};
struct NumLockOff : sc::simple_state< NumLockOff, Active::orthogonal< 0 > >
{
typedef sc::transition< EvNumLockPressed, NumLockOn > reactions;
};
struct CapsLockOn : sc::simple_state< CapsLockOn, Active::orthogonal< 1 > >
{
typedef sc::transition< EvCapsLockPressed, CapsLockOff > reactions;
};
struct CapsLockOff : sc::simple_state< CapsLockOff, Active::orthogonal< 1 > >
{
typedef sc::transition< EvCapsLockPressed, CapsLockOn > reactions;
};
struct ScrollLockOn : sc::simple_state< ScrollLockOn, Active::orthogonal< 2 > >
{
typedef sc::transition< EvScrollLockPressed, ScrollLockOff > reactions;
};
struct ScrollLockOff : sc::simple_state< ScrollLockOff, Active::orthogonal< 2 > >
{
typedef sc::transition< EvScrollLockPressed, ScrollLockOn > reactions;
};
sc::result Active::react( const EvRequestShutdown & )
{
if ( ( state_downcast< const NumLockOff * >() != 0 ) &&
( state_downcast< const CapsLockOff * >() != 0 ) &&
( state_downcast< const ScrollLockOff * >() != 0 ) )
{
std::cout << "Shutdown request accepted\n";
return terminate();
}
else
{
std::cout << "Ignoring shutdown request\n\n";
return discard_event();
}
}
//////////////////////////////////////////////////////////////////////////////
void DisplayStateConfiguration( const Keyboard & keyboard )
{
char orthogonalRegion = 'a';
for ( Keyboard::state_iterator pLeafState = keyboard.state_begin();
pLeafState != keyboard.state_end(); ++pLeafState )
{
std::cout << "Orthogonal region " << orthogonalRegion << ": ";
const Keyboard::state_base_type * pState = &*pLeafState;
while ( pState != 0 )
{
if ( pState != &*pLeafState )
{
std::cout << " -> ";
}
#ifdef BOOST_STATECHART_USE_NATIVE_RTTI
std::cout << std::setw( 15 ) << typeid( *pState ).name();
#else
std::cout << std::setw( 15 ) <<
pState->custom_dynamic_type_ptr< char >();
#endif
pState = pState->outer_state_ptr();
}
std::cout << "\n";
++orthogonalRegion;
}
std::cout << "\n";
}
//////////////////////////////////////////////////////////////////////////////
int main()
{
#ifndef BOOST_STATECHART_USE_NATIVE_RTTI
Active::custom_static_type_ptr( "Active" );
NumLockOn::custom_static_type_ptr( "NumLockOn" );
NumLockOff::custom_static_type_ptr( "NumLockOff" );
CapsLockOn::custom_static_type_ptr( "CapsLockOn" );
CapsLockOff::custom_static_type_ptr( "CapsLockOff" );
ScrollLockOn::custom_static_type_ptr( "ScrollLockOn" );
ScrollLockOff::custom_static_type_ptr( "ScrollLockOff" );
#endif
std::cout << "Boost.Statechart Keyboard example\n\n";
Keyboard keyboard;
keyboard.initiate();
DisplayStateConfiguration( keyboard );
keyboard.process_event( EvNumLockPressed() );
DisplayStateConfiguration( keyboard );
keyboard.process_event( EvRequestShutdown() );
keyboard.process_event( EvCapsLockPressed() );
DisplayStateConfiguration( keyboard );
keyboard.process_event( EvRequestShutdown() );
keyboard.process_event( EvScrollLockPressed() );
DisplayStateConfiguration( keyboard );
keyboard.process_event( EvRequestShutdown() );
keyboard.process_event( EvNumLockPressed() );
DisplayStateConfiguration( keyboard );
keyboard.process_event( EvRequestShutdown() );
keyboard.process_event( EvCapsLockPressed() );
DisplayStateConfiguration( keyboard );
keyboard.process_event( EvRequestShutdown() );
keyboard.process_event( EvScrollLockPressed() );
DisplayStateConfiguration( keyboard );
keyboard.process_event( EvRequestShutdown() );
return 0;
}
|