Use boost::adl_move_swap instead of custom swap or std::swap for performance and compatibility reasons.

This commit is contained in:
Ion Gaztañaga
2014-11-28 15:41:44 +01:00
parent afd1c2d266
commit 32418cab5f
11 changed files with 39 additions and 81 deletions

View File

@@ -35,6 +35,7 @@
#include <boost/type_traits/has_nothrow_assign.hpp>
#include <boost/move/utility_core.hpp>
#include <boost/move/iterator.hpp>
#include <boost/move/adl_move_swap.hpp>
#include <boost/move/algorithm.hpp>
#include <boost/move/detail/move_helpers.hpp>
#include <boost/move/traits.hpp>
@@ -355,10 +356,10 @@ class deque_base
void swap_members(deque_base &x) BOOST_CONTAINER_NOEXCEPT
{
::boost::container::swap_dispatch(this->members_.m_start, x.members_.m_start);
::boost::container::swap_dispatch(this->members_.m_finish, x.members_.m_finish);
::boost::container::swap_dispatch(this->members_.m_map, x.members_.m_map);
::boost::container::swap_dispatch(this->members_.m_map_size, x.members_.m_map_size);
::boost::adl_move_swap(this->members_.m_start, x.members_.m_start);
::boost::adl_move_swap(this->members_.m_finish, x.members_.m_finish);
::boost::adl_move_swap(this->members_.m_map, x.members_.m_map);
::boost::adl_move_swap(this->members_.m_map_size, x.members_.m_map_size);
}
void priv_initialize_map(size_type num_elements)

View File

@@ -36,6 +36,7 @@
#endif
#include <boost/aligned_storage.hpp>
#include <boost/move/make_unique.hpp>
#include <boost/move/adl_move_swap.hpp>
#include <utility> //std::pair
@@ -159,7 +160,7 @@ class flat_tree
void swap(Data &d)
{
value_compare& mycomp = *this, & othercomp = d;
boost::container::swap_dispatch(mycomp, othercomp);
boost::adl_move_swap(mycomp, othercomp);
this->m_vect.swap(d.m_vect);
}

View File

