From: Simon Marchi Date: Mon, 11 Dec 2023 21:09:28 +0000 (-0500) Subject: cpp-common/bt2c: move `uuid-view.*pp` contents to `uuid.hpp` X-Git-Url: https://git.efficios.com/?a=commitdiff_plain;h=12958bb9421f16705d20384349b564c3a877bfa7;p=babeltrace.git cpp-common/bt2c: move `uuid-view.*pp` contents to `uuid.hpp` The two methods in `uuid-view.cpp`, a file which wasn't even mentioned in `src/Makefile.am`, existed to break the cycle because `bt2c::Uuid` and `bt2c::UuidView` need to know eachother. Instead, remove `uuid-view.cpp`, and put both `bt2c::Uuid` and `bt2c::UuidView` in `uuid.hpp`, inlining everything and keeping the close relationship between those two classes. Signed-off-by: Philippe Proulx Change-Id: Ia27de9f23aaa5e38beb446c7bafcae1fb2f17aa7 Reviewed-on: https://review.lttng.org/c/babeltrace/+/11429 Tested-by: jenkins Reviewed-by: Simon Marchi CI-Build: Simon Marchi --- diff --git a/src/Makefile.am b/src/Makefile.am index 4c6126b2..88f54afb 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -54,7 +54,6 @@ noinst_HEADERS = \ cpp-common/bt2c/read-fixed-len-int.hpp \ cpp-common/bt2c/safe-ops.hpp \ cpp-common/bt2c/std-int.hpp \ - cpp-common/bt2c/uuid-view.hpp \ cpp-common/bt2c/uuid.hpp \ cpp-common/bt2c/vector.hpp \ cpp-common/bt2s/make-unique.hpp \ diff --git a/src/cpp-common/bt2/clock-class.hpp b/src/cpp-common/bt2/clock-class.hpp index 6d99ed1f..2c58770b 100644 --- a/src/cpp-common/bt2/clock-class.hpp +++ b/src/cpp-common/bt2/clock-class.hpp @@ -13,7 +13,7 @@ #include -#include "cpp-common/bt2c/uuid-view.hpp" +#include "cpp-common/bt2c/uuid.hpp" #include "cpp-common/bt2s/optional.hpp" #include "borrowed-object.hpp" diff --git a/src/cpp-common/bt2c/uuid-view.cpp b/src/cpp-common/bt2c/uuid-view.cpp deleted file mode 100644 index 2d784a56..00000000 --- a/src/cpp-common/bt2c/uuid-view.cpp +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright (c) 2016-2022 Philippe Proulx - * - * SPDX-License-Identifier: MIT - */ - -#include "uuid-view.hpp" -#include "uuid.hpp" - -namespace bt2c { - -UuidView::UuidView(const Uuid& uuid) noexcept : _mUuid {uuid.data()} -{ -} - -UuidView::operator Uuid() const noexcept -{ - return Uuid {*this}; -} - -} /* namespace bt2c */ diff --git a/src/cpp-common/bt2c/uuid-view.hpp b/src/cpp-common/bt2c/uuid-view.hpp deleted file mode 100644 index a87388ab..00000000 --- a/src/cpp-common/bt2c/uuid-view.hpp +++ /dev/null @@ -1,114 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2020 Philippe Proulx - * - * SPDX-License-Identifier: MIT - */ - -#ifndef BABELTRACE_CPP_COMMON_BT2C_UUID_VIEW_HPP -#define BABELTRACE_CPP_COMMON_BT2C_UUID_VIEW_HPP - -#include -#include -#include - -#include "common/assert.h" -#include "common/uuid.h" - -namespace bt2c { - -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: - 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; - } - - bool operator!=(const UuidView& other) const noexcept - { - return !(*this == other); - } - - bool operator<(const UuidView& other) const noexcept - { - return bt_uuid_compare(_mUuid, other._mUuid) < 0; - } - - static constexpr std::size_t size() noexcept - { - return BT_UUID_LEN; - } - - const Val *data() const noexcept - { - return _mUuid; - } - - Val operator[](const std::size_t index) const noexcept - { - return _mUuid[index]; - } - - 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 Val *_mUuid; -}; - -} /* namespace bt2c */ - -#endif /* BABELTRACE_CPP_COMMON_BT2C_UUID_VIEW_HPP */ diff --git a/src/cpp-common/bt2c/uuid.hpp b/src/cpp-common/bt2c/uuid.hpp index 86d1217f..30ff54a8 100644 --- a/src/cpp-common/bt2c/uuid.hpp +++ b/src/cpp-common/bt2c/uuid.hpp @@ -1,5 +1,6 @@ /* - * Copyright (c) 2022 Francis Deslauriers + * SPDX-FileCopyrightText: 2020-2023 Philippe Proulx + * SPDX-FileCopyrightText: 2022 Francis Deslauriers * * SPDX-License-Identifier: MIT */ @@ -9,15 +10,107 @@ #include #include +#include #include #include "common/assert.h" #include "common/uuid.h" -#include "uuid-view.hpp" - namespace bt2c { +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: + 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; + } + + bool operator!=(const UuidView& other) const noexcept + { + return !(*this == other); + } + + bool operator<(const UuidView& other) const noexcept + { + return bt_uuid_compare(_mUuid, other._mUuid) < 0; + } + + static constexpr std::size_t size() noexcept + { + return BT_UUID_LEN; + } + + const Val *data() const noexcept + { + return _mUuid; + } + + Val operator[](const std::size_t index) const noexcept + { + return _mUuid[index]; + } + + 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 Val *_mUuid; +}; + /* * A universally unique identifier. * @@ -149,6 +242,15 @@ private: std::array _mUuid = {}; }; +inline UuidView::UuidView(const Uuid& uuid) noexcept : _mUuid {uuid.data()} +{ +} + +inline UuidView::operator Uuid() const noexcept +{ + return Uuid {*this}; +} + } /* namespace bt2c */ #endif /* BABELTRACE_CPP_COMMON_BT2C_UUID_HPP */