MIRA
BinaryStream.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
55 #ifndef _MIRA_BINARY_STREAM_H_
56 #define _MIRA_BINARY_STREAM_H_
57 
58 #include <iostream>
59 #include <string.h> // for strlen
60 
61 #include <platform/Endian.h> // for conversion to network byte order
62 
63 #include <utils/Buffer.h>
64 #include <utils/EnumToFlags.h>
65 
66 #include <error/Exceptions.h>
67 
68 namespace mira {
69 
71 
85 {
86 public:
87 
88  typedef enum
89  {
90  none = 0,
91  net = 1L << 0
92  } fmtflags;
94 
96 
97 
110  {
111  fmtflags old = flags;
112  mFmtFlags |= flags;
113  return old;
114  }
115 
122  void unsetf(fmtflags mask)
123  {
124  mFmtFlags &= ~mask;
125  }
126 
127 
128 protected:
129 
130  // our format flags that were set by the manipulators or the setf() unsetf() methods
132 
133 };
134 
136 
143 {
144 public:
145 
146  // a couple of stl conform typedefs
149  typedef std::size_t pos_type;
150 
151 public:
153  mBuffer(buffer), mGetPos(0), mPutPos(0), mFailBit(false)
154  {
155  if(mBuffer->size()==0)
156  mBuffer->resize(1);
157  }
158 
159 public:
160 
161  // access to internal data buffer
163  {
164  return mBuffer;
165  }
166 
167 public:
168 
169  // keep this inline for performance reasons!!!
170  void write(const char* data, std::size_t size)
171  {
172  std::size_t npos = mPutPos+size;
173 
174  // resize the vector buffer if necessary
175  if(npos>mBuffer->size())
176  mBuffer->resize(npos);
177 
178  memcpy(mBuffer->data()+mPutPos, data, size);
179  mPutPos = npos;
180  }
181 
182  // keep this inline for performance reasons!!!
183  void read(char* data, std::size_t size)
184  {
185  std::size_t npos = mGetPos+size;
186 
187  // make sure that we do not read above end of buffer
188  // (see istream::read)
189  if(npos>mBuffer->size()) {
190  size = mBuffer->size() - mGetPos;
191  npos = mBuffer->size();
192  mFailBit = true;
193  }
194 
195  memcpy(data, mBuffer->data()+mGetPos, size);
196  mGetPos = npos;
197  }
198 
199 public:
200 
201  std::size_t tellg() const {
202  return mGetPos;
203  }
204 
205  BinaryBufferStreamBase& seekg(std::size_t pos) {
206  mGetPos = pos;
207  return *this;
208  }
209 
210  std::size_t tellp() const {
211  return mPutPos;
212  }
213 
214  BinaryBufferStreamBase& seekp(std::size_t pos) {
215  mPutPos = pos;
216  return *this;
217  }
218 
219 public:
220 
221  // keep this inline for performance reasons!!!
222  bool eof() const {
223  return mPutPos == mBuffer->size();
224  }
225 
226  bool fail() const {
227  return mFailBit;
228  }
229 
230 private:
231 
232  buffer_type* mBuffer;
233  std::size_t mGetPos;
234  std::size_t mPutPos;
235  bool mFailBit;
236 };
237 
239 
292 template <typename StreamUnderlay = BinaryBufferStreamBase>
293 class BinaryOstream : public StreamUnderlay,
294  public BinaryIosBase // used as mixin
295 {
296  typedef StreamUnderlay Base;
297 
298 public:
299 
300  // a couple of stl conform typedefs
301  typedef typename Base::char_type char_type;
302  typedef decltype(std::declval<Base>().rdbuf()) streambuffer_pointer;
303  typedef typename StreamUnderlay::pos_type pos_type;
304 
305 public:
306 
318 
323  BinaryOstream(Base& s) : Base(s.rdbuf()) {}
324 
328  BinaryOstream(Base& s, pos_type pos) : Base(s.rdbuf()) { this->seekp(pos); }
329 
331  BinaryOstream& operator<<(const char* v) {return writeString(v);}
332  BinaryOstream& operator<<(const std::string& v) {return writeString(v);}
333  BinaryOstream& operator<<(const bool& v) {return toBinary<bool>(v);}
334  BinaryOstream& operator<<(const char& v) {return toBinary<char>(v);}
335  BinaryOstream& operator<<(const uint8& v) {return toBinary<uint8>(v);}
336  BinaryOstream& operator<<(const uint16& v) {return toBinary<uint16>(v);}
337  BinaryOstream& operator<<(const uint32& v) {return toBinary<uint32>(v);}
338  BinaryOstream& operator<<(const uint64& v) {return toBinary<uint64>(v);}
339  BinaryOstream& operator<<(const int8& v) {return toBinary<int8>(v);}
340  BinaryOstream& operator<<(const int16& v) {return toBinary<int16>(v);}
341  BinaryOstream& operator<<(const int32& v) {return toBinary<int32>(v);}
342  BinaryOstream& operator<<(const int64& v) {return toBinary<int64>(v);}
343  BinaryOstream& operator<<(const float& v) {return toBinary<float>(v);}
344  BinaryOstream& operator<<(const double& v) {return toBinary<double>(v);}
345  template<typename T>
346  BinaryOstream& operator<<(const Buffer<T>& v) {
347  uint64 size = v.size();
348  *this << size;
349  write(v.data(), v.size());
350  return *this;
351  }
352 
353  // Interface for manipulators
362  {
363  manipFn(*this); // call the manipulator
364  return *this;
365  }
366 
367 public:
368 
369  template <typename T>
370  void write(const T* data, std::size_t count)
371  {
372  static_assert(sizeof(char_type)==1, "binaryostream must have char_type of size 1");
373  Base::write(reinterpret_cast<const char*>(data), count*sizeof(T));
374  }
375 
384  BinaryOstream& writeString(const char* value)
385  {
386  writeString(value, (uint32)strlen(value));
387  return *this;
388  }
389 
398  BinaryOstream& writeString(const std::string& value)
399  {
400  writeString(value, (uint32)value.length());
401  return *this;
402  }
403 
408  BinaryOstream& writeString(const char* value, uint32 length)
409  {
410  this->operator<<(length); // write the length
411  write(value, length);
412  return *this;
413  }
414 
419  BinaryOstream& writeString(const std::string& value, uint32 length)
420  {
421  this->operator<<(length); // write the length
422  write(value.c_str(), length);
423  return *this;
424  }
425 
426 protected:
427 
432  template<typename T>
433  BinaryOstream& toBinary(const T& value)
434  {
435  if(mFmtFlags & net) { // write in network byte order
436  // convert first
437  typename NetworkTypeTrait<T>::Type v = hostToNetwork<T>(value);
438  write(&v, 1);
439  } else // write in host byte order
440  write(&value, 1);
441  return *this;
442  }
443 };
444 
452 
461 
463 
524 template <typename StreamUnderlay = BinaryBufferStreamBase>
525 class BinaryIstream : public StreamUnderlay,
526  public BinaryIosBase // used as mixin
527 {
528  typedef StreamUnderlay Base;
529 
530 public:
531 
532  typedef typename Base::char_type char_type;
533  typedef decltype(std::declval<Base>().rdbuf()) streambuffer_pointer;
534  typedef typename StreamUnderlay::pos_type pos_type;
535 
536 public:
537 
548  BinaryIstream(streambuffer_pointer buffer) : Base(buffer) {}
549 
554  BinaryIstream(Base& s) : Base(s.rdbuf()) {}
555 
559  BinaryIstream(Base& s, pos_type pos) : Base(s.rdbuf()) { this->seekg(pos); }
560 
562  BinaryIstream& operator>>(char* v) { return stringFromBinary(v); }
563  BinaryIstream& operator>>(std::string& v) { return stringFromBinary(v); }
564  BinaryIstream& operator>>(bool& v) { return fromBinary<bool>(v);}
565  BinaryIstream& operator>>(char& v) { return fromBinary<char>(v);}
566  BinaryIstream& operator>>(uint8& v) { return fromBinary<uint8>(v);}
567  BinaryIstream& operator>>(uint16& v) { return fromBinary<uint16>(v);}
568  BinaryIstream& operator>>(uint32& v) { return fromBinary<uint32>(v);}
569  BinaryIstream& operator>>(uint64& v) { return fromBinary<uint64>(v);}
570  BinaryIstream& operator>>(int8& v) { return fromBinary<int8>(v);}
571  BinaryIstream& operator>>(int16& v) { return fromBinary<int16>(v);}
572  BinaryIstream& operator>>(int32& v) { return fromBinary<int32>(v);}
573  BinaryIstream& operator>>(int64& v) { return fromBinary<int64>(v);}
574  BinaryIstream& operator>>(float& v) { return fromBinary<float>(v);}
575  BinaryIstream& operator>>(double& v) { return fromBinary<double>(v);}
576  template<typename T>
578  uint64 size = 0;
579  *this >> size;
580  if(size!=0 && this->eof())
581  MIRA_THROW(XIO, "Failed to read buffer from binary stream, premature end of stream");
582  v.resize((std::size_t)size);
583  read(v.data(), v.size());
584  return *this;
585  }
586 
596  {
597  manipFn(*this); // call the manipulator
598  return *this;
599  }
600 
601 public:
602 
603  template <typename T>
604  void read(T* data, std::size_t count)
605  {
606  static_assert(sizeof(char_type)==1, "binaryistream must have char_type of size 1");
607  Base::read(reinterpret_cast<char*>(data), count*sizeof(T));
608  }
609 
610 
611 protected:
612 
617  template<typename T>
619  {
620  if(mFmtFlags & net) { // read in network byte order
621  typename NetworkTypeTrait<T>::Type v;
622  read(&v, 1);
623  value = networkToHost<T>(v); // convert first
624  } else // read in host byte order
625  read(&value, 1);
626  return *this;
627  }
628 
634  {
635  uint32 l;
636  this->operator>>(l); // read the length
637  if(this->eof())
638  MIRA_THROW(XIO, "Failed to read string from binary stream, premature end of stream");
639  read(value, l);
640  value[l]='\0';
641  return *this;
642  }
643 
647  BinaryIstream& stringFromBinary(std::string& value)
648  {
649  uint32 l;
650  this->operator>>(l); // read the length
651 
652  if(this->eof())
653  MIRA_THROW(XIO, "Failed to read string from binary stream, premature end of stream");
654 
655  value.resize(l);
656  read((char*)value.c_str(), l);
657  ((char*)value.c_str())[l]='\0';
658  return *this;
659  }
660 };
661 
669 
678 
682 
697 {
698  stream.setf(BinaryIosBase::net);
699  return stream;
700 }
701 
717 {
718  stream.unsetf(BinaryIosBase::net); // remove net-format flag
719  return stream;
720 }
721 
723 
724 }
725 
726 #endif
buffer_type * rdbuf()
Definition: BinaryStream.h:162
void write(const Value &value, std::ostream &ioStream, bool formatted=false, int precision=-1)
Writes a json::Value into a given stream using the JSON format.
BinaryIstream & operator>>(uint64 &v)
Definition: BinaryStream.h:569
BinaryBufferStreamBase & seekp(std::size_t pos)
Definition: BinaryStream.h:214
uint64_t uint64
Definition: Types.h:65
decltype(std::declval< Base >().rdbuf()) typedef streambuffer_pointer
Definition: BinaryStream.h:533
void write(const char *data, std::size_t size)
Definition: BinaryStream.h:170
BinaryOstream & writeString(const std::string &value)
Method for explicitly writing a STL string to the stream.
Definition: BinaryStream.h:398
Base::char_type char_type
Definition: BinaryStream.h:301
BinaryOstream & operator<<(const std::string &v)
Definition: BinaryStream.h:332
BinaryIstream(Base &s)
Constructor.
Definition: BinaryStream.h:554
BinaryOstream & writeString(const std::string &value, uint32 length)
Method for explicitly writing the first &#39;length&#39; characters of the STL-string &#39;value&#39;.
Definition: BinaryStream.h:419
BinaryIstream & operator>>(uint16 &v)
Definition: BinaryStream.h:567
Macros for generating logical operators for using enum values as flags.
T Type
Definition: Endian.h:236
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
fmtflags setf(fmtflags flags)
Setting new format flags.
Definition: BinaryStream.h:109
BinaryIosBase & net(BinaryIosBase &stream)
Manipulators:
Definition: BinaryStream.h:696
bool fail() const
Definition: BinaryStream.h:226
BinaryOstream & writeString(const char *value, uint32 length)
Method for explicitly writing the first &#39;length&#39; characters of the C-string &#39;value&#39;.
Definition: BinaryStream.h:408
BinaryOstream & operator<<(const uint32 &v)
Definition: BinaryStream.h:337
Utilities for byte order conversion between little and big endian.
BinaryOstream & operator<<(const uint8 &v)
Definition: BinaryStream.h:335
void read(const std::string &s, Value &oValue)
Read a json::Value from a string that contains JSON format.
BinaryIosBase & host(BinaryIosBase &stream)
Manipulator that sets a binary input/output stream into host byte order mode and can be used to reset...
Definition: BinaryStream.h:716
BinaryOstream & operator<<(const uint64 &v)
Definition: BinaryStream.h:338
std::size_t tellp() const
Definition: BinaryStream.h:210
uint32_t uint32
Definition: Types.h:64
BinaryIstream & operator>>(int64 &v)
Definition: BinaryStream.h:573
void read(T *data, std::size_t count)
Definition: BinaryStream.h:604
uint8 value_type
Definition: Buffer.h:90
BinaryIstream & operator>>(char *v)
stream operator for the built-in C++ datatypes
Definition: BinaryStream.h:562
Although this class is not a template we must keep its methods inline for performance reasons! write(...
Definition: BinaryStream.h:142
StreamUnderlay::pos_type pos_type
Definition: BinaryStream.h:534
#define MIRA_ENUM_TO_FLAGS_INCLASS(EnumType)
Macro that can be used with enums that contain flags.
Definition: EnumToFlags.h:143
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:78
BinaryOstream & operator<<(BinaryIosBase &(*manipFn)(BinaryIosBase &))
This stream operator is for internal use only.
Definition: BinaryStream.h:361
BinaryOstream & operator<<(const int16 &v)
Definition: BinaryStream.h:340
BinaryOstream & operator<<(const int64 &v)
Definition: BinaryStream.h:342
buffer_type::value_type char_type
Definition: BinaryStream.h:148
BinaryOstream & toBinary(const T &value)
Writes every type T to this binary stream.
Definition: BinaryStream.h:433
Output stream adapter that can be assigned to any output stream and allows binary output using the <<...
Definition: BinaryStream.h:293
Commonly used exception classes.
std::size_t tellg() const
Definition: BinaryStream.h:201
BinaryIstream & operator>>(int32 &v)
Definition: BinaryStream.h:572
size_type size() const
Returns the used size of the buffer set by resize()
Definition: Buffer.h:289
BinaryBufferStreamBase & seekg(std::size_t pos)
Definition: BinaryStream.h:205
decltype(std::declval< Base >().rdbuf()) typedef streambuffer_pointer
Definition: BinaryStream.h:302
uint8_t uint8
Definition: Types.h:62
BinaryOstream & operator<<(const int32 &v)
Definition: BinaryStream.h:341
pointer data()
Returns a pointer to the underlying data.
Definition: Buffer.h:510
Base::char_type char_type
Definition: BinaryStream.h:532
BinaryIstream & operator>>(std::string &v)
Definition: BinaryStream.h:563
BinaryIstream & fromBinary(T &value)
Reads every type T from this binary stream.
Definition: BinaryStream.h:618
BinaryOstream & operator<<(const bool &v)
Definition: BinaryStream.h:333
BinaryIstream & stringFromBinary(char *value)
Special method for reading C-strings (pointer value must point to memory address with enough memory f...
Definition: BinaryStream.h:633
BinaryOstream & operator<<(const double &v)
Definition: BinaryStream.h:344
BinaryIstream & operator>>(BinaryIosBase &(*manipFn)(BinaryIosBase &))
This stream operator is for internal use only.
Definition: BinaryStream.h:595
uint16_t uint16
Definition: Types.h:63
BinaryOstream & operator<<(const char &v)
Definition: BinaryStream.h:334
BinaryIstream & operator>>(int16 &v)
Definition: BinaryStream.h:571
int8_t int8
Definition: Types.h:58
fmtflags
Definition: BinaryStream.h:88
int16_t int16
Definition: Types.h:59
BinaryIstream & operator>>(bool &v)
Definition: BinaryStream.h:564
BinaryOstream & operator<<(const int8 &v)
Definition: BinaryStream.h:339
void unsetf(fmtflags mask)
Clearing format flags.
Definition: BinaryStream.h:122
bool eof() const
Definition: BinaryStream.h:222
Input stream adapter that can be assigned to any input stream and allows binary input using the >> st...
Definition: BinaryStream.h:525
BinaryOstream & operator<<(const float &v)
Definition: BinaryStream.h:343
int32_t int32
Definition: Types.h:60
BinaryIstream & operator>>(uint32 &v)
Definition: BinaryStream.h:568
Definition: BinaryStream.h:90
int64_t int64
Definition: Types.h:61
BinaryOstream & writeString(const char *value)
Method for explicitly writing a C-string to the stream.
Definition: BinaryStream.h:384
void resize(size_type size)
Resizes the buffer.
Definition: Buffer.h:346
BinaryIstream & operator>>(double &v)
Definition: BinaryStream.h:575
Buffer< uint8 > buffer_type
Definition: BinaryStream.h:147
BinaryIstream & operator>>(Buffer< T > &v)
Definition: BinaryStream.h:577
void write(const T *data, std::size_t count)
Definition: BinaryStream.h:370
BinaryIstream & stringFromBinary(std::string &value)
Special method for reading STL-strings.
Definition: BinaryStream.h:647
BinaryOstream(Base &s)
Constructor.
Definition: BinaryStream.h:323
BinaryIstream & operator>>(char &v)
Definition: BinaryStream.h:565
Definition: BinaryStream.h:91
BinaryIstream & operator>>(int8 &v)
Definition: BinaryStream.h:570
BinaryOstream & operator<<(const char *v)
stream operator for the built-in C++ datatypes
Definition: BinaryStream.h:331
Generic buffer class that can be used as a replacement for std::vector.
BinaryOstream(Base &s, pos_type pos)
Constructor specifying the stream and the current write position.
Definition: BinaryStream.h:328
BinaryIstream & operator>>(float &v)
Definition: BinaryStream.h:574
std::size_t pos_type
Definition: BinaryStream.h:149
Helper class that is a base for binaryostream and binaryistream in the same way as ios_base is a base...
Definition: BinaryStream.h:84
BinaryBufferStreamBase(buffer_type *buffer)
Definition: BinaryStream.h:152
BinaryIstream(Base &s, pos_type pos)
Constructor specifying the stream and the current read position.
Definition: BinaryStream.h:559
void read(char *data, std::size_t size)
Definition: BinaryStream.h:183
fmtflags mFmtFlags
Definition: BinaryStream.h:131
BinaryOstream & operator<<(const uint16 &v)
Definition: BinaryStream.h:336
BinaryIstream & operator>>(uint8 &v)
Definition: BinaryStream.h:566