BufferStream.hh
Go to the documentation of this file.
1 
39 #ifndef CRL_MULTISENSE_BUFFERSTREAM_HH
40 #define CRL_MULTISENSE_BUFFERSTREAM_HH
41 
42 #include "Exception.hh"
43 #include "TimeStamp.hh"
44 #include "ReferenceCount.hh"
45 #include "Portability.hh"
46 
47 #include <stdint.h>
48 #include <cstddef>
49 #include <vector>
50 
51 namespace crl {
52 namespace multisense {
53 namespace details {
54 namespace utility {
55 
56 
57 
58 //
59 // The base storage class.
60 //
61 // To read/write from the stream, use the Reader/Writer
62 // derivatives below.
63 //
64 // SENSORPOD_FIRMWARE: microblaze build, no shared() interface
65 
66 class BufferStream {
67 public:
68 
69  void clear () { m_tell = 0; };
70  std::size_t tell () const { return m_tell; };
71  std::size_t size () const { return m_size; };
72  void *data () const { return m_bufferP; };
73  void *peek () const { return &(m_bufferP[m_tell]); };
74 
75 #ifndef SENSORPOD_FIRMWARE
76  bool shared() const { return m_ref.isShared(); };
77 #endif // SENSORPOD_FIRMWARE
78 
79  virtual void read (void *bufferP, std::size_t length) {
80  CRL_EXCEPTION("not implemented");
81  };
82  virtual void write(const void *bufferP, std::size_t length) {
83  CRL_EXCEPTION("not implemented");
84  };
85 
86  //
87  // Move the r/w pointer in the buffer, checking bounds
88 
89  void seek(std::size_t idx) {
90 
91  if (idx > m_size)
92  CRL_EXCEPTION("invalid seek location %d, [0, %d] valid\n",
93  idx, m_size);
94  m_tell = idx;
95  };
96 
97  //
98  // Default constructor
99 
101  m_alloced(false),
102  m_size(0),
103  m_tell(0),
104  m_bufferP(NULL) {};
105 
106  //
107  // Construction, we allocate memory
108 
109  BufferStream(std::size_t size) :
110  m_alloced(false),
111  m_size(size),
112  m_tell(0),
113  m_bufferP(NULL) {
114 
115  m_bufferP = new (std::nothrow) uint8_t[size];
116  if (NULL == m_bufferP)
117  CRL_EXCEPTION("unable to allocate %d bytes", size);
118  m_alloced = true;
119  };
120 
121  //
122  // Construction, memory is already allocated
123 
124  BufferStream(uint8_t *bufP, std::size_t size) :
125  m_alloced(false),
126  m_size(size),
127  m_tell(0),
128  m_bufferP(bufP) {};
129 
130  //
131  // Destruction, free memory only if we allocated
132 
133  virtual ~BufferStream() {
134 #ifdef SENSORPOD_FIRMWARE
135  if (m_alloced)
136 #else
137  if (m_alloced && false == m_ref.isShared())
138 #endif // SENSORPOD_FIRMWARE
139  delete[] m_bufferP;
140  };
141 
142  //
143  // Copy constructor
144 
145  BufferStream(const BufferStream& source) {
146  m_alloced = source.m_alloced;
147  m_size = source.m_size;
148  m_tell = 0; // reset
149  m_bufferP = source.m_bufferP;
150 
151 #ifndef SENSORPOD_FIRMWARE
152  m_ref = source.m_ref;
153 #endif // SENSORPOD_FIRMWARE
154  };
155 
156 protected:
157 
158  bool m_alloced;
159  std::size_t m_size;
160  std::size_t m_tell;
161  uint8_t *m_bufferP;
162 
163 #ifndef SENSORPOD_FIRMWARE
165 #endif // SENSORPOD_FIRMWARE
166 };
167 
168 //
169 // The input (deserialization) implementation. Must operate on
170 // non-const data.
171 
173 public:
174 
177  BufferStreamReader(const uint8_t *p, std::size_t s) : BufferStream(const_cast<uint8_t*>(p), s) {};
178  BufferStreamReader(std::size_t s) : BufferStream(s) {};
179 
180  virtual void read (void *bufferP, std::size_t length) {
181 
182  if (length > (m_size - m_tell))
183  CRL_EXCEPTION("read overflow: tell=%d, size=%d, length=%d\n",
184  m_tell, m_size, length);
185 
186  memcpy(bufferP, &(m_bufferP[m_tell]), length);
187  m_tell += length;
188  };
189 
190  template <typename T> BufferStreamReader& operator&(T &value) {
191  this->read(&value, sizeof(T));
192  return *this;
193  };
194 
195  template <typename T> BufferStreamReader& operator&(std::vector<T>& v) {
196  uint16_t version;
197  uint32_t num;
198  *this & version;
199  *this & num;
200  v.resize(num);
201  for(uint32_t i=0; i<num; i++)
202  v[i].serialize(*this, version);
203  return *this;
204  }
205 
206  BufferStreamReader& operator&(std::string& value) {
207  uint16_t length;
208 
209  this->read(&length, sizeof(length));
210  if (length > 512)
211  CRL_EXCEPTION("unusually large string: %d bytes",
212  length);
213  else if (length > 0) {
214  char buffer[512+1]; // length is guaranteed to be less than or equal to 512
215  buffer[length] = '\0';
216  this->read(buffer, length);
217  value = std::string(buffer);
218  }
219  return *this;
220  };
221 
223  uint32_t seconds;
224  uint32_t microseconds;
225 
226  this->read(&seconds, sizeof(seconds));
227  this->read(&microseconds, sizeof(microseconds));
228 
229  value = seconds + 1e-6 * microseconds;
230 
231  return *this;
232  };
233 };
234 
235 //
236 // The output (serialization) implementation. Able to operate
237 // purely on const data.
238 
240 public:
241 
244  BufferStreamWriter(uint8_t *b, std::size_t s) : BufferStream(b, s) {};
245  BufferStreamWriter(std::size_t s) : BufferStream(s) {};
246 
247  virtual void write(const void *bufferP, std::size_t length) {
248 
249  if ((length + m_tell) > m_size)
250  CRL_EXCEPTION("write overflow: tell=%d, size=%d, length=%d\n",
251  m_tell, m_size, length);
252 
253  memcpy(&(m_bufferP[m_tell]), bufferP, length);
254  m_tell += length;
255  };
256 
257  template <typename T> BufferStreamWriter& operator&(const T& value) {
258  this->write(&value, sizeof(T));
259  return *this;
260  };
261 
262  template <typename T> BufferStreamWriter& operator&(const std::vector<T>& v) {
263  uint16_t version = T::VERSION;
264  uint32_t num = v.size();
265  *this & version;
266  *this & num;
267  for(uint32_t i=0; i<num; i++)
268  const_cast<T*>(&v[i])->serialize(*this, version);
269  return *this;
270  }
271 
272  BufferStreamWriter& operator&(const std::string& value) {
273  uint16_t length = value.size();
274 
275  if (length > 512)
276  CRL_EXCEPTION("unusually large string: %d bytes", length);
277 
278  this->write(&length, sizeof(length));
279  if (length > 0)
280  this->write(value.c_str(), length);
281  return *this;
282  };
283 
285  const uint32_t seconds = value.getSeconds();
286  const uint32_t microseconds = value.getMicroSeconds();
287 
288  this->write(&seconds, sizeof(seconds));
289  this->write(&microseconds, sizeof(microseconds));
290 
291  return *this;
292  };
293 };
294 
295 }}}} // namespaces
296 
297 #endif /* #ifndef CRL_MULTISENSE_BUFFERSTREAM_HH */
virtual void read(void *bufferP, std::size_t length)
#define CRL_EXCEPTION(fmt,...)
Definition: Exception.hh:71
BufferStreamWriter & operator&(const TimeStamp &value)
BufferStreamReader(const uint8_t *p, std::size_t s)
virtual void write(const void *bufferP, std::size_t length)
BufferStreamWriter & operator&(const T &value)
BufferStreamWriter & operator&(const std::string &value)
virtual void write(const void *bufferP, std::size_t length)
Definition: BufferStream.hh:82
BufferStreamReader & operator&(TimeStamp &value)
BufferStream(uint8_t *bufP, std::size_t size)
void serialize(Stream &stream, const T &t)
virtual void read(void *bufferP, std::size_t length)
Definition: BufferStream.hh:79
BufferStreamWriter & operator&(const std::vector< T > &v)
Definition: channel.cc:56
BufferStreamReader & operator&(std::vector< T > &v)
BufferStreamReader & operator&(std::string &value)


multisense_lib
Author(s):
autogenerated on Sat Apr 6 2019 02:16:46