2 * Copyright (c) 2022 Francis Deslauriers <francis.deslauriers@efficios.com>
4 * SPDX-License-Identifier: MIT
7 #ifndef BABELTRACE_CPP_COMMON_BT2C_UUID_HPP
8 #define BABELTRACE_CPP_COMMON_BT2C_UUID_HPP
14 #include "common/assert.h"
15 #include "common/uuid.h"
17 #include "uuid-view.hpp"
22 * A universally unique identifier.
24 * A `Uuid` object contains its UUID data: see `UuidView` to have a
25 * UUID view on existing UUID data.
30 using Val = UuidView::Val;
31 using ConstIter = UuidView::ConstIter;
37 explicit Uuid() noexcept = default;
39 explicit Uuid(const Val * const uuid) noexcept
41 this->_setFromPtr(uuid);
44 explicit Uuid(const char * const str) noexcept
46 const auto ret = bt_uuid_from_str(str, _mUuid.data());
50 explicit Uuid(const std::string& str) noexcept : Uuid {str.c_str()}
54 explicit Uuid(const UuidView& view) noexcept : Uuid {view.data()}
58 Uuid(const Uuid&) noexcept = default;
59 Uuid& operator=(const Uuid&) noexcept = default;
61 Uuid& operator=(const Val * const uuid) noexcept
63 this->_setFromPtr(uuid);
67 static Uuid generate() noexcept
71 bt_uuid_generate(uuidGen);
72 return Uuid {uuidGen};
75 std::string str() const
77 return this->_view().str();
80 bool operator==(const Uuid& other) const noexcept
82 return this->_view() == other._view();
85 bool operator!=(const Uuid& other) const noexcept
87 return this->_view() != other._view();
90 bool operator<(const Uuid& other) const noexcept
92 return this->_view() < other._view();
96 * The returned UUID view must not outlive the UUID object.
98 operator UuidView() const noexcept
100 return this->_view();
103 static constexpr std::size_t size() noexcept
105 return UuidView::size();
108 const Val *data() const noexcept
110 return _mUuid.data();
113 Val operator[](const std::size_t index) const noexcept
115 return this->_view()[index];
118 ConstIter begin() const noexcept
120 return this->_view().begin();
123 ConstIter end() const noexcept
125 return this->_view().end();
128 bool isNil() const noexcept
130 return this->_view().isNil();
135 * std::copy_n() won't throw when simply copying bytes below,
136 * therefore this method won't throw.
138 void _setFromPtr(const Val * const uuid) noexcept
141 std::copy_n(uuid, BT_UUID_LEN, std::begin(_mUuid));
144 UuidView _view() const noexcept
146 return UuidView {_mUuid.data()};
149 std::array<Val, UuidView::size()> _mUuid = {};
152 } /* namespace bt2c */
154 #endif /* BABELTRACE_CPP_COMMON_BT2C_UUID_HPP */