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 <string>
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  static UUID UUID5(const UUID &nsid, const unsigned char *name, size_t name_size);
23  operator std::string() const
24  {
25  char str[40];
26  uuid_unparse(uu, str);
27  return std::string(str);
28  }
32  operator bool() const;
33  const unsigned char *get_bytes() const
34  {
35  return uu;
36  }
37  static constexpr auto size = sizeof(uuid_t);
38 
39  friend bool operator==(const UUID &self, const UUID &other);
40  friend bool operator!=(const UUID &self, const UUID &other);
41  friend bool operator<(const UUID &self, const UUID &other);
42  friend bool operator>(const UUID &self, const UUID &other);
43  size_t hash() const
44  {
45  size_t r = 0;
46  for (size_t i = 0; i < 16; i++) {
47  r ^= ((size_t)uu[i]) << ((i % sizeof(size_t)) * 8);
48  }
49  return r;
50  }
51 
52 private:
53  uuid_t uu;
54 };
55 } // namespace horizon
56 
57 namespace std {
58 template <> struct hash<horizon::UUID> {
59  std::size_t operator()(const horizon::UUID &k) const
60  {
61  return k.hash();
62  }
63 };
64 } // namespace std
This class encapsulates a UUID and allows it to be uses as a value type.
Definition: uuid.hpp:16