Commit | Line | Data |
---|---|---|
b239731a PP |
1 | /* |
2 | * Copyright (c) 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 | ||
6ef77ba5 | 10 | #include <array> |
b239731a | 11 | #include <cstdint> |
6ef77ba5 | 12 | #include <string> |
638a0d12 | 13 | #include <algorithm> |
b239731a PP |
14 | |
15 | #include "common/assert.h" | |
16 | #include "common/uuid.h" | |
17 | ||
18 | namespace bt2_common { | |
19 | ||
638a0d12 SM |
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 | */ | |
e3e662d3 | 28 | class UuidView final |
b239731a PP |
29 | { |
30 | public: | |
638a0d12 SM |
31 | using Val = std::uint8_t; |
32 | using ConstIter = const Val *; | |
33 | ||
34 | public: | |
35 | explicit UuidView(const Val * const uuid) noexcept : _mUuid {uuid} | |
b239731a PP |
36 | { |
37 | BT_ASSERT_DBG(uuid); | |
38 | } | |
39 | ||
638a0d12 | 40 | explicit UuidView(const Uuid& uuid) noexcept; |
b239731a PP |
41 | UuidView(const UuidView&) noexcept = default; |
42 | UuidView& operator=(const UuidView&) noexcept = default; | |
43 | ||
638a0d12 SM |
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 | ||
b239731a PP |
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 | ||
c65e7e80 PP |
72 | bool operator<(const UuidView& other) const noexcept |
73 | { | |
74 | return bt_uuid_compare(_mUuid, other._mUuid) < 0; | |
75 | } | |
76 | ||
638a0d12 | 77 | static constexpr std::size_t size() noexcept |
b239731a | 78 | { |
638a0d12 SM |
79 | return BT_UUID_LEN; |
80 | } | |
99ddb79d | 81 | |
638a0d12 SM |
82 | const Val *data() const noexcept |
83 | { | |
84 | return _mUuid; | |
b239731a PP |
85 | } |
86 | ||
638a0d12 | 87 | Val operator[](const std::size_t index) const noexcept |
b239731a | 88 | { |
638a0d12 | 89 | return _mUuid[index]; |
b239731a PP |
90 | } |
91 | ||
638a0d12 | 92 | ConstIter begin() const noexcept |
b239731a PP |
93 | { |
94 | return _mUuid; | |
95 | } | |
96 | ||
638a0d12 SM |
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 | ||
b239731a | 109 | private: |
638a0d12 | 110 | const Val *_mUuid; |
b239731a PP |
111 | }; |
112 | ||
b5f55e9f | 113 | } /* namespace bt2_common */ |
b239731a | 114 | |
b5f55e9f | 115 | #endif /* BABELTRACE_CPP_COMMON_UUID_VIEW_HPP */ |