ADTF  3.18.2
level_machine.h
Go to the documentation of this file.
1 
8 #ifndef _ADTF_UCOM_ANT_CLASSES_LEVEL_MASCHINE_HEADER_
9 #define _ADTF_UCOM_ANT_CLASSES_LEVEL_MASCHINE_HEADER_
10 
11 namespace adtf
12 {
13 namespace ucom
14 {
15 namespace ant
16 {
17 
24 template <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>
86  struct dispatcher
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 
146 using 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.
#define RETURN_IF_FAILED(s)
Return if expression is failed, which requires the calling function's return type to be tResult.
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
Generator template to create a Basic Level Machine implmentation.
Definition: level_machine.h:26
LevelType GetLevel() const
Returns the current Level of the levelMachine.
Definition: level_machine.h:74
tResult(Subclass::* tChangeMethod)()
Defintion of function type called within the handler implementation.
Definition: level_machine.h:46
LevelType m_eLevel
The current Level.
Definition: level_machine.h:29
tResult ChangeLevel(LevelType eLevel)
Change Level raise or decrease the Level level by level and call the given hendler methods.
virtual ~level_machine()=default
DTOR.
tDirection
Direction of entering a new Level.
Definition: level_machine.h:35
@ Inc
entering a Level by increasing level_machine::m_eLevel
Definition: level_machine.h:37
@ Dec
entering a Level by decreasing level_machine::m_eLevel
Definition: level_machine.h:39
Namespace for entire ADTF SDK.
Internal dispatcher to find handler for level chnage.
Definition: level_machine.h:87
Definition of handler method.
Definition: level_machine.h:56
static const tDirection eDirection
entering direction for the eLevel of the handler
Definition: level_machine.h:60
static tChangeMethod GetMethod()
returns the method to call on level change
Definition: level_machine.h:64
static const LevelType eLevel
level of the handler which is entered
Definition: level_machine.h:58