This commit is contained in:
r-lyeh
2015-11-29 16:57:56 +01:00
parent aa7abf05cf
commit 41c635ef95
6 changed files with 444 additions and 402 deletions

129
README.md
View File

@@ -1,80 +1,91 @@
fsm <a href="https://travis-ci.org/r-lyeh/fsm"><img src="https://api.travis-ci.org/r-lyeh/fsm.svg?branch=master" align="right" /></a>
===
- FSM is a lightweight Hierarchical/Finite-state Machine (H/FSM) class.
- FSM is expressive. Basic usage around `on(state,trigger) -> do lambda` expression.
- FSM is simple. Both HFSM and FSM implementations are provided.
- FSM is tiny. Header-only.
- FSM is cross-platform.
- FSM is stand-alone. No dependencies.
- FSM is zlib/libpng licensed.
FSM is a lightweight finite-state machine class. Both Hierarchical FSM and simple FSM implementations are provided.
### API
@todoc
### Features
- [x] Expressive. Basic usage around `on(state,trigger) -> do lambda` expression.
- [x] Tiny, cross-platform, stand-alone, header-only.
- [x] ZLIB/libPNG licensed.
### Links
[Finite-State Machines: Theory and Implementation](http://gamedevelopment.tutsplus.com/tutorials/finite-state-machines-theory-and-implementation--gamedev-11867)
### Sample
### Showcase
```c++
// basic hfsm sample
#include <iostream>
#include "fsm.hpp"
// common triggers (verbs). you must provide these for all FSMs
fsm::state begin("begin"), end("end"), pause("pause"), resume("resume");
// custom states (gerunds) and actions (infinitives)
// custom states (gerunds) and triggers (verbs)
fsm::state walking("walking"), attacking("attacking");
fsm::state update("udpate");
enum {
walking = 'WALK',
defending = 'DEFN',
class ant {
public:
fsm::stack fsm;
int health, distance, flow;
ant() : health(0), distance(0), flow(1) {
// set initial fsm state
fsm = walking;
// define fsm transitions: on(state,trigger) -> do lambda
fsm.on(walking,resume) = [&]( const fsm::args &args ) {
std::cout << "pre-walk! distance:" << distance << std::endl;
};
fsm.on(walking,update) = [&]( const fsm::args &args ) {
std::cout << "\r" << "\\|/-"[ distance % 4 ] << " walking " << (flow > 0 ? "-->" : "<--") << " ";
distance += flow;
if( 1000 == distance ) {
std::cout << "at food!" << std::endl;
flow = -flow;
}
if( -1000 == distance ) {
std::cout << "at home!" << std::endl;
flow = -flow;
}
};
fsm.on(attacking,begin) = [&]( const fsm::args &args ) {
std::cout << "pre-attack!" << std::endl;
health = 1000;
};
fsm.on(attacking,update) = [&]( const fsm::args &args ) {
std::cout << "\r" << "\\|/-$"[ distance % 4 ] << " fighting !! hp:(" << health << ") ";
--health;
if( health < 0 ) {
std::cout << std::endl;
fsm.pop();
}
};
}
tick = 'tick',
};
int main( int argc, const char **argv ) {
// hfsm ant sample
ant mini;
for(;;) {
if( 0 == rand() % 10000 ) mini.fsm.push(attacking);
mini.fsm(update());
struct ant_t {
fsm::stack fsm;
int health, distance, flow;
ant_t() : health(0), distance(0), flow(1) {
// define fsm transitions: on(state,trigger) -> do lambda
fsm.on(walking, 'init') = [&]( const fsm::args &args ) {
std::cout << "initializing" << std::endl;
};
fsm.on(walking, 'quit') = [&]( const fsm::args &args ) {
std::cout << "exiting" << std::endl;
};
fsm.on(walking, 'push') = [&]( const fsm::args &args ) {
std::cout << "pushing current task." << std::endl;
};
fsm.on(walking, 'back') = [&]( const fsm::args &args ) {
std::cout << "back from another task. remaining distance: " << distance << std::endl;
};
fsm.on(walking, tick) = [&]( const fsm::args &args ) {
std::cout << "\r" << "\\|/-"[ distance % 4 ] << " walking " << (flow > 0 ? "-->" : "<--") << " ";
distance += flow;
if( 1000 == distance ) {
std::cout << "at food!" << std::endl;
flow = -flow;
}
if( -1000 == distance ) {
std::cout << "at home!" << std::endl;
flow = -flow;
}
};
fsm.on(defending, 'init') = [&]( const fsm::args &args ) {
health = 1000;
std::cout << "somebody is attacking me! he has " << health << " health points" << std::endl;
};
fsm.on(defending, tick) = [&]( const fsm::args &args ) {
std::cout << "\r" << "\\|/-$"[ health % 4 ] << " health: (" << health << ") ";
--health;
if( health < 0 ) {
std::cout << std::endl;
fsm.pop();
}
};
// set initial fsm state
fsm.set( walking );
}
};
int main() {
ant_t ant;
for(int i = 0; i < 12000; ++i) {
if( 0 == rand() % 10000 ) {
ant.fsm.push(defending);
}
ant.fsm.command(tick);
}
}
```
### Changelog
- v1.0.0 (2015/11/29): Code revisited to use fourcc integers (much faster); clean ups suggested by Chang Qian
- v0.0.0 (2014/02/15): Initial version

3
demo1.cc Normal file
View File

@@ -0,0 +1,3 @@
#define FSM_BUILD_SAMPLE1
#include "fsm.hpp"

3
demo2.cc Normal file
View File

@@ -0,0 +1,3 @@
#define FSM_BUILD_SAMPLE2
#include "fsm.hpp"

536
fsm.hpp
View File

@@ -1,61 +1,29 @@
/*
* Simple FSM/HFSM class
// Simple FSM/HFSM class
// - rlyeh [2011..2015], zlib/libpng licensed.
* - inspiration: BOOST MSM (eUML FSM)
* format: source + event [guard(s)] [/ action(s)] == target
* original sample:
*
* BOOST_MSM_EUML_TRANSITION_TABLE((
* Stopped + play [some_guard] / (some_action , start_playback) == Playing ,
* Stopped + open_close/ open_drawer == Open ,
* Stopped + stop == Stopped ,
* Open + open_close / close_drawer == Empty ,
* Empty + open_close / open_drawer == Open ,
* Empty + cd_detected [good_disk_format] / store_cd_info == Stopped
* ),transition_table)
*
* - proposal:
* format: return
* current_state && trigger [&& guard(s)] ? ( [action(s),] new_state) :
* [...]
* 0
* sample becomes:
*
* return
* Stopped && play && some_guard() ? (some_action(), start_playback(), Playing) :
* Stopped && open_close ? (open_drawer(), Open) :
* Stopped && stop ? (Stopped) :
* Open && open_close ? (close_drawer(), Empty) :
* Empty && open_close ? (open_drawer(), Open) :
* Empty && cd_detected && good_disk_format() ? (store_cd_info(), Stopped) :
* 0
* refs:
* - http://en.wikipedia.org/wiki/Finite-state_machine
* todo:
* - GOAP? behavior trees?
* - trigger args
* - counters
* - rlyeh [2011..2015], zlib/libpng licensed
*/
// - note on child states (tree of fsm's):
// - triggers are handled to the most inner active state in the decision tree
// - unhandled triggers are delegated to the parent state handler until handled or discarded by root state
// [ref] http://en.wikipedia.org/wiki/Finite-state_machine
// [todo] GOAP? behavior trees?
// [todo] counters
// [note] common actions are 'init', 'quit', 'push', 'back' (integers)
// - init and quit are called everytime a state is created or destroyed.
// - push and back are called everytime a state is paused or resumed. Ie, when pushing and popping the stack tree.
// [note] on child states (tree of fsm's):
// - actions are handled to the most inner active state in the decision tree
// - unhandled actions are delegated to the parent state handler until handled or discarded by root state
#pragma once
#define FSM_VERSION "1.0.0" /* (2015/11/29) Code revisited to use fourcc integers (much faster); clean ups suggested by Chang Qian
#define FSM_VERSION "0.0.0" // (2014/02/15) Initial version */
#include <algorithm>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <iostream>
#include <sstream>
#include <algorithm>
namespace fsm
{
@@ -70,147 +38,149 @@ namespace fsm
return t;
}
struct args : public std::vector< std::string > {
args() : std::vector< std::string >()
{}
template<typename T0>
args( const T0 &t0 ) : std::vector< std::string >(1) {
(*this)[0] = fsm::to_string(t0);
}
template<typename T0, typename T1>
args( const T0 &t0, const T1 &t1 ) : std::vector< std::string >(2) {
(*this)[0] = fsm::to_string(t0);
(*this)[1] = fsm::to_string(t1);
}
};
class state;
typedef std::function< void( const fsm::args &args ) > code;
class state : public std::string {
public:
typedef std::vector<std::string> args;
typedef std::function< void( const fsm::args &args ) > call;
struct state {
int name;
fsm::args args;
explicit
state( const std::string &name = std::string() ) : std::string(name)
state( const int &name = 'null' ) : name(name)
{}
state operator()() const {
state self = *this;
self.args = fsm::args();
self.args = {};
return self;
}
template<typename T0>
state operator()( const T0 &t0 ) const {
state self = *this;
self.args = fsm::args(t0);
self.args = { fsm::to_string(t0) };
return self;
}
template<typename T0, typename T1>
state operator()( const T0 &t0, const T1 &t1 ) const {
state self = *this;
self.args = fsm::args(t0,t1);
self.args = { fsm::to_string(t0), fsm::to_string(t1) };
return self;
}
protected: std::vector< fsm::state > stack;
operator int () const {
return name;
}
bool operator<( const state &other ) const {
return name < other.name;
}
bool operator==( const state &other ) const {
return name == other.name;
}
template<typename ostream>
inline friend ostream &operator<<( ostream &out, const state &t ) {
if( t.name >= 256 ) {
out << char((t.name >> 24) & 0xff);
out << char((t.name >> 16) & 0xff);
out << char((t.name >> 8) & 0xff);
out << char((t.name >> 0) & 0xff);
} else {
out << t.name;
}
out << "(";
std::string sep;
for(auto &arg : t.args ) {
out << sep << arg;
sep = ',';
}
out << ")";
return out;
}
};
typedef state trigger;
struct transition {
fsm::state previous, trigger, current;
template<typename ostream>
inline friend ostream &operator<<( ostream &out, const transition &t ) {
out << t.previous << " -> " << t.trigger << " -> " << t.current;
return out;
}
};
class stack {
public:
stack( const fsm::state &start = fsm::state("{undefined}") ) : deque(1) {
stack( const fsm::state &start = 'null' ) : deque(1) {
deque[0] = start;
begin( deque.back() );
call( deque.back(), 'init' );
}
stack( int start ) : stack( fsm::state(start) )
{}
~stack() {
// ensure state destructors are called (w/ 'quit')
while( size() ) {
pop();
}
}
// pause current state (w/ 'push') and create a new active child (w/ 'init')
void push( const fsm::state &state ) {
if( deque.size() && deque.back() == state ) {
return;
}
// queue
pause( deque.back() );
call( deque.back(), 'push' );
deque.push_back( state );
begin( deque.back() );
call( deque.back(), 'init' );
}
void next( const fsm::state &state ) {
// terminate current state and return to parent (if any)
void pop() {
if( deque.size() ) {
follow( deque.back(), state );
call( deque.back(), 'quit' );
deque.pop_back();
}
if( deque.size() ) {
call( deque.back(), 'back' );
}
}
// set current active state
void set( const fsm::state &state ) {
if( deque.size() ) {
replace( deque.back(), state );
} else {
push(state);
}
}
void pop() {
if( deque.size() ) {
end( deque.back() );
deque.pop_back();
}
if( deque.size() ) {
resume( deque.back() );
}
}
// number of children (stack)
size_t size() const {
return deque.size();
}
void start(const fsm::state &state) {
next( state );
}
bool exec( const fsm::state &trigger ) {
size_t size = this->size();
if( !size ) {
return false;
}
current_trigger = fsm::state();
std::deque< rit > cancelled;
for( rit it = deque.rbegin(); it != deque.rend(); ++it ) {
fsm::state &self = *it;
if( !call(self,trigger) ) {
cancelled.push_back(it);
continue;
}
for( std::deque< rit >::iterator it = cancelled.begin(), end = cancelled.end(); it != end; ++it ) {
(*it)->end();
deque.erase(--(it->base()));
}
current_trigger = trigger;
return true;
}
return false;
}
// info
// [] classic behaviour: "hello"[5] = undefined, "hello"[-1] = undefined
// [] extended behaviour: "hello"[5] = h, "hello"[-1] = o, "hello"[-2] = l
fsm::state get_state( signed int pos = -1 ) const {
fsm::state get_state( signed pos = -1 ) const {
signed size = (signed)(deque.size());
if( !size ) {
return fsm::state();
}
return *( deque.begin() + ( pos >= 0 ? pos % size : size - 1 + ((pos+1) % size) ) );
return size ? *( deque.begin() + (pos >= 0 ? pos % size : size - 1 + ((pos+1) % size) ) ) : fsm::state();
}
fsm::transition get_log( signed int pos = -1 ) const {
fsm::transition get_log( signed pos = -1 ) const {
signed size = (signed)(log.size());
if( !size ) {
return fsm::transition();
}
return *( log.begin() + ( pos >= 0 ? pos % size : size - 1 + ((pos+1) % size) ) );
return size ? *( log.begin() + (pos >= 0 ? pos % size : size - 1 + ((pos+1) % size) ) ) : fsm::transition();
}
std::string get_trigger() const {
return current_trigger;
std::stringstream ss;
return ss << current_trigger, ss.str();
}
bool is( const fsm::state &state ) const {
bool is_state( const fsm::state &state ) const {
return deque.empty() ? false : ( deque.back() == state );
}
@@ -220,70 +190,300 @@ namespace fsm
bool is_hold() const { return transition.previous == transition.current; }
bool is_released() const { return transition.previous == transition.current; } */
std::string debug() const {
std::string out;
for( cit it = deque.begin(), end = deque.end(); it != end; ++it ) {
out += (*it) + ',';
}
return out;
// setup
fsm::call &on( const fsm::state &from, const fsm::state &to ) {
return callbacks[ bistate(from,to) ];
}
bool call( const fsm::state &from, const fsm::state &to ) /*const*/ {
std::map< bistate, fsm::code >::const_iterator code = callbacks.find(bistate(from,to));
if( code == callbacks.end() ) {
return false;
} else {
// generic call
bool call( const fsm::state &from, const fsm::state &to ) const {
std::map< bistate, fsm::call >::const_iterator found = callbacks.find(bistate(from,to));
if( found != callbacks.end() ) {
log.push_back( { from, current_trigger, to } );
if( log.size() > 50 ) {
log.pop_front();
}
code->second( to.args );
found->second( to.args );
return true;
}
return false;
}
fsm::code &on( const fsm::state &from, const fsm::state &to ) {
return callbacks[ bistate(from,to) ] = callbacks[ bistate(from,to) ];
// user commands
bool command( const fsm::state &trigger ) {
size_t size = this->size();
if( !size ) {
return false;
}
current_trigger = fsm::state();
std::deque< states::reverse_iterator > aborted;
for( auto it = deque.rbegin(); it != deque.rend(); ++it ) {
fsm::state &self = *it;
if( !call(self,trigger) ) {
aborted.push_back(it);
continue;
}
for( auto it = aborted.begin(), end = aborted.end(); it != end; ++it ) {
call(**it, 'quit');
deque.erase(--(it->base()));
}
current_trigger = trigger;
return true;
}
return false;
}
template<typename T>
bool command( const fsm::state &trigger, const T &arg1 ) {
return command( trigger(arg1) );
}
template<typename T, typename U>
bool command( const fsm::state &trigger, const T &arg1, const U &arg2 ) {
return command( trigger(arg1, arg2) );
}
// debug
template<typename ostream>
ostream &debug( ostream &out ) {
int total = log.size();
out << "status {" << std::endl;
std::string sep = "\t";
for( states::const_reverse_iterator it = deque.rbegin(), end = deque.rend(); it != end; ++it ) {
out << sep << *it;
sep = " -> ";
}
out << std::endl;
out << "} log (" << total << " entries) {" << std::endl;
for( int i = 0 ; i < total; ++i ) {
out << "\t" << log[i] << std::endl;
}
out << "}" << std::endl;
return out;
}
// aliases
bool operator()( const fsm::state &trigger ) {
return exec( trigger );
return command( trigger );
}
template<typename T>
bool operator()( const fsm::state &trigger, const T &arg1 ) {
return command( trigger(arg1) );
}
template<typename T, typename U>
bool operator()( const fsm::state &trigger, const T &arg1, const U &arg2 ) {
return command( trigger(arg1, arg2) );
}
template<typename ostream>
inline friend ostream &operator<<( ostream &out, const stack &t ) {
return t.debug( out ), out;
}
protected:
void begin( const fsm::state &state ) {
call( state, fsm::state("begin") );
}
void end( const fsm::state &state ) {
call( state, fsm::state("end") );
}
void pause( const fsm::state &state ) {
call( state, fsm::state("pause") );
}
void resume( const fsm::state &state ) {
call( state, fsm::state("resume") );
}
void follow( fsm::state &current, const fsm::state &next ) {
end( current );
void replace( fsm::state &current, const fsm::state &next ) {
call( current, 'quit' );
current = next;
begin( current );
call( current, 'init' );
}
typedef std::pair<std::string, std::string> bistate;
std::map< bistate, fsm::code > callbacks;
typedef std::pair<int, int> bistate;
std::map< bistate, fsm::call > callbacks;
std::deque< fsm::transition > log;
mutable std::deque< fsm::transition > log;
std::deque< fsm::state > deque;
fsm::state current_trigger;
typedef std::deque< fsm::state >::const_iterator cit;
typedef std::deque< fsm::state >::reverse_iterator rit;
typedef std::deque< fsm::state > states;
};
}
#ifdef FSM_BUILD_SAMPLE1
// basic fsm, CD player sample
#include <iostream>
// custom states (gerunds) and actions (infinitives)
enum {
opening,
closing,
waiting,
playing,
open,
close,
play,
stop,
insert,
eject,
};
struct cd_player {
// implementation variables
bool has_cd;
// implementation conditions / guards
bool good_disk_format() { return true; }
// implementation actions
void open_tray() { std::cout << "opening tray" << std::endl; }
void close_tray() { std::cout << "closing tray" << std::endl; }
void get_cd_info() { std::cout << "retrieving CD info" << std::endl; }
void start_playback( const std::string &track ) { std::cout << "playing track #" << track << std::endl; }
// the core
fsm::stack fsm;
cd_player() : has_cd(false)
{
// define fsm transitions: on(state,trigger) -> do lambda
fsm.on(opening,close) = [&]( const fsm::args &args ) {
close_tray();
if( !has_cd ) {
fsm.set( closing );
} else {
get_cd_info();
fsm.set( waiting );
}
};
fsm.on(opening,insert) = [&]( const fsm::args &args ) {
has_cd = true;
fsm.set( opening );
};
fsm.on(opening,eject) = [&]( const fsm::args &args ) {
has_cd = false;
fsm.set( opening );
};
fsm.on(closing,open) = [&]( const fsm::args &args ) {
open_tray();
fsm.set( opening );
};
fsm.on(waiting,play) = [&]( const fsm::args &args ) {
if( !good_disk_format() ) {
fsm.set( waiting );
} else {
start_playback( args[0] );
fsm.set( playing );
}
};
fsm.on(waiting,open) = [&]( const fsm::args &args ) {
open_tray();
fsm.set( opening );
};
fsm.on(playing,open) = [&]( const fsm::args &args ) {
open_tray();
fsm.set( opening );
};
fsm.on(playing,stop) = [&]( const fsm::args &args ) {
fsm.set( waiting );
};
// set initial fsm state
fsm.set(opening);
}
};
// usage
int main() {
cd_player cd;
for(;;) {
std::cout << "[" << cd.fsm.get_state() << "] ";
std::cout << "(o)pen lid/(c)lose lid, (i)nsert cd/(e)ject cd, (p)lay/(s)top cd? ";
char cmd;
std::cin >> cmd;
switch( cmd ) {
case 'p': cd.fsm.command(play,1+rand()%10); break;
case 'o': cd.fsm.command(open); break;
case 'c': cd.fsm.command(close); break;
case 's': cd.fsm.command(stop); break;
case 'i': cd.fsm.command(insert); break;
case 'e': cd.fsm.command(eject); break;
default : std::cout << "what?" << std::endl;
}
}
}
#endif
#ifdef FSM_BUILD_SAMPLE2
// basic hfsm sample
#include <iostream>
// custom states (gerunds) and actions (infinitives)
enum {
walking = 'WALK',
defending = 'DEFN',
tick = 'tick',
};
struct ant_t {
fsm::stack fsm;
int health, distance, flow;
ant_t() : health(0), distance(0), flow(1) {
// define fsm transitions: on(state,trigger) -> do lambda
fsm.on(walking, 'init') = [&]( const fsm::args &args ) {
std::cout << "initializing" << std::endl;
};
fsm.on(walking, 'quit') = [&]( const fsm::args &args ) {
std::cout << "exiting" << std::endl;
};
fsm.on(walking, 'push') = [&]( const fsm::args &args ) {
std::cout << "pushing current task." << std::endl;
};
fsm.on(walking, 'back') = [&]( const fsm::args &args ) {
std::cout << "back from another task. remaining distance: " << distance << std::endl;
};
fsm.on(walking, tick) = [&]( const fsm::args &args ) {
std::cout << "\r" << "\\|/-"[ distance % 4 ] << " walking " << (flow > 0 ? "-->" : "<--") << " ";
distance += flow;
if( 1000 == distance ) {
std::cout << "at food!" << std::endl;
flow = -flow;
}
if( -1000 == distance ) {
std::cout << "at home!" << std::endl;
flow = -flow;
}
};
fsm.on(defending, 'init') = [&]( const fsm::args &args ) {
health = 1000;
std::cout << "somebody is attacking me! he has " << health << " health points" << std::endl;
};
fsm.on(defending, tick) = [&]( const fsm::args &args ) {
std::cout << "\r" << "\\|/-$"[ health % 4 ] << " health: (" << health << ") ";
--health;
if( health < 0 ) {
std::cout << std::endl;
fsm.pop();
}
};
// set initial fsm state
fsm.set( walking );
}
};
int main() {
ant_t ant;
for(int i = 0; i < 12000; ++i) {
if( 0 == rand() % 10000 ) {
ant.fsm.push(defending);
}
ant.fsm.command(tick);
}
}
#endif

View File

@@ -1,60 +0,0 @@
// basic hfsm sample
#include <iostream>
#include "fsm.hpp"
// common triggers (verbs). you must provide these for all FSMs
fsm::state begin("begin"), end("end"), pause("pause"), resume("resume");
// custom states (gerunds) and triggers (verbs)
fsm::state walking("walking"), attacking("attacking");
fsm::state update("udpate");
class ant {
public:
fsm::stack fsm;
int health, distance, flow;
ant() : health(0), distance(0), flow(1) {
// set initial fsm state
fsm = walking;
// define fsm transitions: on(state,trigger) -> do lambda
fsm.on(walking,resume) = [&]( const fsm::args &args ) {
std::cout << "pre-walk! distance:" << distance << std::endl;
};
fsm.on(walking,update) = [&]( const fsm::args &args ) {
std::cout << "\r" << "\\|/-"[ distance % 4 ] << " walking " << (flow > 0 ? "-->" : "<--") << " ";
distance += flow;
if( 1000 == distance ) {
std::cout << "at food!" << std::endl;
flow = -flow;
}
if( -1000 == distance ) {
std::cout << "at home!" << std::endl;
flow = -flow;
}
};
fsm.on(attacking,begin) = [&]( const fsm::args &args ) {
std::cout << "pre-attack!" << std::endl;
health = 1000;
};
fsm.on(attacking,update) = [&]( const fsm::args &args ) {
std::cout << "\r" << "\\|/-$"[ distance % 4 ] << " fighting !! hp:(" << health << ") ";
--health;
if( health < 0 ) {
std::cout << std::endl;
fsm.pop();
}
};
}
};
int main( int argc, const char **argv ) {
// hfsm ant sample
ant mini;
for(;;) {
if( 0 == rand() % 10000 ) mini.fsm.push(attacking);
mini.fsm(update());
}
}

View File

@@ -1,115 +0,0 @@
// basic fsm, CD player sample
#include <iostream>
#include "fsm.hpp"
// common triggers (verbs). you must provide these for all FSMs
fsm::state begin("begin"), end("end"), pause("pause"), resume("resume");
// custom states (gerunds)
fsm::state
opening("opening"), closing("closing"),
waiting("waiting"), playing("playing");
// custom triggers (verbs)
fsm::state
open("open"), close("close"),
play("play"), stop("stop"),
insert("insert"), eject("eject");
class cd_player
{
// implementation conditions / guards
bool good_disk_format() { return true; }
// implementation actions
void open_tray() { std::cout << "opening tray" << std::endl; }
void close_tray() { std::cout << "closing tray" << std::endl; }
void get_cd_info() { std::cout << "retrieving CD info" << std::endl; }
void start_playback( const std::string &track ) { std::cout << "playing track #" << track << std::endl; }
// implementation variables
bool has_cd;
public:
fsm::stack fsm;
cd_player() : has_cd(false)
{
// set initial fsm state
fsm = opening;
// define fsm transitions: on(state,trigger) -> do lambda
fsm.on(opening,close) = [&]( const fsm::args &args ) {
close_tray();
if( !has_cd ) {
fsm.start( closing );
} else {
get_cd_info();
fsm.start( waiting );
}
};
fsm.on(opening,insert) = [&]( const fsm::args &args ) {
has_cd = true;
fsm.start( opening );
};
fsm.on(opening,eject) = [&]( const fsm::args &args ) {
has_cd = false;
fsm.start( opening );
};
fsm.on(closing,open) = [&]( const fsm::args &args ) {
open_tray();
fsm.start( opening );
};
fsm.on(waiting,play) = [&]( const fsm::args &args ) {
if( !good_disk_format() ) {
fsm.start( waiting );
} else {
start_playback( args[0] );
fsm.start( playing );
}
};
fsm.on(waiting,open) = [&]( const fsm::args &args ) {
open_tray();
fsm.start( opening );
};
fsm.on(playing,open) = [&]( const fsm::args &args ) {
open_tray();
fsm.start( opening );
};
fsm.on(playing,stop) = [&]( const fsm::args &args ) {
fsm.start( waiting );
};
}
};
int main( int argc, const char **argv ) {
cd_player cd;
for(;;) {
char cmd;
std::cout << "[" << cd.fsm.get_state() << "] ";
std::cout << "(o)pen lid/(c)lose lid, (i)nsert cd/(e)ject cd, (p)lay/(s)top cd, (q)uit? ";
std::cin >> cmd;
switch( cmd )
{
case 'p': cd.fsm(play(1+rand()%10)); break;
case 'o': cd.fsm(open()); break;
case 'c': cd.fsm(close()); break;
case 's': cd.fsm(stop()); break;
case 'i': cd.fsm(insert()); break;
case 'e': cd.fsm(eject()); break;
case 'q': break;
default : std::cout << "what?" << std::endl;
}
if( cmd == 'q' ) break;
}
}