/* * Copyright (c) 2012-2014 Glen Joseph Fernandes * glenfe at live dot com * * Distributed under the Boost Software License, * Version 1.0. (See accompanying file LICENSE_1_0.txt * or copy at http://boost.org/LICENSE_1_0.txt) */ #ifndef BOOST_SMART_PTR_DETAIL_ARRAY_UTILITY_HPP #define BOOST_SMART_PTR_DETAIL_ARRAY_UTILITY_HPP #include #include #include #if !defined(BOOST_NO_CXX11_ALLOCATOR) #include #endif namespace boost { namespace detail { typedef boost::true_type ms_is_trivial; typedef boost::false_type ms_no_trivial; template inline void ms_destroy(T*, std::size_t, ms_is_trivial) { } template inline void ms_destroy(T* memory, std::size_t size, ms_no_trivial) { for (std::size_t i = size; i > 0;) { memory[--i].~T(); } } template inline void ms_destroy(T* memory, std::size_t size) { boost::has_trivial_destructor trivial; ms_destroy(memory, size, trivial); } template inline void ms_init(T* memory, std::size_t size, ms_is_trivial) { for (std::size_t i = 0; i < size; i++) { void* p1 = memory + i; ::new(p1) T(); } } template inline void ms_init(T* memory, std::size_t size, ms_no_trivial) { #if !defined(BOOST_NO_EXCEPTIONS) std::size_t i = 0; try { for (; i < size; i++) { void* p1 = memory + i; ::new(p1) T(); } } catch (...) { ms_destroy(memory, i); throw; } #else for (std::size_t i = 0; i < size; i++) { void* p1 = memory + i; ::new(p1) T(); } #endif } template inline void ms_init(T* memory, std::size_t size) { boost::has_trivial_default_constructor trivial; ms_init(memory, size, trivial); } template inline void ms_init(T* memory, std::size_t size, const T* list) { #if !defined(BOOST_NO_EXCEPTIONS) std::size_t i = 0; try { for (; i < size; i++) { void* p1 = memory + i; ::new(p1) T(list[i % N]); } } catch (...) { ms_destroy(memory, i); throw; } #else for (std::size_t i = 0; i < size; i++) { void* p1 = memory + i; ::new(p1) T(list[i % N]); } #endif } #if !defined(BOOST_NO_CXX11_ALLOCATOR) template inline void as_destroy(const A& allocator, T* memory, std::size_t size) { typedef typename std::allocator_traits:: template rebind_alloc TA; typedef typename std::allocator_traits:: template rebind_traits TT; TA a2(allocator); for (std::size_t i = size; i > 0;) { TT::destroy(a2, &memory[--i]); } } template inline void as_init(const A& allocator, T* memory, std::size_t size, ms_is_trivial) { typedef typename std::allocator_traits:: template rebind_alloc TA; typedef typename std::allocator_traits:: template rebind_traits TT; TA a2(allocator); for (std::size_t i = 0; i < size; i++) { TT::construct(a2, memory + i); } } template inline void as_init(const A& allocator, T* memory, std::size_t size, ms_no_trivial) { typedef typename std::allocator_traits:: template rebind_alloc TA; typedef typename std::allocator_traits:: template rebind_traits TT; TA a2(allocator); #if !defined(BOOST_NO_EXCEPTIONS) std::size_t i = 0; try { for (; i < size; i++) { TT::construct(a2, memory + i); } } catch (...) { as_destroy(a2, memory, i); throw; } #else for (std::size_t i = 0; i < size; i++) { TT::construct(a2, memory + i); } #endif } template inline void as_init(const A& allocator, T* memory, std::size_t size) { boost::has_trivial_default_constructor trivial; as_init(allocator, memory, size, trivial); } template inline void as_init(const A& allocator, T* memory, std::size_t size, const T* list) { typedef typename std::allocator_traits:: template rebind_alloc TA; typedef typename std::allocator_traits:: template rebind_traits TT; TA a2(allocator); #if !defined(BOOST_NO_EXCEPTIONS) std::size_t i = 0; try { for (; i < size; i++) { TT::construct(a2, memory + i, list[i % N]); } } catch (...) { as_destroy(a2, memory, i); throw; } #else for (std::size_t i = 0; i < size; i++) { TT::construct(a2, memory + i, list[i % N]); } #endif } #endif template inline void ms_noinit(T*, std::size_t, ms_is_trivial) { } template inline void ms_noinit(T* memory, std::size_t size, ms_no_trivial) { #if !defined(BOOST_NO_EXCEPTIONS) std::size_t i = 0; try { for (; i < size; i++) { void* p1 = memory + i; ::new(p1) T; } } catch (...) { ms_destroy(memory, i); throw; } #else for (std::size_t i = 0; i < size; i++) { void* p1 = memory + i; ::new(p1) T; } #endif } template inline void ms_noinit(T* memory, std::size_t size) { boost::has_trivial_default_constructor trivial; ms_noinit(memory, size, trivial); } } } #endif