Skip to content

Commit 30351cf

Browse files
* fixed ControlT const methods accessing context (#26)
1 parent 9b95f51 commit 30351cf

16 files changed

+908
-6
lines changed

examples/temp/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
3+
project(temp)
4+
5+
file(GLOB SOURCE_FILES "*.cpp")
6+
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
7+
8+
target_include_directories(${PROJECT_NAME} PRIVATE
9+
"${CMAKE_CURRENT_LIST_DIR}/../../include")
10+
11+
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17)
12+
13+
if(MSVC)
14+
target_compile_options(${PROJECT_NAME} PRIVATE /W4 /WX)
15+
else()
16+
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -Werror -pedantic)
17+
endif()

examples/temp/main.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#define HFSM2_ENABLE_UTILITY_THEORY
2+
#include <hfsm2/machine_dev.hpp>
3+
#include <iostream>
4+
5+
struct Context {
6+
uint8_t cycleCount = 0;
7+
};
8+
9+
using M = hfsm2::MachineT<hfsm2::Config::ContextT<Context>>;
10+
11+
#define S(s) struct s
12+
using FSM = M::PeerRoot<
13+
M::Utilitarian<S(Red),
14+
S(Yellow),
15+
S(Green)
16+
>,
17+
S(Off)
18+
>;
19+
#undef S
20+
21+
struct On
22+
: FSM::State
23+
{
24+
void enter(Control& control) {
25+
control.context().cycleCount = 0;
26+
std::cout << "On" << std::endl;
27+
}
28+
};
29+
30+
struct Red
31+
: FSM::State
32+
{
33+
void enter(Control& control) {
34+
++control.context().cycleCount;
35+
std::cout << " Red" << std::endl;
36+
}
37+
38+
void update(FullControl& control) {
39+
if (control.context().cycleCount > 3)
40+
control.changeTo<Off>();
41+
else
42+
control.changeTo<Green>();
43+
}
44+
};
45+
46+
struct Yellow
47+
: FSM::State
48+
{
49+
void enter(Control&) { std::cout << " Yellow ^" << std::endl; }
50+
void update(FullControl& control) { control.changeTo<Red>(); }
51+
///
52+
Utility utility(const Control& control) { return 0.5f + control.context().cycleCount; }
53+
///
54+
};
55+
56+
struct Green
57+
: FSM::State
58+
{
59+
void enter(Control&) { std::cout << " Green" << std::endl; }
60+
void update(FullControl& control) { control.changeTo<Yellow>(); }
61+
};
62+
63+
struct Off
64+
: FSM::State
65+
{
66+
void enter(Control&) { std::cout << "Off" << std::endl; }
67+
};
68+
69+
int main() {
70+
Context context;
71+
FSM::Instance machine{ context };
72+
73+
while (machine.isActive<Off>() == false)
74+
machine.update();
75+
76+
return 0;
77+
}

