Motorcortex Core  version: 2.7.6
sm_event.h
1 /*
2  * Developer : Alexey Zakharov (alexey.zakharov@vectioneer.com)
3  * All rights reserved. Copyright (c) 2014-2016 VECTIONEER.
4  */
5 
6 #ifndef STATE_MACHINE_SM_EVENT_H
7 #define STATE_MACHINE_SM_EVENT_H
8 
9 #include "utl_log.h"
10 #include <cstdint>
11 
12 namespace mcx::state_machine {
13 
14 enum EventPriority {
15  HIGH_PRIORITY_EVENT = 0,
16  LOW_PRIORITY_EVENT = 1,
17 };
18 
19 enum EventStatus {
20  EVENT_NONE = -1,
21  EVENT_DONE = 0,
22  EVENT_DELAY = 1,
23  EVENT_REPEAT = 2,
24  EVENT_TERMINATE = 3,
25  EVENT_REPEAT_NO_TERMINATE = 4
26 };
27 
28 template <class R>
29 struct DR {
30  typedef R T;
31 };
32 
33 template <class R>
34 struct DR<R&> {
35  typedef R T;
36 };
37 
38 template <class R>
39 struct DR<const R&> {
40  typedef R T;
41 };
42 
43 template <class STATE>
44 class Event {
45  typedef EventStatus (STATE::*Signature)();
46 
47 public:
48  Event() : onceExecutedFlag_(false), timeoutSec_(0), termFunc_(nullptr) {}
49 
50  virtual ~Event() = default;
51 
52  virtual EventStatus dispatchEvent(STATE* state) = 0;
53 
54  void setTerminateFunc(Signature term_func) { termFunc_ = term_func; }
55 
56  const Signature& getTerminateFunc() { return termFunc_; }
57 
58  void setExecutedOnceFlag(bool once_executed_flag) { onceExecutedFlag_ = once_executed_flag; }
59 
60  bool getExecutedOnceFlag() { return onceExecutedFlag_; }
61 
62  void setTimeoutSec(double timeout_sec) { timeoutSec_ = timeout_sec; }
63 
64  double getTimeoutSec() { return timeoutSec_; }
65 
66  virtual bool compare(const Event* cmp) { return false; }
67 
68 protected:
69  bool onceExecutedFlag_;
70  double timeoutSec_;
71  Signature termFunc_;
72 };
73 
74 template <class STATE>
75 class _Event0 : public Event<STATE> {
76  typedef EventStatus (STATE::*Signature)();
77 
78 public:
79  _Event0(Signature event_func) : eventFunc_(event_func) {}
80 
81  virtual ~_Event0() = default;
82 
83  EventStatus dispatchEvent(STATE* state) { return (state->*eventFunc_)(); }
84 
85  bool compare(const Event<STATE>* cmp) { return (static_cast<const _Event0<STATE>*>(cmp))->eventFunc_ == eventFunc_; }
86 
87 protected:
88  Signature eventFunc_;
89 };
90 
91 template <class STATE, class P1>
92 class _Event1 : public Event<STATE> {
93  typedef EventStatus (STATE::*Signature)(P1);
94 
95 public:
96  _Event1(Signature event_func, const typename DR<P1>::T& param1) : eventFunc_(event_func), param1_(param1) {}
97 
98  EventStatus dispatchEvent(STATE* state) { return (state->*eventFunc_)(param1_); }
99 
100  bool compare(const Event<STATE>* cmp) {
101  return (static_cast<const _Event1<STATE, P1>*>(cmp))->eventFunc_ == eventFunc_;
102  }
103 
104 protected:
105  Signature eventFunc_;
106  const typename DR<P1>::T param1_;
107 };
108 
109 template <class STATE, class P1, class P2>
110 class _Event2 : public Event<STATE> {
111  typedef EventStatus (STATE::*Signature)(P1, P2);
112 
113 public:
114  _Event2(Signature event_func, const typename DR<P1>::T& param1, const typename DR<P2>::T& param2)
115  : eventFunc_(event_func), param1_(param1), param2_(param2) {}
116 
117  EventStatus dispatchEvent(STATE* state) { return (state->*eventFunc_)(param1_, param2_); }
118 
119  bool compare(const Event<STATE>* cmp) {
120  return (static_cast<const _Event2<STATE, P1, P2>*>(cmp))->eventFunc_ == eventFunc_;
121  }
122 
123 protected:
124  Signature eventFunc_;
125  const typename DR<P1>::T param1_;
126  const typename DR<P2>::T param2_;
127 };
128 
129 template <class STATE, class P1, class P2, class P3>
130 class _Event3 : public Event<STATE> {
131  typedef EventStatus (STATE::*Signature)(P1, P2, P3);
132 
133 public:
134  _Event3(Signature event_func, const typename DR<P1>::T& param1, const typename DR<P2>::T& param2,
135  const typename DR<P3>::T& param3)
136  : eventFunc_(event_func), param1_(param1), param2_(param2), param3_(param3) {}
137 
138  EventStatus dispatchEvent(STATE* state) { return (state->*eventFunc_)(param1_, param2_, param3_); }
139 
140  bool compare(const Event<STATE>* cmp) {
141  return (static_cast<const _Event3<STATE, P1, P2, P3>*>(cmp))->eventFunc_ == eventFunc_;
142  }
143 
144 protected:
145  Signature eventFunc_;
146  const typename DR<P1>::T param1_;
147  const typename DR<P2>::T param2_;
148  const typename DR<P3>::T param3_;
149 };
150 
151 template <class STATE>
152 class EventBase {
153 protected:
154  Event<STATE>* event_;
155 
156 public:
157  virtual ~EventBase() = default;
158 
159  Event<STATE>* getEvent() const { return event_; }
160 };
161 
162 template <class STATE>
163 class Event0 : public EventBase<STATE> {
164 public:
165  Event0(EventStatus (STATE::*event)()) { this->event_ = new _Event0<STATE>(event); }
166 };
167 
168 template <class STATE>
169 class Event1 : public EventBase<STATE> {
170 public:
171  template <class P1>
172  Event1(EventStatus (STATE::*event)(P1), const typename DR<P1>::T& p1 = {}) {
173  this->event_ = new _Event1<STATE, P1>(event, p1);
174  }
175 };
176 
177 template <class STATE>
178 class Event2 : public EventBase<STATE> {
179 public:
180  template <class P1, class P2>
181  Event2(EventStatus (STATE::*event)(P1, P2), const typename DR<P1>::T& p1 = {}, const typename DR<P2>::T& p2 = {}) {
182  this->event_ = new _Event2<STATE, P1, P2>(event, p1, p2);
183  }
184 };
185 
186 template <class STATE>
187 class Event3 : public EventBase<STATE> {
188 public:
189  template <class P1, class P2, class P3>
190  Event3(EventStatus (STATE::*event)(P1, P2, P3), const typename DR<P1>::T& p1 = {}, const typename DR<P2>::T& p2 = {},
191  const typename DR<P3>::T& p3 = {}) {
192  this->event_ = new _Event3<STATE, P1, P2, P3>(event, p1, p2, p3);
193  }
194 };
195 
196 } // namespace mcx::state_machine
197 
198 #endif // STATE_MACHINE_SM_EVENT_H
mcx::state_machine::_Event0
Definition: sm_event.h:75
mcx::state_machine::Event1
Definition: sm_event.h:169
mcx::state_machine::EventBase
Definition: sm_event.h:152
mcx::state_machine::Event
Definition: sm_event.h:44
mcx::state_machine::_Event3
Definition: sm_event.h:130
mcx::state_machine::_Event1
Definition: sm_event.h:92
mcx::state_machine::Event0
Definition: sm_event.h:163
mcx::state_machine::_Event2
Definition: sm_event.h:110
mcx::state_machine::DR
Definition: sm_event.h:29
mcx::state_machine::Event3
Definition: sm_event.h:187
mcx::state_machine::Event2
Definition: sm_event.h:178