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