Go to the documentation of this file.00001
00026 #ifndef ODVA_ETHERNETIP_SERIALIZATION_BUFFER_READER_H
00027 #define ODVA_ETHERNETIP_SERIALIZATION_BUFFER_READER_H
00028
00029 #include <stdexcept>
00030 #include <boost/asio.hpp>
00031
00032 #include "odva_ethernetip/eip_types.h"
00033 #include "odva_ethernetip/serialization/reader.h"
00034
00035 using boost::asio::buffer;
00036 using boost::asio::mutable_buffer;
00037 using boost::asio::buffer_size;
00038
00039 namespace eip {
00040 namespace serialization {
00041
00045 class BufferReader : public Reader
00046 {
00047 public:
00048
00053 BufferReader(mutable_buffer buf) : buf_(buf), byte_count_(0) { }
00054
00061 virtual void readBytes(void* buf, size_t n)
00062 {
00063 readBuffer(buffer(buf, n));
00064 }
00065
00072 virtual void* readBytes(size_t n)
00073 {
00074 return boost::asio::buffer_cast<void*>(readBuffer(n));
00075 }
00076
00083 virtual mutable_buffer readBuffer(size_t n)
00084 {
00085 if (buffer_size(buf_) < n)
00086 {
00087 throw std::length_error("Buffer too small to deserialize value");
00088 }
00089 mutable_buffer b = buffer(buf_, n);
00090 byte_count_ += n;
00091 buf_ = buf_ + n;
00092 return b;
00093 }
00094
00102 virtual void readBuffer(mutable_buffer buf)
00103 {
00104 size_t n = buffer_size(buf);
00105 if (buffer_size(buf_) < n)
00106 {
00107 throw std::length_error("Buffer too small to deserialize value");
00108 }
00109 boost::asio::buffer_copy(buf, buf_);
00110 byte_count_ += n;
00111 buf_ = buf_ + n;
00112 }
00113
00118 virtual size_t getByteCount()
00119 {
00120 return byte_count_;
00121 }
00122
00128 virtual void skip(size_t n)
00129 {
00130 if (buffer_size(buf_) < n)
00131 {
00132 throw std::length_error("End of buffer reached skipping forward");
00133 }
00134 byte_count_ += n;
00135 buf_ = buf_ + n;
00136 }
00137 private:
00138 mutable_buffer buf_;
00139 size_t byte_count_;
00140 };
00141
00142 }
00143 }
00144 #endif // ODVA_ETHERNETIP_SERIALIZATION_BUFFER_READER_H