]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | #ifndef _CEPH_UUID_H |
2 | #define _CEPH_UUID_H | |
3 | ||
4 | /* | |
5 | * Thin C++ wrapper around libuuid. | |
6 | */ | |
7 | ||
8 | #include "encoding.h" | |
91327a77 | 9 | |
7c673cae | 10 | #include <ostream> |
91327a77 | 11 | #include <random> |
7c673cae FG |
12 | |
13 | #include <boost/uuid/uuid.hpp> | |
14 | #include <boost/uuid/uuid_generators.hpp> | |
15 | #include <boost/uuid/uuid_io.hpp> | |
7c673cae FG |
16 | |
17 | struct uuid_d { | |
18 | boost::uuids::uuid uuid; | |
19 | ||
20 | uuid_d() { | |
21 | boost::uuids::nil_generator gen; | |
22 | uuid = gen(); | |
23 | } | |
24 | ||
25 | bool is_zero() const { | |
26 | return uuid.is_nil(); | |
27 | } | |
28 | ||
29 | void generate_random() { | |
91327a77 AA |
30 | std::random_device rng; |
31 | boost::uuids::basic_random_generator<decltype(rng)> gen(rng); | |
7c673cae FG |
32 | uuid = gen(); |
33 | } | |
34 | ||
35 | bool parse(const char *s) { | |
36 | try { | |
37 | boost::uuids::string_generator gen; | |
38 | uuid = gen(s); | |
39 | return true; | |
40 | } catch (std::runtime_error& e) { | |
41 | return false; | |
42 | } | |
43 | } | |
44 | void print(char *s) const { | |
45 | memcpy(s, boost::uuids::to_string(uuid).c_str(), 37); | |
46 | } | |
47 | ||
48 | std::string to_string() const { | |
49 | return boost::uuids::to_string(uuid); | |
50 | } | |
51 | ||
52 | char *bytes() const { | |
53 | return (char*)uuid.data; | |
54 | } | |
55 | ||
56 | void encode(bufferlist& bl) const { | |
57 | ::encode_raw(uuid, bl); | |
58 | } | |
59 | ||
60 | void decode(bufferlist::iterator& p) const { | |
61 | ::decode_raw(uuid, p); | |
62 | } | |
63 | }; | |
64 | WRITE_CLASS_ENCODER(uuid_d) | |
65 | ||
66 | inline std::ostream& operator<<(std::ostream& out, const uuid_d& u) { | |
67 | char b[37]; | |
68 | u.print(b); | |
69 | return out << b; | |
70 | } | |
71 | ||
72 | inline bool operator==(const uuid_d& l, const uuid_d& r) { | |
73 | return l.uuid == r.uuid; | |
74 | } | |
75 | inline bool operator!=(const uuid_d& l, const uuid_d& r) { | |
76 | return l.uuid != r.uuid; | |
77 | } | |
c07f9fc5 FG |
78 | inline bool operator<(const uuid_d& l, const uuid_d& r) { |
79 | return l.to_string() < r.to_string(); | |
80 | } | |
7c673cae FG |
81 | |
82 | ||
83 | #endif |