--- /dev/null
+//////////////////////////////////////////////////////////////////////////////
+// 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)
+//////////////////////////////////////////////////////////////////////////////
+
+
+
+#include "Precompiled.hpp"
+#include "Camera.hpp"
+#include "Configuring.hpp"
+#include "Shooting.hpp"
+
+
+
+//////////////////////////////////////////////////////////////////////////////
+NotShooting::NotShooting()
+{
+ std::cout << "Entering NotShooting\n";
+}
+
+NotShooting::~NotShooting()
+{
+ std::cout << "Exiting NotShooting\n";
+}
+
+sc::result NotShooting::react( const EvShutterHalf & )
+{
+ if ( context< Camera >().IsBatteryLow() )
+ {
+ return forward_event();
+ }
+ else
+ {
+ return transit< Shooting >();
+ }
+}
+
+//////////////////////////////////////////////////////////////////////////////
+Idle::Idle()
+{
+ std::cout << "Entering Idle\n";
+}
+
+Idle::~Idle()
+{
+ std::cout << "Exiting Idle\n";
+}
+
+sc::result Idle::react( const EvConfig & )
+{
+ return transit< Configuring >();
+}
--- /dev/null
+#ifndef BOOST_STATECHART_EXAMPLE_CAMERA_HPP_INCLUDED
+#define BOOST_STATECHART_EXAMPLE_CAMERA_HPP_INCLUDED
+//////////////////////////////////////////////////////////////////////////////
+// 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)
+//////////////////////////////////////////////////////////////////////////////
+
+
+
+#include <boost/statechart/event.hpp>
+#include <boost/statechart/state_machine.hpp>
+#include <boost/statechart/simple_state.hpp>
+#include <boost/statechart/custom_reaction.hpp>
+
+#include <boost/config.hpp>
+
+#ifdef BOOST_INTEL
+# pragma warning( disable: 304 ) // access control not specified
+#endif
+
+
+
+namespace sc = boost::statechart;
+
+
+
+//////////////////////////////////////////////////////////////////////////////
+struct EvShutterHalf : sc::event< EvShutterHalf > {};
+struct EvShutterFull : sc::event< EvShutterFull > {};
+struct EvShutterRelease : sc::event< EvShutterRelease > {};
+struct EvConfig : sc::event< EvConfig > {};
+
+struct NotShooting;
+struct Camera : sc::state_machine< Camera, NotShooting >
+{
+ bool IsMemoryAvailable() const { return true; }
+ bool IsBatteryLow() const { return false; }
+};
+
+struct Idle;
+struct NotShooting : sc::simple_state< NotShooting, Camera, Idle >
+{
+ typedef sc::custom_reaction< EvShutterHalf > reactions;
+
+ NotShooting();
+ ~NotShooting();
+
+ sc::result react( const EvShutterHalf & );
+};
+
+ struct Idle : sc::simple_state< Idle, NotShooting >
+ {
+ typedef sc::custom_reaction< EvConfig > reactions;
+
+ Idle();
+ ~Idle();
+
+ sc::result react( const EvConfig & );
+ };
+
+
+
+#endif
--- /dev/null
+//////////////////////////////////////////////////////////////////////////////
+// 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)
+//////////////////////////////////////////////////////////////////////////////
+
+
+
+#include "Precompiled.hpp"
+#include "Configuring.hpp"
+#include <iostream>
+#include <stdexcept>
+
+
+
+Configuring::Configuring()
+{
+ std::cout << "Entering Configuring\n";
+}
+
+Configuring::~Configuring()
+{
+ std::cout << "Exiting Configuring\n";
+}
--- /dev/null
+#ifndef BOOST_STATECHART_EXAMPLE_CONFIGURING_HPP_INCLUDED
+#define BOOST_STATECHART_EXAMPLE_CONFIGURING_HPP_INCLUDED
+//////////////////////////////////////////////////////////////////////////////
+// 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)
+//////////////////////////////////////////////////////////////////////////////
+
+
+
+#include "Camera.hpp"
+
+#include <boost/statechart/simple_state.hpp>
+#include <boost/statechart/transition.hpp>
+
+#include <boost/config.hpp>
+
+#ifdef BOOST_INTEL
+# pragma warning( disable: 304 ) // access control not specified
+#endif
+
+
+
+namespace sc = boost::statechart;
+
+
+
+//////////////////////////////////////////////////////////////////////////////
+struct Configuring : sc::simple_state< Configuring, NotShooting >
+{
+ typedef sc::transition< EvConfig, Idle > reactions;
+
+ Configuring();
+ ~Configuring();
+};
+
+
+
+#endif
--- /dev/null
+//////////////////////////////////////////////////////////////////////////////
+// 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)
+//////////////////////////////////////////////////////////////////////////////
+
+
+
+//////////////////////////////////////////////////////////////////////////////
+// This program shows how a state machine can be spread over several
+// translation units if necessary. The inner workings of a digital camera are
+// modeled, the corresponding state chart looks as follows:
+//
+// ---------------------------
+// | |
+// | NotShooting |
+// | |
+// | ------------- |<---O
+// | O--->| Idle | | --------------
+// | ------------- | EvShutterHalf | |
+// | | ^ |------------------>| Shooting |
+// | EvConfig | | EvConfig | | |
+// | v | | EvShutterRelease | |
+// | ------------- |<------------------| |
+// | | Configuring | | | |
+// | ------------- | --------------
+// ---------------------------
+//
+// The states Configuring and Shooting will contain a large amount of logic,
+// so they are implemented in their own translation units. This way one team
+// could implement the Configuring mode while the other would work on the
+// Shooting mode. Once the above state chart is implemented, the teams could
+// work completely independently of each other.
+
+
+
+#include "Precompiled.cpp"
+#include "Camera.cpp"
+#include "Shooting.cpp"
+#include "Configuring.cpp"
+
+
+#include <iostream>
+
+
+
+//////////////////////////////////////////////////////////////////////////////
+char GetKey()
+{
+ char key;
+ std::cin >> key;
+ return key;
+}
+
+
+//////////////////////////////////////////////////////////////////////////////
+int main()
+{
+ std::cout << "Boost.Statechart Camera example\n\n";
+
+ std::cout << "h<CR>: Press shutter half-way\n";
+ std::cout << "f<CR>: Press shutter fully\n";
+ std::cout << "r<CR>: Release shutter\n";
+ std::cout << "c<CR>: Enter/exit configuration\n";
+ std::cout << "e<CR>: Exits the program\n\n";
+ std::cout << "You may chain commands, e.g. hfr<CR> first presses the shutter half-way,\n";
+ std::cout << "fully and then releases it.\n\n";
+
+
+ Camera myCamera;
+ myCamera.initiate();
+
+ char key = GetKey();
+
+ while ( key != 'e' )
+ {
+ switch( key )
+ {
+ case 'h':
+ {
+ myCamera.process_event( EvShutterHalf() );
+ }
+ break;
+
+ case 'f':
+ {
+ myCamera.process_event( EvShutterFull() );
+ }
+ break;
+
+ case 'r':
+ {
+ myCamera.process_event( EvShutterRelease() );
+ }
+ break;
+
+ case 'c':
+ {
+ myCamera.process_event( EvConfig() );
+ }
+ break;
+
+ default:
+ {
+ std::cout << "Invalid key!\n";
+ }
+ break;
+ }
+
+ key = GetKey();
+ }
+
+ return 0;
+}
--- /dev/null
+//////////////////////////////////////////////////////////////////////////////
+// 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)
+//////////////////////////////////////////////////////////////////////////////
+
+
+
+#include "Precompiled.hpp"
--- /dev/null
+#ifndef BOOST_STATECHART_EXAMPLE_PRECOMPILED_HPP_INCLUDED
+#define BOOST_STATECHART_EXAMPLE_PRECOMPILED_HPP_INCLUDED
+//////////////////////////////////////////////////////////////////////////////
+// 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)
+//////////////////////////////////////////////////////////////////////////////
+
+
+
+#include <boost/statechart/state_machine.hpp>
+#include <boost/statechart/event.hpp>
+#include <boost/statechart/simple_state.hpp>
+#include <boost/statechart/custom_reaction.hpp>
+#include <boost/mpl/list.hpp>
+#include <iostream>
+
+
+
+#endif
--- /dev/null
+//////////////////////////////////////////////////////////////////////////////
+// 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)
+//////////////////////////////////////////////////////////////////////////////
+
+
+
+#include "Precompiled.hpp"
+#include "Shooting.hpp"
+#include <iostream>
+
+#include <boost/config.hpp>
+
+#ifdef BOOST_INTEL
+# pragma warning( disable: 383 ) // reference to temporary used
+#endif
+
+
+
+//////////////////////////////////////////////////////////////////////////////
+Shooting::Shooting()
+{
+ std::cout << "Entering Shooting\n";
+}
+
+Shooting::~Shooting()
+{
+ std::cout << "Exiting Shooting\n";
+}
+
+//////////////////////////////////////////////////////////////////////////////
+struct Storing : sc::simple_state< Storing, Shooting >
+{
+ Storing()
+ {
+ std::cout << "Picture taken!\n";
+ }
+};
+
+//////////////////////////////////////////////////////////////////////////////
+struct Focused : sc::simple_state< Focused, Shooting >
+{
+ typedef sc::custom_reaction< EvShutterFull > reactions;
+
+ sc::result react( const EvShutterFull & );
+};
+
+sc::result Focused::react( const EvShutterFull & )
+{
+ if ( context< Camera >().IsMemoryAvailable() )
+ {
+ return transit< Storing >();
+ }
+ else
+ {
+ std::cout << "Cache memory full. Please wait...\n";
+ return discard_event();
+ }
+}
+
+//////////////////////////////////////////////////////////////////////////////
+Focusing::Focusing( my_context ctx ) : my_base( ctx )
+{
+ post_event( boost::intrusive_ptr< EvInFocus >( new EvInFocus() ) );
+}
+
+sc::result Focusing::react( const EvInFocus & evt )
+{
+ return transit< Focused >( &Shooting::DisplayFocused, evt );
+}
--- /dev/null
+#ifndef BOOST_STATECHART_EXAMPLE_SHOOTING_HPP_INCLUDED
+#define BOOST_STATECHART_EXAMPLE_SHOOTING_HPP_INCLUDED
+//////////////////////////////////////////////////////////////////////////////
+// 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)
+//////////////////////////////////////////////////////////////////////////////
+
+
+
+#include "Camera.hpp"
+
+#include <boost/statechart/event.hpp>
+#include <boost/statechart/simple_state.hpp>
+#include <boost/statechart/state.hpp>
+#include <boost/statechart/transition.hpp>
+#include <boost/statechart/custom_reaction.hpp>
+#include <boost/statechart/deferral.hpp>
+
+#include <boost/mpl/list.hpp>
+#include <boost/config.hpp>
+
+#ifdef BOOST_INTEL
+# pragma warning( disable: 304 ) // access control not specified
+#endif
+
+
+
+namespace sc = boost::statechart;
+namespace mpl = boost::mpl;
+
+
+
+//////////////////////////////////////////////////////////////////////////////
+struct EvInFocus : sc::event< EvInFocus > {};
+
+struct Focusing;
+struct Shooting : sc::simple_state< Shooting, Camera, Focusing >
+{
+ typedef sc::transition< EvShutterRelease, NotShooting > reactions;
+
+ Shooting();
+ ~Shooting();
+
+ void DisplayFocused( const EvInFocus & )
+ {
+ std::cout << "Focused!\n";
+ }
+};
+
+ struct Focusing : sc::state< Focusing, Shooting >
+ {
+ typedef mpl::list<
+ sc::custom_reaction< EvInFocus >,
+ sc::deferral< EvShutterFull >
+ > reactions;
+
+ Focusing( my_context ctx );
+ sc::result react( const EvInFocus & );
+ };
+
+
+
+#endif
-include ../Makefile.config
.PHONY: all
-all: test.pdf StopWatch.pdf main.pdf
+all: test.pdf StopWatch.pdf main.pdf Camera/Main.pdf
CLANG++ ?= $(shell $(LLVM_CONFIG) --bindir)/clang++
ifeq ($(wildcard $(CLANG++)),)
void HandleReaction(const Type *T, const SourceLocation Loc, CXXRecordDecl *SrcState)
{
+ // TODO: Improve Loc tracking
if (const ElaboratedType *ET = dyn_cast<ElaboratedType>(T))
HandleReaction(ET->getNamedType().getTypePtr(), Loc, SrcState);
else if (const TemplateSpecializationType *TST = dyn_cast<TemplateSpecializationType>(T)) {
} else if (name == "boost::mpl::list") {
for (TemplateSpecializationType::iterator Arg = TST->begin(), End = TST->end(); Arg != End; ++Arg)
HandleReaction(Arg->getAsType().getTypePtr(), Loc, SrcState);
- }
- //->getDecl()->getQualifiedNameAsString();
+ } else
+ Diag(Loc, diag_unhandled_reaction_type) << name;
} else
Diag(Loc, diag_unhandled_reaction_type) << T->getTypeClassName();
}
Model::Context *c = model.findContext(Context->getName());
if (c)
c->add(state);
+ // TODO: else
if (CXXRecordDecl *InnerInitialState = getTemplateArgDecl(Base->getType().getTypePtr(), 2))
state->setInitialInnerState(InnerInitialState->getName());