From b239731a3e335b5f7371fcfcfa553013a58733e3 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Wed, 16 Dec 2020 23:30:00 -0500 Subject: [PATCH] Add `bt2_common::UuidView` This patch adds the `bt2_common::UuidView` class which is like `std::string_view`, but for UUIDs. You cannot modify the underlying UUID (`const std::uint8_t *`). Compare UUID views with operator==() and operator!=(). Get a UUID view's data with its data() method. Get a UUID view's data size with its size() method. Get the canonical string of a UUID view with its string() method, which returns a new `std::string` instance. Signed-off-by: Philippe Proulx Change-Id: Icc633b44b852383d223ff8e73034736f47b64579 Reviewed-on: https://review.lttng.org/c/babeltrace/+/4601 --- src/cpp-common/uuid-view.hpp | 62 ++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/cpp-common/uuid-view.hpp diff --git a/src/cpp-common/uuid-view.hpp b/src/cpp-common/uuid-view.hpp new file mode 100644 index 00000000..78116642 --- /dev/null +++ b/src/cpp-common/uuid-view.hpp @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2020 Philippe Proulx + * + * SPDX-License-Identifier: MIT + */ + +#ifndef BABELTRACE_CPP_COMMON_UUID_VIEW_HPP +#define BABELTRACE_CPP_COMMON_UUID_VIEW_HPP + +#include + +#include "common/assert.h" +#include "common/uuid.h" + +namespace bt2_common { + +class UuidView +{ +public: + explicit UuidView(const std::uint8_t * const uuid) noexcept : _mUuid {uuid} + { + BT_ASSERT_DBG(uuid); + } + + UuidView(const UuidView&) noexcept = default; + UuidView& operator=(const UuidView&) noexcept = default; + + bool operator==(const UuidView& other) const noexcept + { + return bt_uuid_compare(_mUuid, other._mUuid) == 0; + } + + bool operator!=(const UuidView& other) const noexcept + { + return !(*this == other); + } + + std::string string() const + { + std::array buf; + + bt_uuid_to_str(_mUuid, buf.data()); + return {buf.data(), buf.size()}; + } + + static std::size_t size() noexcept + { + return BT_UUID_LEN; + } + + const std::uint8_t *data() const noexcept + { + return _mUuid; + } + +private: + const std::uint8_t *_mUuid; +}; + +} // namespace bt2_common + +#endif // BABELTRACE_CPP_COMMON_UUID_VIEW_HPP -- 2.34.1