OpenRTM-aist  2.1.0
EventPort.h
Go to the documentation of this file.
1 // -*- C++ -*-
18 #ifndef RTC_EVENTINPORT_H
19 #define RTC_EVENTINPORT_H
20 
21 #include <string>
22 #include <vector>
23 #include <iostream>
24 
25 #include <coil/OS.h>
26 #include <mutex>
27 
28 #include <rtm/RTC.h>
29 #include <rtm/Typename.h>
30 #include <rtm/InPortBase.h>
31 #include <rtm/CdrBufferBase.h>
32 #include <rtm/PortCallback.h>
33 #include <rtm/InPortConnector.h>
34 #include <rtm/Timestamp.h>
35 #include <rtm/StaticFSM.h>
36 #include <rtm/RingBuffer.h>
37 #include <rtm/EventBase.h>
38 
39 namespace RTC
40 {
41 
42 
43  template <class FSM, class TOP, class R>
46  {
47  USE_CONNLISTENER_STATUS;
48  public:
49  EventBinder0(FSM& fsm,
50  const char* event_name,
51  R (TOP::*handler)(),
52  RingBuffer<EventBase*> &buffer)
53  : m_fsm(fsm), m_eventName(event_name), m_handler(handler), m_buffer(buffer) {}
54 
55  ~EventBinder0() override = default;
56 
57  ReturnCode operator()(ConnectorInfo& info,
58  ByteData& /*data*/, const std::string& /*marshalingtype*/) override
59  {
60  if (info.properties["fsm_event_name"] == m_eventName ||
61  info.name == m_eventName)
62  {
63  m_buffer.write(new Event0(this));
64  return NO_CHANGE;
65  }
66  return NO_CHANGE;
67  }
68 
69  void run() override
70  {
71 
72  m_fsm.dispatch(Macho::Event(m_handler));
73  }
74 
75  FSM& m_fsm;
76  std::string m_eventName;
77  R (TOP::*m_handler)();
79 
80  };
81 
82  template <class FSM, class TOP, class R, class P0>
85  {
86  USE_CONNLISTENER_STATUS;
87  public:
88  EventBinder1(FSM& fsm,
89  const char* event_name,
90  R (TOP::*handler)(P0),
91  RingBuffer<EventBase*> &buffer)
92  : m_fsm(fsm), m_eventName(event_name), m_handler(handler), m_buffer(buffer) {}
93 
94  ~EventBinder1() override = default;
95 
96  ReturnCode operator()(ConnectorInfo& info, P0& data) override
97  {
98  if (info.properties["fsm_event_name"] == m_eventName ||
99  info.name == m_eventName)
100  {
101  m_buffer.write(new Event1<P0>(this, data));
102  return NO_CHANGE;
103  }
104  return NO_CHANGE;
105  }
106 
107  void run(P0& data) override
108  {
109  m_fsm.dispatch(Macho::Event(m_handler, data));
110  }
111 
112  FSM& m_fsm;
113  std::string m_eventName;
114  R (TOP::*m_handler)(P0);
116  };
117 
119  : public ConnectorListener
120  {
121  USE_CONNLISTENER_STATUS;
122  public:
124  m_buffer(buffer), m_thebuffer(m_thebuffer) {}
125  ~EventConnListener() override = default;
126 
127  ReturnCode operator()(ConnectorInfo& info) override
128  {
129  coil::Properties prop;
130  prop["write.full_policy"] = "do_nothing";
131  prop["read.empty_policy"] = "do_nothing";
132  m_thebuffer->init(prop);
133 
134  coil::Properties prop_(info.properties.getNode("dataport.buffer"));
135  prop_ << info.properties.getNode("inport.buffer");
136 
137  m_buffer.init(prop_);
138 
139 
140  return NO_CHANGE;
141  }
144  };
145 
146 
193  template <class FsmType>
195  : public InPortBase
196  {
197  public:
222  EventInPort(const char* name, FsmType& fsm)
223  : InPortBase(name, "any"),
224  m_name(name), m_fsm(fsm), m_buffer(fsm.getBuffer())
225  {
226  }
227 
243  ~EventInPort() override;
244 
264  virtual const char* name()
265  {
266  return m_name.c_str();
267  }
268 
269  void init(coil::Properties& prop) override
270  {
271  InPortBase::init(prop);
274  new EventConnListener(m_buffer, m_thebuffer));
275  }
276 
277  template <class TOP, class R, class P0>
278  void bindEvent(const char* name,
279  R (TOP::*handler)(P0))
280  {
283  new EventBinder1<FsmType, TOP, R, P0>(m_fsm, name, handler, m_buffer));
284  }
285  template <typename TOP, class R>
286  void bindEvent(const char* name,
287  R (TOP::*handler)())
288  {
291  new EventBinder0<FsmType, TOP, R>(m_fsm, name, handler, m_buffer));
292  }
293  bool read(std::string /*name*/="") override { return true; }
294  private:
302  std::string m_name;
303  FsmType& m_fsm;
304  RingBuffer<EventBase*> &m_buffer;
305  };
306 
307  template <class FsmType>
309 } // namespace RTC
310 
311 #endif // RTC_EVENTINPORT_H
RTC::Port implementation for InPort.
InPortConnector base class.
PortCallback class.
RTComponent header.
Defautl Buffer class.
Static FSM framework based on Macho.
Timestamp listener class.
Typename function.
BufferBase abstract class.
Definition: BufferBase.h:106
virtual void init(const coil::Properties &prop)=0
Set the buffer.
Definition: ByteData.h:30
ConnectorDataListenerT class.
Definition: ConnectorListener.h:515
ConnectorDataListener class.
Definition: ConnectorListener.h:408
ConnectorInfo class.
Definition: ConnectorBase.h:50
coil::Properties properties
Connection properties.
Definition: ConnectorBase.h:178
std::string name
Connection name.
Definition: ConnectorBase.h:154
@ NO_CHANGE
Definition: ConnectorListener.h:71
ConnectorListener class.
Definition: ConnectorListener.h:884
Definition: EventBase.h:49
Definition: EventBase.h:66
Definition: EventPort.h:46
EventBinder0(FSM &fsm, const char *event_name, R(TOP::*handler)(), RingBuffer< EventBase * > &buffer)
Definition: EventPort.h:49
std::string m_eventName
Definition: EventPort.h:76
ReturnCode operator()(ConnectorInfo &info, ByteData &, const std::string &) override
Virtual Callback method.
Definition: EventPort.h:57
~EventBinder0() override=default
R(TOP::* m_handler)()
Definition: EventPort.h:77
RingBuffer< EventBase * > & m_buffer
Definition: EventPort.h:78
void run() override
Definition: EventPort.h:69
FSM & m_fsm
Definition: EventPort.h:75
Definition: EventPort.h:85
~EventBinder1() override=default
ReturnCode operator()(ConnectorInfo &info, P0 &data) override
Virtual Callback method.
Definition: EventPort.h:96
R(TOP::* m_handler)(P0)
Definition: EventPort.h:114
EventBinder1(FSM &fsm, const char *event_name, R(TOP::*handler)(P0), RingBuffer< EventBase * > &buffer)
Definition: EventPort.h:88
RingBuffer< EventBase * > & m_buffer
Definition: EventPort.h:115
std::string m_eventName
Definition: EventPort.h:113
FSM & m_fsm
Definition: EventPort.h:112
void run(P0 &data) override
Definition: EventPort.h:107
Definition: EventBase.h:26
Definition: EventBase.h:33
Definition: EventPort.h:120
ReturnCode operator()(ConnectorInfo &info) override
Virtual Callback method.
Definition: EventPort.h:127
RingBuffer< EventBase * > & m_buffer
Definition: EventPort.h:142
CdrBufferBase * m_thebuffer
Definition: EventPort.h:143
EventConnListener(RingBuffer< EventBase * > &buffer, CdrBufferBase *m_thebuffer)
Definition: EventPort.h:123
~EventConnListener() override=default
EventInPort template class.
Definition: EventPort.h:196
bool read(std::string="") override
It is a virtual method that is called from RTObject_impl::readAll(). This method reads out data from ...
Definition: EventPort.h:293
void init(coil::Properties &prop) override
Initializing properties.
Definition: EventPort.h:269
void bindEvent(const char *name, R(TOP::*handler)())
Definition: EventPort.h:286
void bindEvent(const char *name, R(TOP::*handler)(P0))
Definition: EventPort.h:278
virtual const char * name()
Get port name.
Definition: EventPort.h:264
EventInPort(const char *name, FsmType &fsm)
A constructor.
Definition: EventPort.h:222
~EventInPort() override
Destructor.
Port for InPort.
Definition: InPortBase.h:70
virtual void init(coil::Properties &prop)
Initializing properties.
void addConnectorListener(ConnectorListenerType type, ConnectorListener *listener, bool autoclean=true)
Adding ConnectorListener type listener.
CdrBufferBase * m_thebuffer
Buffer.
Definition: InPortBase.h:860
void addConnectorDataListener(ConnectorDataListenerType type, ConnectorDataListener *listener, bool autoclean=true)
Adding BufferDataListener type listener.
Ring buffer implementation class.
Definition: RingBuffer.h:90
IEvent< TOP > * Event(R(TOP::*handler)(P1, P2, P3, P4, P5, P6), const P1 &p1, const P2 &p2, const P3 &p3, const P4 &p4, const P5 &p5, const P6 &p6)
Definition: Macho.h:1050
RT-Component.
coil::Properties Properties
Definition: RTC.h:72