-
Notifications
You must be signed in to change notification settings - Fork 31
/
Uuid.hpp
82 lines (69 loc) · 1.5 KB
/
Uuid.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#ifndef UUID_HPP
#define UUID_HPP
#include<cstdint>
#include<iostream>
#include<memory>
#include<string>
#include<utility>
/** class Uuid
*
* @brief a unique 128-bit identifier.
*
* @desc this is ***not*** an RFC4122-standard UUID!
* This is just 16 random bytes that we think is
* sufficiently unique for this application.
*/
class Uuid {
private:
struct Impl {
std::uint8_t data[16];
};
std::shared_ptr<Impl> pimpl;
public:
Uuid() =default;
Uuid(Uuid&&) =default;
Uuid(Uuid const&) =default;
Uuid& operator=(Uuid&&) =default;
Uuid& operator=(Uuid const&) =default;
~Uuid() =default;
/* Construct a fresh random UUID that has never
* been used before (with high probability). */
static Uuid random();
bool operator==(Uuid const& o) const;
bool operator!=(Uuid const& o) const {
return !(*this == o);
}
operator bool() const;
bool operator!() const {
return !bool(*this);
}
explicit operator std::string() const;
explicit Uuid(std::string const&);
static
bool valid_string(std::string const&);
std::size_t hash() const {
if (!pimpl)
return 0;
return *reinterpret_cast<std::size_t*>(pimpl->data);
}
};
inline
std::ostream& operator<<(std::ostream& os, Uuid const& i) {
return os << std::string(i);
}
inline
std::istream& operator>>(std::istream& is, Uuid& i) {
auto s = std::string();
is >> s;
i = Uuid(s);
return is;
}
namespace std {
template<>
struct hash<Uuid> {
std::size_t operator()(Uuid const& i) const {
return i.hash();
}
};
}
#endif /* !defined(UUID_HPP) */