Sort includes in C++ files
[babeltrace.git] / src / cpp-common / uuid-view.hpp
CommitLineData
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>
6ef77ba5 11#include <array>
b239731a 12#include <cstdint>
6ef77ba5 13#include <string>
b239731a
PP
14
15#include "common/assert.h"
16#include "common/uuid.h"
17
18namespace bt2_common {
19
638a0d12
SM
20class 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 28class UuidView final
b239731a
PP
29{
30public:
638a0d12
SM
31 using Val = std::uint8_t;
32 using ConstIter = const Val *;
33
34public:
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 109private:
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 */
This page took 0.039596 seconds and 4 git commands to generate.