buffer_reader.h
Go to the documentation of this file.
1 
26 #ifndef ODVA_ETHERNETIP_SERIALIZATION_BUFFER_READER_H
27 #define ODVA_ETHERNETIP_SERIALIZATION_BUFFER_READER_H
28 
29 #include <stdexcept>
30 #include <boost/asio.hpp>
31 
34 
35 using boost::asio::buffer;
36 using boost::asio::mutable_buffer;
37 using boost::asio::buffer_size;
38 
39 namespace eip {
40 namespace serialization {
41 
45 class BufferReader : public Reader
46 {
47 public:
48 
53  BufferReader(mutable_buffer buf) : buf_(buf), byte_count_(0) { }
54 
61  virtual void readBytes(void* buf, size_t n)
62  {
63  readBuffer(buffer(buf, n));
64  }
65 
72  virtual void* readBytes(size_t n)
73  {
74  return boost::asio::buffer_cast<void*>(readBuffer(n));
75  }
76 
83  virtual mutable_buffer readBuffer(size_t n)
84  {
85  if (buffer_size(buf_) < n)
86  {
87  throw std::length_error("Buffer too small to deserialize value");
88  }
89  mutable_buffer b = buffer(buf_, n);
90  byte_count_ += n;
91  buf_ = buf_ + n;
92  return b;
93  }
94 
102  virtual void readBuffer(mutable_buffer buf)
103  {
104  size_t n = buffer_size(buf);
105  if (buffer_size(buf_) < n)
106  {
107  throw std::length_error("Buffer too small to deserialize value");
108  }
109  boost::asio::buffer_copy(buf, buf_);
110  byte_count_ += n;
111  buf_ = buf_ + n;
112  }
113 
118  virtual size_t getByteCount()
119  {
120  return byte_count_;
121  }
122 
128  virtual void skip(size_t n)
129  {
130  if (buffer_size(buf_) < n)
131  {
132  throw std::length_error("End of buffer reached skipping forward");
133  }
134  byte_count_ += n;
135  buf_ = buf_ + n;
136  }
137 private:
138  mutable_buffer buf_;
139  size_t byte_count_;
140 };
141 
142 } // namespace serialization
143 } // namespace eip
144 #endif // ODVA_ETHERNETIP_SERIALIZATION_BUFFER_READER_H
virtual void readBuffer(mutable_buffer buf)
BufferReader(mutable_buffer buf)
Definition: buffer_reader.h:53
Definition: connection.h:41
virtual void * readBytes(size_t n)
Definition: buffer_reader.h:72
virtual mutable_buffer readBuffer(size_t n)
Definition: buffer_reader.h:83
virtual void skip(size_t n)
virtual void readBytes(void *buf, size_t n)
Definition: buffer_reader.h:61


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