From ee3581fd7dbacc981a26ad32d90057d572825d87 Mon Sep 17 00:00:00 2001 From: Simon Hoinkis Date: Mon, 17 Apr 2023 17:42:18 +0200 Subject: [PATCH] iox-#180 Implement move operations for 'iox::span' Signed-off-by: Simon Hoinkis --- .../test/moduletests/test_vocabulary_span.cpp | 2 +- .../vocabulary/include/iox/detail/span.inl | 25 ++++++++++++-- iceoryx_dust/vocabulary/include/iox/span.hpp | 33 ++++++++++++++++--- 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/iceoryx_dust/test/moduletests/test_vocabulary_span.cpp b/iceoryx_dust/test/moduletests/test_vocabulary_span.cpp index ae30397fd1..cc0dd744f8 100644 --- a/iceoryx_dust/test/moduletests/test_vocabulary_span.cpp +++ b/iceoryx_dust/test/moduletests/test_vocabulary_span.cpp @@ -315,7 +315,7 @@ TEST(span_test, CheckConstexprIterOfSpan) { ::testing::Test::RecordProperty("TEST_ID", "8764fcfb-27df-4f39-b8cd-56bf881db382"); static constexpr int arr[] = {1, 6, 1, 8, 0}; - constexpr span span(arr); + IOX_MAYBE_UNUSED constexpr span span(arr); // Explicitly not use EXPECT_TRUE here to be able to execute the test case during compile-time // 'static_assert' is not possible as not being supported with GCC5 diff --git a/iceoryx_dust/vocabulary/include/iox/detail/span.inl b/iceoryx_dust/vocabulary/include/iox/detail/span.inl index faa1857d5a..085c136e6a 100644 --- a/iceoryx_dust/vocabulary/include/iox/detail/span.inl +++ b/iceoryx_dust/vocabulary/include/iox/detail/span.inl @@ -124,6 +124,25 @@ inline constexpr span::span(const span& other) { } +template +inline constexpr span::span(span&& other) noexcept + : span_storage_t(std::move(other)) +{ + *this = std::move(other); +} + +template +inline constexpr span& span::operator=(span&& other) noexcept +{ + if (this != &other) + { + span_storage_t::operator=(std::move(other)); + m_data = other.m_data; + other.m_data = nullptr; + } + return *this; +} + template template inline constexpr span span::first() const noexcept @@ -254,14 +273,14 @@ constexpr uint64_t span::extent; // object representation template -span as_bytes(span s) noexcept +inline span as_bytes(span s) noexcept { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) return {reinterpret_cast(s.data()), s.size_bytes()}; } -template ::value>> -span as_writable_bytes(span s) noexcept +template +inline span as_writable_bytes(span s) noexcept { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) return {reinterpret_cast(s.data()), s.size_bytes()}; diff --git a/iceoryx_dust/vocabulary/include/iox/span.hpp b/iceoryx_dust/vocabulary/include/iox/span.hpp index 99a26aa6d6..7c9521d0d5 100644 --- a/iceoryx_dust/vocabulary/include/iox/span.hpp +++ b/iceoryx_dust/vocabulary/include/iox/span.hpp @@ -193,8 +193,26 @@ struct span_storage return m_size; } + span_storage(const span_storage& other) noexcept = default; + span_storage& operator=(const span_storage& other) noexcept = default; + + span_storage(span_storage&& other) noexcept + { + *this = std::move(other); + } + span_storage& operator=(span_storage&& other) noexcept + { + if (this != &other) + { + m_size = other.m_size; + other.m_size = 0; + } + return *this; + } + ~span_storage() noexcept = default; + private: - uint64_t m_size; + uint64_t m_size{0}; }; template @@ -319,10 +337,11 @@ class span : public detail::span_storage typename = detail::enable_if_conversion_allowed_t> constexpr explicit span(const span& other); - constexpr span& operator=(const span& other) noexcept = default; + constexpr span& operator=(const span&) noexcept = default; + + constexpr span(span&& other) noexcept; + constexpr span& operator=(span&& other) noexcept; - constexpr span(span&&) noexcept = delete; - span& operator=(span&&) noexcept = delete; ~span() noexcept = default; // subviews @@ -419,6 +438,12 @@ class span : public detail::span_storage T* m_data; }; +template +span as_bytes(span s) noexcept; + +template ::value>> +span as_writable_bytes(span s) noexcept; + } // namespace iox #include "iox/detail/span.inl"