OpenRTM  1.0.0
InPort.h
[詳解]
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 
565  OnRead<DataType>* m_OnRead;
566 
574  OnReadConvert<DataType>* m_OnReadConvert;
575 
576  };
577 }; // End of namesepace RTM
578 
579 #endif // RTC_INPORT_H
#define RTC_ERROR(fmt)
エラーログ出力マクロ。
Definition: SystemLogger.h:422
#define DATAPORTSTATUS_ENUM
RTC::DataPortStatus 導入
Definition: DataPortStatus.h:233
#define RTC_TRACE(fmt)
トレースログ出力マクロ。
Definition: SystemLogger.h:510
coil::Mutex m_connectorsMutex
Definition: PortBase.h:2088
virtual ~InPort(void)
デストラクタ
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)
コンストラクタ
Definition: InPort.h:145
virtual const char * name()
ポート名称を取得する。
Definition: InPort.h:195
PortCallback class.
Typename function.
#define RTC_WARN(fmt)
ワーニングログ出力マクロ。
Definition: SystemLogger.h:444
virtual bool isEmpty()
バッファが空かどうか確認する
Definition: InPort.h:276
read() 時のデータ変換コールバック抽象クラス
Definition: PortCallback.h:385
void setOnReadConvert(OnReadConvert< DataType > *on_rconvert)
InPort バッファへデータ読み出し時のコールバックの設定
Definition: InPort.h:533
#define RTC_DEBUG(fmt)
デバッグログ出力マクロ。
Definition: SystemLogger.h:488
InPortConnector base class.
virtual void update()
バインドされた T 型の変数に InPort バッファの最新値を読み込む ...
Definition: InPort.h:452
InPortBase(const char *name, const char *data_type)
コンストラクタ
InPort 用 Port.
Definition: InPortBase.h:67
Definition: DataPortStatus.h:145
RTC::Port implementation for InPort.
InPort テンプレートクラス
Definition: InPort.h:89
virtual bool isNew()
最新データが存在するか確認する
Definition: InPort.h:225
RTComponent header.
void setOnRead(OnRead< DataType > *on_read)
InPort バッファへデータ読み込み時のコールバックの設定
Definition: InPort.h:505
Definition: DataPortStatus.h:150
void operator>>(DataType &rhs)
T 型のデータへ InPort の最新値データを読み込む
Definition: InPort.h:477
ConnectorList m_connectors
接続リスト
Definition: InPortBase.h:853
bool read()
DataPort から値を読み出す
Definition: InPort.h:378
read() 時のコールバック抽象クラス
Definition: PortCallback.h:323
Definition: DataPortStatus.h:149