/* Copyright 2019 Glen Joseph Fernandes (glenjofe@gmail.com) Distributed under the Boost Software License, Version 1.0. (http://www.boost.org/LICENSE_1_0.txt) */ #ifndef BOOST_CORE_ALLOC_CONSTRUCT_HPP #define BOOST_CORE_ALLOC_CONSTRUCT_HPP #include namespace boost { #if !defined(BOOST_NO_CXX11_ALLOCATOR) template inline void alloc_destroy(A& a, T* p) { std::allocator_traits::destroy(a, p); } template inline void alloc_destroy_n(A& a, T* p, std::size_t n) { while (n > 0) { std::allocator_traits::destroy(a, p + --n); } } #else template inline void alloc_destroy(A&, T* p) { p->~T(); } template inline void alloc_destroy_n(A&, T* p, std::size_t n) { while (n > 0) { p[--n].~T(); } } #endif namespace detail { template class alloc_destroyer { public: alloc_destroyer(A& a, T* p) BOOST_NOEXCEPT : a_(a), p_(p), n_(0) { } ~alloc_destroyer() { boost::alloc_destroy_n(a_, p_, n_); } std::size_t& size() BOOST_NOEXCEPT { return n_; } private: alloc_destroyer(const alloc_destroyer&); alloc_destroyer& operator=(const alloc_destroyer&); A& a_; T* p_; std::size_t n_; }; } /* detail */ #if !defined(BOOST_NO_CXX11_ALLOCATOR) template inline void alloc_construct(A& a, T* p) { std::allocator_traits::construct(a, p); } #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) template inline void alloc_construct(A& a, T* p, U&& u, V&&... v) { std::allocator_traits::construct(a, p, std::forward(u), std::forward(v)...); } #else template inline void alloc_construct(A& a, T* p, U&& u) { std::allocator_traits::construct(a, p, std::forward(u)); } #endif #else template inline void alloc_construct(A& a, T* p, const U& u) { std::allocator_traits::construct(a, p, u); } template inline void alloc_construct(A& a, T* p, U& u) { std::allocator_traits::construct(a, p, u); } #endif template inline void alloc_construct_n(A& a, T* p, std::size_t n) { detail::alloc_destroyer hold(a, p); for (std::size_t& i = hold.size(); i < n; ++i) { std::allocator_traits::construct(a, p + i); } hold.size() = 0; } template inline void alloc_construct_n(A& a, T* p, std::size_t n, const T* l, std::size_t m) { detail::alloc_destroyer hold(a, p); for (std::size_t& i = hold.size(); i < n; ++i) { std::allocator_traits::construct(a, p + i, l[i % m]); } hold.size() = 0; } template inline void alloc_construct_n(A& a, T* p, std::size_t n, I b) { detail::alloc_destroyer hold(a, p); for (std::size_t& i = hold.size(); i < n; void(++i), void(++b)) { std::allocator_traits::construct(a, p + i, *b); } hold.size() = 0; } #else template inline void alloc_construct(A&, T* p) { ::new(static_cast(p)) T(); } template inline void alloc_construct(noinit_adaptor&, T* p) { ::new(static_cast(p)) T; } #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) template inline void alloc_construct(A&, T* p, U&& u, V&&... v) { ::new(static_cast(p)) T(std::forward(u), std::forward(v)...); } #else template inline void alloc_construct(A& a, T* p, U&& u) { ::new(static_cast(p)) T(std::forward(u)); } #endif #else template inline void alloc_construct(A&, T* p, const U& u) { ::new(static_cast(p)) T(u); } template inline void alloc_construct(A&, T* p, U& u) { ::new(static_cast(p)) T(u); } #endif template inline void alloc_construct_n(A& a, T* p, std::size_t n) { detail::alloc_destroyer hold(a, p); for (std::size_t& i = hold.size(); i < n; ++i) { ::new(static_cast(p + i)) T(); } hold.size() = 0; } template inline void alloc_construct_n(noinit_adaptor& a, T* p, std::size_t n) { detail::alloc_destroyer, T> hold(a, p); for (std::size_t& i = hold.size(); i < n; ++i) { ::new(static_cast(p + i)) T; } hold.size() = 0; } template inline void alloc_construct_n(A& a, T* p, std::size_t n, const T* l, std::size_t m) { detail::alloc_destroyer hold(a, p); for (std::size_t& i = hold.size(); i < n; ++i) { ::new(static_cast(p + i)) T(l[i % m]); } hold.size() = 0; } template inline void alloc_construct_n(A& a, T* p, std::size_t n, I b) { detail::alloc_destroyer hold(a, p); for (std::size_t& i = hold.size(); i < n; void(++i), void(++b)) { ::new(static_cast(p + i)) T(*b); } hold.size() = 0; } #endif } /* boost */ #endif