OpenRTM-aist 2.0.2
Loading...
Searching...
No Matches
StaticFSM.h
Go to the documentation of this file.
1// -*- C++ -*-
17#ifndef RTC_STATICFSM_H
18#define RTC_STATICFSM_H
19
20#include <rtm/RTObject.h>
21#include <rtm/Macho.h>
22#include <rtm/RingBuffer.h>
23#include <rtm/EventBase.h>
24
70#define FSM_TOPSTATE(TOP) \
71 struct TOP \
72 : public ::RTC::Link< TOP, ::Macho::TopBase< TOP > >
73
77#define FSM_SUBSTATE(STATE, SUPERSTATE) \
78 struct STATE \
79 : public ::RTC::Link< STATE, SUPERSTATE >
80
84#define FSM_STATE(S) \
85 public: \
86 using SELF = S; \
87 S(::Macho::_StateInstance & instance) \
88 : ::RTC::Link<S, SUPER>(instance) \
89 { \
90 using MustDeriveFromLink = ::CheckSameType<::RTC::Link<S, SUPER>, \
91 LINK>::Check; \
92 static_assert(static_cast<MustDeriveFromLink*>(nullptr)==nullptr, \
93 "dummy assert for suppress warning"); \
94 } \
95 ~S() override {} \
96 static const char * _state_name() { return #S; } \
97 Box & box() { return *static_cast<Box *>(_box()); } \
98 friend class ::_VS8_Bug_101615
99
100#define FSM_INIT_VALUE(S) \
101 template<> \
102 const ::Macho::ID Macho::StateID<S>::value = ::Macho::Machine<typename S::TOP>::theStateCount++;
103
104namespace RTC
105{
114 template<class TOP>
115 class Machine
116 : public Macho::Machine<TOP>
117 {
118 public:
120 : Macho::Machine<TOP>(), rtComponent(comp)
121 {
122 }
123 ~Machine() override = default;
125 {
126 return m_buffer;
127 }
128 virtual void run_event()
129 {
130 while (m_buffer.readable() > 0)
131 {
132 EventBase* ebt = m_buffer.get();
133 (*ebt)();
134 m_buffer.advanceRptr();
135 delete ebt;
136 }
137 }
138
139 private:
140 Machine(const Machine<TOP> & other) = delete;
141 Machine<TOP> & operator=(const Machine<TOP> & other) = delete;
142
143 template<class C, class P>
144 friend class Link;
145
146#ifdef MACHO_SNAPSHOTS
147 friend class Macho::Snapshot<TOP>;
148#endif
149 template<class T> friend class Macho::StateID;
150
151 RTObject_impl* rtComponent;
152 RingBuffer<EventBase*> m_buffer;
153 };
154
166 template<class C, class P>
167 class Link
168 : public Macho::Link<C, P>
169 {
170 protected:
172 : Macho::Link<C, P>(instance), rtComponent(nullptr)
173 {
174 }
175 ~Link() override = default;
176
177 void setrtc()
178 {
179 if (rtComponent != nullptr) { return; }
180 const RTC::Machine<typename P::TOP>* machine =
181 dynamic_cast<const RTC::Machine<typename P::TOP>*>(&P::machine());
182 if (machine != nullptr) { rtComponent = machine->rtComponent; }
183 }
184 public:
186
187 void entry() override
188 {
189 setrtc();
190 if (rtComponent == nullptr)
191 {
192 onEntry();
193 }
194 else
195 {
196 rtComponent->postOnFsmStateChange(C::_state_name(), RTC::RTC_OK);
197 rtComponent->preOnFsmEntry(C::_state_name());
198 rtComponent->postOnFsmEntry(C::_state_name(), onEntry());
199 }
200 }
201 void init() override
202 {
203 setrtc();
204 if (rtComponent == nullptr)
205 {
206 onInit();
207 }
208 else
209 {
210 rtComponent->preOnFsmInit(C::_state_name());
211 rtComponent->postOnFsmInit(C::_state_name(), onInit());
212 }
213 }
214 void exit() override
215 {
216 setrtc();
217 if (rtComponent == nullptr)
218 {
219 onExit();
220 }
221 else
222 {
223 rtComponent->preOnFsmExit(C::_state_name());
224 rtComponent->postOnFsmExit(C::_state_name(), onExit());
225 rtComponent->preOnFsmStateChange(C::_state_name());
226 }
227 }
228#if defined(__clang__)
229#if defined(_WIN32) || defined(_WIN64)
230#pragma clang diagnostic push
231#pragma clang diagnostic ignored "-Wsuggest-override"
232#endif
233#pragma clang diagnostic push
234#pragma clang diagnostic ignored "-Winconsistent-missing-override"
235#endif
236#if defined(__GNUC__) && (__GNUC__ >= 5) && !defined(__clang__)
237#pragma GCC diagnostic push
238#pragma GCC diagnostic ignored "-Wsuggest-override"
239#endif
240 // We can't use 'override' insetd of 'virtual', becase these
241 // methods are used as TOP LEVEL SUPPER CLASS and SUB CLASS.
242 virtual RTC::ReturnCode_t onEntry() { return RTC::RTC_OK; }
243 virtual RTC::ReturnCode_t onInit() { return RTC::RTC_OK; }
244 virtual RTC::ReturnCode_t onExit() { return RTC::RTC_OK; }
245#if defined(__GNUC__) && (__GNUC__ >= 5) && !defined(__clang__)
246#pragma GCC diagnostic pop
247#endif
248#if defined(__clang__)
249#pragma clang diagnostic pop
250#if defined(_WIN32) || defined(_WIN64)
251#pragma clang diagnostic pop
252#endif
253#endif
254
256 };
257} // namespace RTC
258#endif // RTC_STATICFSM_H
RT component base class.
Defautl Buffer class.
Definition Macho.h:1648
Machine()
Definition Macho.h:1668
Definition Macho.h:618
Definition Macho.h:629
Definition EventBase.h:41
~Machine() override=default
Machine(RTC::RTObject_impl *comp)
Definition StaticFSM.h:119
virtual RingBuffer< EventBase * > & getBuffer()
Definition StaticFSM.h:124
virtual void run_event()
Definition StaticFSM.h:128
RT-Component class.
Definition RTObject.h:93
void preOnFsmEntry(const char *state)
Definition RTObject.h:4974
void postOnFsmExit(const char *state, ReturnCode_t ret)
Definition RTObject.h:5005
void preOnFsmExit(const char *state)
Definition RTObject.h:4982
void postOnFsmInit(const char *state, ReturnCode_t ret)
Definition RTObject.h:4993
void postOnFsmEntry(const char *state, ReturnCode_t ret)
Definition RTObject.h:4997
void preOnFsmStateChange(const char *state)
Definition RTObject.h:4986
void postOnFsmStateChange(const char *state, ReturnCode_t ret)
Definition RTObject.h:5009
void preOnFsmInit(const char *state)
Definition RTObject.h:4970
Ring buffer implementation class.
Definition RingBuffer.h:90
Definition Macho.h:289
RT-Component.