OpenRTM-aist  2.0.1
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::InPortBase::m_thebuffer
CdrBufferBase * m_thebuffer
Buffer.
Definition: InPortBase.h:860
InPortBase.h
RTC::Port implementation for InPort.
RTC::EventBinder1::m_eventName
std::string m_eventName
Definition: EventPort.h:113
RTC::ConnectorListenerStatus::NO_CHANGE
@ NO_CHANGE
Definition: ConnectorListener.h:71
RTC::EventConnListener::m_buffer
RingBuffer< EventBase * > & m_buffer
Definition: EventPort.h:142
RTC::EventInPort::bindEvent
void bindEvent(const char *name, R(TOP::*handler)())
Definition: EventPort.h:286
RTC::ConnectorListenerType::ON_CONNECT
@ ON_CONNECT
RTC::ConnectorInfo::name
std::string name
Connection name.
Definition: ConnectorBase.h:154
RTC::EventBinder1::~EventBinder1
~EventBinder1() override=default
RTC::InPortBase::addConnectorDataListener
void addConnectorDataListener(ConnectorDataListenerType type, ConnectorDataListener *listener, bool autoclean=true)
Adding BufferDataListener type listener.
RTC::EventBinder1::operator()
ReturnCode operator()(ConnectorInfo &info, P0 &data) override
Virtual Callback method.
Definition: EventPort.h:96
RTC::EventBinder0::m_handler
R(TOP::* m_handler)()
Definition: EventPort.h:77
RTC::EventInPort
EventInPort template class.
Definition: EventPort.h:194
RTC::EventBinderBase1
Definition: EventBase.h:32
RTC::EventBinder0::EventBinder0
EventBinder0(FSM &fsm, const char *event_name, R(TOP::*handler)(), RingBuffer< EventBase * > &buffer)
Definition: EventPort.h:49
RTC::Event0
Definition: EventBase.h:48
RTC::InPortBase::init
virtual void init(coil::Properties &prop)
Initializing properties.
RTC::EventInPort::~EventInPort
~EventInPort() override
Destructor.
RTC::EventBinder0
Definition: EventPort.h:44
RTC::EventBinder1::run
void run(P0 &data) override
Definition: EventPort.h:107
RTC::EventBinder0::m_fsm
FSM & m_fsm
Definition: EventPort.h:75
RTC::EventInPort::name
virtual const char * name()
Get port name.
Definition: EventPort.h:264
RTC::EventBinderBase0
Definition: EventBase.h:25
Typename.h
Typename function.
RTC::BufferBase
BufferBase abstract class.
Definition: BufferBase.h:105
RTC::EventBinder1::m_buffer
RingBuffer< EventBase * > & m_buffer
Definition: EventPort.h:115
RTC::EventBinder1::EventBinder1
EventBinder1(FSM &fsm, const char *event_name, R(TOP::*handler)(P0), RingBuffer< EventBase * > &buffer)
Definition: EventPort.h:88
RTC::ConnectorDataListener
ConnectorDataListener class.
Definition: ConnectorListener.h:406
RTC::EventBinder1::m_handler
R(TOP::* m_handler)(P0)
Definition: EventPort.h:114
RTC::EventConnListener
Definition: EventPort.h:118
InPortConnector.h
InPortConnector base class.
Macho::Event
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:1042
RTC::EventConnListener::m_thebuffer
CdrBufferBase * m_thebuffer
Definition: EventPort.h:143
RTC::InPortBase::addConnectorListener
void addConnectorListener(ConnectorListenerType type, ConnectorListener *listener, bool autoclean=true)
Adding ConnectorListener type listener.
StaticFSM.h
Static FSM framework based on Macho.
PortCallback.h
PortCallback class.
RTC::EventBinder0::m_eventName
std::string m_eventName
Definition: EventPort.h:76
RTC::Event1
Definition: EventBase.h:65
RTC::EventConnListener::operator()
ReturnCode operator()(ConnectorInfo &info) override
Virtual Callback method.
Definition: EventPort.h:127
RTC::ConnectorInfo
ConnectorInfo class.
Definition: ConnectorBase.h:49
RingBuffer.h
Defautl Buffer class.
RTC::ConnectorDataListenerType::ON_RECEIVED
@ ON_RECEIVED
RTC::EventBinder0::run
void run() override
Definition: EventPort.h:69
EventBase.h
RTC::EventBinder1::m_fsm
FSM & m_fsm
Definition: EventPort.h:112
RTC::EventBinder1
Definition: EventPort.h:83
RTC::BufferBase::init
virtual void init(const coil::Properties &prop)=0
Set the buffer.
RTC::EventBinder0::~EventBinder0
~EventBinder0() override=default
RTC::ConnectorDataListenerT
ConnectorDataListenerT class.
Definition: ConnectorListener.h:513
RTC
RT-Component.
RTC::EventInPort::EventInPort
EventInPort(const char *name, FsmType &fsm)
A constructor.
Definition: EventPort.h:222
RTC::RingBuffer
Ring buffer implementation class.
Definition: RingBuffer.h:88
RTC::Properties
coil::Properties Properties
Definition: RTC.h:72
RTC::EventBinder0::operator()
ReturnCode operator()(ConnectorInfo &info, ByteData &, const std::string &) override
Virtual Callback method.
Definition: EventPort.h:57
RTC::EventBinder0::m_buffer
RingBuffer< EventBase * > & m_buffer
Definition: EventPort.h:78
RTC::EventConnListener::EventConnListener
EventConnListener(RingBuffer< EventBase * > &buffer, CdrBufferBase *m_thebuffer)
Definition: EventPort.h:123
RTC::EventConnListener::~EventConnListener
~EventConnListener() override=default
RTC.h
RTComponent header.
RTC::ByteData
Definition: ByteData.h:29
RTC::ConnectorListener
ConnectorListener class.
Definition: ConnectorListener.h:882
CdrBufferBase.h
RTC::ConnectorInfo::properties
coil::Properties properties
Connection properties.
Definition: ConnectorBase.h:178
RTC::InPortBase
Port for InPort.
Definition: InPortBase.h:68
RTC::EventInPort::bindEvent
void bindEvent(const char *name, R(TOP::*handler)(P0))
Definition: EventPort.h:278
Timestamp.h
Timestamp listener class.
RTC::EventInPort::read
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
RTC::EventInPort::init
void init(coil::Properties &prop) override
Initializing properties.
Definition: EventPort.h:269