2 * SPDX-FileCopyrightText: 2020-2023 Philippe Proulx <pproulx@efficios.com>
3 * SPDX-FileCopyrightText: 2022 Francis Deslauriers <francis.deslauriers@efficios.com>
5 * SPDX-License-Identifier: MIT
8 #ifndef BABELTRACE_CPP_COMMON_BT2C_UUID_HPP
9 #define BABELTRACE_CPP_COMMON_BT2C_UUID_HPP
16 #include "common/assert.h"
17 #include "common/uuid.h"
24 * A view on existing UUID data.
26 * A `UuidView` object doesn't contain its UUID data: see `Uuid` for a
27 * UUID data container.
32 using Val = std::uint8_t;
33 using ConstIter = const Val *;
36 explicit UuidView(const Val * const uuid) noexcept : _mUuid {uuid}
41 explicit UuidView(const Uuid& uuid) noexcept;
42 UuidView(const UuidView&) noexcept = default;
43 UuidView& operator=(const UuidView&) noexcept = default;
45 UuidView& operator=(const Val * const uuid) noexcept
51 operator Uuid() const noexcept;
53 std::string str() const
57 s.resize(BT_UUID_STR_LEN);
58 bt_uuid_to_str(_mUuid, &s[0]);
63 bool operator==(const UuidView& other) const noexcept
65 return bt_uuid_compare(_mUuid, other._mUuid) == 0;
68 bool operator!=(const UuidView& other) const noexcept
70 return !(*this == other);
73 bool operator<(const UuidView& other) const noexcept
75 return bt_uuid_compare(_mUuid, other._mUuid) < 0;
78 static constexpr std::size_t size() noexcept
83 const Val *data() const noexcept
88 Val operator[](const std::size_t index) const noexcept
93 ConstIter begin() const noexcept
98 ConstIter end() const noexcept
100 return _mUuid + this->size();
103 bool isNil() const noexcept
105 return std::all_of(this->begin(), this->end(), [](const std::uint8_t byte) {
115 * A universally unique identifier.
117 * A `Uuid` object contains its UUID data: see `UuidView` to have a
118 * UUID view on existing UUID data.
123 using Val = UuidView::Val;
124 using ConstIter = UuidView::ConstIter;
130 explicit Uuid() noexcept = default;
132 explicit Uuid(const Val * const uuid) noexcept
134 this->_setFromPtr(uuid);
137 explicit Uuid(const char * const str) noexcept
139 const auto ret = bt_uuid_from_str(str, _mUuid.data());
143 explicit Uuid(const std::string& str) noexcept : Uuid {str.c_str()}
147 explicit Uuid(const UuidView& view) noexcept : Uuid {view.data()}
151 Uuid(const Uuid&) noexcept = default;
152 Uuid& operator=(const Uuid&) noexcept = default;
154 Uuid& operator=(const Val * const uuid) noexcept
156 this->_setFromPtr(uuid);
160 static Uuid generate() noexcept
164 bt_uuid_generate(uuidGen);
165 return Uuid {uuidGen};
168 std::string str() const
170 return this->_view().str();
173 bool operator==(const Uuid& other) const noexcept
175 return this->_view() == other._view();
178 bool operator!=(const Uuid& other) const noexcept
180 return this->_view() != other._view();
183 bool operator<(const Uuid& other) const noexcept
185 return this->_view() < other._view();
189 * The returned UUID view must not outlive the UUID object.
191 operator UuidView() const noexcept
193 return this->_view();
196 static constexpr std::size_t size() noexcept
198 return UuidView::size();
201 const Val *data() const noexcept
203 return _mUuid.data();
206 Val operator[](const std::size_t index) const noexcept
208 return this->_view()[index];
211 ConstIter begin() const noexcept
213 return this->_view().begin();
216 ConstIter end() const noexcept
218 return this->_view().end();
221 bool isNil() const noexcept
223 return this->_view().isNil();
228 * std::copy_n() won't throw when simply copying bytes below,
229 * therefore this method won't throw.
231 void _setFromPtr(const Val * const uuid) noexcept
234 std::copy_n(uuid, BT_UUID_LEN, std::begin(_mUuid));
237 UuidView _view() const noexcept
239 return UuidView {_mUuid.data()};
242 std::array<Val, UuidView::size()> _mUuid = {};
245 inline UuidView::UuidView(const Uuid& uuid) noexcept : _mUuid {uuid.data()}
249 inline UuidView::operator Uuid() const noexcept
254 } /* namespace bt2c */
256 #endif /* BABELTRACE_CPP_COMMON_BT2C_UUID_HPP */