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