Horizon
uuid.hpp
1 #pragma once
2 #ifdef WIN32_UUID
3 #include "uuid_win32.hpp"
4 #else
5 #include <uuid/uuid.h>
6 #endif
7 
8 #include <iostream>
9 
10 namespace horizon {
16 class UUID {
17 public:
18  UUID();
19  static UUID random();
20  UUID(const char *str);
21  UUID(const std::string &str);
22  operator std::string() const
23  {
24  char str[40];
25  uuid_unparse(uu, str);
26  return std::string(str);
27  }
31  operator bool() const;
32 
33  friend bool operator==(const UUID &self, const UUID &other);
34  friend bool operator!=(const UUID &self, const UUID &other);
35  friend bool operator<(const UUID &self, const UUID &other);
36  friend bool operator>(const UUID &self, const UUID &other);
37  size_t hash() const
38  {
39  size_t r = 0;
40  for (size_t i = 0; i < 16; i++) {
41  r ^= ((size_t)uu[i]) << ((i % sizeof(size_t)) * 8);
42  }
43  return r;
44  }
45 
46 private:
47  uuid_t uu;
48 };
49 } // namespace horizon
50 
51 namespace std {
52 template <> struct hash<horizon::UUID> {
53  std::size_t operator()(const horizon::UUID &k) const
54  {
55  return k.hash();
56  }
57 };
58 } // namespace std
horizon::UUID
This class encapsulates a UUID and allows it to be uses as a value type.
Definition: uuid.hpp:16