Servus  1.4.0
C++ network oriented utilities including a zeroconf implementation
uint128_t.h
1 /* Copyright (c) 2010-2015, Cedric Stalder <cedric.stalder@gmail.com>
2  * Stefan Eilemann <eile@eyescale.ch>
3  * Daniel Nachbaur <danielnachbaur@gmail.com>
4  *
5  * This file is part of Servus <https://github.com/HBPVIS/Servus>
6  *
7  * This library is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Lesser General Public License version 3.0 as published
9  * by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 #ifndef SERVUS_UINT128_H
22 #define SERVUS_UINT128_H
23 
24 #include <servus/api.h>
25 
26 #include <sstream>
27 #ifdef _MSC_VER
28 // Don't include <servus/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 servus
37 {
38 class uint128_t;
39 std::ostream& operator << ( std::ostream& os, const uint128_t& id );
40 
46 class uint128_t
47 {
48 public:
52  explicit uint128_t( const unsigned long long low_ = 0 )
53  : _high( 0 ), _low( low_ ) {}
54 
58  explicit uint128_t( const unsigned long low_ ) : _high( 0 ), _low( low_ ) {}
59 
63  explicit uint128_t( const int low_ ) : _high( 0 ), _low( low_ ) {}
64 
68  uint128_t( const uint64_t high_, const uint64_t low_ )
69  : _high( high_ ), _low( low_ ) {}
70 
74  explicit uint128_t( const std::string& string )
75  : _high( 0 ), _low( 0 ) { *this = string; }
76 
81  bool isUUID() const { return high() != 0; }
82 
85  {
86  _high = rhs._high;
87  _low = rhs._low;
88  return *this;
89  }
90 
92  uint128_t& operator = ( const uint64_t rhs )
93  {
94  _high = 0;
95  _low = rhs;
96  return *this;
97  }
98 
100  uint128_t& operator = ( const int rhs )
101  {
102  _high = 0;
103  _low = rhs;
104  return *this;
105  }
106 
108  SERVUS_API uint128_t& operator = ( const std::string& from );
109 
113  bool operator == ( const servus::uint128_t& rhs ) const
114  { return _high == rhs._high && _low == rhs._low; }
115 
119  bool operator != ( const servus::uint128_t& rhs ) const
120  { return _high != rhs._high || _low != rhs._low; }
121 
125  bool operator == ( const unsigned long long& low_ ) const
126  { return *this == uint128_t( low_ ); }
127 
131  bool operator != ( const unsigned long long& low_ ) const
132  { return *this != uint128_t( low_ ); }
133 
137  bool operator < ( const servus::uint128_t& rhs ) const
138  {
139  if( _high < rhs._high )
140  return true;
141  if( _high > rhs._high )
142  return false;
143  return _low < rhs._low;
144  }
145 
149  bool operator > ( const servus::uint128_t& rhs ) const
150  {
151  if( _high > rhs._high )
152  return true;
153  if( _high < rhs._high )
154  return false;
155  return _low > rhs._low;
156  }
157 
162  bool operator <= ( const servus::uint128_t& rhs ) const
163  {
164  if( _high < rhs._high )
165  return true;
166  if( _high > rhs._high )
167  return false;
168  return _low <= rhs._low;
169  }
170 
175  bool operator >= ( const servus::uint128_t& rhs ) const
176  {
177  if( _high > rhs._high )
178  return true;
179  if( _high < rhs._high )
180  return false;
181  return _low >= rhs._low;
182  }
183 
186  {
187  ++_low;
188  if( !_low )
189  ++_high;
190 
191  return *this;
192  }
193 
196  {
197  if( !_low )
198  --_high;
199  --_low;
200  return *this;
201  }
202 
205  {
206  const uint64_t oldLow = _low;
207  _low += rhs._low;
208  if( _low < oldLow ) // overflow
209  _high += rhs._high + 1;
210  else
211  _high += rhs._high;
212  return *this;
213  }
214 
216  const uint64_t& low() const { return _low; }
218  const uint64_t& high() const { return _high; }
219 
221  uint64_t& low() { return _low; }
223  uint64_t& high() { return _high; }
224 
226  std::string getShortString() const
227  {
228  std::stringstream stream;
229  stream << std::hex << _high << _low;
230  const std::string str = stream.str();
231  return str.substr( 0, 3 ) + ".." +
232  str.substr( str.length() - 3, std::string::npos );
233  }
234 
236  std::string getString() const
237  {
238  std::stringstream stream;
239  stream << *this;
240  return stream.str();
241  }
242 
244  template< class Archive >
245  void serialize( Archive& ar, const unsigned int /*version*/ )
246  {
247  ar & low();
248  ar & high();
249  }
250 
251 private:
252  uint64_t _high;
253  uint64_t _low;
254 };
255 
257 inline std::ostream& operator << ( std::ostream& os, const uint128_t& id )
258 {
259  if( id.high() == 0 )
260  os << std::hex << id.low() << std::dec;
261  else
262  os << std::hex << id.high() << ':' << id.low() << std::dec;
263  return os;
264 }
265 
267 inline std::istream& operator >> ( std::istream& is, uint128_t& id )
268 {
269  std::string str;
270  is >> str;
271  id = str;
272  return is;
273 }
274 
276 inline uint128_t operator+ ( const servus::uint128_t& a, const uint64_t& b )
277 {
278  uint128_t result = a;
279  result.low() += b;
280  if( result.low() < a.low( ))
281  ++result.high();
282  return result;
283 }
284 
286 inline uint128_t operator+ ( const servus::uint128_t& a,
287  const servus::uint128_t& b )
288 {
289  uint128_t result = a;
290  result += b;
291  return result;
292 }
293 
295 inline uint128_t operator- ( const servus::uint128_t& a, const uint64_t& b )
296 {
297  uint128_t result = a;
298  result.low() -= b;
299  if( result.low() > a.low( ))
300  --result.high();
301  return result;
302 }
303 
305 inline uint128_t operator & ( const servus::uint128_t& a,
306  const servus::uint128_t& b )
307 {
308  uint128_t result = a;
309  result.high() &= b.high();
310  result.low() &= b.low();
311  return result;
312 }
313 
315 inline uint128_t operator | ( const servus::uint128_t& a,
316  const servus::uint128_t& b )
317 {
318  uint128_t result = a;
319  result.high() |= b.high();
320  result.low() |= b.low();
321  return result;
322 }
323 
331 SERVUS_API uint128_t make_uint128( const char* string );
332 
334 inline uint128_t make_uint128( const std::string& string )
335  { return make_uint128( string.c_str( )); }
336 
341 SERVUS_API uint128_t make_UUID();
342 
343 }
344 
345 #if __cplusplus >= 201103L || _MSC_VER > 1500
346 // C++11
347 # define SERVUS_HASH_NAMESPACE_OPEN namespace std {
348 # define SERVUS_HASH_NAMESPACE_CLOSE }
349 # define SERVUS_HASH_USE_STRUCT
350 # include <functional>
351 #elif defined __clang__ || defined __xlC__ || defined __GNUC__
352 // C++03 with clang and xlC
353 # define SERVUS_HASH_NAMESPACE_OPEN namespace std { namespace tr1 {
354 # define SERVUS_HASH_NAMESPACE_CLOSE }}
355 # define SERVUS_HASH_USE_STRUCT
356 # include <tr1/unordered_set>
357 #elif _MSC_VER < 1600
358 // C++03 with MSVC
359 # define SERVUS_HASH_NAMESPACE_OPEN namespace std {
360 # define SERVUS_HASH_NAMESPACE_CLOSE }
361 # define SERVUS_HASH_USE_FUNCTORS
362 #else
363 // Unknown compiler, hash support is disabled.
364 # define SERVUS_HASH_NONE
365 #endif
366 
367 #ifndef SERVUS_HASH_NONE
368 SERVUS_HASH_NAMESPACE_OPEN
369 # ifdef SERVUS_HASH_USE_STRUCT
370 
371 template<> struct hash< servus::uint128_t >
372 {
373  typedef size_t result_type;
374 
375  result_type operator()( const servus::uint128_t& in ) const
376  {
377  hash< uint64_t > forward;
378  return forward( in.high( )) ^ forward( in.low( ));
379  }
380 };
381 
382 # else // SERVUS_HASH_USE_FUNCTORS
383 
384 template<> inline size_t hash_compare< servus::uint128_t >::operator()
385  ( const servus::uint128_t& key ) const
386 {
387  return static_cast< size_t >( key.high() ^ key.low( ));
388 }
389 
390 template<> inline size_t hash_value( const servus::uint128_t& key )
391  { return static_cast< size_t >( key.high() ^ key.low( )); }
392 # endif
393 
394 SERVUS_HASH_NAMESPACE_CLOSE
395 
396 #endif // SERVUS_HASH
397 #endif // SERVUS_UINT128_H
uint64_t & low()
Definition: uint128_t.h:221
const uint64_t & low() const
Definition: uint128_t.h:216
Defines export visibility macros for library Servus.
uint128_t & operator++()
Increment the value.
Definition: uint128_t.h:185
std::string getString() const
Definition: uint128_t.h:236
bool operator==(const servus::uint128_t &rhs) const
Definition: uint128_t.h:113
uint128_t(const int low_)
Construct a new 128 bit integer with a default value.
Definition: uint128_t.h:63
void serialize(Archive &ar, const unsigned int)
Serialize this object to a boost archive.
Definition: uint128_t.h:245
uint128_t & operator--()
Decrement the value.
Definition: uint128_t.h:195
uint128_t & operator+=(const servus::uint128_t &rhs)
Add value and return the new value.
Definition: uint128_t.h:204
bool operator<(const servus::uint128_t &rhs) const
Definition: uint128_t.h:137
bool operator>=(const servus::uint128_t &rhs) const
Definition: uint128_t.h:175
uint64_t & high()
Definition: uint128_t.h:223
bool operator!=(const servus::uint128_t &rhs) const
Definition: uint128_t.h:119
const uint64_t & high() const
Definition: uint128_t.h:218
std::string getShortString() const
Definition: uint128_t.h:226
bool operator>(const servus::uint128_t &rhs) const
Definition: uint128_t.h:149
uint128_t(const std::string &string)
Construct a new 128 bit integer from a string representation.
Definition: uint128_t.h:74
bool operator<=(const servus::uint128_t &rhs) const
Definition: uint128_t.h:162
uint128_t(const unsigned long long low_=0)
Construct a new 128 bit integer with a default value.
Definition: uint128_t.h:52
uint128_t(const unsigned long low_)
Construct a new 128 bit integer with a default value.
Definition: uint128_t.h:58
A base type for 128 bit unsigned integer values.
Definition: uint128_t.h:46
bool isUUID() const
Definition: uint128_t.h:81
uint128_t(const uint64_t high_, const uint64_t low_)
Construct a new 128 bit integer with default values.
Definition: uint128_t.h:68
uint128_t & operator=(const servus::uint128_t &rhs)
Assign another 128 bit value.
Definition: uint128_t.h:84