Commit | Line | Data |
---|---|---|
638a0d12 | 1 | /* |
12958bb9 SM |
2 | * SPDX-FileCopyrightText: 2020-2023 Philippe Proulx <pproulx@efficios.com> |
3 | * SPDX-FileCopyrightText: 2022 Francis Deslauriers <francis.deslauriers@efficios.com> | |
638a0d12 SM |
4 | * |
5 | * SPDX-License-Identifier: MIT | |
6 | */ | |
7 | ||
094bf3f2 SM |
8 | #ifndef BABELTRACE_CPP_COMMON_BT2C_UUID_HPP |
9 | #define BABELTRACE_CPP_COMMON_BT2C_UUID_HPP | |
638a0d12 SM |
10 | |
11 | #include <algorithm> | |
12 | #include <array> | |
12958bb9 | 13 | #include <cstdint> |
638a0d12 SM |
14 | #include <string> |
15 | ||
16 | #include "common/assert.h" | |
17 | #include "common/uuid.h" | |
c802cacb | 18 | |
094bf3f2 | 19 | namespace bt2c { |
638a0d12 | 20 | |
12958bb9 SM |
21 | class Uuid; |
22 | ||
23 | /* | |
24 | * A view on existing UUID data. | |
25 | * | |
26 | * A `UuidView` object doesn't contain its UUID data: see `Uuid` for a | |
27 | * UUID data container. | |
28 | */ | |
29 | class UuidView final | |
30 | { | |
31 | public: | |
32 | using Val = std::uint8_t; | |
33 | using ConstIter = const Val *; | |
34 | ||
35 | public: | |
36 | explicit UuidView(const Val * const uuid) noexcept : _mUuid {uuid} | |
37 | { | |
38 | BT_ASSERT_DBG(uuid); | |
39 | } | |
40 | ||
41 | explicit UuidView(const Uuid& uuid) noexcept; | |
42 | UuidView(const UuidView&) noexcept = default; | |
43 | UuidView& operator=(const UuidView&) noexcept = default; | |
44 | ||
45 | UuidView& operator=(const Val * const uuid) noexcept | |
46 | { | |
47 | _mUuid = uuid; | |
48 | return *this; | |
49 | } | |
50 | ||
51 | operator Uuid() const noexcept; | |
52 | ||
53 | std::string str() const | |
54 | { | |
55 | std::string s; | |
56 | ||
57 | s.resize(BT_UUID_STR_LEN); | |
58 | bt_uuid_to_str(_mUuid, &s[0]); | |
59 | ||
60 | return s; | |
61 | } | |
62 | ||
63 | bool operator==(const UuidView& other) const noexcept | |
64 | { | |
65 | return bt_uuid_compare(_mUuid, other._mUuid) == 0; | |
66 | } | |
67 | ||
68 | bool operator!=(const UuidView& other) const noexcept | |
69 | { | |
70 | return !(*this == other); | |
71 | } | |
72 | ||
73 | bool operator<(const UuidView& other) const noexcept | |
74 | { | |
75 | return bt_uuid_compare(_mUuid, other._mUuid) < 0; | |
76 | } | |
77 | ||
78 | static constexpr std::size_t size() noexcept | |
79 | { | |
80 | return BT_UUID_LEN; | |
81 | } | |
82 | ||
83 | const Val *data() const noexcept | |
84 | { | |
85 | return _mUuid; | |
86 | } | |
87 | ||
88 | Val operator[](const std::size_t index) const noexcept | |
89 | { | |
90 | return _mUuid[index]; | |
91 | } | |
92 | ||
93 | ConstIter begin() const noexcept | |
94 | { | |
95 | return _mUuid; | |
96 | } | |
97 | ||
98 | ConstIter end() const noexcept | |
99 | { | |
100 | return _mUuid + this->size(); | |
101 | } | |
102 | ||
103 | bool isNil() const noexcept | |
104 | { | |
105 | return std::all_of(this->begin(), this->end(), [](const std::uint8_t byte) { | |
106 | return byte == 0; | |
107 | }); | |
108 | } | |
109 | ||
110 | private: | |
111 | const Val *_mUuid; | |
112 | }; | |
113 | ||
638a0d12 SM |
114 | /* |
115 | * A universally unique identifier. | |
116 | * | |
117 | * A `Uuid` object contains its UUID data: see `UuidView` to have a | |
118 | * UUID view on existing UUID data. | |
119 | */ | |
120 | class Uuid final | |
121 | { | |
122 | public: | |
123 | using Val = UuidView::Val; | |
124 | using ConstIter = UuidView::ConstIter; | |
125 | ||
126 | public: | |
127 | /* | |
128 | * Builds a nil UUID. | |
129 | */ | |
130 | explicit Uuid() noexcept = default; | |
131 | ||
132 | explicit Uuid(const Val * const uuid) noexcept | |
133 | { | |
134 | this->_setFromPtr(uuid); | |
135 | } | |
136 | ||
137 | explicit Uuid(const char * const str) noexcept | |
138 | { | |
139 | const auto ret = bt_uuid_from_str(str, _mUuid.data()); | |
140 | BT_ASSERT(ret == 0); | |
141 | } | |
142 | ||
143 | explicit Uuid(const std::string& str) noexcept : Uuid {str.c_str()} | |
144 | { | |
145 | } | |
146 | ||
147 | explicit Uuid(const UuidView& view) noexcept : Uuid {view.data()} | |
148 | { | |
149 | } | |
150 | ||
151 | Uuid(const Uuid&) noexcept = default; | |
152 | Uuid& operator=(const Uuid&) noexcept = default; | |
153 | ||
154 | Uuid& operator=(const Val * const uuid) noexcept | |
155 | { | |
156 | this->_setFromPtr(uuid); | |
157 | return *this; | |
158 | } | |
159 | ||
160 | static Uuid generate() noexcept | |
161 | { | |
162 | bt_uuid_t uuidGen; | |
163 | ||
164 | bt_uuid_generate(uuidGen); | |
165 | return Uuid {uuidGen}; | |
166 | } | |
167 | ||
168 | std::string str() const | |
169 | { | |
170 | return this->_view().str(); | |
171 | } | |
172 | ||
173 | bool operator==(const Uuid& other) const noexcept | |
174 | { | |
175 | return this->_view() == other._view(); | |
176 | } | |
177 | ||
178 | bool operator!=(const Uuid& other) const noexcept | |
179 | { | |
180 | return this->_view() != other._view(); | |
181 | } | |
182 | ||
183 | bool operator<(const Uuid& other) const noexcept | |
184 | { | |
185 | return this->_view() < other._view(); | |
186 | } | |
187 | ||
188 | /* | |
189 | * The returned UUID view must not outlive the UUID object. | |
190 | */ | |
191 | operator UuidView() const noexcept | |
192 | { | |
193 | return this->_view(); | |
194 | } | |
195 | ||
196 | static constexpr std::size_t size() noexcept | |
197 | { | |
198 | return UuidView::size(); | |
199 | } | |
200 | ||
201 | const Val *data() const noexcept | |
202 | { | |
203 | return _mUuid.data(); | |
204 | } | |
205 | ||
206 | Val operator[](const std::size_t index) const noexcept | |
207 | { | |
208 | return this->_view()[index]; | |
209 | } | |
210 | ||
211 | ConstIter begin() const noexcept | |
212 | { | |
213 | return this->_view().begin(); | |
214 | } | |
215 | ||
216 | ConstIter end() const noexcept | |
217 | { | |
218 | return this->_view().end(); | |
219 | } | |
220 | ||
221 | bool isNil() const noexcept | |
222 | { | |
223 | return this->_view().isNil(); | |
224 | } | |
225 | ||
226 | private: | |
227 | /* | |
228 | * std::copy_n() won't throw when simply copying bytes below, | |
229 | * therefore this method won't throw. | |
230 | */ | |
231 | void _setFromPtr(const Val * const uuid) noexcept | |
232 | { | |
233 | BT_ASSERT(uuid); | |
234 | std::copy_n(uuid, BT_UUID_LEN, std::begin(_mUuid)); | |
235 | } | |
236 | ||
237 | UuidView _view() const noexcept | |
238 | { | |
239 | return UuidView {_mUuid.data()}; | |
240 | } | |
241 | ||
242 | std::array<Val, UuidView::size()> _mUuid = {}; | |
243 | }; | |
244 | ||
12958bb9 SM |
245 | inline UuidView::UuidView(const Uuid& uuid) noexcept : _mUuid {uuid.data()} |
246 | { | |
247 | } | |
248 | ||
249 | inline UuidView::operator Uuid() const noexcept | |
250 | { | |
251 | return Uuid {*this}; | |
252 | } | |
253 | ||
094bf3f2 | 254 | } /* namespace bt2c */ |
638a0d12 | 255 | |
094bf3f2 | 256 | #endif /* BABELTRACE_CPP_COMMON_BT2C_UUID_HPP */ |