Skip to content

Commit 02e2124

Browse files
Experimental (#18)
+ documented most methods and classes ^ hid logger code under HFSM_IF_LOG_INTERFACE ^ hid utility code under HFSM_ENABLE_UTILITY_THEORY * fixed linking issues with feature configuration macros ~ renamed 'HFSM_*' macros to 'HFSM2_*'
1 parent e413f2d commit 02e2124

File tree

95 files changed

+5563
-3530
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+5563
-3530
lines changed

examples/advanced_event_handling/main.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
// changed to Target
2323

2424
// optional: enable FSM structure report in debugger
25-
#define HFSM_ENABLE_STRUCTURE_REPORT
25+
#define HFSM2_ENABLE_STRUCTURE_REPORT
2626
#include <hfsm2/machine.hpp>
2727

2828
#include <iostream>
@@ -38,6 +38,29 @@ using TransitionEvent = char;
3838

3939
//------------------------------------------------------------------------------
4040

41+
#if 0
42+
43+
// states need to be forward declared to be used in FSM struct declaration
44+
class Reactive;
45+
class NonHandler;
46+
class ConcreteHandler;
47+
class TemplateHandler;
48+
class EnableIfHandler;
49+
class Target;
50+
51+
using FSM = M::PeerRoot<
52+
M::Orthogonal<Reactive,
53+
NonHandler,
54+
ConcreteHandler,
55+
TemplateHandler,
56+
EnableIfHandler
57+
>,
58+
Target
59+
>;
60+
61+
#else
62+
63+
// alternatively, some macro magic can be invoked to simplify FSM structure declaration
4164
#define S(s) struct s
4265

4366
using FSM = M::PeerRoot<
@@ -52,6 +75,8 @@ using FSM = M::PeerRoot<
5275

5376
#undef S
5477

78+
#endif
79+
5580
//------------------------------------------------------------------------------
5681

5782
static_assert(FSM::regionId<Reactive>() == 1, "");

examples/basic_audio_player/main.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
// An HFSM2 port of https://gist.github.com/martinmoene/797b1923f9c6c1ae355bb2d6870be25e
55
// by Martin Moene (see https://twitter.com/MartinMoene/status/1118453128834232320)
66

7-
#define HFSM_ENABLE_LOG_INTERFACE
8-
#define HFSM_ENABLE_STRUCTURE_REPORT
7+
#define HFSM2_ENABLE_LOG_INTERFACE
8+
#define HFSM2_ENABLE_STRUCTURE_REPORT
99
#include <hfsm2/machine.hpp>
1010

1111
#include <assert.h>
@@ -25,15 +25,34 @@ struct Stop {};
2525
using Context = std::string;
2626
using M = hfsm2::MachineT<hfsm2::Config::ContextT<Context>>;
2727

28-
// fsm structure
28+
#if 0
29+
30+
// states need to be forward declared to be used in FSM struct declaration
31+
struct Idle;
32+
struct Playing;
33+
struct Paused;
34+
35+
using FSM = M::PeerRoot<
36+
Idle,
37+
Playing,
38+
Paused
39+
>;
40+
41+
#else
42+
43+
// alternatively, some macro magic can be invoked to simplify FSM structure declaration
2944
#define S(s) struct s
45+
3046
using FSM = M::PeerRoot<
3147
S(Idle),
3248
S(Playing),
3349
S(Paused)
3450
>;
51+
3552
#undef S
3653

54+
#endif
55+
3756
//------------------------------------------------------------------------------
3857

3958
// double-check state ids for Logger::stateName()

examples/basic_traffic_light/main.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
// Off
3434

3535
// optional: enable FSM structure report in debugger
36-
#define HFSM_ENABLE_STRUCTURE_REPORT
36+
#define HFSM2_ENABLE_STRUCTURE_REPORT
3737
#include <hfsm2/machine.hpp>
3838

3939
#include <iostream>
@@ -48,6 +48,31 @@ struct Context {
4848
// convenience typedef
4949
using M = hfsm2::MachineT<hfsm2::Config::ContextT<Context>>;
5050

51+
#if 0
52+
53+
// states need to be forward declared to be used in FSM struct declaration
54+
struct On;
55+
struct Red;
56+
struct YellowDownwards;
57+
struct YellowUpwards;
58+
struct Green;
59+
struct Off;
60+
61+
using FSM = M::PeerRoot<
62+
// sub-machine ..
63+
M::Composite<On,
64+
// .. with 4 sub-states
65+
Red,
66+
YellowDownwards,
67+
YellowUpwards,
68+
Green
69+
>,
70+
Off
71+
>;
72+
73+
#else
74+
75+
// alternatively, some macro magic can be invoked to simplify FSM structure declaration
5176
#define S(s) struct s
5277

5378
// state machine structure
@@ -65,6 +90,8 @@ using FSM = M::PeerRoot<
6590

6691
#undef S
6792

93+
#endif
94+
6895
//------------------------------------------------------------------------------
6996

7097
static_assert(FSM::regionId<On>() == 1, "");

examples/calculator/main.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
#include <iostream>
88
#include <conio.h>
9-
#include <cmath>
109

1110

1211
#define DEBUG 1

examples/debug_logger_interface/main.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
// ---------- done! ---------
4040

4141
// enable logger functionality
42-
#define HFSM_ENABLE_LOG_INTERFACE
42+
#define HFSM2_ENABLE_LOG_INTERFACE
4343
#include <hfsm2/machine.hpp>
4444

4545
#include <iostream>
@@ -49,6 +49,21 @@
4949
// convenience typedef
5050
using M = hfsm2::Machine;
5151

52+
#if 0
53+
54+
// states need to be forward declared to be used in FSM struct declaration
55+
struct Top;
56+
struct From;
57+
struct To;
58+
59+
using FSM = M::Root<Top,
60+
From,
61+
To
62+
>;
63+
64+
#else
65+
66+
// alternatively, some macro magic can be invoked to simplify FSM structure declaration
5267
#define S(s) struct s
5368

5469
using FSM = M::Root<S(Top),
@@ -58,6 +73,8 @@ using FSM = M::Root<S(Top),
5873

5974
#undef S
6075

76+
#endif
77+
6178
//------------------------------------------------------------------------------
6279

6380
static_assert(FSM::regionId<Top>() == 0, "");

examples/snippets/docs_serialization.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
// HFSM2 (hierarchical state machine for games and interactive applications)
22
// Created by Andrew Gresyk
33

4-
#define HFSM_ENABLE_SERIALIZATION
4+
#define HFSM2_ENABLE_SERIALIZATION
55

6-
//#include <hfsm2/machine.hpp>
7-
#include <hfsm2/machine_dev.hpp>
6+
#include <hfsm2/machine.hpp>
87

98
#include <catch2/catch.hpp>
109
#undef assert

examples/snippets/wiki_transitions-within-hierarchy.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// HFSM2 (hierarchical state machine for games and interactive applications)
22
// Created by Andrew Gresyk
33

4+
#define HFSM2_ENABLE_UTILITY_THEORY
45
#include <hfsm2/machine.hpp>
56

67
#include <catch2/catch.hpp>

examples/snippets/wiki_utility-theory.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// HFSM2 (hierarchical state machine for games and interactive applications)
22
// Created by Andrew Gresyk
33

4-
#define HFSM_ENABLE_LOG_INTERFACE
5-
#define HFSM_ENABLE_VERBOSE_DEBUG_LOG
4+
#define HFSM2_ENABLE_LOG_INTERFACE
5+
#define HFSM2_ENABLE_UTILITY_THEORY
6+
#define HFSM2_ENABLE_VERBOSE_DEBUG_LOG
67
#include <hfsm2/machine.hpp>
78

89
#include <catch2/catch.hpp>
@@ -38,7 +39,7 @@ using Events = std::vector<Event>;
3839
//------------------------------------------------------------------------------
3940

4041
struct Logger
41-
: hfsm2::LoggerInterface // requires HFSM_ENABLE_LOG_INTERFACE defined
42+
: hfsm2::LoggerInterface // requires HFSM2_ENABLE_LOG_INTERFACE defined
4243
{
4344
void recordMethod(Context& /*context*/,
4445
const StateID state,
@@ -122,7 +123,7 @@ using FSM = M::PeerRoot<
122123
>;
123124

124125
//------------------------------------------------------------------------------
125-
// with HFSM_ENABLE_VERBOSE_DEBUG_LOG inherited state methods will also be logged
126+
// with HFSM2_ENABLE_VERBOSE_DEBUG_LOG inherited state methods will also be logged
126127

127128
struct Origin : FSM::State {};
128129

include/hfsm2/detail/debug/logger_interface.hpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
#pragma once
2-
31
namespace hfsm2 {
42

5-
#if defined HFSM_ENABLE_LOG_INTERFACE || defined HFSM_ENABLE_VERBOSE_DEBUG_LOG
3+
#ifdef HFSM2_ENABLE_LOG_INTERFACE
64

75
////////////////////////////////////////////////////////////////////////////////
86

9-
template <typename TContext = EmptyContext,
10-
typename TUtilty = float>
7+
template <typename TContext = EmptyContext
8+
9+
#ifdef HFSM2_ENABLE_UTILITY_THEORY
10+
, typename TUtilty = float
11+
#endif
12+
13+
>
1114
struct LoggerInterfaceT {
1215
using Context = TContext;
16+
17+
#ifdef HFSM2_ENABLE_UTILITY_THEORY
1318
using Utilty = TUtilty;
19+
#endif
1420

1521
using Method = ::hfsm2::Method;
1622
using StateID = ::hfsm2::StateID;
@@ -44,6 +50,8 @@ struct LoggerInterfaceT {
4450
const StateID /*origin*/)
4551
{}
4652

53+
#ifdef HFSM2_ENABLE_UTILITY_THEORY
54+
4755
virtual void recordUtilityResolution(Context& /*context*/,
4856
const StateID /*head*/,
4957
const StateID /*prong*/,
@@ -55,6 +63,8 @@ struct LoggerInterfaceT {
5563
const StateID /*prong*/,
5664
const Utilty /*utilty*/)
5765
{}
66+
67+
#endif
5868
};
5969

6070
////////////////////////////////////////////////////////////////////////////////

include/hfsm2/detail/debug/shared.hpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
#pragma once
2-
31
////////////////////////////////////////////////////////////////////////////////
42

53
namespace hfsm2 {
64

75
enum class Method : uint8_t {
86
NONE,
97

8+
#ifdef HFSM2_ENABLE_UTILITY_THEORY
109
RANK,
1110
UTILITY,
11+
#endif
12+
1213
ENTRY_GUARD,
1314
CONSTRUCT,
1415
ENTER,
@@ -28,8 +29,12 @@ enum class TransitionType : uint8_t {
2829
CHANGE,
2930
RESTART,
3031
RESUME,
32+
33+
#ifdef HFSM2_ENABLE_UTILITY_THEORY
3134
UTILIZE,
3235
RANDOMIZE,
36+
#endif
37+
3338
SCHEDULE,
3439

3540
COUNT
@@ -74,8 +79,12 @@ static inline
7479
const char*
7580
methodName(const Method method) {
7681
switch (method) {
82+
83+
#ifdef HFSM2_ENABLE_UTILITY_THEORY
7784
case Method::RANK: return "rank";
7885
case Method::UTILITY: return "utility";
86+
#endif
87+
7988
case Method::ENTRY_GUARD: return "entryGuard";
8089
case Method::ENTER: return "enter";
8190
case Method::CONSTRUCT: return "construct";
@@ -89,7 +98,7 @@ methodName(const Method method) {
8998
case Method::PLAN_FAILED: return "planFailed";
9099

91100
default:
92-
HFSM_BREAK();
101+
HFSM2_BREAK();
93102
return nullptr;
94103
}
95104
}
@@ -103,12 +112,16 @@ transitionName(const TransitionType transitionType) {
103112
case TransitionType::CHANGE: return "changeTo";
104113
case TransitionType::RESTART: return "restart";
105114
case TransitionType::RESUME: return "resume";
115+
116+
#ifdef HFSM2_ENABLE_UTILITY_THEORY
106117
case TransitionType::UTILIZE: return "utilize";
107118
case TransitionType::RANDOMIZE: return "randomize";
119+
#endif
120+
108121
case TransitionType::SCHEDULE: return "schedule";
109122

110123
default:
111-
HFSM_BREAK();
124+
HFSM2_BREAK();
112125
return nullptr;
113126
}
114127
}

0 commit comments

Comments
 (0)