From 2c0baefd4a0ecbf213e89fa43adac93574fb551c Mon Sep 17 00:00:00 2001 From: Dennis Liu Date: Mon, 20 Nov 2023 13:33:53 +0800 Subject: [PATCH 1/4] iox-#2052 Refactor copy_and_move_impl Refactor copy_and_move_impl by new struct `SpecialCreationHelper` and enum class `SpecialCreationOperations` with C++17 features. Deleter __cplusplus branchs. Signed-off-by: Dennis Liu --- .../iox/detail/fixed_position_container.inl | 95 +++++----- .../fixed_position_container_helper.hpp | 167 ++++++++++++------ .../include/iox/fixed_position_container.hpp | 3 +- 3 files changed, 166 insertions(+), 99 deletions(-) diff --git a/iceoryx_dust/container/include/iox/detail/fixed_position_container.inl b/iceoryx_dust/container/include/iox/detail/fixed_position_container.inl index 613ea1aca6..98ce810420 100644 --- a/iceoryx_dust/container/include/iox/detail/fixed_position_container.inl +++ b/iceoryx_dust/container/include/iox/detail/fixed_position_container.inl @@ -18,10 +18,8 @@ #ifndef IOX_DUST_CONTAINER_DETAIL_FIXED_POSITION_CONTAINER_INL #define IOX_DUST_CONTAINER_DETAIL_FIXED_POSITION_CONTAINER_INL -#include "iox/fixed_position_container.hpp" -#if __cplusplus < 201703L #include "iox/detail/fixed_position_container_helper.hpp" -#endif +#include "iox/fixed_position_container.hpp" namespace iox { @@ -58,23 +56,13 @@ inline FixedPositionContainer::~FixedPositionContainer() noexcept template inline FixedPositionContainer::FixedPositionContainer(const FixedPositionContainer& rhs) noexcept { - for (IndexType i = 0; i < CAPACITY; ++i) - { - m_status[i] = SlotStatus::FREE; - } - - *this = rhs; + copy_and_move_impl(rhs); } template inline FixedPositionContainer::FixedPositionContainer(FixedPositionContainer&& rhs) noexcept { - for (IndexType i = 0; i < CAPACITY; ++i) - { - m_status[i] = SlotStatus::FREE; - } - - *this = std::move(rhs); + copy_and_move_impl(std::move(rhs)); } template @@ -83,7 +71,7 @@ FixedPositionContainer::operator=(const FixedPositionContainer& rhs { if (this != &rhs) { - copy_and_move_impl(rhs); + copy_and_move_impl(rhs); } return *this; } @@ -94,73 +82,79 @@ FixedPositionContainer::operator=(FixedPositionContainer&& rhs) noe { if (this != &rhs) { - copy_and_move_impl(std::move(rhs)); - - // clear rhs - rhs.clear(); + copy_and_move_impl(std::move(rhs)); } return *this; } template -template +template inline void FixedPositionContainer::copy_and_move_impl(RhsType&& rhs) noexcept { - // we already make sure rhs is always passed by std::move() for move case, therefore - // the result of "decltype(rhs)" is same as "decltype(std::forward(rhs))" - static_assert(std::is_rvalue_reference::value - || (std::is_lvalue_reference::value - && std::is_const>::value), - "RhsType must be const lvalue reference or rvalue reference"); + static_assert( + std::is_rvalue_reference_v< + decltype(rhs)> || (std::is_lvalue_reference_v && std::is_const_v>), + "RhsType must be const lvalue reference or rvalue reference"); + + // alias helper struct + using Helper = detail::SpecialCreationHelper; - constexpr bool is_move = std::is_rvalue_reference::value; + constexpr bool is_ctor = Helper::is_ctor(); + constexpr bool is_move = Helper::is_move(); + + if constexpr (is_ctor) + { + for (IndexType i = 0; i < CAPACITY; ++i) + { + m_status[i] = SlotStatus::FREE; + } + } IndexType i{Index::FIRST}; auto rhs_it = (std::forward(rhs)).begin(); - // transfer src data to destination for (; rhs_it.to_index() != Index::INVALID; ++i, ++rhs_it) { if (m_status[i] == SlotStatus::USED) { -#if __cplusplus >= 201703L + // When the slot is in the 'USED' state, it is safe to proceed with either construction (ctor) or assignment + // operation. Therefore, creation can be carried out according to the option specified by Opt. + + // TODO: moveCtor will failed if using move_or_copy_it. Twice time moveCtor will be called. + // Helper::create(m_data[i], Helper::move_or_copy_it(rhs_it)); + if constexpr (is_move) { - m_data[i] = std::move(*rhs_it); + Helper::create(m_data[i], std::move(*rhs_it)); } else { - m_data[i] = *rhs_it; + Helper::create(m_data[i], *rhs_it); } -#else - // We introduce a helper struct primarily due to the test case - // e1cc7c9f-c1b5-4047-811b-004302af5c00. It demands compile-time if-else branching - // for classes that are either non-copyable or non-moveable. - // Note: With C++17's 'if constexpr', the need for these helper structs can be eliminated. - detail::AssignmentHelper::assign(m_data[i], detail::MoveHelper::move_or_copy(rhs_it)); -#endif } - // use ctor to avoid UB for non-initialized free slots else { -#if __cplusplus >= 201703L + // When the slot is in the 'FREE' state, it is unsafe to proceed with assignment operation. + // Therefore, we need to force helper to use ctor create to make sure that the 'FREE' slots get initialized. + + // TODO: moveCtor will failed if using move_or_copy_it. Twice time moveCtor will be called. + // Helper::ctor_create(m_data[i], Helper::move_or_copy_it(rhs_it)); + if constexpr (is_move) { - new (&m_data[i]) T(std::move(*rhs_it)); + Helper::ctor_create(m_data[i], std::move(*rhs_it)); } else { - new (&m_data[i]) T(*rhs_it); + Helper::ctor_create(m_data[i], *rhs_it); } -#else - // Same as above - detail::CtorHelper::construct(m_data[i], detail::MoveHelper::move_or_copy(rhs_it)); -#endif } + m_status[i] = SlotStatus::USED; m_next[i] = static_cast(i + 1U); } + // reset rest for (; i < CAPACITY; ++i) { if (m_status[i] == SlotStatus::USED) @@ -174,6 +168,7 @@ inline void FixedPositionContainer::copy_and_move_impl(RhsType&& rh m_next[i] = next; } + // correct m_next m_next[Index::LAST] = Index::INVALID; if (!rhs.empty()) { @@ -183,6 +178,12 @@ inline void FixedPositionContainer::copy_and_move_impl(RhsType&& rh m_begin_free = static_cast(rhs.m_size); m_begin_used = rhs.empty() ? Index::INVALID : Index::FIRST; m_size = rhs.m_size; + + // reset rhs if is_move is true + if constexpr (is_move) + { + rhs.clear(); + } } template diff --git a/iceoryx_dust/container/include/iox/detail/fixed_position_container_helper.hpp b/iceoryx_dust/container/include/iox/detail/fixed_position_container_helper.hpp index ad1c12cb08..06a06aadd3 100644 --- a/iceoryx_dust/container/include/iox/detail/fixed_position_container_helper.hpp +++ b/iceoryx_dust/container/include/iox/detail/fixed_position_container_helper.hpp @@ -17,81 +17,146 @@ #ifndef IOX_DUST_CONTAINER_DETAIL_FIXED_POSITION_CONTAINER_HELPER_HPP #define IOX_DUST_CONTAINER_DETAIL_FIXED_POSITION_CONTAINER_HELPER_HPP +#include #include namespace iox { namespace detail { -template -struct AssignmentHelper; -template <> -struct AssignmentHelper +enum class SpecialCreationOperations { - template - static void assign(T& dest, T&& src) - { - dest = std::forward(src); - } + CopyConstructor, + CopyAssignment, + MoveConstructor, + MoveAssignment, }; -template <> -struct AssignmentHelper +/// @brief SpecialCreationHelper is a template structure used to create or assign objects based on the provided +/// operation type (Opt). +/// @tparam Opt The operation type that determines how objects are created or assigned. +template +struct SpecialCreationHelper { - template - static void assign(T& dest, const T& src) + /// @brief Creates or assigns an object to 'dest' based on the specail operation type. + /// @tparam T The type of the object to be created or assigned. + /// @tparam V The type of the source object, kept as a universal reference to preserve its lvalue or rvalue nature. + /// @param[out] dest The destination object where the new object is created or to which the source object is + /// assigned. + /// @param[in] src The source object, either for copy or move operations. + template + static inline void create(T& dest, V&& src) noexcept { - dest = src; + if constexpr (is_ctor()) + { + ctor_create(dest, std::forward(src)); + } + else + { + assignment_create(dest, std::forward(src)); + } } -}; -template -struct CtorHelper; - -template <> -struct CtorHelper -{ - template - static void construct(T& dest, T&& src) + /// @brief Force to use constructor to create an object at the destination. + /// @tparam T The type of the object to be constructed. + /// @tparam V The type of the source object, used for move or copy construction. + /// @param[out] dest The destination object where the new object is constructed. + /// @param[in] src The source object, either for move or copy construction. + template + static inline void ctor_create(T& dest, V&& src) noexcept { - new (&dest) T(std::forward(src)); + if constexpr (is_move()) + { + static_assert(std::is_rvalue_reference_v, "src should be rvalue reference"); + static_assert(std::is_convertible_v, "src type is not convertible to dest type"); + new (&dest) T(std::forward(src)); + } + else + { + // TODO: enable this when move_or_copy_it / move_or_copy works + // static_assert(std::is_lvalue_reference_v, "src should be lvalue reference"); + // static_assert(std::is_const_v>, "src should has const + // modifier"); static_assert(std::is_convertible_v, "Source type is not convertible to destination + // type"); new (&dest) T(src.get()); + + static_assert(std::is_lvalue_reference_v, "src should be lvalue reference"); + static_assert(std::is_const_v>, "src should has 'const' modifier"); + static_assert(std::is_convertible_v, "src type is not convertible to dest type"); + new (&dest) T(src); + } } -}; -template <> -struct CtorHelper -{ - template - static void construct(T& dest, const T& src) + /// @brief Force to use assignment to assign an object to the destination. + /// @tparam T The type of the destination object. + /// @tparam V The type of the source object, used for move or copy assignment. + /// @param dest The destination object where the source object is assigned. + /// @param src The source object, either for move or copy assignment. + template + static inline void assignment_create(T& dest, V&& src) noexcept { - new (&dest) T(src); - } -}; + if constexpr (is_move()) + { + static_assert(std::is_rvalue_reference_v, "src should be rvalue reference"); + dest = std::forward(src); + } + else + { + // TODO: enable this when move_or_copy_it / move_or_copy works + // static_assert(std::is_lvalue_reference_v, "src should be lvalue reference"); + // static_assert(std::is_const_v>, "src should has const + // modifier"); dest = src.get(); -template -struct MoveHelper; + static_assert(std::is_lvalue_reference_v, "src should be lvalue reference"); + static_assert(std::is_const_v>, "src should has 'const' modifier"); + dest = src; + } + } -template <> -struct MoveHelper -{ - template - static auto move_or_copy(Iterator& it) -> decltype(std::move(*it)) + /// @brief Checks if the current special operation is a constructor call. + /// @return True if the operation is a copy or move constructor, false otherwise. + static constexpr bool is_ctor() noexcept { - return std::move(*it); + return Opt == SpecialCreationOperations::CopyConstructor || Opt == SpecialCreationOperations::MoveConstructor; } -}; -template <> -struct MoveHelper -{ - template - static auto move_or_copy(Iterator& it) -> decltype(*it) + /// @brief Checks if the current special operation is a move operation. + /// @return True if the operation is a move constructor or move assignment, false otherwise. + static constexpr bool is_move() noexcept { - return *it; + return Opt == SpecialCreationOperations::MoveAssignment || Opt == SpecialCreationOperations::MoveConstructor; } -}; + // template + // static inline constexpr auto move_or_copy(T&& value) noexcept + // { + // if constexpr (is_move()) + // { + // // FIXME: See https://godbolt.org/z/hz9Kbb8nr. + // return std::move(std::forward(value)); + // } + // else + // { + // return std::cref(std::forward(value)); + // } + // } + + // template + // static inline constexpr auto move_or_copy_it(Iterator& it) noexcept + // { + // if constexpr (is_move()) + // { + // // FIXME: Twice time movector will be called. + // return std::move(*it); + // } + // else + // { + // // std::cref fix twice time copyCtor problem? + // // See https://godbolt.org/z/W41ndvhh5 + // return std::cref(*it); + // } + // } +}; +} // namespace detail +} // namespace iox #endif -} -} diff --git a/iceoryx_dust/container/include/iox/fixed_position_container.hpp b/iceoryx_dust/container/include/iox/fixed_position_container.hpp index 45da5de095..f9ac5cbad3 100644 --- a/iceoryx_dust/container/include/iox/fixed_position_container.hpp +++ b/iceoryx_dust/container/include/iox/fixed_position_container.hpp @@ -19,6 +19,7 @@ #include "iceoryx_hoofs/cxx/requires.hpp" #include "iox/algorithm.hpp" +#include "iox/detail/fixed_position_container_helper.hpp" #include "iox/uninitialized_array.hpp" #include @@ -315,7 +316,7 @@ class FixedPositionContainer final }; private: - template + template void copy_and_move_impl(RhsType&& rhs) noexcept; private: From e5bfb07ab44bf28d5dbda1ccf60447a7e610ded3 Mon Sep 17 00:00:00 2001 From: Dennis Liu Date: Tue, 21 Nov 2023 15:23:54 +0800 Subject: [PATCH 2/4] iox-#2052 Try to fix clang format error When we commit, Clang Format should have already been applied once. However, the CI process failed. I suspect it's due to an issue with inappropriate new lines. Signed-off-by: Dennis Liu --- .../include/iox/detail/fixed_position_container.inl | 8 ++++---- .../iox/detail/fixed_position_container_helper.hpp | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/iceoryx_dust/container/include/iox/detail/fixed_position_container.inl b/iceoryx_dust/container/include/iox/detail/fixed_position_container.inl index 98ce810420..5191902fa0 100644 --- a/iceoryx_dust/container/include/iox/detail/fixed_position_container.inl +++ b/iceoryx_dust/container/include/iox/detail/fixed_position_container.inl @@ -92,8 +92,8 @@ template inline void FixedPositionContainer::copy_and_move_impl(RhsType&& rhs) noexcept { static_assert( - std::is_rvalue_reference_v< - decltype(rhs)> || (std::is_lvalue_reference_v && std::is_const_v>), + std::is_rvalue_reference::value + || (std::is_lvalue_reference_v && std::is_const_v>), "RhsType must be const lvalue reference or rvalue reference"); // alias helper struct @@ -120,7 +120,7 @@ inline void FixedPositionContainer::copy_and_move_impl(RhsType&& rh // When the slot is in the 'USED' state, it is safe to proceed with either construction (ctor) or assignment // operation. Therefore, creation can be carried out according to the option specified by Opt. - // TODO: moveCtor will failed if using move_or_copy_it. Twice time moveCtor will be called. + // @todo iox-2052: moveCtor will failed if using move_or_copy_it. Twice time moveCtor will be called. // Helper::create(m_data[i], Helper::move_or_copy_it(rhs_it)); if constexpr (is_move) @@ -137,7 +137,7 @@ inline void FixedPositionContainer::copy_and_move_impl(RhsType&& rh // When the slot is in the 'FREE' state, it is unsafe to proceed with assignment operation. // Therefore, we need to force helper to use ctor create to make sure that the 'FREE' slots get initialized. - // TODO: moveCtor will failed if using move_or_copy_it. Twice time moveCtor will be called. + // @todo iox-2052: moveCtor will failed if using move_or_copy_it. Twice time moveCtor will be called. // Helper::ctor_create(m_data[i], Helper::move_or_copy_it(rhs_it)); if constexpr (is_move) diff --git a/iceoryx_dust/container/include/iox/detail/fixed_position_container_helper.hpp b/iceoryx_dust/container/include/iox/detail/fixed_position_container_helper.hpp index 06a06aadd3..9bca11d2a5 100644 --- a/iceoryx_dust/container/include/iox/detail/fixed_position_container_helper.hpp +++ b/iceoryx_dust/container/include/iox/detail/fixed_position_container_helper.hpp @@ -74,7 +74,7 @@ struct SpecialCreationHelper } else { - // TODO: enable this when move_or_copy_it / move_or_copy works + // @todo iox-2052: enable this when move_or_copy_it / move_or_copy works // static_assert(std::is_lvalue_reference_v, "src should be lvalue reference"); // static_assert(std::is_const_v>, "src should has const // modifier"); static_assert(std::is_convertible_v, "Source type is not convertible to destination @@ -102,7 +102,7 @@ struct SpecialCreationHelper } else { - // TODO: enable this when move_or_copy_it / move_or_copy works + // @todo iox-2052: enable this when move_or_copy_it / move_or_copy works // static_assert(std::is_lvalue_reference_v, "src should be lvalue reference"); // static_assert(std::is_const_v>, "src should has const // modifier"); dest = src.get(); From 49f0783ce53dd380aaab53105474413f07d36df4 Mon Sep 17 00:00:00 2001 From: Dennis Liu Date: Tue, 21 Nov 2023 15:35:37 +0800 Subject: [PATCH 3/4] iox-#2052 Fix todo syntax Signed-off-by: Dennis Liu --- .../container/include/iox/detail/fixed_position_container.inl | 4 ++-- .../include/iox/detail/fixed_position_container_helper.hpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/iceoryx_dust/container/include/iox/detail/fixed_position_container.inl b/iceoryx_dust/container/include/iox/detail/fixed_position_container.inl index 5191902fa0..70bd7afc2c 100644 --- a/iceoryx_dust/container/include/iox/detail/fixed_position_container.inl +++ b/iceoryx_dust/container/include/iox/detail/fixed_position_container.inl @@ -120,7 +120,7 @@ inline void FixedPositionContainer::copy_and_move_impl(RhsType&& rh // When the slot is in the 'USED' state, it is safe to proceed with either construction (ctor) or assignment // operation. Therefore, creation can be carried out according to the option specified by Opt. - // @todo iox-2052: moveCtor will failed if using move_or_copy_it. Twice time moveCtor will be called. + // @todo iox-#2052: moveCtor will failed if using move_or_copy_it. Twice time moveCtor will be called. // Helper::create(m_data[i], Helper::move_or_copy_it(rhs_it)); if constexpr (is_move) @@ -137,7 +137,7 @@ inline void FixedPositionContainer::copy_and_move_impl(RhsType&& rh // When the slot is in the 'FREE' state, it is unsafe to proceed with assignment operation. // Therefore, we need to force helper to use ctor create to make sure that the 'FREE' slots get initialized. - // @todo iox-2052: moveCtor will failed if using move_or_copy_it. Twice time moveCtor will be called. + // @todo iox-#2052: moveCtor will failed if using move_or_copy_it. Twice time moveCtor will be called. // Helper::ctor_create(m_data[i], Helper::move_or_copy_it(rhs_it)); if constexpr (is_move) diff --git a/iceoryx_dust/container/include/iox/detail/fixed_position_container_helper.hpp b/iceoryx_dust/container/include/iox/detail/fixed_position_container_helper.hpp index 9bca11d2a5..acc2dc286f 100644 --- a/iceoryx_dust/container/include/iox/detail/fixed_position_container_helper.hpp +++ b/iceoryx_dust/container/include/iox/detail/fixed_position_container_helper.hpp @@ -74,7 +74,7 @@ struct SpecialCreationHelper } else { - // @todo iox-2052: enable this when move_or_copy_it / move_or_copy works + // @todo iox-#2052: enable this when move_or_copy_it / move_or_copy works // static_assert(std::is_lvalue_reference_v, "src should be lvalue reference"); // static_assert(std::is_const_v>, "src should has const // modifier"); static_assert(std::is_convertible_v, "Source type is not convertible to destination @@ -102,7 +102,7 @@ struct SpecialCreationHelper } else { - // @todo iox-2052: enable this when move_or_copy_it / move_or_copy works + // @todo iox-#2052: enable this when move_or_copy_it / move_or_copy works // static_assert(std::is_lvalue_reference_v, "src should be lvalue reference"); // static_assert(std::is_const_v>, "src should has const // modifier"); dest = src.get(); From 7bd507e9dc248315fc551cc68e8e38ed74f7a0d1 Mon Sep 17 00:00:00 2001 From: Dennis Liu Date: Thu, 23 Nov 2023 00:20:00 +0800 Subject: [PATCH 4/4] iox-#2052 Remove unnecessary static_assert and comment Signed-off-by: Dennis Liu --- .../iox/detail/fixed_position_container.inl | 30 +++------- .../fixed_position_container_helper.hpp | 55 +++---------------- .../include/iox/fixed_position_container.hpp | 2 +- 3 files changed, 17 insertions(+), 70 deletions(-) diff --git a/iceoryx_dust/container/include/iox/detail/fixed_position_container.inl b/iceoryx_dust/container/include/iox/detail/fixed_position_container.inl index 70bd7afc2c..8be6a9b4e5 100644 --- a/iceoryx_dust/container/include/iox/detail/fixed_position_container.inl +++ b/iceoryx_dust/container/include/iox/detail/fixed_position_container.inl @@ -56,13 +56,13 @@ inline FixedPositionContainer::~FixedPositionContainer() noexcept template inline FixedPositionContainer::FixedPositionContainer(const FixedPositionContainer& rhs) noexcept { - copy_and_move_impl(rhs); + copy_and_move_impl(rhs); } template inline FixedPositionContainer::FixedPositionContainer(FixedPositionContainer&& rhs) noexcept { - copy_and_move_impl(std::move(rhs)); + copy_and_move_impl(std::move(rhs)); } template @@ -71,7 +71,7 @@ FixedPositionContainer::operator=(const FixedPositionContainer& rhs { if (this != &rhs) { - copy_and_move_impl(rhs); + copy_and_move_impl(rhs); } return *this; } @@ -82,26 +82,22 @@ FixedPositionContainer::operator=(FixedPositionContainer&& rhs) noe { if (this != &rhs) { - copy_and_move_impl(std::move(rhs)); + copy_and_move_impl(std::move(rhs)); } return *this; } template -template +template inline void FixedPositionContainer::copy_and_move_impl(RhsType&& rhs) noexcept { - static_assert( - std::is_rvalue_reference::value - || (std::is_lvalue_reference_v && std::is_const_v>), - "RhsType must be const lvalue reference or rvalue reference"); - // alias helper struct - using Helper = detail::SpecialCreationHelper; + using Helper = detail::MoveAndCopyHelper; constexpr bool is_ctor = Helper::is_ctor(); constexpr bool is_move = Helper::is_move(); + // status array is not yet initialized for constructor creation if constexpr (is_ctor) { for (IndexType i = 0; i < CAPACITY; ++i) @@ -119,27 +115,19 @@ inline void FixedPositionContainer::copy_and_move_impl(RhsType&& rh { // When the slot is in the 'USED' state, it is safe to proceed with either construction (ctor) or assignment // operation. Therefore, creation can be carried out according to the option specified by Opt. - - // @todo iox-#2052: moveCtor will failed if using move_or_copy_it. Twice time moveCtor will be called. - // Helper::create(m_data[i], Helper::move_or_copy_it(rhs_it)); - if constexpr (is_move) { - Helper::create(m_data[i], std::move(*rhs_it)); + Helper::transfer(m_data[i], std::move(*rhs_it)); } else { - Helper::create(m_data[i], *rhs_it); + Helper::transfer(m_data[i], *rhs_it); } } else { // When the slot is in the 'FREE' state, it is unsafe to proceed with assignment operation. // Therefore, we need to force helper to use ctor create to make sure that the 'FREE' slots get initialized. - - // @todo iox-#2052: moveCtor will failed if using move_or_copy_it. Twice time moveCtor will be called. - // Helper::ctor_create(m_data[i], Helper::move_or_copy_it(rhs_it)); - if constexpr (is_move) { Helper::ctor_create(m_data[i], std::move(*rhs_it)); diff --git a/iceoryx_dust/container/include/iox/detail/fixed_position_container_helper.hpp b/iceoryx_dust/container/include/iox/detail/fixed_position_container_helper.hpp index acc2dc286f..76ff0de14a 100644 --- a/iceoryx_dust/container/include/iox/detail/fixed_position_container_helper.hpp +++ b/iceoryx_dust/container/include/iox/detail/fixed_position_container_helper.hpp @@ -25,7 +25,7 @@ namespace iox namespace detail { -enum class SpecialCreationOperations +enum class MoveAndCopyOperations { CopyConstructor, CopyAssignment, @@ -33,11 +33,11 @@ enum class SpecialCreationOperations MoveAssignment, }; -/// @brief SpecialCreationHelper is a template structure used to create or assign objects based on the provided +/// @brief MoveAndCopyHelper is a template structure used to create or assign objects based on the provided /// operation type (Opt). /// @tparam Opt The operation type that determines how objects are created or assigned. -template -struct SpecialCreationHelper +template +struct MoveAndCopyHelper { /// @brief Creates or assigns an object to 'dest' based on the specail operation type. /// @tparam T The type of the object to be created or assigned. @@ -46,7 +46,7 @@ struct SpecialCreationHelper /// assigned. /// @param[in] src The source object, either for copy or move operations. template - static inline void create(T& dest, V&& src) noexcept + static inline void transfer(T& dest, V&& src) noexcept { if constexpr (is_ctor()) { @@ -74,12 +74,6 @@ struct SpecialCreationHelper } else { - // @todo iox-#2052: enable this when move_or_copy_it / move_or_copy works - // static_assert(std::is_lvalue_reference_v, "src should be lvalue reference"); - // static_assert(std::is_const_v>, "src should has const - // modifier"); static_assert(std::is_convertible_v, "Source type is not convertible to destination - // type"); new (&dest) T(src.get()); - static_assert(std::is_lvalue_reference_v, "src should be lvalue reference"); static_assert(std::is_const_v>, "src should has 'const' modifier"); static_assert(std::is_convertible_v, "src type is not convertible to dest type"); @@ -102,11 +96,6 @@ struct SpecialCreationHelper } else { - // @todo iox-#2052: enable this when move_or_copy_it / move_or_copy works - // static_assert(std::is_lvalue_reference_v, "src should be lvalue reference"); - // static_assert(std::is_const_v>, "src should has const - // modifier"); dest = src.get(); - static_assert(std::is_lvalue_reference_v, "src should be lvalue reference"); static_assert(std::is_const_v>, "src should has 'const' modifier"); dest = src; @@ -117,45 +106,15 @@ struct SpecialCreationHelper /// @return True if the operation is a copy or move constructor, false otherwise. static constexpr bool is_ctor() noexcept { - return Opt == SpecialCreationOperations::CopyConstructor || Opt == SpecialCreationOperations::MoveConstructor; + return Opt == MoveAndCopyOperations::CopyConstructor || Opt == MoveAndCopyOperations::MoveConstructor; } /// @brief Checks if the current special operation is a move operation. /// @return True if the operation is a move constructor or move assignment, false otherwise. static constexpr bool is_move() noexcept { - return Opt == SpecialCreationOperations::MoveAssignment || Opt == SpecialCreationOperations::MoveConstructor; + return Opt == MoveAndCopyOperations::MoveAssignment || Opt == MoveAndCopyOperations::MoveConstructor; } - - // template - // static inline constexpr auto move_or_copy(T&& value) noexcept - // { - // if constexpr (is_move()) - // { - // // FIXME: See https://godbolt.org/z/hz9Kbb8nr. - // return std::move(std::forward(value)); - // } - // else - // { - // return std::cref(std::forward(value)); - // } - // } - - // template - // static inline constexpr auto move_or_copy_it(Iterator& it) noexcept - // { - // if constexpr (is_move()) - // { - // // FIXME: Twice time movector will be called. - // return std::move(*it); - // } - // else - // { - // // std::cref fix twice time copyCtor problem? - // // See https://godbolt.org/z/W41ndvhh5 - // return std::cref(*it); - // } - // } }; } // namespace detail } // namespace iox diff --git a/iceoryx_dust/container/include/iox/fixed_position_container.hpp b/iceoryx_dust/container/include/iox/fixed_position_container.hpp index f9ac5cbad3..0902b3ec96 100644 --- a/iceoryx_dust/container/include/iox/fixed_position_container.hpp +++ b/iceoryx_dust/container/include/iox/fixed_position_container.hpp @@ -316,7 +316,7 @@ class FixedPositionContainer final }; private: - template + template void copy_and_move_impl(RhsType&& rhs) noexcept; private: