OpenRTM-aist  2.1.0
ByteDataStreamBase.h
Go to the documentation of this file.
1 // -*- C++ -*-
20 #ifndef RTC_BYTEDATASTREAMBASE_H
21 #define RTC_BYTEDATASTREAMBASE_H
22 
23 #include <coil/Properties.h>
24 #include <coil/Factory.h>
25 #include <rtm/Typename.h>
26 
41 namespace RTC
42 {
63  {
64  public:
80 
97 
111  virtual void init(const coil::Properties &prop);
128  virtual void writeData(const unsigned char *buffer, unsigned long length) = 0;
145  virtual void readData(unsigned char *buffer, unsigned long length) const = 0;
159  virtual unsigned long getDataLength() const = 0;
173  virtual void isLittleEndian(bool little_endian);
174  };
175 
198  template <typename DataType>
200  {
201  public:
216  ByteDataStream() = default;
217 
233  ~ByteDataStream() override = default;
234 
250  virtual bool serialize(const DataType &data) = 0;
266  virtual bool deserialize(DataType &data) = 0;
267  };
268 
269  using SerializerFactory = coil::GlobalFactory<ByteDataStreamBase>;
270 
291  template <class DataType>
292  std::string addDataTypeToMarshalingType(const std::string &marshalingtype)
293  {
294  std::string mtype{std::string(::CORBA_Util::toRepositoryId<DataType>()) + ":" + marshalingtype};
295  return mtype;
296  }
297 
313  template <class DataType, class SerializerType>
314  void addSerializer(const std::string &marshalingtype)
315  {
316  std::string mtype = addDataTypeToMarshalingType<DataType>(marshalingtype);
317  SerializerFactory::instance()
318  .addFactory(mtype,
319  ::coil::Creator<::RTC::ByteDataStreamBase,
320  SerializerType>,
321  ::coil::Destructor<::RTC::ByteDataStreamBase,
322  SerializerType>);
323  }
324 
340  template <class DataType>
341  void removeSerializer(const std::string &marshalingtype)
342  {
343  std::string mtype = addDataTypeToMarshalingType<DataType>(marshalingtype);
344  SerializerFactory::instance().removeFactory(mtype);
345  }
346 
362  template <class DataType>
363  ::RTC::ByteDataStreamBase *createSerializer(const std::string &marshalingtype)
364  {
365  std::string mtype = addDataTypeToMarshalingType<DataType>(marshalingtype);
366  return SerializerFactory::instance().createObject(mtype);
367  }
368 
384  template <class DataType>
385  std::vector<std::string> getSerializerList()
386  {
387  std::vector<std::string> available_types;
388  std::vector<std::string> types = SerializerFactory::instance().getIdentifiers();
389 
390  for (auto type : types)
391  {
392  std::string id{::CORBA_Util::toRepositoryId<DataType>()};
393  if (type.size() >= id.size() && std::equal(std::begin(id), std::end(id), std::begin(type)))
394  {
395  type.erase(0, id.size() + 1);
396  available_types.push_back(type);
397  }
398  }
399  return available_types;
400  }
401 
402 } // namespace RTC
403 
404 EXTERN template class DLL_PLUGIN coil::GlobalFactory<::RTC::ByteDataStreamBase>;
405 
406 #endif // RTC_BYTEDATASTREAMBASE_H
Typename function.
Definition: ByteDataStreamBase.h:63
ByteDataStreamBase()
Constructor.
virtual void init(const coil::Properties &prop)
virtual unsigned long getDataLength() const =0
virtual void writeData(const unsigned char *buffer, unsigned long length)=0
virtual void readData(unsigned char *buffer, unsigned long length) const =0
virtual void isLittleEndian(bool little_endian)
virtual ~ByteDataStreamBase()
Virtual destractor.
Definition: ByteDataStreamBase.h:200
ByteDataStream()=default
Constructor.
~ByteDataStream() override=default
Virtual destractor.
virtual bool deserialize(DataType &data)=0
virtual bool serialize(const DataType &data)=0
RT-Component.
void removeSerializer(const std::string &marshalingtype)
Definition: ByteDataStreamBase.h:341
coil::GlobalFactory< ByteDataStreamBase > SerializerFactory
Definition: ByteDataStreamBase.h:269
::RTC::ByteDataStreamBase * createSerializer(const std::string &marshalingtype)
Definition: ByteDataStreamBase.h:363
std::string addDataTypeToMarshalingType(const std::string &marshalingtype)
Definition: ByteDataStreamBase.h:292
coil::Properties Properties
Definition: RTC.h:72
void addSerializer(const std::string &marshalingtype)
Definition: ByteDataStreamBase.h:314
std::vector< std::string > getSerializerList()
Definition: ByteDataStreamBase.h:385