Skip to content

Commit

Permalink
iox-#180 Implement move operations for 'iox::span'
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Hoinkis <simon.hoinkis@apex.ai>
  • Loading branch information
mossmaurice committed Apr 17, 2023
1 parent b6651a5 commit ee3581f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
2 changes: 1 addition & 1 deletion iceoryx_dust/test/moduletests/test_vocabulary_span.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const int> span(arr);
IOX_MAYBE_UNUSED constexpr span<const int> 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
Expand Down
25 changes: 22 additions & 3 deletions iceoryx_dust/vocabulary/include/iox/detail/span.inl
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,25 @@ inline constexpr span<T, Extent>::span(const span<U, OtherExtent>& other)
{
}

template <typename T, uint64_t Extent>
inline constexpr span<T, Extent>::span(span&& other) noexcept
: span_storage_t(std::move(other))
{
*this = std::move(other);
}

template <typename T, uint64_t Extent>
inline constexpr span<T, Extent>& span<T, Extent>::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 <typename T, uint64_t Extent>
template <uint64_t Count>
inline constexpr span<T, Count> span<T, Extent>::first() const noexcept
Expand Down Expand Up @@ -254,14 +273,14 @@ constexpr uint64_t span<T, Extent>::extent;

// object representation
template <typename T, uint64_t X>
span<const uint8_t, (X == DYNAMIC_EXTENT ? DYNAMIC_EXTENT : sizeof(T) * X)> as_bytes(span<T, X> s) noexcept
inline span<const uint8_t, (X == DYNAMIC_EXTENT ? DYNAMIC_EXTENT : sizeof(T) * X)> as_bytes(span<T, X> s) noexcept
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
return {reinterpret_cast<const uint8_t*>(s.data()), s.size_bytes()};
}

template <typename T, uint64_t X, typename = std::enable_if_t<!std::is_const<T>::value>>
span<uint8_t, (X == DYNAMIC_EXTENT ? DYNAMIC_EXTENT : sizeof(T) * X)> as_writable_bytes(span<T, X> s) noexcept
template <typename T, uint64_t X, typename>
inline span<uint8_t, (X == DYNAMIC_EXTENT ? DYNAMIC_EXTENT : sizeof(T) * X)> as_writable_bytes(span<T, X> s) noexcept
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
return {reinterpret_cast<uint8_t*>(s.data()), s.size_bytes()};
Expand Down
33 changes: 29 additions & 4 deletions iceoryx_dust/vocabulary/include/iox/span.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,26 @@ struct span_storage<DYNAMIC_EXTENT>
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 <typename T>
Expand Down Expand Up @@ -319,10 +337,11 @@ class span : public detail::span_storage<Extent>
typename = detail::enable_if_conversion_allowed_t<U, OtherExtent, T, Extent>>
constexpr explicit span(const span<U, OtherExtent>& 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
Expand Down Expand Up @@ -419,6 +438,12 @@ class span : public detail::span_storage<Extent>
T* m_data;
};

template <typename T, uint64_t X>
span<const uint8_t, (X == DYNAMIC_EXTENT ? DYNAMIC_EXTENT : sizeof(T) * X)> as_bytes(span<T, X> s) noexcept;

template <typename T, uint64_t X, typename = std::enable_if_t<!std::is_const<T>::value>>
span<uint8_t, (X == DYNAMIC_EXTENT ? DYNAMIC_EXTENT : sizeof(T) * X)> as_writable_bytes(span<T, X> s) noexcept;

} // namespace iox

#include "iox/detail/span.inl"
Expand Down

0 comments on commit ee3581f

Please sign in to comment.