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 
451 
459 
461 
522 template <typename StreamUnderlay = BinaryBufferStreamBase>
523 class BinaryIstream : public StreamUnderlay,
524  public BinaryIosBase // used as mixin
525 {
526  typedef StreamUnderlay Base;
527 
528 public:
529 
530  typedef typename Base::char_type char_type;
531  typedef decltype(std::declval<Base>().rdbuf()) streambuffer_pointer;
532  typedef typename StreamUnderlay::pos_type pos_type;
533 
534 public:
535 
546  BinaryIstream(streambuffer_pointer buffer) : Base(buffer) {}
547 
552  BinaryIstream(Base& s) : Base(s.rdbuf()) {}
553 
557  BinaryIstream(Base& s, pos_type pos) : Base(s.rdbuf()) { this->seekg(pos); }
558 
560  BinaryIstream& operator>>(char* v) { return stringFromBinary(v); }
561  BinaryIstream& operator>>(std::string& v) { return stringFromBinary(v); }
562  BinaryIstream& operator>>(bool& v) { return fromBinary<bool>(v);}
563  BinaryIstream& operator>>(char& v) { return fromBinary<char>(v);}
564  BinaryIstream& operator>>(uint8& v) { return fromBinary<uint8>(v);}
565  BinaryIstream& operator>>(uint16& v) { return fromBinary<uint16>(v);}
566  BinaryIstream& operator>>(uint32& v) { return fromBinary<uint32>(v);}
567  BinaryIstream& operator>>(uint64& v) { return fromBinary<uint64>(v);}
568  BinaryIstream& operator>>(int8& v) { return fromBinary<int8>(v);}
569  BinaryIstream& operator>>(int16& v) { return fromBinary<int16>(v);}
570  BinaryIstream& operator>>(int32& v) { return fromBinary<int32>(v);}
571  BinaryIstream& operator>>(int64& v) { return fromBinary<int64>(v);}
572  BinaryIstream& operator>>(float& v) { return fromBinary<float>(v);}
573  BinaryIstream& operator>>(double& v) { return fromBinary<double>(v);}
574  template<typename T>
576  uint64 size = 0;
577  *this >> size;
578  if(size!=0 && this->eof())
579  MIRA_THROW(XIO, "Failed to read buffer from binary stream, premature end of stream");
580  v.resize((std::size_t)size);
581  read(v.data(), v.size());
582  return *this;
583  }
584 
594  {
595  manipFn(*this); // call the manipulator
596  return *this;
597  }
598 
599 public:
600 
601  template <typename T>
602  void read(T* data, std::size_t count)
603  {
604  static_assert(sizeof(char_type)==1, "binaryistream must have char_type of size 1");
605  Base::read(reinterpret_cast<char*>(data), count*sizeof(T));
606  }
607 
608 
609 protected:
610 
615  template<typename T>
617  {
618  if(mFmtFlags & net) { // read in network byte order
619  typename NetworkTypeTrait<T>::Type v;
620  read(&v, 1);
621  value = networkToHost<T>(v); // convert first
622  } else // read in host byte order
623  read(&value, 1);
624  return *this;
625  }
626 
632  {
633  uint32 l;
634  this->operator>>(l); // read the length
635  if(this->eof())
636  MIRA_THROW(XIO, "Failed to read string from binary stream, premature end of stream");
637  read(value, l);
638  value[l]='\0';
639  return *this;
640  }
641 
645  BinaryIstream& stringFromBinary(std::string& value)
646  {
647  uint32 l;
648  this->operator>>(l); // read the length
649 
650  if(this->eof())
651  MIRA_THROW(XIO, "Failed to read string from binary stream, premature end of stream");
652 
653  value.resize(l);
654  read((char*)value.c_str(), l);
655  ((char*)value.c_str())[l]='\0';
656  return *this;
657  }
658 };
659 
666 
674 
678 
693 {
694  stream.setf(BinaryIosBase::net);
695  return stream;
696 }
697 
713 {
714  stream.unsetf(BinaryIosBase::net); // remove net-format flag
715  return stream;
716 }
717 
719 
720 }
721 
722 #endif
buffer_type * rdbuf()
Definition: BinaryStream.h:162
BinaryOstream< std::basic_ostream< char, std::char_traits< char > > > BinaryStlOstream
Typedef for binary output streams based on STL streams.
Definition: BinaryStream.h:450
BinaryIstream< BinaryBufferStreamBase > BinaryBufferIstream
Typedef for binary input streams based on a Buffer.
Definition: BinaryStream.h:673
BinaryIstream & operator>>(uint64 &v)
Definition: BinaryStream.h:567
BinaryBufferStreamBase & seekp(std::size_t pos)
Definition: BinaryStream.h:214
decltype(std::declval< Base >().rdbuf()) typedef streambuffer_pointer
Definition: BinaryStream.h:531
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< std::basic_istream< char, std::char_traits< char > > > BinaryStlIstream
Typedef for binary input streams based on STL streams.
Definition: BinaryStream.h:665
BinaryIstream(Base &s)
Constructor.
Definition: BinaryStream.h:552
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:565
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:692
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
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:712
BinaryOstream & operator<<(const uint64 &v)
Definition: BinaryStream.h:338
std::size_t tellp() const
Definition: BinaryStream.h:210
BinaryIstream & operator>>(int64 &v)
Definition: BinaryStream.h:571
void read(T *data, std::size_t count)
Definition: BinaryStream.h:602
uint8 value_type
Definition: Buffer.h:90
BinaryIstream & operator>>(char *v)
stream operator for the built-in C++ datatypes
Definition: BinaryStream.h:560
Although this class is not a template we must keep its methods inline for performance reasons! write(...
Definition: BinaryStream.h:142
BinaryOstream< BinaryBufferStreamBase > BinaryBufferOstream
Typedef for binary output streams based on a Buffer.
Definition: BinaryStream.h:458
StreamUnderlay::pos_type pos_type
Definition: BinaryStream.h:532
#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:82
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
MIRA_BASE_EXPORT 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.
Commonly used exception classes.
std::size_t tellg() const
Definition: BinaryStream.h:201
BinaryIstream & operator>>(int32 &v)
Definition: BinaryStream.h:570
size_type size() const
Returns the used size of the buffer set by resize()
Definition: Buffer.h:293
BinaryBufferStreamBase & seekg(std::size_t pos)
Definition: BinaryStream.h:205
decltype(std::declval< Base >().rdbuf()) typedef streambuffer_pointer
Definition: BinaryStream.h:302
BinaryOstream & operator<<(const int32 &v)
Definition: BinaryStream.h:341
pointer data()
Returns a pointer to the underlying data.
Definition: Buffer.h:514
Base::char_type char_type
Definition: BinaryStream.h:530
BinaryIstream & operator>>(std::string &v)
Definition: BinaryStream.h:561
MIRA_BASE_EXPORT void read(const std::string &s, Value &oValue)
Read a json::Value from a string that contains JSON format.
BinaryIstream & fromBinary(T &value)
Reads every type T from this binary stream.
Definition: BinaryStream.h:616
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:631
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:593
BinaryOstream & operator<<(const char &v)
Definition: BinaryStream.h:334
BinaryIstream & operator>>(int16 &v)
Definition: BinaryStream.h:569
fmtflags
Definition: BinaryStream.h:88
BinaryIstream & operator>>(bool &v)
Definition: BinaryStream.h:562
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:523
BinaryOstream & operator<<(const float &v)
Definition: BinaryStream.h:343
BinaryIstream & operator>>(uint32 &v)
Definition: BinaryStream.h:566
Definition: BinaryStream.h:90
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:350
BinaryIstream & operator>>(double &v)
Definition: BinaryStream.h:573
Buffer< uint8 > buffer_type
Definition: BinaryStream.h:147
BinaryIstream & operator>>(Buffer< T > &v)
Definition: BinaryStream.h:575
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:645
BinaryOstream(Base &s)
Constructor.
Definition: BinaryStream.h:323
BinaryIstream & operator>>(char &v)
Definition: BinaryStream.h:563
Definition: BinaryStream.h:91
BinaryIstream & operator>>(int8 &v)
Definition: BinaryStream.h:568
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:572
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:557
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:564