@@ -57,12 +57,6 @@
#endif
#include BOOST_PP_ITERATE()
#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME swap
#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEGIN namespace boost { namespace container { namespace container_detail {
#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END }}}
#define BOOST_PP_ITERATION_PARAMS_1 (3, (1, 1, <boost/intrusive/detail/has_member_function_callable_with.hpp>))
#include BOOST_PP_ITERATE()
namespace boost {
namespace container {
namespace container_detail {

View File

@@ -40,10 +40,10 @@
#include <boost/container/detail/type_traits.hpp>
#include <boost/container/allocator_traits.hpp>
#include <boost/core/no_exceptions_support.hpp>
#include <boost/container/detail/memory_util.hpp>
#include <boost/intrusive/pointer_traits.hpp>
#include <boost/aligned_storage.hpp>
#include <boost/move/adl_move_swap.hpp>
namespace boost {
namespace container {
@@ -54,51 +54,6 @@ namespace container {
//
//////////////////////////////////////////////////////////////////////////////
namespace container_swap {
template<class T, bool IsClass = boost::is_class<T>::value >
struct has_member_swap
{
static const bool value = boost::container::container_detail::
has_member_function_callable_with_swap<T, T &>::value;
};
template<class T>
struct has_member_swap<T, false>
{
static const bool value = false;
};
} //namespace container_swap {
template<class T> inline
typename container_detail::enable_if_c
<container_swap::has_member_swap<T>::value, void>::type
swap_dispatch(T &left, T &right) //swap using member swap
{
left.swap(right); // may throw
}
template<class T> inline
typename container_detail::enable_if_c
<!container_swap::has_member_swap<T>::value/* && boost::has_move_emulation_enabled<T>::value*/, void>::type
swap_dispatch(T &left, T &right)
{
T temp(boost::move(left)); // may throw
left = boost::move(right); // may throw
right = boost::move(temp); // may throw
}
/*
template<class T> inline
typename container_detail::enable_if_c
<!container_swap::has_member_swap<T>::value && !boost::has_move_emulation_enabled<T>::value, void>::type
swap_dispatch(T &left, T &right)
{
using std::swap;
swap(left, right); // may throw
}
*/
namespace container_detail {
template <typename T>
@@ -194,7 +149,7 @@ inline void swap_alloc(AllocatorType &, AllocatorType &, container_detail::false
template<class AllocatorType>
inline void swap_alloc(AllocatorType &l, AllocatorType &r, container_detail::true_type)
{ boost::container::swap_dispatch(l, r); }
{ boost::adl_move_swap(l, r); }
template<class AllocatorType>
inline void assign_alloc(AllocatorType &, const AllocatorType &, container_detail::false_type)
@@ -1102,7 +1057,7 @@ inline typename container_detail::disable_if_memtransfer_copy_assignable<F, G, v
{
typename allocator_traits<A>::size_type n = 0;
for (; n != n_i ; ++short_range_f, ++large_range_f, ++n){
boost::container::swap_dispatch(*short_range_f, *large_range_f);
boost::adl_move_swap(*short_range_f, *large_range_f);
}
boost::container::uninitialized_move_alloc_n(a, large_range_f, n_j - n_i, short_range_f); // may throw
boost::container::destroy_alloc_n(a, large_range_f, n_j - n_i);

View File

@@ -32,6 +32,7 @@
#include <boost/container/detail/pair.hpp>
#include <boost/move/utility_core.hpp>
#include <boost/core/no_exceptions_support.hpp>
#include <boost/move/adl_move_swap.hpp>
namespace boost { namespace container {
@@ -668,8 +669,8 @@ class scoped_allocator_adaptor_base
void swap(scoped_allocator_adaptor_base &r)
{
boost::container::swap_dispatch(this->outer_allocator(), r.outer_allocator());
boost::container::swap_dispatch(this->m_inner, r.inner_allocator());
boost::adl_move_swap(this->outer_allocator(), r.outer_allocator());
boost::adl_move_swap(this->m_inner, r.inner_allocator());
}
friend void swap(scoped_allocator_adaptor_base &l, scoped_allocator_adaptor_base &r)
@@ -839,8 +840,8 @@ class scoped_allocator_adaptor_base<OuterAlloc, true
\
void swap(scoped_allocator_adaptor_base &r) \
{ \
boost::container::swap_dispatch(this->outer_allocator(), r.outer_allocator()); \
boost::container::swap_dispatch(this->m_inner, r.inner_allocator()); \
boost::adl_move_swap(this->outer_allocator(), r.outer_allocator()); \
boost::adl_move_swap(this->m_inner, r.inner_allocator()); \
} \
\
friend void swap(scoped_allocator_adaptor_base &l, scoped_allocator_adaptor_base &r) \
@@ -976,7 +977,7 @@ class scoped_allocator_adaptor_base
void swap(scoped_allocator_adaptor_base &r)
{
boost::container::swap_dispatch(this->outer_allocator(), r.outer_allocator());
boost::adl_move_swap(this->outer_allocator(), r.outer_allocator());
}
friend void swap(scoped_allocator_adaptor_base &l, scoped_allocator_adaptor_base &r)

View File

@@ -43,6 +43,7 @@
#include <boost/move/iterator.hpp>
#include <boost/move/detail/move_helpers.hpp>
#include <boost/container/detail/placement_new.hpp>
#include <boost/move/adl_move_swap.hpp>
#include <utility> //std::pair
@@ -1911,7 +1912,7 @@ class stable_vector
void priv_swap_members(stable_vector &x)
{
boost::container::swap_dispatch(this->internal_data.pool_size, x.internal_data.pool_size);
boost::adl_move_swap(this->internal_data.pool_size, x.internal_data.pool_size);
index_traits_type::readjust_end_node(this->index, this->internal_data.end_node);
index_traits_type::readjust_end_node(x.index, x.internal_data.end_node);
}

View File

@@ -30,6 +30,7 @@
#include <boost/container/detail/mpl.hpp>
#include <boost/container/detail/iterator.hpp>
#include <boost/move/utility_core.hpp>
#include <boost/move/adl_move_swap.hpp>
#include <boost/static_assert.hpp>
#include <boost/functional/hash.hpp>
#include <boost/intrusive/pointer_traits.hpp>
@@ -433,7 +434,7 @@ class basic_string_base
this->members_.m_repr.short_repr() = short_backup;
}
else{
boost::container::swap_dispatch(this->members_.m_repr.long_repr(), other.members_.m_repr.long_repr());
boost::adl_move_swap(this->members_.m_repr.long_repr(), other.members_.m_repr.long_repr());
}
}
}

View File

@@ -26,6 +26,7 @@
#include <boost/move/algorithm.hpp>
#include <boost/move/detail/move_helpers.hpp>
#include <boost/move/traits.hpp>
#include <boost/move/adl_move_swap.hpp>
#include <boost/container/detail/version_type.hpp>
#include <boost/container/detail/allocation_type.hpp>
@@ -363,9 +364,9 @@ struct vector_alloc_holder
void swap(vector_alloc_holder &x) BOOST_CONTAINER_NOEXCEPT
{
boost::container::swap_dispatch(this->m_start, x.m_start);
boost::container::swap_dispatch(this->m_size, x.m_size);
boost::container::swap_dispatch(this->m_capacity, x.m_capacity);
boost::adl_move_swap(this->m_start, x.m_start);
boost::adl_move_swap(this->m_size, x.m_size);
boost::adl_move_swap(this->m_capacity, x.m_capacity);
}
void move_from_empty(vector_alloc_holder &x) BOOST_CONTAINER_NOEXCEPT
@@ -515,7 +516,7 @@ struct vector_alloc_holder<Allocator, container_detail::integral_constant<unsign
else{
boost::container::deep_swap_alloc_n<MaxTmpStorage>(this->alloc(), first_x, x.m_size, first_this, this->m_size);
}
boost::container::swap_dispatch(this->m_size, x.m_size);
boost::adl_move_swap(this->m_size, x.m_size);
}
};

View File

@@ -28,6 +28,7 @@
#include <boost/container/detail/multiallocation_chain.hpp>
#include <boost/container/throw_exception.hpp>
#include <boost/move/utility_core.hpp>
#include <boost/move/adl_move_swap.hpp>
#include <memory>
#include <algorithm>
#include <cstddef>
@@ -314,12 +315,12 @@ class propagation_test_allocator
void swap(propagation_test_allocator &r)
{
++this->swaps_; ++r.swaps_;
boost::container::swap_dispatch(this->id_, r.id_);
boost::container::swap_dispatch(this->ctr_copies_, r.ctr_copies_);
boost::container::swap_dispatch(this->ctr_moves_, r.ctr_moves_);
boost::container::swap_dispatch(this->assign_copies_, r.assign_copies_);
boost::container::swap_dispatch(this->assign_moves_, r.assign_moves_);
boost::container::swap_dispatch(this->swaps_, r.swaps_);
boost::adl_move_swap(this->id_, r.id_);
boost::adl_move_swap(this->ctr_copies_, r.ctr_copies_);
boost::adl_move_swap(this->ctr_moves_, r.ctr_moves_);
boost::adl_move_swap(this->assign_copies_, r.assign_copies_);
boost::adl_move_swap(this->assign_moves_, r.assign_moves_);
boost::adl_move_swap(this->swaps_, r.swaps_);
}
friend void swap(propagation_test_allocator &l, propagation_test_allocator &r)

View File

@@ -24,6 +24,7 @@
#include <boost/assert.hpp>
#include <boost/container/detail/utilities.hpp>
#include <boost/container/detail/version_type.hpp>
#include <boost/move/adl_move_swap.hpp>
#include <memory>
#include <algorithm>
#include <cstddef>
@@ -109,9 +110,9 @@ class expand_bwd_test_allocator
friend void swap(self_t &alloc1, self_t &alloc2)
{
boost::container::swap_dispatch(alloc1.mp_buffer, alloc2.mp_buffer);
boost::container::swap_dispatch(alloc1.m_size, alloc2.m_size);
boost::container::swap_dispatch(alloc1.m_offset, alloc2.m_offset);
boost::adl_move_swap(alloc1.mp_buffer, alloc2.mp_buffer);
boost::adl_move_swap(alloc1.m_size, alloc2.m_size);
boost::adl_move_swap(alloc1.m_offset, alloc2.m_offset);
}
//Experimental version 2 expand_bwd_test_allocator functions

View File

@@ -13,6 +13,7 @@
#include <cstddef>
#include <boost/container/detail/mpl.hpp>
#include <boost/move/utility_core.hpp>
#include <boost/move/adl_move_swap.hpp>
#include <memory>
using namespace boost::container;
@@ -475,8 +476,8 @@ int main()
{
Scoped0Inner s0i2;
Scoped1Inner s1i2;
boost::container::swap_dispatch(s0i, s0i2);
boost::container::swap_dispatch(s1i, s1i2);
boost::adl_move_swap(s0i, s0i2);
boost::adl_move_swap(s1i, s1i2);
}
}