ADTF
Loading...
Searching...
No Matches
level_machine.h
Go to the documentation of this file.
1
7
8#ifndef _ADTF_UCOM_ANT_CLASSES_LEVEL_MASCHINE_HEADER_
9#define _ADTF_UCOM_ANT_CLASSES_LEVEL_MASCHINE_HEADER_
10
11namespace adtf
12{
13namespace ucom
14{
15namespace ant
16{
17
24template <typename Subclass, typename LevelType, LevelType START_LEVEL>
26{
27 private:
29 LevelType m_eLevel = START_LEVEL;
30
31 protected:
32
34 enum tDirection : uint8_t
35 {
37 Inc = 0,
39 Dec = 1
40 };
41
43 virtual ~level_machine() = default;
44
46 typedef tResult (Subclass::*tChangeMethod)();
54 template<LevelType level, tDirection direction, tChangeMethod method>
55 struct handler
56 {
58 static const LevelType eLevel = level;
60 static const tDirection eDirection = direction;
61
65 {
66 return method;
67 }
68 };
69
74 LevelType GetLevel() const
75 {
76 return m_eLevel;
77 }
78
79 protected:
80
85 template<typename ...InnerHandlers>
87 {
88 static tResult DispatchMethod(Subclass* /* pObj */, LevelType /* eLevel */, tDirection /* eDir */)
89 {
91 }
92 };
93
98 template<typename Handler, typename ...InnerHandlers>
99 struct dispatcher<Handler, InnerHandlers...>
100 {
101 static tResult DispatchMethod(Subclass* pObj, LevelType eLevel, tDirection eDir)
102 {
103 if (Handler::eLevel == eLevel && Handler::eDirection == eDir)
104 {
105 RETURN_IF_FAILED((pObj->*Handler::GetMethod())());
106 }
107
108 return dispatcher<InnerHandlers...>::DispatchMethod(pObj, eLevel, eDir);
109 }
110 };
111
112 public:
113
120 template<typename ...InnerHandlers>
121 tResult ChangeLevel(LevelType eLevel)
122 {
123 if (m_eLevel < eLevel)
124 {
125 for (int64_t nNext = static_cast<int64_t>(m_eLevel) + 1; nNext <= static_cast<int64_t>(eLevel); ++nNext)
126 {
127 RETURN_IF_FAILED(dispatcher<InnerHandlers...>::DispatchMethod(static_cast<Subclass*>(this), static_cast<LevelType>(nNext), Inc));
128 m_eLevel = static_cast<LevelType>(nNext);
129 }
130 }
131 else if (m_eLevel > eLevel)
132 {
133 for (int64_t nNext = static_cast<int64_t>(m_eLevel) - 1; nNext >= static_cast<int64_t>(eLevel); --nNext)
134 {
135 RETURN_IF_FAILED(dispatcher<InnerHandlers...>::DispatchMethod(static_cast<Subclass*>(this), static_cast<LevelType>(nNext), Dec));
136 m_eLevel = static_cast<LevelType>(nNext);
137 }
138 }
139
141 }
142};
143
144}
145
146using ant::level_machine;
147
148}
149
150}
151
152#endif
153
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
Definition result.h:736
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
Definition result.h:29
Definition level_machine.h:26
tResult ChangeLevel(LevelType eLevel)
Definition level_machine.h:121
LevelType GetLevel() const
Definition level_machine.h:74
tResult(Subclass::* tChangeMethod)()
Defintion of function type called within the handler implementation.
Definition level_machine.h:46
tDirection
Direction of entering a new Level.
Definition level_machine.h:35
@ Dec
entering a Level by decreasing level_machine::m_eLevel
Definition level_machine.h:39
@ Inc
entering a Level by increasing level_machine::m_eLevel
Definition level_machine.h:37
virtual ~level_machine()=default
DTOR.
Namespace for all functionality of the ADTF UCOM SDK provided since v3.0.
Definition test_runtime.h:14
Namespace for the ADTF uCOM SDK.
Definition adtf_system.h:324
Namespace for all functionality provided by ADTF and its SDKs.
Definition adtf_client_connector.h:14
Definition level_machine.h:87
Definition level_machine.h:56
static const LevelType eLevel
level of the handler which is entered
Definition level_machine.h:58
static tChangeMethod GetMethod()
Definition level_machine.h:64
static const tDirection eDirection
entering direction for the eLevel of the handler
Definition level_machine.h:60