/* * 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 namespace boost { namespace detail { template inline void array_destroy(T*, std::size_t, boost::true_type) { } template inline void array_destroy(T* memory, std::size_t size, boost::false_type) { for (std::size_t i = size; i > 0; ) { memory[--i].~T(); } } template inline void array_destroy(T* memory, std::size_t size) { boost::has_trivial_destructor type; array_destroy(memory, size, type); } template inline void array_value(T* memory, std::size_t size, boost::true_type) { for (std::size_t i = 0; i < size; i++) { void* p1 = memory + i; ::new(p1) T(); } } template inline void array_value(T* memory, std::size_t size, boost::false_type) { #if !defined(BOOST_NO_EXCEPTIONS) std::size_t i = 0; try { for (; i < size; i++) { void* p1 = memory + i; ::new(p1) T(); } } catch (...) { array_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 array_init(T* memory, std::size_t size) { boost::has_trivial_default_constructor type; array_value(memory, size, type); } template inline void array_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 (...) { array_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 } template inline void array_default(T*, std::size_t, boost::true_type) { } template inline void array_default(T* memory, std::size_t size, boost::false_type) { #if !defined(BOOST_NO_EXCEPTIONS) std::size_t i = 0; try { for (; i < size; i++) { void* p1 = memory + i; ::new(p1) T; } } catch (...) { array_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 array_noinit(T* memory, std::size_t size) { boost::has_trivial_default_constructor type; array_default(memory, size, type); } } } #endif