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"
18 #include "cpp-common/bt2c/c-string-view.hpp"
25 * A view on existing UUID data.
27 * A `UuidView` object doesn't contain its UUID data: see `Uuid` for a
28 * UUID data container.
33 using Val = std::uint8_t;
34 using ConstIter = const Val *;
37 explicit UuidView(const Val * const uuid) noexcept : _mUuid {uuid}
42 explicit UuidView(const Uuid& uuid) noexcept;
43 UuidView(const UuidView&) noexcept = default;
44 UuidView& operator=(const UuidView&) noexcept = default;
46 UuidView& operator=(const Val * const uuid) noexcept
52 operator Uuid() const noexcept;
54 std::string str() const
58 s.resize(BT_UUID_STR_LEN);
59 bt_uuid_to_str(_mUuid, &s[0]);
64 bool operator==(const UuidView& other) const noexcept
66 return bt_uuid_compare(_mUuid, other._mUuid) == 0;
69 bool operator!=(const UuidView& other) const noexcept
71 return !(*this == other);
74 bool operator<(const UuidView& other) const noexcept
76 return bt_uuid_compare(_mUuid, other._mUuid) < 0;
79 static constexpr std::size_t size() noexcept
84 const Val *data() const noexcept
89 Val operator[](const std::size_t index) const noexcept
94 ConstIter begin() const noexcept
99 ConstIter end() const noexcept
101 return _mUuid + this->size();
104 bool isNil() const noexcept
106 return std::all_of(this->begin(), this->end(), [](const std::uint8_t byte) {
116 * A universally unique identifier.
118 * A `Uuid` object contains its UUID data: see `UuidView` to have a
119 * UUID view on existing UUID data.
124 using Val = UuidView::Val;
125 using ConstIter = UuidView::ConstIter;
131 explicit Uuid() noexcept = default;
133 explicit Uuid(const Val * const uuid) noexcept
135 this->_setFromPtr(uuid);
138 explicit Uuid(const bt2c::CStringView str) noexcept
140 const auto ret = bt_uuid_from_str(str.data(), _mUuid.data());
144 explicit Uuid(const UuidView& view) noexcept : Uuid {view.data()}
148 Uuid(const Uuid&) noexcept = default;
149 Uuid& operator=(const Uuid&) noexcept = default;
151 Uuid& operator=(const Val * const uuid) noexcept
153 this->_setFromPtr(uuid);
157 static Uuid generate() noexcept
161 bt_uuid_generate(uuidGen);
162 return Uuid {uuidGen};
165 std::string str() const
167 return this->_view().str();
170 bool operator==(const Uuid& other) const noexcept
172 return this->_view() == other._view();
175 bool operator!=(const Uuid& other) const noexcept
177 return this->_view() != other._view();
180 bool operator<(const Uuid& other) const noexcept
182 return this->_view() < other._view();
186 * The returned UUID view must not outlive the UUID object.
188 operator UuidView() const noexcept
190 return this->_view();
193 static constexpr std::size_t size() noexcept
195 return UuidView::size();
198 const Val *data() const noexcept
200 return _mUuid.data();
203 Val operator[](const std::size_t index) const noexcept
205 return this->_view()[index];
208 ConstIter begin() const noexcept
210 return this->_view().begin();
213 ConstIter end() const noexcept
215 return this->_view().end();
218 bool isNil() const noexcept
220 return this->_view().isNil();
225 * std::copy_n() won't throw when simply copying bytes below,
226 * therefore this method won't throw.
228 void _setFromPtr(const Val * const uuid) noexcept
231 std::copy_n(uuid, BT_UUID_LEN, std::begin(_mUuid));
234 UuidView _view() const noexcept
236 return UuidView {_mUuid.data()};
239 std::array<Val, UuidView::size()> _mUuid = {};
242 inline UuidView::UuidView(const Uuid& uuid) noexcept : _mUuid {uuid.data()}
246 inline UuidView::operator Uuid() const noexcept
251 } /* namespace bt2c */
253 #endif /* BABELTRACE_CPP_COMMON_BT2C_UUID_HPP */