Sort includes in C++ files
[babeltrace.git] / src / cpp-common / uuid.hpp
CommitLineData
638a0d12
SM
1/*
2 * Copyright (c) 2022 Francis Deslauriers <francis.deslauriers@efficios.com>
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
7#ifndef BABELTRACE_CPP_COMMON_UUID_HPP
8#define BABELTRACE_CPP_COMMON_UUID_HPP
9
10#include <algorithm>
11#include <array>
12#include <cstdint>
13#include <string>
14
15#include "common/assert.h"
16#include "common/uuid.h"
c802cacb 17
638a0d12
SM
18#include "uuid-view.hpp"
19
20namespace bt2_common {
21
22/*
23 * A universally unique identifier.
24 *
25 * A `Uuid` object contains its UUID data: see `UuidView` to have a
26 * UUID view on existing UUID data.
27 */
28class Uuid final
29{
30public:
31 using Val = UuidView::Val;
32 using ConstIter = UuidView::ConstIter;
33
34public:
35 /*
36 * Builds a nil UUID.
37 */
38 explicit Uuid() noexcept = default;
39
40 explicit Uuid(const Val * const uuid) noexcept
41 {
42 this->_setFromPtr(uuid);
43 }
44
45 explicit Uuid(const char * const str) noexcept
46 {
47 const auto ret = bt_uuid_from_str(str, _mUuid.data());
48 BT_ASSERT(ret == 0);
49 }
50
51 explicit Uuid(const std::string& str) noexcept : Uuid {str.c_str()}
52 {
53 }
54
55 explicit Uuid(const UuidView& view) noexcept : Uuid {view.data()}
56 {
57 }
58
59 Uuid(const Uuid&) noexcept = default;
60 Uuid& operator=(const Uuid&) noexcept = default;
61
62 Uuid& operator=(const Val * const uuid) noexcept
63 {
64 this->_setFromPtr(uuid);
65 return *this;
66 }
67
68 static Uuid generate() noexcept
69 {
70 bt_uuid_t uuidGen;
71
72 bt_uuid_generate(uuidGen);
73 return Uuid {uuidGen};
74 }
75
76 std::string str() const
77 {
78 return this->_view().str();
79 }
80
81 bool operator==(const Uuid& other) const noexcept
82 {
83 return this->_view() == other._view();
84 }
85
86 bool operator!=(const Uuid& other) const noexcept
87 {
88 return this->_view() != other._view();
89 }
90
91 bool operator<(const Uuid& other) const noexcept
92 {
93 return this->_view() < other._view();
94 }
95
96 /*
97 * The returned UUID view must not outlive the UUID object.
98 */
99 operator UuidView() const noexcept
100 {
101 return this->_view();
102 }
103
104 static constexpr std::size_t size() noexcept
105 {
106 return UuidView::size();
107 }
108
109 const Val *data() const noexcept
110 {
111 return _mUuid.data();
112 }
113
114 Val operator[](const std::size_t index) const noexcept
115 {
116 return this->_view()[index];
117 }
118
119 ConstIter begin() const noexcept
120 {
121 return this->_view().begin();
122 }
123
124 ConstIter end() const noexcept
125 {
126 return this->_view().end();
127 }
128
129 bool isNil() const noexcept
130 {
131 return this->_view().isNil();
132 }
133
134private:
135 /*
136 * std::copy_n() won't throw when simply copying bytes below,
137 * therefore this method won't throw.
138 */
139 void _setFromPtr(const Val * const uuid) noexcept
140 {
141 BT_ASSERT(uuid);
142 std::copy_n(uuid, BT_UUID_LEN, std::begin(_mUuid));
143 }
144
145 UuidView _view() const noexcept
146 {
147 return UuidView {_mUuid.data()};
148 }
149
150 std::array<Val, UuidView::size()> _mUuid = {};
151};
152
153} /* namespace bt2_common */
154
155#endif /* BABELTRACE_CPP_COMMON_UUID_HPP */
This page took 0.031687 seconds and 4 git commands to generate.