cpp-common/bt2c: add bt2c::call()
[babeltrace.git] / src / cpp-common / bt2c / 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_BT2C_UUID_VIEW_HPP
8 #define BABELTRACE_CPP_COMMON_BT2C_UUID_VIEW_HPP
9
10 #include <algorithm>
11 #include <cstdint>
12 #include <string>
13
14 #include "common/assert.h"
15 #include "common/uuid.h"
16
17 namespace bt2c {
18
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 */
27 class UuidView final
28 {
29 public:
30 using Val = std::uint8_t;
31 using ConstIter = const Val *;
32
33 public:
34 explicit UuidView(const Val * const uuid) noexcept : _mUuid {uuid}
35 {
36 BT_ASSERT_DBG(uuid);
37 }
38
39 explicit UuidView(const Uuid& uuid) noexcept;
40 UuidView(const UuidView&) noexcept = default;
41 UuidView& operator=(const UuidView&) noexcept = default;
42
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
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
71 bool operator<(const UuidView& other) const noexcept
72 {
73 return bt_uuid_compare(_mUuid, other._mUuid) < 0;
74 }
75
76 static constexpr std::size_t size() noexcept
77 {
78 return BT_UUID_LEN;
79 }
80
81 const Val *data() const noexcept
82 {
83 return _mUuid;
84 }
85
86 Val operator[](const std::size_t index) const noexcept
87 {
88 return _mUuid[index];
89 }
90
91 ConstIter begin() const noexcept
92 {
93 return _mUuid;
94 }
95
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
108 private:
109 const Val *_mUuid;
110 };
111
112 } /* namespace bt2c */
113
114 #endif /* BABELTRACE_CPP_COMMON_BT2C_UUID_VIEW_HPP */
This page took 0.032463 seconds and 4 git commands to generate.