src/cpp-common: add bt2_common::safeTo*() and bt2_common::safe*()
[babeltrace.git] / src / cpp-common / uuid-view.hpp
CommitLineData
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>
b239731a
PP
13
14#include "common/assert.h"
15#include "common/uuid.h"
16
17namespace bt2_common {
18
19class UuidView
20{
21public:
22 explicit UuidView(const std::uint8_t * const uuid) noexcept : _mUuid {uuid}
23 {
24 BT_ASSERT_DBG(uuid);
25 }
26
27 UuidView(const UuidView&) noexcept = default;
28 UuidView& operator=(const UuidView&) noexcept = default;
29
30 bool operator==(const UuidView& other) const noexcept
31 {
32 return bt_uuid_compare(_mUuid, other._mUuid) == 0;
33 }
34
35 bool operator!=(const UuidView& other) const noexcept
36 {
37 return !(*this == other);
38 }
39
40 std::string string() const
41 {
99ddb79d 42 std::string s;
b239731a 43
99ddb79d 44 s.resize(BT_UUID_STR_LEN);
f442e7c3 45 bt_uuid_to_str(_mUuid, &s[0]);
99ddb79d
MJ
46
47 return s;
b239731a
PP
48 }
49
50 static std::size_t size() noexcept
51 {
52 return BT_UUID_LEN;
53 }
54
55 const std::uint8_t *data() const noexcept
56 {
57 return _mUuid;
58 }
59
60private:
61 const std::uint8_t *_mUuid;
62};
63
b5f55e9f 64} /* namespace bt2_common */
b239731a 65
b5f55e9f 66#endif /* BABELTRACE_CPP_COMMON_UUID_VIEW_HPP */
This page took 0.031401 seconds and 4 git commands to generate.