Commit | Line | Data |
---|---|---|
638a0d12 SM |
1 | /* |
2 | * Copyright (c) 2022 Francis Deslauriers <francis.deslauriers@efficios.com> | |
3 | * | |
4 | * SPDX-License-Identifier: MIT | |
5 | */ | |
6 | ||
7 | #ifndef BABELTRACE_CPP_COMMON_UUID_HPP | |
8 | #define BABELTRACE_CPP_COMMON_UUID_HPP | |
9 | ||
10 | #include <algorithm> | |
11 | #include <array> | |
12 | #include <cstdint> | |
13 | #include <string> | |
14 | ||
15 | #include "common/assert.h" | |
16 | #include "common/uuid.h" | |
17 | #include "uuid-view.hpp" | |
18 | ||
19 | namespace bt2_common { | |
20 | ||
21 | /* | |
22 | * A universally unique identifier. | |
23 | * | |
24 | * A `Uuid` object contains its UUID data: see `UuidView` to have a | |
25 | * UUID view on existing UUID data. | |
26 | */ | |
27 | class Uuid final | |
28 | { | |
29 | public: | |
30 | using Val = UuidView::Val; | |
31 | using ConstIter = UuidView::ConstIter; | |
32 | ||
33 | public: | |
34 | /* | |
35 | * Builds a nil UUID. | |
36 | */ | |
37 | explicit Uuid() noexcept = default; | |
38 | ||
39 | explicit Uuid(const Val * const uuid) noexcept | |
40 | { | |
41 | this->_setFromPtr(uuid); | |
42 | } | |
43 | ||
44 | explicit Uuid(const char * const str) noexcept | |
45 | { | |
46 | const auto ret = bt_uuid_from_str(str, _mUuid.data()); | |
47 | BT_ASSERT(ret == 0); | |
48 | } | |
49 | ||
50 | explicit Uuid(const std::string& str) noexcept : Uuid {str.c_str()} | |
51 | { | |
52 | } | |
53 | ||
54 | explicit Uuid(const UuidView& view) noexcept : Uuid {view.data()} | |
55 | { | |
56 | } | |
57 | ||
58 | Uuid(const Uuid&) noexcept = default; | |
59 | Uuid& operator=(const Uuid&) noexcept = default; | |
60 | ||
61 | Uuid& operator=(const Val * const uuid) noexcept | |
62 | { | |
63 | this->_setFromPtr(uuid); | |
64 | return *this; | |
65 | } | |
66 | ||
67 | static Uuid generate() noexcept | |
68 | { | |
69 | bt_uuid_t uuidGen; | |
70 | ||
71 | bt_uuid_generate(uuidGen); | |
72 | return Uuid {uuidGen}; | |
73 | } | |
74 | ||
75 | std::string str() const | |
76 | { | |
77 | return this->_view().str(); | |
78 | } | |
79 | ||
80 | bool operator==(const Uuid& other) const noexcept | |
81 | { | |
82 | return this->_view() == other._view(); | |
83 | } | |
84 | ||
85 | bool operator!=(const Uuid& other) const noexcept | |
86 | { | |
87 | return this->_view() != other._view(); | |
88 | } | |
89 | ||
90 | bool operator<(const Uuid& other) const noexcept | |
91 | { | |
92 | return this->_view() < other._view(); | |
93 | } | |
94 | ||
95 | /* | |
96 | * The returned UUID view must not outlive the UUID object. | |
97 | */ | |
98 | operator UuidView() const noexcept | |
99 | { | |
100 | return this->_view(); | |
101 | } | |
102 | ||
103 | static constexpr std::size_t size() noexcept | |
104 | { | |
105 | return UuidView::size(); | |
106 | } | |
107 | ||
108 | const Val *data() const noexcept | |
109 | { | |
110 | return _mUuid.data(); | |
111 | } | |
112 | ||
113 | Val operator[](const std::size_t index) const noexcept | |
114 | { | |
115 | return this->_view()[index]; | |
116 | } | |
117 | ||
118 | ConstIter begin() const noexcept | |
119 | { | |
120 | return this->_view().begin(); | |
121 | } | |
122 | ||
123 | ConstIter end() const noexcept | |
124 | { | |
125 | return this->_view().end(); | |
126 | } | |
127 | ||
128 | bool isNil() const noexcept | |
129 | { | |
130 | return this->_view().isNil(); | |
131 | } | |
132 | ||
133 | private: | |
134 | /* | |
135 | * std::copy_n() won't throw when simply copying bytes below, | |
136 | * therefore this method won't throw. | |
137 | */ | |
138 | void _setFromPtr(const Val * const uuid) noexcept | |
139 | { | |
140 | BT_ASSERT(uuid); | |
141 | std::copy_n(uuid, BT_UUID_LEN, std::begin(_mUuid)); | |
142 | } | |
143 | ||
144 | UuidView _view() const noexcept | |
145 | { | |
146 | return UuidView {_mUuid.data()}; | |
147 | } | |
148 | ||
149 | std::array<Val, UuidView::size()> _mUuid = {}; | |
150 | }; | |
151 | ||
152 | } /* namespace bt2_common */ | |
153 | ||
154 | #endif /* BABELTRACE_CPP_COMMON_UUID_HPP */ |