buffer_writer.h
Go to the documentation of this file.
00001 
00026 #ifndef ODVA_ETHERNETIP_SERIALIZATION_BUFFER_WRITER_H
00027 #define ODVA_ETHERNETIP_SERIALIZATION_BUFFER_WRITER_H
00028 
00029 #include <stdexcept>
00030 #include <boost/asio.hpp>
00031 
00032 #include "odva_ethernetip/eip_types.h"
00033 #include "odva_ethernetip/serialization/writer.h"
00034 
00035 using boost::asio::const_buffer;
00036 using boost::asio::mutable_buffer;
00037 
00038 namespace eip {
00039 namespace serialization {
00040 
00044 class BufferWriter : public Writer
00045 {
00046 public:
00047 
00052   BufferWriter(mutable_buffer buf) : buf_(buf), byte_count_(0) { }
00053 
00061   void writeBytes(const void* p, size_t n)
00062   {
00063     writeBuffer(boost::asio::buffer(p, n));
00064   }
00065 
00074   void writeBuffer(const_buffer b)
00075   {
00076     using boost::asio::buffer_size;
00077     if (buffer_size(buf_) < buffer_size(b))
00078     {
00079       throw std::length_error("Buffer to small to serialize value");
00080     }
00081     boost::asio::buffer_copy(buf_, b);
00082     byte_count_ += buffer_size(b);
00083     buf_ = buf_ + buffer_size(b);
00084   }
00085 
00090   size_t getByteCount()
00091   {
00092     return byte_count_;
00093   }
00094 
00095 private:
00096   mutable_buffer buf_;
00097   size_t byte_count_;
00098 };
00099 
00100 } // namespace serialization
00101 } // namespace eip
00102 #endif  // ODVA_ETHERNETIP_SERIALIZATION_BUFFER_WRITER_H


odva_ethernetip
Author(s): Kareem Shehata
autogenerated on Sat Jun 8 2019 20:21:23