2 * Copyright (c) 2022 Francis Deslauriers <francis.deslauriers@efficios.com>
4 * SPDX-License-Identifier: MIT
7 #ifndef BABELTRACE_CPP_COMMON_UUID_HPP
8 #define BABELTRACE_CPP_COMMON_UUID_HPP
15 #include "common/assert.h"
16 #include "common/uuid.h"
18 #include "uuid-view.hpp"
20 namespace bt2_common {
23 * A universally unique identifier.
25 * A `Uuid` object contains its UUID data: see `UuidView` to have a
26 * UUID view on existing UUID data.
31 using Val = UuidView::Val;
32 using ConstIter = UuidView::ConstIter;
38 explicit Uuid() noexcept = default;
40 explicit Uuid(const Val * const uuid) noexcept
42 this->_setFromPtr(uuid);
45 explicit Uuid(const char * const str) noexcept
47 const auto ret = bt_uuid_from_str(str, _mUuid.data());
51 explicit Uuid(const std::string& str) noexcept : Uuid {str.c_str()}
55 explicit Uuid(const UuidView& view) noexcept : Uuid {view.data()}
59 Uuid(const Uuid&) noexcept = default;
60 Uuid& operator=(const Uuid&) noexcept = default;
62 Uuid& operator=(const Val * const uuid) noexcept
64 this->_setFromPtr(uuid);
68 static Uuid generate() noexcept
72 bt_uuid_generate(uuidGen);
73 return Uuid {uuidGen};
76 std::string str() const
78 return this->_view().str();
81 bool operator==(const Uuid& other) const noexcept
83 return this->_view() == other._view();
86 bool operator!=(const Uuid& other) const noexcept
88 return this->_view() != other._view();
91 bool operator<(const Uuid& other) const noexcept
93 return this->_view() < other._view();
97 * The returned UUID view must not outlive the UUID object.
99 operator UuidView() const noexcept
101 return this->_view();
104 static constexpr std::size_t size() noexcept
106 return UuidView::size();
109 const Val *data() const noexcept
111 return _mUuid.data();
114 Val operator[](const std::size_t index) const noexcept
116 return this->_view()[index];
119 ConstIter begin() const noexcept
121 return this->_view().begin();
124 ConstIter end() const noexcept
126 return this->_view().end();
129 bool isNil() const noexcept
131 return this->_view().isNil();
136 * std::copy_n() won't throw when simply copying bytes below,
137 * therefore this method won't throw.
139 void _setFromPtr(const Val * const uuid) noexcept
142 std::copy_n(uuid, BT_UUID_LEN, std::begin(_mUuid));
145 UuidView _view() const noexcept
147 return UuidView {_mUuid.data()};
150 std::array<Val, UuidView::size()> _mUuid = {};
153 } /* namespace bt2_common */
155 #endif /* BABELTRACE_CPP_COMMON_UUID_HPP */