include/hfsm2/detail/root.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,14 @@ class R_ {
398398

399399
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
400400

401+
/// @brief Get the last transition that caused the state to be activated
402+
/// @param stateId State identifier
403+
/// @return Pointer to the last transition that activated the state
401404
const Transition* lastTransition(const StateID stateId) const;
402405

406+
/// @brief Get the last transition that caused the state to be activated
407+
/// @tparam TState State type
408+
/// @return Pointer to the last transition that activated the state
403409
template <typename TState>
404410
const Transition* lastTransition() const { return lastTransition(stateId<TState>()); }
405411

include/hfsm2/detail/root/control.hpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,28 @@ class ControlT {
118118
/// @brief Access FSM context (data shared between states and/or data interface between FSM and external code)
119119
/// @return context
120120
/// @see Control::context()
121-
HFSM2_INLINE Context& _() { return _context; }
121+
HFSM2_INLINE Context& _() { return _context; }
122+
123+
/// @brief Access FSM context (data shared between states and/or data interface between FSM and external code)
124+
/// @return context
125+
/// @see Control::context()
126+
HFSM2_INLINE const Context& _() const { return _context; }
127+
128+
/// @brief Access FSM context (data shared between states and/or data interface between FSM and external code)
129+
/// @return context
130+
/// @see Control::_()
131+
HFSM2_INLINE Context& context() { return _context; }
122132

123133
/// @brief Access FSM context (data shared between states and/or data interface between FSM and external code)
124134
/// @return context
125135
/// @see Control::_()
126-
HFSM2_INLINE Context& context() { return _context; }
136+
HFSM2_INLINE const Context& context() const { return _context; }
127137

128138
//----------------------------------------------------------------------
129139

130140
/// @brief Inspect current transition requests
131141
/// @return Array of transition requests
132-
HFSM2_INLINE const TransitionSet& requests() { return _requests; }
142+
HFSM2_INLINE const TransitionSet& requests() const { return _requests; }
133143

134144
//----------------------------------------------------------------------
135145

@@ -207,11 +217,19 @@ class ControlT {
207217

208218
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
209219

220+
/// @brief Get the last transition that caused the state to be activated
221+
/// @param stateId State identifier
222+
/// @return Pointer to the last transition that activated the state
210223
const Transition* lastTransition(const StateID stateId) const;
211224

225+
/// @brief Get the last transition that caused the state to be activated
226+
/// @tparam TState State type
227+
/// @return Pointer to the last transition that activated the state
212228
template <typename TState>
213229
const Transition* lastTransition() const { return lastTransition(stateId<TState>()); }
214230

231+
/// @brief Get the last transition that caused the current state to be activated
232+
/// @return Pointer to the last transition that activated the current state
215233
const Transition* lastTransition() const;
216234

217235
#endif

include/hfsm2/machine.hpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5004,18 +5004,28 @@ class ControlT {
50045004
/// @brief Access FSM context (data shared between states and/or data interface between FSM and external code)
50055005
/// @return context
50065006
/// @see Control::context()
5007-
HFSM2_INLINE Context& _() { return _context; }
5007+
HFSM2_INLINE Context& _() { return _context; }
5008+
5009+
/// @brief Access FSM context (data shared between states and/or data interface between FSM and external code)
5010+
/// @return context
5011+
/// @see Control::context()
5012+
HFSM2_INLINE const Context& _() const { return _context; }
5013+
5014+
/// @brief Access FSM context (data shared between states and/or data interface between FSM and external code)
5015+
/// @return context
5016+
/// @see Control::_()
5017+
HFSM2_INLINE Context& context() { return _context; }
50085018

50095019
/// @brief Access FSM context (data shared between states and/or data interface between FSM and external code)
50105020
/// @return context
50115021
/// @see Control::_()
5012-
HFSM2_INLINE Context& context() { return _context; }
5022+
HFSM2_INLINE const Context& context() const { return _context; }
50135023

50145024
//----------------------------------------------------------------------
50155025

50165026
/// @brief Inspect current transition requests
50175027
/// @return Array of transition requests
5018-
HFSM2_INLINE const TransitionSet& requests() { return _requests; }
5028+
HFSM2_INLINE const TransitionSet& requests() const { return _requests; }
50195029

50205030
//----------------------------------------------------------------------
50215031

@@ -5093,11 +5103,19 @@ class ControlT {
50935103

50945104
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
50955105

5106+
/// @brief Get the last transition that caused the state to be activated
5107+
/// @param stateId State identifier
5108+
/// @return Pointer to the last transition that activated the state
50965109
const Transition* lastTransition(const StateID stateId) const;
50975110

5111+
/// @brief Get the last transition that caused the state to be activated
5112+
/// @tparam TState State type
5113+
/// @return Pointer to the last transition that activated the state
50985114
template <typename TState>
50995115
const Transition* lastTransition() const { return lastTransition(stateId<TState>()); }
51005116

5117+
/// @brief Get the last transition that caused the current state to be activated
5118+
/// @return Pointer to the last transition that activated the current state
51015119
const Transition* lastTransition() const;
51025120

51035121
#endif
@@ -13165,8 +13183,14 @@ class R_ {
1316513183

1316613184
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1316713185

13186+
/// @brief Get the last transition that caused the state to be activated
13187+
/// @param stateId State identifier
13188+
/// @return Pointer to the last transition that activated the state
1316813189
const Transition* lastTransition(const StateID stateId) const;
1316913190

13191+
/// @brief Get the last transition that caused the state to be activated
13192+
/// @tparam TState State type
13193+
/// @return Pointer to the last transition that activated the state
1317013194
template <typename TState>
1317113195
const Transition* lastTransition() const { return lastTransition(stateId<TState>()); }
1317213196

projects/code-lite/hfsm2.workspace

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<VirtualDirectory Name="examples">
44
<Project Name="snippets-gcc-tdm" Path="snippets-gcc-tdm/snippets-gcc-tdm.project" Active="No"/>
55
<Project Name="advanced_event_handling-gcc-tdm" Path="advanced_event_handling-gcc-tdm/advanced_event_handling-gcc-tdm.project"/>
6+
<Project Name="temp-gcc-tdm" Path="temp-gcc-tdm/temp-gcc-tdm.project"/>
67
</VirtualDirectory>
78
<VirtualDirectory Name="tests">
89
<Project Name="test-gcc-tdm" Path="test-gcc-tdm/test-gcc-tdm.project" Active="Yes"/>
@@ -13,12 +14,14 @@
1314
<Project Name="snippets-gcc-tdm" ConfigName="Debug"/>
1415
<Project Name="test-gcc-tdm" ConfigName="Debug"/>
1516
<Project Name="advanced_event_handling-gcc-tdm" ConfigName="Debug"/>
17+
<Project Name="temp-gcc-tdm" ConfigName="Debug"/>
1618
</WorkspaceConfiguration>
1719
<WorkspaceConfiguration Name="Release" Selected="yes">
1820
<Environment/>
1921
<Project Name="snippets-gcc-tdm" ConfigName="Release"/>
2022
<Project Name="test-gcc-tdm" ConfigName="Release"/>
2123
<Project Name="advanced_event_handling-gcc-tdm" ConfigName="Release"/>
24+
<Project Name="temp-gcc-tdm" ConfigName="Release"/>
2225
</WorkspaceConfiguration>
2326
</BuildMatrix>
2427
</CodeLite_Workspace>
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<CodeLite_Project Name="temp-gcc-tdm" Version="11000" InternalType="Console">
3+
<Plugins>
4+
<Plugin Name="qmake">
5+
<![CDATA[00020001N0005Debug0000000000000001N0007Release000000000000]]>
6+
</Plugin>
7+
</Plugins>
8+
<Reconciliation>
9+
<Regexes/>
10+
<Excludepaths/>
11+
<Ignorefiles/>
12+
<Extensions>
13+
<![CDATA[*.cpp;*.hpp;*.inl]]>
14+
</Extensions>
15+
<Topleveldir>$(WorkspacePath)/../../test</Topleveldir>
16+
</Reconciliation>
17+
<VirtualDirectory Name="source">
18+
<File Name="../../../examples/temp/main.cpp"/>
19+
</VirtualDirectory>
20+
<Description/>
21+
<Dependencies/>
22+
<Dependencies Name="Debug"/>
23+
<Dependencies Name="Release"/>
24+
<Settings Type="Executable">
25+
<GlobalSettings>
26+
<Compiler Options="-Wall;-std=c++11" C_Options="-Wall;-std=c++11" Assembler="">
27+
<IncludePath Value="../../../external"/>
28+
<IncludePath Value="../../../include"/>
29+
</Compiler>
30+
<Linker Options="">
31+
<LibraryPath Value="."/>
32+
</Linker>
33+
<ResourceCompiler Options=""/>
34+
</GlobalSettings>
35+
<Configuration Name="Debug" CompilerType="MinGW ( gcc.exe (tdm64-1) 5.1.0 )" DebuggerType="GNU gdb debugger" Type="Executable" BuildCmpWithGlobalSettings="append" BuildLnkWithGlobalSettings="append" BuildResWithGlobalSettings="append">
36+
<Compiler Options="-g;-O0" C_Options="-g;-O0" Assembler="" Required="yes" PreCompiledHeader="" PCHInCommandLine="no" PCHFlags="" PCHFlagsPolicy="0">
37+
<Preprocessor Value="_DEBUG"/>
38+
</Compiler>
39+
<Linker Options="" Required="yes"/>
40+
<ResourceCompiler Options="" Required="no"/>
41+
<General OutputFile="" IntermediateDirectory="$(BUILD_ROOT)/$(WorkspaceName)/$(ProjectName)-$(WorkspaceConfiguration)" Command="$(WorkspacePath)\cmake-build-$(WorkspaceConfiguration)\output\$(ProjectName)" CommandArguments="" UseSeparateDebugArgs="no" DebugArguments="" WorkingDirectory="$(IntermediateDirectory)" PauseExecWhenProcTerminates="yes" IsGUIProgram="no" IsEnabled="yes"/>
42+
<BuildSystem Name="CMake"/>
43+
<Environment EnvVarSetName="&lt;Use Defaults&gt;" DbgSetName="&lt;Use Defaults&gt;">
44+
<![CDATA[]]>
45+
</Environment>
46+
<Debugger IsRemote="no" RemoteHostName="" RemoteHostPort="" DebuggerPath="" IsExtended="no">
47+
<DebuggerSearchPaths/>
48+
<PostConnectCommands/>
49+
<StartupCommands/>
50+
</Debugger>
51+
<PreBuild/>
52+
<PostBuild/>
53+
<CustomBuild Enabled="no">
54+
<RebuildCommand/>
55+
<CleanCommand/>
56+
<BuildCommand/>
57+
<PreprocessFileCommand/>
58+
<SingleFileCommand/>
59+
<MakefileGenerationCommand/>
60+
<ThirdPartyToolName>None</ThirdPartyToolName>
61+
<WorkingDirectory/>
62+
</CustomBuild>
63+
<AdditionalRules>
64+
<CustomPostBuild/>
65+
<CustomPreBuild/>
66+
</AdditionalRules>
67+
<Completion EnableCpp11="yes" EnableCpp14="no">
68+
<ClangCmpFlagsC/>
69+
<ClangCmpFlags/>
70+
<ClangPP/>
71+
<SearchPaths/>
72+
</Completion>
73+
</Configuration>
74+
<Configuration Name="Release" CompilerType="MinGW ( gcc.exe (tdm64-1) 5.1.0 )" DebuggerType="GNU gdb debugger" Type="Executable" BuildCmpWithGlobalSettings="append" BuildLnkWithGlobalSettings="append" BuildResWithGlobalSettings="append">
75+
<Compiler Options="-O2" C_Options="-O2" Assembler="" Required="yes" PreCompiledHeader="" PCHInCommandLine="no" PCHFlags="" PCHFlagsPolicy="0">
76+
<Preprocessor Value="NDEBUG"/>
77+
</Compiler>
78+
<Linker Options="-O2" Required="yes"/>
79+
<ResourceCompiler Options="" Required="no"/>
80+
<General OutputFile="" IntermediateDirectory="$(BUILD_ROOT)/$(WorkspaceName)/$(ProjectName)-$(WorkspaceConfiguration)" Command="$(WorkspacePath)\cmake-build-$(WorkspaceConfiguration)\output\$(ProjectName)" CommandArguments="" UseSeparateDebugArgs="no" DebugArguments="" WorkingDirectory="$(IntermediateDirectory)" PauseExecWhenProcTerminates="yes" IsGUIProgram="no" IsEnabled="yes"/>
81+
<BuildSystem Name="CMake"/>
82+
<Environment EnvVarSetName="&lt;Use Defaults&gt;" DbgSetName="&lt;Use Defaults&gt;">
83+
<![CDATA[]]>
84+
</Environment>
85+
<Debugger IsRemote="no" RemoteHostName="" RemoteHostPort="" DebuggerPath="" IsExtended="no">
86+
<DebuggerSearchPaths/>
87+
<PostConnectCommands/>
88+
<StartupCommands/>
89+
</Debugger>
90+
<PreBuild/>
91+
<PostBuild/>
92+
<CustomBuild Enabled="no">
93+
<RebuildCommand/>
94+
<CleanCommand/>
95+
<BuildCommand/>
96+
<PreprocessFileCommand/>
97+
<SingleFileCommand/>
98+
<MakefileGenerationCommand/>
99+
<ThirdPartyToolName>None</ThirdPartyToolName>
100+
<WorkingDirectory/>
101+
</CustomBuild>
102+
<AdditionalRules>
103+
<CustomPostBuild/>
104+
<CustomPreBuild/>
105+
</AdditionalRules>
106+
<Completion EnableCpp11="yes" EnableCpp14="no">
107+
<ClangCmpFlagsC/>
108+
<ClangCmpFlags/>
109+
<ClangPP/>
110+
<SearchPaths/>
111+
</Completion>
112+
</Configuration>
113+
</Settings>
114+
</CodeLite_Project>

0 commit comments

Comments
 (0)