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