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  m_status(1)
157  {
158  }
159 
175  virtual ~InPort(void){};
176 
196  virtual const char* name()
197  {
198  return m_name.c_str();
199  }
200 
201 
226  virtual bool isNew()
227  {
228  RTC_TRACE(("isNew()"));
229 
230  // In single-buffer mode, all connectors share the same buffer. This
231  // means that we only need to read from the first connector to get data
232  // received by any connector.
233  int r(0);
234  {
235  Guard guard(m_connectorsMutex);
236  if (m_connectors.size() == 0)
237  {
238  RTC_DEBUG(("no connectors"));
239  return false;
240  }
241  r = m_connectors[0]->getBuffer()->readable();
242  }
243 
244  if (r > 0)
245  {
246  RTC_DEBUG(("isNew() = true, readable data: %d", r));
247  return true;
248  }
249 
250  RTC_DEBUG(("isNew() = false, no readable data"));
251  return false;
252  }
253 
277  virtual bool isEmpty()
278  {
279  RTC_TRACE(("isEmpty()"));
280  int r(0);
281 
282  {
283  Guard guard(m_connectorsMutex);
284  if (m_connectors.size() == 0)
285  {
286  RTC_DEBUG(("no connectors"));
287  return true;
288  }
289  // In single-buffer mode, all connectors share the same buffer. This
290  // means that we only need to read from the first connector to get data
291  // received by any connector.
292  r = m_connectors[0]->getBuffer()->readable();
293  }
294 
295  if (r == 0)
296  {
297  RTC_DEBUG(("isEmpty() = true, buffer is empty"));
298  return true;
299  }
300 
301  RTC_DEBUG(("isEmpty() = false, data exists in the buffer"));
302  return false;
303  }
304 
379  bool read()
380  {
381  RTC_TRACE(("DataType read()"));
382 
383  if (m_OnRead != NULL)
384  {
385  (*m_OnRead)();
386  RTC_TRACE(("OnRead called"));
387  }
388 
389  cdrMemoryStream cdr;
390  ReturnCode ret;
391  {
392  Guard guard(m_connectorsMutex);
393  if (m_connectors.size() == 0)
394  {
395  RTC_DEBUG(("no connectors"));
396  return false;
397  }
398 
399  // In single-buffer mode, all connectors share the same buffer. This
400  // means that we only need to read from the first connector to get data
401  // received by any connector.
402  ret = m_connectors[0]->read(cdr);
403  m_status[0] = ret;
404  }
405  if (ret == PORT_OK)
406  {
407  RTC_DEBUG(("data read succeeded"));
408  m_value <<= cdr;
409  if (m_OnReadConvert != 0)
410  {
411  m_value = (*m_OnReadConvert)(m_value);
412  RTC_DEBUG(("OnReadConvert called"));
413  return true;
414  }
415  return true;
416  }
417  else if (ret == BUFFER_EMPTY)
418  {
419  RTC_WARN(("buffer empty"));
420  return false;
421  }
422  else if (ret == BUFFER_TIMEOUT)
423  {
424  RTC_WARN(("buffer read timeout"));
425  return false;
426  }
427  RTC_ERROR(("unknown retern value from buffer.read()"));
428  return false;
429  }
430 
431 
454  virtual void update()
455  {
456  this->read();
457  };
458 
479  void operator>>(DataType& rhs)
480  {
481  this->read();
482  rhs = m_value;
483  return;
484  }
485 
519  {
520  return m_status[0];
521  }
553  {
554  return m_status;
555  }
556 
578  inline void setOnRead(OnRead<DataType>* on_read)
579  {
580  m_OnRead = on_read;
581  }
582 
606  inline void setOnReadConvert(OnReadConvert<DataType>* on_rconvert)
607  {
608  m_OnReadConvert = on_rconvert;
609  }
610 
611  private:
612  std::string m_typename;
620  std::string m_name;
621 
629  DataType& m_value;
630 
639 
648 
650  };
651 }; // End of namesepace RTM
652 
653 #endif // RTC_INPORT_H
#define RTC_ERROR(fmt)
Error log output macro.
Definition: SystemLogger.h:422
#define DATAPORTSTATUS_ENUM
Importing RTC::DataPortStatus macro.
DataPortStatusList m_status
Definition: InPort.h:649
RT-Component.
coil::Mutex m_connectorsMutex
Definition: PortBase.h:2088
virtual ~InPort(void)
Destructor.
Definition: InPort.h:175
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:196
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:629
#define RTC_WARN(fmt)
Warning log output macro.
Definition: SystemLogger.h:444
std::string m_name
Definition: InPort.h:620
virtual bool isEmpty()
Check whether the data is newest.
Definition: InPort.h:277
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:606
std::vector< DataPortStatus::Enum > DataPortStatusList
#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:647
#define RTC_TRACE(fmt)
OnRead< DataType > * m_OnRead
Pointer to OnRead callback functor.
Definition: InPort.h:638
InPortConnector base class.
list index
Definition: rtimages.py:10
virtual void update()
Read the newly value to type-T variable which is bound to InPort&#39;s buffer.
Definition: InPort.h:454
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:226
DataPortStatus::Enum getStatus(int index)
Getting specified connector&#39;s writing status.
Definition: InPort.h:518
RTComponent header.
void setOnRead(OnRead< DataType > *on_read)
Set callback when data is read from the InPort buffer.
Definition: InPort.h:578
DataPortStatusList getStatusList()
Getting specified connector&#39;s writing status list.
Definition: InPort.h:552
void operator>>(DataType &rhs)
Read the newly value data in InPort to type-T variable.
Definition: InPort.h:479
std::string m_typename
Definition: InPort.h:612
Enum
DataPortStatus return codes.
ConnectorList m_connectors
Connection list.
Definition: InPortBase.h:853
bool read()
Readout the value from DataPort.
Definition: InPort.h:379
Callback abstract class on read()
Definition: PortCallback.h:323


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Feb 28 2022 23:00:43