X-Git-Url: http://drtracing.org/?a=blobdiff_plain;f=src%2Fcpp-common%2Fuuid-view.hpp;h=d719d877a3b87b306cd226845aa2e7d02a8485e8;hb=9bdf67d7e10e435e427d85fc534cc8980b3443e2;hp=8829019b8154414362c15c0fe6b4612fbeddd92b;hpb=e3e662d30a75231e283abd26a0d0349f8d5c8816;p=babeltrace.git diff --git a/src/cpp-common/uuid-view.hpp b/src/cpp-common/uuid-view.hpp index 8829019b..d719d877 100644 --- a/src/cpp-common/uuid-view.hpp +++ b/src/cpp-common/uuid-view.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Philippe Proulx + * SPDX-FileCopyrightText: 2020 Philippe Proulx * * SPDX-License-Identifier: MIT */ @@ -7,7 +7,7 @@ #ifndef BABELTRACE_CPP_COMMON_UUID_VIEW_HPP #define BABELTRACE_CPP_COMMON_UUID_VIEW_HPP -#include +#include #include #include @@ -16,17 +16,48 @@ namespace bt2_common { +class Uuid; + +/* + * A view on existing UUID data. + * + * A `UuidView` object doesn't contain its UUID data: see `Uuid` for a + * UUID data container. + */ class UuidView final { public: - explicit UuidView(const std::uint8_t * const uuid) noexcept : _mUuid {uuid} + using Val = std::uint8_t; + using ConstIter = const Val *; + +public: + explicit UuidView(const Val * const uuid) noexcept : _mUuid {uuid} { BT_ASSERT_DBG(uuid); } + explicit UuidView(const Uuid& uuid) noexcept; UuidView(const UuidView&) noexcept = default; UuidView& operator=(const UuidView&) noexcept = default; + UuidView& operator=(const Val * const uuid) noexcept + { + _mUuid = uuid; + return *this; + } + + operator Uuid() const noexcept; + + std::string str() const + { + std::string s; + + s.resize(BT_UUID_STR_LEN); + bt_uuid_to_str(_mUuid, &s[0]); + + return s; + } + bool operator==(const UuidView& other) const noexcept { return bt_uuid_compare(_mUuid, other._mUuid) == 0; @@ -42,28 +73,40 @@ public: return bt_uuid_compare(_mUuid, other._mUuid) < 0; } - std::string str() const + static constexpr std::size_t size() noexcept { - std::string s; - - s.resize(BT_UUID_STR_LEN); - bt_uuid_to_str(_mUuid, &s[0]); + return BT_UUID_LEN; + } - return s; + const Val *data() const noexcept + { + return _mUuid; } - static constexpr std::size_t size() noexcept + Val operator[](const std::size_t index) const noexcept { - return BT_UUID_LEN; + return _mUuid[index]; } - const std::uint8_t *data() const noexcept + ConstIter begin() const noexcept { return _mUuid; } + ConstIter end() const noexcept + { + return _mUuid + this->size(); + } + + bool isNil() const noexcept + { + return std::all_of(this->begin(), this->end(), [](const std::uint8_t byte) { + return byte == 0; + }); + } + private: - const std::uint8_t *_mUuid; + const Val *_mUuid; }; } /* namespace bt2_common */