Lunchbox  1.9.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
uint128_t.h
1 
2 /* Copyright (c) 2010, Cedric Stalder <cedric.stalder@gmail.com>
3  * 2010-2014, Stefan Eilemann <eile@eyescale.ch>
4  * 2010-2012, Daniel Nachbaur <danielnachbaur@gmail.com>
5  *
6  * This library is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License version 2.1 as published
8  * by the Free Software Foundation.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef LUNCHBOX_UINT128_H
21 #define LUNCHBOX_UINT128_H
22 
23 #include <lunchbox/api.h>
24 #include <lunchbox/compiler.h>
25 
26 #include <sstream>
27 #ifdef _MSC_VER
28 // Don't include <lunchbox/types.h> to be minimally intrusive for apps
29 // using uint128_t
30 # include <basetsd.h>
31 typedef UINT64 uint64_t;
32 #else
33 # include <stdint.h>
34 #endif
35 
36 namespace lunchbox
37 {
38 class uint128_t;
39 std::ostream& operator << ( std::ostream& os, const uint128_t& id );
40 
46 class uint128_t
47 {
48 public:
53  explicit uint128_t( const unsigned long long low_ = 0 )
54  : _high( 0 ), _low( low_ ) {}
55 
60  explicit uint128_t( const unsigned long low_ ) : _high( 0 ), _low( low_ ) {}
61 
66  explicit uint128_t( const int low_ ) : _high( 0 ), _low( low_ ) {}
67 
68 #ifdef LUNCHBOX_USE_V1_API
69 
77  LUNCHBOX_API explicit uint128_t( const bool generate ) LB_DEPRECATED;
78 #endif
79 
84  uint128_t( const uint64_t high_, const uint64_t low_ )
85  : _high( high_ ), _low( low_ ) {}
86 
91  explicit uint128_t( const std::string& string )
92  : _high( 0 ), _low( 0 ) { *this = string; }
93 
99  bool isUUID() const { return high() != 0; }
100 
103  {
104  _high = rhs._high;
105  _low = rhs._low;
106  return *this;
107  }
108 
110  uint128_t& operator = ( const uint64_t rhs )
111  {
112  _high = 0;
113  _low = rhs;
114  return *this;
115  }
116 
118  uint128_t& operator = ( const int rhs )
119  {
120  _high = 0;
121  _low = rhs;
122  return *this;
123  }
124 
126  LUNCHBOX_API uint128_t& operator = ( const std::string& from );
127 
132  bool operator == ( const lunchbox::uint128_t& rhs ) const
133  { return _high == rhs._high && _low == rhs._low; }
134 
139  bool operator != ( const lunchbox::uint128_t& rhs ) const
140  { return _high != rhs._high || _low != rhs._low; }
141 
146  bool operator == ( const unsigned long long& low_ ) const
147  { return *this == uint128_t( low_ ); }
148 
153  bool operator != ( const unsigned long long& low_ ) const
154  { return *this != uint128_t( low_ ); }
155 
160  bool operator < ( const lunchbox::uint128_t& rhs ) const
161  {
162  if( _high < rhs._high )
163  return true;
164  if( _high > rhs._high )
165  return false;
166  return _low < rhs._low;
167  }
168 
173  bool operator > ( const lunchbox::uint128_t& rhs ) const
174  {
175  if( _high > rhs._high )
176  return true;
177  if( _high < rhs._high )
178  return false;
179  return _low > rhs._low;
180  }
181 
187  bool operator <= ( const lunchbox::uint128_t& rhs ) const
188  {
189  if( _high < rhs._high )
190  return true;
191  if( _high > rhs._high )
192  return false;
193  return _low <= rhs._low;
194  }
195 
201  bool operator >= ( const lunchbox::uint128_t& rhs ) const
202  {
203  if( _high > rhs._high )
204  return true;
205  if( _high < rhs._high )
206  return false;
207  return _low >= rhs._low;
208  }
209 
212  {
213  ++_low;
214  if( !_low )
215  ++_high;
216 
217  return *this;
218  }
219 
222  {
223  if( !_low )
224  --_high;
225  --_low;
226  return *this;
227  }
228 
231  {
232  const uint64_t oldLow = _low;
233  _low += rhs._low;
234  if( _low < oldLow ) // overflow
235  _high += rhs._high + 1;
236  else
237  _high += rhs._high;
238  return *this;
239  }
240 
242  const uint64_t& low() const { return _low; }
244  const uint64_t& high() const { return _high; }
245 
247  uint64_t& low() { return _low; }
249  uint64_t& high() { return _high; }
250 
252  std::string getShortString() const
253  {
254  std::stringstream stream;
255  stream << std::hex << _high << _low;
256  const std::string str = stream.str();
257  return str.substr( 0, 3 ) + ".." +
258  str.substr( str.length() - 3, std::string::npos );
259  }
260 
262  std::string getString() const
263  {
264  std::stringstream stream;
265  stream << *this;
266  return stream.str();
267  }
268 
270  template< class Archive >
271  void serialize( Archive& ar, const unsigned int /*version*/ )
272  {
273  ar & low();
274  ar & high();
275  }
276 
277 #ifdef LUNCHBOX_USE_V1_API
278 
281  bool isGenerated() const LB_DEPRECATED { return high() != 0; }
282 
284  static LUNCHBOX_API const uint128_t ZERO LB_DEPRECATED;
285 #endif
286 
287 private:
288  uint64_t _high;
289  uint64_t _low;
290 };
291 
293 inline std::ostream& operator << ( std::ostream& os, const uint128_t& id )
294 {
295  if( id.high() == 0 )
296  os << std::hex << id.low() << std::dec;
297  else
298  os << std::hex << id.high() << ':' << id.low() << std::dec;
299  return os;
300 }
301 
303 inline std::istream& operator >> ( std::istream& is, uint128_t& id )
304 {
305  std::string str;
306  is >> str;
307  id = str;
308  return is;
309 }
310 
312 inline uint128_t operator+ ( const lunchbox::uint128_t& a, const uint64_t& b )
313 {
314  uint128_t result = a;
315  result.low() += b;
316  if( result.low() < a.low( ))
317  ++result.high();
318  return result;
319 }
320 
323  const lunchbox::uint128_t& b )
324 {
325  uint128_t result = a;
326  result += b;
327  return result;
328 }
329 
331 inline uint128_t operator- ( const lunchbox::uint128_t& a, const uint64_t& b )
332 {
333  uint128_t result = a;
334  result.low() -= b;
335  if( result.low() > a.low( ))
336  --result.high();
337  return result;
338 }
339 
342  const lunchbox::uint128_t& b )
343 {
344  uint128_t result = a;
345  result.high() &= b.high();
346  result.low() &= b.low();
347  return result;
348 }
349 
352  const lunchbox::uint128_t& b )
353 {
354  uint128_t result = a;
355  result.high() |= b.high();
356  result.low() |= b.low();
357  return result;
358 }
359 
368 LUNCHBOX_API uint128_t make_uint128( const char* string );
369 
375 LUNCHBOX_API uint128_t make_UUID();
376 
377 }
378 #endif // LUNCHBOX_UINT128_H
void serialize(Archive &ar, const unsigned int)
Serialize this object to a boost archive.
Definition: uint128_t.h:271
Defines export visibility macros for Lunchbox.
uint128_t & operator--()
Decrement the value.
Definition: uint128_t.h:221
uint128_t operator|(const lunchbox::uint128_t &a, const lunchbox::uint128_t &b)
Bitwise or operation on two 128 bit values.
Definition: uint128_t.h:351
uint128_t(const unsigned long long low_=0)
Construct a new 128 bit integer with a default value.
Definition: uint128_t.h:53
uint128_t operator&(const lunchbox::uint128_t &a, const lunchbox::uint128_t &b)
Bitwise and operation on two 128 bit values.
Definition: uint128_t.h:341
const uint64_t & high() const
Definition: uint128_t.h:244
uint128_t & operator+=(const lunchbox::uint128_t &rhs)
Add value and return the new value.
Definition: uint128_t.h:230
uint64_t & high()
Definition: uint128_t.h:249
bool operator>(const lunchbox::uint128_t &rhs) const
Definition: uint128_t.h:173
uint128_t & operator=(const lunchbox::uint128_t &rhs)
Assign another 128 bit value.
Definition: uint128_t.h:102
uint128_t(const uint64_t high_, const uint64_t low_)
Construct a new 128 bit integer with default values.
Definition: uint128_t.h:84
bool operator<(const lunchbox::uint128_t &rhs) const
Definition: uint128_t.h:160
bool operator<=(const lunchbox::uint128_t &rhs) const
Definition: uint128_t.h:187
const uint64_t & low() const
Definition: uint128_t.h:242
bool operator==(const lunchbox::uint128_t &rhs) const
Definition: uint128_t.h:132
std::ostream & operator<<(std::ostream &os, const Array< T > &array)
Pretty-print all members of the array.
Definition: array.h:47
std::string getShortString() const
Definition: uint128_t.h:252
A base type for 128 bit unsigned integer values.
Definition: uint128_t.h:46
bool operator!=(const lunchbox::uint128_t &rhs) const
Definition: uint128_t.h:139
uint128_t(const std::string &string)
Construct a new 128 bit integer from a string representation.
Definition: uint128_t.h:91
std::string getString() const
Definition: uint128_t.h:262
uint64_t & low()
Definition: uint128_t.h:247
uint128_t operator+(const lunchbox::uint128_t &a, const uint64_t &b)
Add a 64 bit value to a 128 bit value.
Definition: uint128_t.h:312
uint128_t(const int low_)
Construct a new 128 bit integer with a default value.
Definition: uint128_t.h:66
std::istream & operator>>(std::istream &is, uint128_t &id)
istream operator for 128 bit unsigned integers.
Definition: uint128_t.h:303
bool isUUID() const
Definition: uint128_t.h:99
uint128_t(const unsigned long low_)
Construct a new 128 bit integer with a default value.
Definition: uint128_t.h:60
LUNCHBOX_API uint128_t make_uint128(const char *string)
Create a 128 bit integer based on a string.
bool operator>=(const lunchbox::uint128_t &rhs) const
Definition: uint128_t.h:201
uint128_t operator-(const lunchbox::uint128_t &a, const uint64_t &b)
Subtract a 64 bit value from a 128 bit value.
Definition: uint128_t.h:331
uint128_t & operator++()
Increment the value.
Definition: uint128_t.h:211
LUNCHBOX_API uint128_t make_UUID()
Construct a new 128 bit integer with a generated universally unique identifier.