serializable_buffer.h
Go to the documentation of this file.
1 
26 #ifndef ODVA_ETHERNETIP_SERIALIZATION_SERIALIZABLE_BUFFER_H
27 #define ODVA_ETHERNETIP_SERIALIZATION_SERIALIZABLE_BUFFER_H
28 
29 #include <boost/asio.hpp>
30 
35 
36 using boost::asio::mutable_buffer;
37 using boost::asio::buffer_size;
38 
39 namespace eip {
40 namespace serialization {
41 
46 {
47 public:
48 
53  SerializableBuffer() : data_(NULL, 0), allocated_buffer_(NULL) { }
54 
59  SerializableBuffer(mutable_buffer data) : data_(data), allocated_buffer_(NULL) { }
60 
62  {
64  data_ = mutable_buffer(NULL, 0);
65  }
66 
71  virtual size_t getLength() const
72  {
73  return buffer_size(data_);
74  }
75 
80  virtual mutable_buffer getData() const
81  {
82  return data_;
83  }
84 
89  virtual void setData(mutable_buffer data)
90  {
91  // TODO: It's possible that the user could pass in the same buffer again,
92  // or a subset of it. That would be absurd, but possible.
94  data_ = data;
95  }
96 
103  virtual Writer& serialize(Writer& writer) const
104  {
105  writer.writeBuffer(data_);
106  return writer;
107  }
108 
118  virtual Reader& deserialize(Reader& reader, size_t length)
119  {
120  // with a BufferReader, we can read without copying
121  BufferReader* br = dynamic_cast<BufferReader*>(&reader);
122  if (br)
123  {
125  data_ = br->readBuffer(length);
126  }
127  else
128  {
129  if (length != buffer_size(data_))
130  {
131  // must allocate a new buffer
133  allocated_buffer_ = new char[length];
134  data_ = buffer(allocated_buffer_, length);
135  }
136  reader.readBuffer(data_);
137  }
138  return reader;
139  }
140 
148  virtual Reader& deserialize(Reader& reader)
149  {
150  reader.readBuffer(data_);
151  return reader;
152  }
153 
154  void operator=(mutable_buffer b)
155  {
156  data_ = b;
157  }
158 
159 private:
160  mutable_buffer data_;
162 
167  {
168  if (allocated_buffer_)
169  {
170  delete[] allocated_buffer_;
171  allocated_buffer_ = NULL;
172  }
173  }
174 };
175 
176 } // namespace serialization
177 } // namespace eip
178 #endif // ODVA_ETHERNETIP_SERIALIZATION_SERIALIZABLE_BUFFER_H
eip::serialization::BufferReader::readBuffer
virtual mutable_buffer readBuffer(size_t n)
Definition: buffer_reader.h:83
eip::serialization::SerializableBuffer::getLength
virtual size_t getLength() const
Definition: serializable_buffer.h:71
eip::serialization::Writer
Definition: writer.h:41
eip::serialization::Reader
Definition: reader.h:42
eip::serialization::SerializableBuffer::serialize
virtual Writer & serialize(Writer &writer) const
Definition: serializable_buffer.h:103
eip
Definition: connection.h:41
eip::serialization::SerializableBuffer::deserialize
virtual Reader & deserialize(Reader &reader)
Definition: serializable_buffer.h:148
eip::serialization::SerializableBuffer::deleteAllocatedBuffer
void deleteAllocatedBuffer()
Definition: serializable_buffer.h:166
eip::serialization::Reader::readBuffer
virtual void readBuffer(mutable_buffer buf)=0
eip::serialization::SerializableBuffer::SerializableBuffer
SerializableBuffer(mutable_buffer data)
Definition: serializable_buffer.h:59
eip::serialization::SerializableBuffer::getData
virtual mutable_buffer getData() const
Definition: serializable_buffer.h:80
eip::serialization::Serializable
Definition: serializable.h:38
eip::serialization::SerializableBuffer::~SerializableBuffer
virtual ~SerializableBuffer()
Definition: serializable_buffer.h:61
eip::serialization::SerializableBuffer::data_
mutable_buffer data_
Definition: serializable_buffer.h:160
reader.h
eip::serialization::SerializableBuffer::setData
virtual void setData(mutable_buffer data)
Definition: serializable_buffer.h:89
eip::serialization::SerializableBuffer::SerializableBuffer
SerializableBuffer()
Definition: serializable_buffer.h:53
eip::serialization::SerializableBuffer::allocated_buffer_
char * allocated_buffer_
Definition: serializable_buffer.h:161
eip::serialization::SerializableBuffer::operator=
void operator=(mutable_buffer b)
Definition: serializable_buffer.h:154
buffer_reader.h
eip::serialization::SerializableBuffer
Definition: serializable_buffer.h:45
eip::serialization::Writer::writeBuffer
virtual void writeBuffer(const_buffer b)=0
eip::serialization::SerializableBuffer::deserialize
virtual Reader & deserialize(Reader &reader, size_t length)
Definition: serializable_buffer.h:118
eip::serialization::BufferReader
Definition: buffer_reader.h:45
serializable.h
writer.h


odva_ethernetip
Author(s): Kareem Shehata
autogenerated on Wed Mar 2 2022 00:38:56