From a3c8c2125a86f09215b3a511f04adb71d9600853 Mon Sep 17 00:00:00 2001 From: Glen Fernandes Date: Tue, 4 Jul 2017 13:45:56 -0400 Subject: [PATCH] Use has_trivial_assign in construction utilities --- .../boost/smart_ptr/allocate_shared_array.hpp | 127 +++++++++--------- 1 file changed, 60 insertions(+), 67 deletions(-) diff --git a/include/boost/smart_ptr/allocate_shared_array.hpp b/include/boost/smart_ptr/allocate_shared_array.hpp index 98adead..23c35af 100644 --- a/include/boost/smart_ptr/allocate_shared_array.hpp +++ b/include/boost/smart_ptr/allocate_shared_array.hpp @@ -10,8 +10,8 @@ Distributed under the Boost Software License, Version 1.0. #include #include +#include #include -#include #include #include @@ -154,99 +154,103 @@ sp_array_destroy(A&, T*, std::size_t) BOOST_SP_NOEXCEPT { } template inline typename sp_enable::value>::type -sp_array_destroy(A&, T* storage, std::size_t size) +sp_array_destroy(A&, T* start, std::size_t size) { while (size > 0) { - storage[--size].~T(); + start[--size].~T(); } } #if !defined(BOOST_NO_CXX11_ALLOCATOR) template inline typename sp_enable::type -sp_array_destroy(A& allocator, T* storage, std::size_t size) +sp_array_destroy(A& allocator, T* start, std::size_t size) { while (size > 0) { - std::allocator_traits::destroy(allocator, storage + --size); + std::allocator_traits::destroy(allocator, start + --size); } } #endif -#if !defined(BOOST_NO_EXCEPTIONS) template inline typename sp_enable::value || - boost::has_trivial_destructor::value)>::type -sp_array_construct(A&, T* storage, std::size_t size) + boost::has_trivial_constructor::value && + boost::has_trivial_assign::value>::type +sp_array_construct(A&, T* start, std::size_t size) { for (std::size_t i = 0; i < size; ++i) { - ::new(static_cast(storage + i)) T(); + start[i] = T(); } } template inline typename sp_enable::value && - !boost::has_trivial_destructor::value>::type -sp_array_construct(A& none, T* storage, std::size_t size) + boost::has_trivial_constructor::value && + boost::has_trivial_assign::value>::type +sp_array_construct(A&, T* start, std::size_t size, const T* list, + std::size_t count) +{ + for (std::size_t i = 0; i < size; ++i) { + start[i] = list[i % count]; + } +} + +#if !defined(BOOST_NO_EXCEPTIONS) +template +inline typename sp_enable::value && + boost::has_trivial_assign::value)>::type +sp_array_construct(A& none, T* start, std::size_t size) { std::size_t i = 0; try { for (; i < size; ++i) { - ::new(static_cast(storage + i)) T(); + ::new(static_cast(start + i)) T(); } } catch (...) { - sp_array_destroy(none, storage, i); + sp_array_destroy(none, start, i); throw; } } template inline typename sp_enable::value || - boost::has_trivial_destructor::value)>::type -sp_array_construct(A&, T* storage, std::size_t size, const T* list, - std::size_t count) -{ - for (std::size_t i = 0; i < size; ++i) { - ::new(static_cast(storage + i)) T(list[i % count]); - } -} - -template -inline typename sp_enable::value && - !boost::has_trivial_destructor::value>::type -sp_array_construct(A& none, T* storage, std::size_t size, const T* list, + !(boost::has_trivial_constructor::value && + boost::has_trivial_assign::value)>::type +sp_array_construct(A& none, T* start, std::size_t size, const T* list, std::size_t count) { std::size_t i = 0; try { for (; i < size; ++i) { - ::new(static_cast(storage + i)) T(list[i % count]); + ::new(static_cast(start + i)) T(list[i % count]); } } catch (...) { - sp_array_destroy(none, storage, i); + sp_array_destroy(none, start, i); throw; } } #else template -inline typename sp_enable::type -sp_array_construct(A&, T* storage, std::size_t size) +inline typename sp_enable::value && + boost::has_trivial_assign::value)>::type +sp_array_construct(A&, T* start, std::size_t size) { for (std::size_t i = 0; i < size; ++i) { - ::new(static_cast(storage + i)) T(); + ::new(static_cast(start + i)) T(); } } template -inline typename sp_enable::type -sp_array_construct(A&, T* storage, std::size_t size, const T* list, +inline typename sp_enable::value && + boost::has_trivial_assign::value)>::type +sp_array_construct(A&, T* start, std::size_t size, const T* list, std::size_t count) { for (std::size_t i = 0; i < size; ++i) { - ::new(static_cast(storage + i)) T(list[i % count]); + ::new(static_cast(start + i)) T(list[i % count]); } } #endif @@ -255,52 +259,52 @@ sp_array_construct(A&, T* storage, std::size_t size, const T* list, #if !defined(BOOST_NO_EXCEPTIONS) template inline typename sp_enable::type -sp_array_construct(A& allocator, T* storage, std::size_t size) +sp_array_construct(A& allocator, T* start, std::size_t size) { std::size_t i = 0; try { for (i = 0; i < size; ++i) { - std::allocator_traits::construct(allocator, storage + i); + std::allocator_traits::construct(allocator, start + i); } } catch (...) { - sp_array_destroy(allocator, storage, i); + sp_array_destroy(allocator, start, i); throw; } } template inline typename sp_enable::type -sp_array_construct(A& allocator, T* storage, std::size_t size, - const T* list, std::size_t count) +sp_array_construct(A& allocator, T* start, std::size_t size, const T* list, + std::size_t count) { std::size_t i = 0; try { for (i = 0; i < size; ++i) { - std::allocator_traits::construct(allocator, storage + i, + std::allocator_traits::construct(allocator, start + i, list[i % count]); } } catch (...) { - sp_array_destroy(allocator, storage, i); + sp_array_destroy(allocator, start, i); throw; } } #else template inline typename sp_enable::type -sp_array_construct(A& allocator, T* storage, std::size_t size) +sp_array_construct(A& allocator, T* start, std::size_t size) { for (std::size_t i = 0; i < size; ++i) { - std::allocator_traits::construct(allocator, storage + i); + std::allocator_traits::construct(allocator, start + i); } } template inline typename sp_enable::type -sp_array_construct(A& allocator, T* storage, std::size_t size, - const T* list, std::size_t count) +sp_array_construct(A& allocator, T* start, std::size_t size, const T* list, + std::size_t count) { for (std::size_t i = 0; i < size; ++i) { - std::allocator_traits::construct(allocator, storage + i, + std::allocator_traits::construct(allocator, start + i, list[i % count]); } } @@ -313,37 +317,26 @@ sp_array_default(A&, T*, std::size_t) BOOST_SP_NOEXCEPT { } #if !defined(BOOST_NO_EXCEPTIONS) template -inline typename sp_enable::value && - boost::has_trivial_destructor::value>::type -sp_array_default(A&, T* storage, std::size_t size) -{ - for (std::size_t i = 0; i < size; ++i) { - ::new(static_cast(storage + i)) T; - } -} - -template -inline typename sp_enable::value && - !boost::has_trivial_destructor::value>::type -sp_array_default(A& none, T* storage, std::size_t size) +inline typename sp_enable::value>::type +sp_array_default(A& none, T* start, std::size_t size) { std::size_t i = 0; try { for (; i < size; ++i) { - ::new(static_cast(storage + i)) T; + ::new(static_cast(start + i)) T; } } catch (...) { - sp_array_destroy(none, storage, i); + sp_array_destroy(none, start, i); throw; } } #else template inline typename sp_enable::value>::type -sp_array_default(A&, T* storage, std::size_t size) +sp_array_default(A&, T* start, std::size_t size) { for (std::size_t i = 0; i < size; ++i) { - ::new(static_cast(storage + i)) T; + ::new(static_cast(start + i)) T; } } #endif