InPort.h
Go to the documentation of this file.
1 // -*- C++ -*-
20 #ifndef RTC_INPORT_H
21 #define RTC_INPORT_H
22 
23 #include <string>
24 #include <vector>
25 #include <iostream>
26 
27 #include <coil/TimeValue.h>
28 #include <coil/Time.h>
29 #include <coil/OS.h>
30 
31 #include <rtm/RTC.h>
32 #include <rtm/Typename.h>
33 #include <rtm/InPortBase.h>
34 #include <rtm/CdrBufferBase.h>
35 #include <rtm/PortCallback.h>
36 #include <rtm/InPortConnector.h>
37 
38 namespace RTC
39 {
88  template <class DataType>
89  class InPort
90  : public InPortBase
91  {
92  public:
145  InPort(const char* name, DataType& value,
146  int bufsize=64,
147  bool read_block = false, bool write_block = false,
148  int read_timeout = 0, int write_timeout = 0)
149 #if defined(__GNUC__) && (__GNUC__ <= 3 && __GNUC_MINOR__ <= 3)
150  : InPortBase(name, ::CORBA_Util::toRepositoryIdOfStruct<DataType>()),
151 #else
152  : InPortBase(name, ::CORBA_Util::toRepositoryId<DataType>()),
153 #endif
154  m_name(name), m_value(value),
155  m_OnRead(NULL), m_OnReadConvert(NULL)
156  {
157  }
158 
174  virtual ~InPort(void){};
175 
195  virtual const char* name()
196  {
197  return m_name.c_str();
198  }
199 
200 
225  virtual bool isNew()
226  {
227  RTC_TRACE(("isNew()"));
228 
229  // In single-buffer mode, all connectors share the same buffer. This
230  // means that we only need to read from the first connector to get data
231  // received by any connector.
232  int r(0);
233  {
234  Guard guard(m_connectorsMutex);
235  if (m_connectors.size() == 0)
236  {
237  RTC_DEBUG(("no connectors"));
238  return false;
239  }
240  r = m_connectors[0]->getBuffer()->readable();
241  }
242 
243  if (r > 0)
244  {
245  RTC_DEBUG(("isNew() = true, readable data: %d", r));
246  return true;
247  }
248 
249  RTC_DEBUG(("isNew() = false, no readable data"));
250  return false;
251  }
252 
276  virtual bool isEmpty()
277  {
278  RTC_TRACE(("isEmpty()"));
279  int r(0);
280 
281  {
282  Guard guard(m_connectorsMutex);
283  if (m_connectors.size() == 0)
284  {
285  RTC_DEBUG(("no connectors"));
286  return true;
287  }
288  // In single-buffer mode, all connectors share the same buffer. This
289  // means that we only need to read from the first connector to get data
290  // received by any connector.
291  r = m_connectors[0]->getBuffer()->readable();
292  }
293 
294  if (r == 0)
295  {
296  RTC_DEBUG(("isEmpty() = true, buffer is empty"));
297  return true;
298  }
299 
300  RTC_DEBUG(("isEmpty() = false, data exists in the buffer"));
301  return false;
302  }
303 
378  bool read()
379  {
380  RTC_TRACE(("DataType read()"));
381 
382  if (m_OnRead != NULL)
383  {
384  (*m_OnRead)();
385  RTC_TRACE(("OnRead called"));
386  }
387 
388  cdrMemoryStream cdr;
389  ReturnCode ret;
390  {
391  Guard guard(m_connectorsMutex);
392  if (m_connectors.size() == 0)
393  {
394  RTC_DEBUG(("no connectors"));
395  return false;
396  }
397 
398  // In single-buffer mode, all connectors share the same buffer. This
399  // means that we only need to read from the first connector to get data
400  // received by any connector.
401  ret = m_connectors[0]->read(cdr);
402  }
403  if (ret == PORT_OK)
404  {
405  RTC_DEBUG(("data read succeeded"));
406  m_value <<= cdr;
407  if (m_OnReadConvert != 0)
408  {
409  m_value = (*m_OnReadConvert)(m_value);
410  RTC_DEBUG(("OnReadConvert called"));
411  return true;
412  }
413  return true;
414  }
415  else if (ret == BUFFER_EMPTY)
416  {
417  RTC_WARN(("buffer empty"));
418  return false;
419  }
420  else if (ret == BUFFER_TIMEOUT)
421  {
422  RTC_WARN(("buffer read timeout"));
423  return false;
424  }
425  RTC_ERROR(("unknown retern value from buffer.read()"));
426  return false;
427  }
428 
429 
452  virtual void update()
453  {
454  this->read();
455  };
456 
477  void operator>>(DataType& rhs)
478  {
479  this->read();
480  rhs = m_value;
481  return;
482  }
483 
505  inline void setOnRead(OnRead<DataType>* on_read)
506  {
507  m_OnRead = on_read;
508  }
509 
533  inline void setOnReadConvert(OnReadConvert<DataType>* on_rconvert)
534  {
535  m_OnReadConvert = on_rconvert;
536  }
537 
538  private:
539  std::string m_typename;
547  std::string m_name;
548 
556  DataType& m_value;
557 
566 
575 
576  };
577 }; // End of namesepace RTM
578 
579 #endif // RTC_INPORT_H
#define RTC_ERROR(fmt)
Error log output macro.
Definition: SystemLogger.h:422
#define DATAPORTSTATUS_ENUM
Importing RTC::DataPortStatus macro.
RT-Component.
coil::Mutex m_connectorsMutex
Definition: PortBase.h:2088
virtual ~InPort(void)
Destructor.
Definition: InPort.h:174
DATAPORTSTATUS_ENUM InPort(const char *name, DataType &value, int bufsize=64, bool read_block=false, bool write_block=false, int read_timeout=0, int write_timeout=0)
A constructor.
Definition: InPort.h:145
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
virtual const char * name()
Get port name.
Definition: InPort.h:195
PortCallback class.
Typename function.
Enum
DataPortStatus return codes.
Definition: BufferStatus.h:84
DataType & m_value
The reference to type-T value bound this OutPort.
Definition: InPort.h:556
#define RTC_WARN(fmt)
Warning log output macro.
Definition: SystemLogger.h:444
std::string m_name
Definition: InPort.h:547
virtual bool isEmpty()
Check whether the data is newest.
Definition: InPort.h:276
Data convert callback abstract class on read()
Definition: PortCallback.h:385
void setOnReadConvert(OnReadConvert< DataType > *on_rconvert)
Set callback when data is readout to the InPort buffer.
Definition: InPort.h:533
#define RTC_DEBUG(fmt)
Debug level log output macro.
Definition: SystemLogger.h:488
OnReadConvert< DataType > * m_OnReadConvert
Pointer to OnReadConvert callback functor.
Definition: InPort.h:574
#define RTC_TRACE(fmt)
OnRead< DataType > * m_OnRead
Pointer to OnRead callback functor.
Definition: InPort.h:565
InPortConnector base class.
virtual void update()
Read the newly value to type-T variable which is bound to InPort&#39;s buffer.
Definition: InPort.h:452
InPortBase(const char *name, const char *data_type)
Constructor.
Definition: InPortBase.cpp:42
RTC::Port implementation for InPort.
virtual bool isNew()
Check whether the data is newest.
Definition: InPort.h:225
RTComponent header.
void setOnRead(OnRead< DataType > *on_read)
Set callback when data is read from the InPort buffer.
Definition: InPort.h:505
void operator>>(DataType &rhs)
Read the newly value data in InPort to type-T variable.
Definition: InPort.h:477
std::string m_typename
Definition: InPort.h:539
ConnectorList m_connectors
Connection list.
Definition: InPortBase.h:853
bool read()
Readout the value from DataPort.
Definition: InPort.h:378
Callback abstract class on read()
Definition: PortCallback.h:323


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Jun 10 2019 14:07:52