MIRA
HashStream.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 
47 #ifndef _MIRA_HASH_STREAM_H_
48 #define _MIRA_HASH_STREAM_H_
49 
50 #include <ostream>
51 #include <string>
52 #include <vector>
53 
54 #include <platform/Types.h>
55 
56 namespace mira {
57 
59 
66  public std::vector<uint8>
67 {
68 public:
73  HashDigest(size_t iLength = 0) :
74  std::vector<uint8>(iLength) {}
75 
80  std::string toString() const;
81 };
82 
84 
94  public std::basic_streambuf<char, std::char_traits<char> >
95 {
96 public:
100  virtual void reset() = 0;
101 
106  virtual HashDigest getDigest() const = 0;
107 };
108 
110 
156 template<class T>
157 class HashStream :
158  public std::ostream
159 {
160 public:
162  HashStream() : std::ostream(&mBuffer) {}
163 
165  virtual ~HashStream() {}
166 
170  virtual void reset() { mBuffer.reset(); }
171 
176  HashDigest getDigest() const { return(mBuffer.getDigest()); }
177 
178 public:
180  HashStream& operator<<(const char* value) {
181  write(value, strlen(value));
182  return *this;
183  }
184 
186  HashStream& operator<<(const std::string& value) {
187  write(value.c_str(), value.size());
188  return *this;
189  }
190 
192  HashStream& operator<<(const bool& value) {
193  write((char*)&value, sizeof(bool));
194  return *this;
195  }
196 
198  HashStream& operator<<(const char& value) {
199  write(&value, sizeof(char));
200  return *this;
201  }
202 
204  HashStream& operator<<(const uint8& value) {
205  write((char*)&value, sizeof(uint8));
206  return *this;
207  }
208 
210  HashStream& operator<<(const uint16& value) {
211  write((char*)&value, sizeof(uint16));
212  return *this;
213  }
214 
216  HashStream& operator<<(const uint32& value) {
217  write((char*)&value, sizeof(uint32));
218  return *this;
219  }
220 
222  HashStream& operator<<(const uint64& value) {
223  write((char*)&value, sizeof(uint64));
224  return *this;
225  }
226 
228  HashStream& operator<<(const int8& value) {
229  write((char*)&value, sizeof(int8));
230  return *this;
231  }
232 
234  HashStream& operator<<(const int16& value) {
235  write((char*)&value, sizeof(int16));
236  return *this;
237  }
238 
240  HashStream& operator<<(const int32& value) {
241  write((char*)&value, sizeof(int32));
242  return *this;
243  }
244 
246  HashStream& operator<<(const int64& value) {
247  write((char*)&value, sizeof(int64));
248  return *this;
249  }
250 
252  HashStream& operator<<(const float& value) {
253  write((char*)&value, sizeof(float));
254  return *this;
255  }
256 
258  HashStream& operator<<(const double& value) {
259  write((char*)&value, sizeof(double));
260  return *this;
261  }
262 
263 private:
264  T mBuffer;
265 };
266 
268 
269 } // namespace
270 
271 #endif
HashStream & operator<<(const std::string &value)
Put a STL string in the hash stream.
Definition: HashStream.h:186
HashDigest getDigest() const
Return the current digest of the hash algorithm.
Definition: HashStream.h:176
A generic hash streambuf class.
Definition: HashStream.h:93
Typedefs for OS independent basic data types.
HashStream & operator<<(const char *value)
Put a C-string in the hash stream.
Definition: HashStream.h:180
HashStream & operator<<(const uint32 &value)
Put a uint32 in the hash stream.
Definition: HashStream.h:216
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
HashStream & operator<<(const char &value)
Put a char in the hash stream.
Definition: HashStream.h:198
HashStream & operator<<(const uint64 &value)
Put a uint64 in the hash stream.
Definition: HashStream.h:222
STL namespace.
virtual void reset()=0
Reset the hash value.
virtual void reset()
Reset the hash value.
Definition: HashStream.h:170
std::string toString(const T &value, int precision=-1)
Converts any data type to string (the data type must support the stream << operator).
Definition: ToString.h:256
HashStream & operator<<(const int16 &value)
Put a int16 in the hash stream.
Definition: HashStream.h:234
HashStream & operator<<(const int64 &value)
Put a int64 in the hash stream.
Definition: HashStream.h:246
A generic hash digest, which consists of an array of bytes.
Definition: HashStream.h:65
HashStream & operator<<(const double &value)
Put a double in the hash stream.
Definition: HashStream.h:258
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.
HashStream & operator<<(const int32 &value)
Put a int32 in the hash stream.
Definition: HashStream.h:240
HashStream & operator<<(const float &value)
Put a float in the hash stream.
Definition: HashStream.h:252
HashStream & operator<<(const uint8 &value)
Put a uint8 in the hash stream.
Definition: HashStream.h:204
virtual HashDigest getDigest() const =0
Return the current digest of the hash algorithm.
HashStream & operator<<(const uint16 &value)
Put a uint16 in the hash stream.
Definition: HashStream.h:210
virtual ~HashStream()
The destructor.
Definition: HashStream.h:165
HashStream & operator<<(const int8 &value)
Put a int8 in the hash stream.
Definition: HashStream.h:228
HashStream()
Th default constructor.
Definition: HashStream.h:162
#define MIRA_BASE_EXPORT
This is required because on windows there is a macro defined called ERROR.
Definition: Platform.h:153
HashDigest(size_t iLength=0)
Constructor.
Definition: HashStream.h:73
HashStream & operator<<(const bool &value)
Put a bool in the hash stream.
Definition: HashStream.h:192
A template base class for hash functions based on std::ostream.
Definition: HashStream.h:157