mirror of
https://github.com/boostorg/msm.git
synced 2026-02-19 14:32:34 +00:00
20 lines
5.3 KiB
HTML
20 lines
5.3 KiB
HTML
<html><head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
|
<title>Founding idea</title><meta name="generator" content="DocBook XSL-NS Stylesheets V1.75.2"><link rel="home" href="index.html" title="Meta State Machine (MSM)"><link rel="up" href="index.html" title="Meta State Machine (MSM)"><link rel="prev" href="index.html" title="Meta State Machine (MSM)"><link rel="next" href="ar01s03.html" title="UML Short Guide"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Founding idea</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="index.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ar01s03.html">Next</a></td></tr></table><hr></div><div class="sect1" title="Founding idea"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="d0e99"></a>Founding idea</h2></div></div></div><p>Let's start with an example taken from the C++ Template Metaprogramming book:</p><p>
|
|
<code class="code">class player : public state_machine<player></code></p><p><code class="code">{ </code></p><p><code class="code">// The list of FSM states enum states { Empty, Open, Stopped, Playing, Paused ,
|
|
initial_state = Empty }; </code></p><p><code class="code">// transition actions void start_playback(play const&) { std::cout <<
|
|
"player::start_playback\n"; } </code></p><p><code class="code">void open_drawer(open_close const&) { std::cout <<
|
|
"player::open_drawer\n"; } </code></p><p><code class="code">void close_drawer(open_close const&) { std::cout <<
|
|
"player::close_drawer\n"; } </code></p><p><code class="code">void store_cd_info(cd_detected const&) { std::cout <<
|
|
"player::store_cd_info\n"; } </code></p><p><code class="code">void stop_playback(stop const&) { std::cout <<
|
|
"player::stop_playback\n"; } </code></p><p><code class="code">void pause_playback(pause const&) { std::cout <<
|
|
"player::pause_playback\n"; } </code></p><p><code class="code">void resume_playback(play const&) { std::cout <<
|
|
"player::resume_playback\n"; } </code></p><p><code class="code">void stop_and_open(open_close const&) { std::cout <<
|
|
"player::stop_and_open\n"; } </code></p><p><code class="code">friend class state_machine<player>; </code></p><p><code class="code">typedef player p; // makes transition table cleaner </code></p><p><code class="code">// Transition table </code></p><p><code class="code">struct transition_table : mpl::vector11< </code></p><p><code class="code">row < Stopped , play , Playing , &p::start_playback >, </code></p><p><code class="code">row < Stopped , open_close , Open , &p::open_drawer >, </code></p><p><code class="code">row < Open , open_close , Empty , &p::close_drawer >, </code></p><p><code class="code">row < Empty , open_close , Open , &p::open_drawer >, </code></p><p><code class="code">row < Empty , cd_detected , Stopped , &p::store_cd_info >, </code></p><p><code class="code">row < Playing , stop , Stopped , &p::stop_playback >, </code></p><p><code class="code">row < Playing , pause , Paused , &p::pause_playback >, </code></p><p><code class="code">row < Playing , open_close , Open , &p::stop_and_open >, </code></p><p><code class="code">row < Paused , play , Playing , &p::resume_playback >, </code></p><p><code class="code">row < Paused , stop , Stopped , &p::stop_playback >, </code></p><p><code class="code">row < Paused , open_close , Open , &p::stop_and_open > </code></p><p><code class="code">> {}; </code></p><p><code class="code">// Replaces the default no-transition response. </code></p><p><code class="code">template <class Event> int no_transition(int state, Event const& e) {
|
|
std::cout << "no transition from state " << state << " on event "
|
|
<< typeid(e).name() << std::endl; return state; } };</code>
|
|
</p><p><code class="code">void test() { player p; p.process_event(open_close());...}</code></p><p>This example is the foundation for the idea driving MSM: a descriptive and expressive
|
|
language based on a transition table with as little syntactic noise as possible, all
|
|
this while offering as many features from the UML 2.0 standard as possible. MSM also
|
|
offers several expressive state machine definition syntaxes with different
|
|
trade-offs.</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="index.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="ar01s03.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Meta State Machine (MSM) </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> UML Short Guide</td></tr></table></div></body></html> |