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
virtual Reader & deserialize(Reader &reader)
virtual Reader & deserialize(Reader &reader, size_t length)
virtual void readBuffer(mutable_buffer buf)=0
Definition: connection.h:41
virtual mutable_buffer readBuffer(size_t n)
Definition: buffer_reader.h:83
virtual Writer & serialize(Writer &writer) const
virtual mutable_buffer getData() const
virtual void setData(mutable_buffer data)
virtual void writeBuffer(const_buffer b)=0


odva_ethernetip
Author(s): Kareem Shehata
autogenerated on Mon Jun 10 2019 14:00:16