diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:54:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:54:28 +0000 |
commit | e6918187568dbd01842d8d1d2c808ce16a894239 (patch) | |
tree | 64f88b554b444a49f656b6c656111a145cbbaa28 /src/boost/libs/statechart/example/StopWatch | |
parent | Initial commit. (diff) | |
download | ceph-e6918187568dbd01842d8d1d2c808ce16a894239.tar.xz ceph-e6918187568dbd01842d8d1d2c808ce16a894239.zip |
Adding upstream version 18.2.2.upstream/18.2.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/statechart/example/StopWatch')
3 files changed, 687 insertions, 0 deletions
diff --git a/src/boost/libs/statechart/example/StopWatch/StopWatch.cpp b/src/boost/libs/statechart/example/StopWatch/StopWatch.cpp new file mode 100644 index 000000000..93c50ecf6 --- /dev/null +++ b/src/boost/libs/statechart/example/StopWatch/StopWatch.cpp @@ -0,0 +1,189 @@ +////////////////////////////////////////////////////////////////////////////// +// 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 code implements the state-machine (this is the version +// discussed in the tutorial): +// +// -------------------------------- +// | | +// | O Active | +// | | |<---- +// | v | | EvReset +// | ---------------------------- | | +// | | | |----- +// | | Stopped | | +// | ---------------------------- | +// | | ^ | +// | | EvStartStop | EvStartStop |<-----O +// | v | | +// | ---------------------------- | +// | | | | +// | | Running | | +// | ---------------------------- | +// -------------------------------- + + + +#include <boost/statechart/event.hpp> +#include <boost/statechart/state_machine.hpp> +#include <boost/statechart/simple_state.hpp> +#include <boost/statechart/transition.hpp> + +#include <boost/config.hpp> + +#include <ctime> +#include <iostream> + +#ifdef BOOST_NO_STDC_NAMESPACE +namespace std +{ + using ::time; + using ::difftime; + using ::time_t; +} +#endif + +#ifdef BOOST_INTEL +# pragma warning( disable: 304 ) // access control not specified +# pragma warning( disable: 444 ) // destructor for base is not virtual +# pragma warning( disable: 981 ) // operands are evaluated in unspecified order +#endif + + + +namespace sc = boost::statechart; + + + +////////////////////////////////////////////////////////////////////////////// +struct EvStartStop : sc::event< EvStartStop > {}; +struct EvReset : sc::event< EvReset > {}; + +struct IElapsedTime +{ + virtual double ElapsedTime() const = 0; +}; + +struct Active; +struct StopWatch : sc::state_machine< StopWatch, Active > {}; + +struct Stopped; +struct Active : sc::simple_state< Active, StopWatch, Stopped > +{ + public: + typedef sc::transition< EvReset, Active > reactions; + + Active() : elapsedTime_( 0.0 ) {} + + double & ElapsedTime() + { + return elapsedTime_; + } + + double ElapsedTime() const + { + return elapsedTime_; + } + + private: + double elapsedTime_; +}; + + struct Running : IElapsedTime, sc::simple_state< Running, Active > + { + public: + typedef sc::transition< EvStartStop, Stopped > reactions; + + Running() : startTime_( std::time( 0 ) ) {} + + ~Running() + { + context< Active >().ElapsedTime() = ElapsedTime(); + } + + virtual double ElapsedTime() const + { + return context< Active >().ElapsedTime() + + std::difftime( std::time( 0 ), startTime_ ); + } + + private: + std::time_t startTime_; + }; + + struct Stopped : IElapsedTime, sc::simple_state< Stopped, Active > + { + typedef sc::transition< EvStartStop, Running > reactions; + + virtual double ElapsedTime() const + { + return context< Active >().ElapsedTime(); + } + }; + + +////////////////////////////////////////////////////////////////////////////// +char GetKey() +{ + char key; + std::cin >> key; + return key; +} + + +////////////////////////////////////////////////////////////////////////////// +int main() +{ + std::cout << "Boost.Statechart StopWatch example\n\n"; + std::cout << "s<CR>: Starts/Stops stop watch\n"; + std::cout << "r<CR>: Resets stop watch\n"; + std::cout << "d<CR>: Displays the elapsed time in seconds\n"; + std::cout << "e<CR>: Exits the program\n\n"; + std::cout << "You may chain commands, e.g. rs<CR> resets and starts stop watch\n\n"; + + StopWatch stopWatch; + stopWatch.initiate(); + + char key = GetKey(); + + while ( key != 'e' ) + { + switch( key ) + { + case 'r': + { + stopWatch.process_event( EvReset() ); + } + break; + + case 's': + { + stopWatch.process_event( EvStartStop() ); + } + break; + + case 'd': + { + std::cout << "Elapsed time: " << + stopWatch.state_cast< const IElapsedTime & >().ElapsedTime() << "\n"; + } + break; + + default: + { + std::cout << "Invalid key!\n"; + } + break; + } + + key = GetKey(); + } + + return 0; +} diff --git a/src/boost/libs/statechart/example/StopWatch/StopWatch.vcproj b/src/boost/libs/statechart/example/StopWatch/StopWatch.vcproj new file mode 100644 index 000000000..f04b6c760 --- /dev/null +++ b/src/boost/libs/statechart/example/StopWatch/StopWatch.vcproj @@ -0,0 +1,282 @@ +<?xml version="1.0" encoding="Windows-1252"?> +<VisualStudioProject + ProjectType="Visual C++" + Version="9.00" + Name="StopWatch" + ProjectGUID="{DFA55264-E7F5-43FE-841F-D56AC48FF2A8}" + RootNamespace="StopWatch" + Keyword="Win32Proj" + TargetFrameworkVersion="131072" + > + <Platforms> + <Platform + Name="Win32" + /> + </Platforms> + <ToolFiles> + </ToolFiles> + <Configurations> + <Configuration + Name="Debug|Win32" + OutputDirectory="Debug" + IntermediateDirectory="Debug" + ConfigurationType="1" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + AdditionalIncludeDirectories="..\..\..\.." + PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE" + MinimalRebuild="true" + BasicRuntimeChecks="3" + RuntimeLibrary="3" + BufferSecurityCheck="true" + DisableLanguageExtensions="true" + TreatWChar_tAsBuiltInType="true" + ForceConformanceInForLoopScope="true" + RuntimeTypeInfo="true" + UsePrecompiledHeader="0" + WarningLevel="4" + WarnAsError="true" + Detect64BitPortabilityProblems="false" + DebugInformationFormat="4" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + OutputFile="$(OutDir)/StopWatch.exe" + LinkIncremental="2" + GenerateDebugInformation="true" + ProgramDatabaseFile="$(OutDir)/StopWatch.pdb" + SubSystem="1" + RandomizedBaseAddress="1" + DataExecutionPrevention="0" + TargetMachine="1" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + <Configuration + Name="Release|Win32" + OutputDirectory="Release" + IntermediateDirectory="Release" + ConfigurationType="1" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="3" + InlineFunctionExpansion="2" + AdditionalIncludeDirectories="..\..\..\.." + PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE" + StringPooling="true" + RuntimeLibrary="2" + BufferSecurityCheck="false" + EnableFunctionLevelLinking="true" + DisableLanguageExtensions="true" + TreatWChar_tAsBuiltInType="true" + ForceConformanceInForLoopScope="true" + RuntimeTypeInfo="true" + UsePrecompiledHeader="0" + WarningLevel="4" + WarnAsError="true" + Detect64BitPortabilityProblems="false" + DebugInformationFormat="0" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + OutputFile="$(OutDir)/StopWatch.exe" + LinkIncremental="1" + GenerateDebugInformation="false" + SubSystem="1" + OptimizeReferences="2" + EnableCOMDATFolding="2" + RandomizedBaseAddress="1" + DataExecutionPrevention="0" + TargetMachine="1" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + </Configurations> + <References> + </References> + <Files> + <Filter + Name="Source Files" + Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm" + > + <File + RelativePath="StopWatch.cpp" + > + </File> + </Filter> + <Filter + Name="Statechart Header Files" + Filter="h;hpp;hxx;hm;inl;inc" + > + <File + RelativePath="..\..\..\..\boost\statechart\asynchronous_state_machine.hpp" + > + </File> + <File + RelativePath="..\..\..\..\boost\statechart\custom_reaction.hpp" + > + </File> + <File + RelativePath="..\..\..\..\boost\statechart\deep_history.hpp" + > + </File> + <File + RelativePath="..\..\..\..\boost\statechart\deferral.hpp" + > + </File> + <File + RelativePath="..\..\..\..\boost\statechart\event.hpp" + > + </File> + <File + RelativePath="..\..\..\..\boost\statechart\event_base.hpp" + > + </File> + <File + RelativePath="..\..\..\..\boost\statechart\event_processor.hpp" + > + </File> + <File + RelativePath="..\..\..\..\boost\statechart\exception_translator.hpp" + > + </File> + <File + RelativePath="..\..\..\..\boost\statechart\fifo_scheduler.hpp" + > + </File> + <File + RelativePath="..\..\..\..\boost\statechart\in_state_reaction.hpp" + > + </File> + <File + RelativePath="..\..\..\..\boost\statechart\null_exception_translator.hpp" + > + </File> + <File + RelativePath="..\..\..\..\boost\statechart\result.hpp" + > + </File> + <File + RelativePath="..\..\..\..\boost\statechart\shallow_history.hpp" + > + </File> + <File + RelativePath="..\..\..\..\boost\statechart\simple_state.hpp" + > + </File> + <File + RelativePath="..\..\..\..\boost\statechart\state.hpp" + > + </File> + <File + RelativePath="..\..\..\..\boost\statechart\state_machine.hpp" + > + </File> + <File + RelativePath="..\..\..\..\boost\statechart\termination.hpp" + > + </File> + <File + RelativePath="..\..\..\..\boost\statechart\transition.hpp" + > + </File> + </Filter> + </Files> + <Globals> + </Globals> +</VisualStudioProject> diff --git a/src/boost/libs/statechart/example/StopWatch/StopWatch2.cpp b/src/boost/libs/statechart/example/StopWatch/StopWatch2.cpp new file mode 100644 index 000000000..3a11b2475 --- /dev/null +++ b/src/boost/libs/statechart/example/StopWatch/StopWatch2.cpp @@ -0,0 +1,216 @@ +////////////////////////////////////////////////////////////////////////////// +// 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 code implements the state-machine (this version details an +// alternative way of retrieving the elapsed time from the main program): +// +// -------------------------------- +// | | +// | O Active | +// | | |<---- +// | v | | EvReset +// | ---------------------------- | | +// | | | |----- +// | | Stopped | | +// | ---------------------------- | +// | | ^ | +// | | EvStartStop | EvStartStop |<-----O +// | v | | +// | ---------------------------- | +// | | | | +// | | Running | | +// | ---------------------------- | +// -------------------------------- + + + +#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 <ctime> +#include <iostream> + +#ifdef BOOST_NO_STDC_NAMESPACE +namespace std +{ + using ::time; + using ::difftime; + using ::time_t; +} +#endif + +#ifdef BOOST_INTEL +# pragma warning( disable: 304 ) // access control not specified +# pragma warning( disable: 444 ) // destructor for base is not virtual +# pragma warning( disable: 981 ) // operands are evaluated in unspecified order +#endif + + + +namespace sc = boost::statechart; +namespace mpl = boost::mpl; + + + +struct EvStartStop : sc::event< EvStartStop > {}; +struct EvReset : sc::event< EvReset > {}; +struct EvGetElapsedTime : sc::event< EvGetElapsedTime > +{ + public: + EvGetElapsedTime( double & time ) : time_( time ) {} + + void Assign( double time ) const + { + time_ = time; + } + + private: + double & time_; +}; + + +struct Active; +struct StopWatch : sc::state_machine< StopWatch, Active > {}; + + +struct Stopped; +struct Active : sc::simple_state< Active, StopWatch, Stopped > +{ + public: + typedef sc::transition< EvReset, Active > reactions; + + Active() : elapsedTime_( 0.0 ) {} + + double & ElapsedTime() + { + return elapsedTime_; + } + + double ElapsedTime() const + { + return elapsedTime_; + } + + private: + double elapsedTime_; +}; + +struct Running : sc::simple_state< Running, Active > +{ + public: + typedef mpl::list< + sc::custom_reaction< EvGetElapsedTime >, + sc::transition< EvStartStop, Stopped > + > reactions; + + Running() : startTime_( std::time( 0 ) ) {} + + ~Running() + { + context< Active >().ElapsedTime() = ElapsedTime(); + } + + sc::result react( const EvGetElapsedTime & evt ) + { + evt.Assign( ElapsedTime() ); + return discard_event(); + } + + private: + double ElapsedTime() const + { + return context< Active >().ElapsedTime() + + std::difftime( std::time( 0 ), startTime_ ); + } + + std::time_t startTime_; +}; + +struct Stopped : sc::simple_state< Stopped, Active > +{ + typedef mpl::list< + sc::custom_reaction< EvGetElapsedTime >, + sc::transition< EvStartStop, Running > + > reactions; + + sc::result react( const EvGetElapsedTime & evt ) + { + evt.Assign( context< Active >().ElapsedTime() ); + return discard_event(); + } +}; + + +namespace +{ + char GetKey() + { + char key; + std::cin >> key; + return key; + } +} + +int main() +{ + std::cout << "Boost.Statechart StopWatch example\n\n"; + std::cout << "s<CR>: Starts/Stops stop watch\n"; + std::cout << "r<CR>: Resets stop watch\n"; + std::cout << "d<CR>: Displays the elapsed time in seconds\n"; + std::cout << "e<CR>: Exits the program\n\n"; + std::cout << "You may chain commands, e.g. rs<CR> resets and starts stop watch\n\n"; + + StopWatch stopWatch; + stopWatch.initiate(); + + char key = GetKey(); + + while ( key != 'e' ) + { + switch( key ) + { + case 'r': + { + stopWatch.process_event( EvReset() ); + } + break; + + case 's': + { + stopWatch.process_event( EvStartStop() ); + } + break; + + case 'd': + { + double elapsedTime = 0.0; + stopWatch.process_event( EvGetElapsedTime( elapsedTime ) ); + std::cout << "Elapsed time: " << elapsedTime << "\n"; + } + break; + + default: + { + std::cout << "Invalid key!\n"; + } + break; + } + + key = GetKey(); + } + + return 0; +} |