70b6e031ff992e9e8e34f6f129b58c6459af1ee8
[babeltrace.git] / src / cpp-common / uuid-view.hpp
1 /*
2 * SPDX-FileCopyrightText: 2020 Philippe Proulx <pproulx@efficios.com>
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
7 #ifndef BABELTRACE_CPP_COMMON_UUID_VIEW_HPP
8 #define BABELTRACE_CPP_COMMON_UUID_VIEW_HPP
9
10 #include <array>
11 #include <cstdint>
12 #include <string>
13 #include <algorithm>
14
15 #include "common/assert.h"
16 #include "common/uuid.h"
17
18 namespace bt2_common {
19
20 class Uuid;
21
22 /*
23 * A view on existing UUID data.
24 *
25 * A `UuidView` object doesn't contain its UUID data: see `Uuid` for a
26 * UUID data container.
27 */
28 class UuidView final
29 {
30 public:
31 using Val = std::uint8_t;
32 using ConstIter = const Val *;
33
34 public:
35 explicit UuidView(const Val * const uuid) noexcept : _mUuid {uuid}
36 {
37 BT_ASSERT_DBG(uuid);
38 }
39
40 explicit UuidView(const Uuid& uuid) noexcept;
41 UuidView(const UuidView&) noexcept = default;
42 UuidView& operator=(const UuidView&) noexcept = default;
43
44 UuidView& operator=(const Val * const uuid) noexcept
45 {
46 _mUuid = uuid;
47 return *this;
48 }
49
50 operator Uuid() const noexcept;
51
52 std::string str() const
53 {
54 std::string s;
55
56 s.resize(BT_UUID_STR_LEN);
57 bt_uuid_to_str(_mUuid, &s[0]);
58
59 return s;
60 }
61
62 bool operator==(const UuidView& other) const noexcept
63 {
64 return bt_uuid_compare(_mUuid, other._mUuid) == 0;
65 }
66
67 bool operator!=(const UuidView& other) const noexcept
68 {
69 return !(*this == other);
70 }
71
72 bool operator<(const UuidView& other) const noexcept
73 {
74 return bt_uuid_compare(_mUuid, other._mUuid) < 0;
75 }
76
77 static constexpr std::size_t size() noexcept
78 {
79 return BT_UUID_LEN;
80 }
81
82 const Val *data() const noexcept
83 {
84 return _mUuid;
85 }
86
87 Val operator[](const std::size_t index) const noexcept
88 {
89 return _mUuid[index];
90 }
91
92 ConstIter begin() const noexcept
93 {
94 return _mUuid;
95 }
96
97 ConstIter end() const noexcept
98 {
99 return _mUuid + this->size();
100 }
101
102 bool isNil() const noexcept
103 {
104 return std::all_of(this->begin(), this->end(), [](const std::uint8_t byte) {
105 return byte == 0;
106 });
107 }
108
109 private:
110 const Val *_mUuid;
111 };
112
113 } /* namespace bt2_common */
114
115 #endif /* BABELTRACE_CPP_COMMON_UUID_VIEW_HPP */
This page took 0.031295 seconds and 3 git commands to generate.