/* * Copyright (c) 2012 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_ALLOCATE_SHARED_ARRAY_HPP #define BOOST_SMART_PTR_ALLOCATE_SHARED_ARRAY_HPP #include #include #include #include #include #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) #include #endif namespace boost { template inline typename boost::detail::sp_if_array::type allocate_shared(const A& allocator, std::size_t size) { typedef typename boost::detail::array_inner::type T1; typedef typename boost::detail::array_base::type T2; T1* p1 = 0; T2* p2 = 0; std::size_t n1 = size * boost::detail::array_total::size; boost::detail::allocate_array_helper a1(allocator, n1, &p2); boost::detail::array_deleter d1(n1); boost::shared_ptr s1(p1, d1, a1); typedef boost::detail::array_deleter* D2; p1 = reinterpret_cast(p2); D2 d2 = static_cast(s1._internal_get_untyped_deleter()); d2->init(p2); return boost::shared_ptr(s1, p1); } #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) template inline typename boost::detail::sp_if_array::type allocate_shared(const A& allocator, std::size_t size, Args&&... args) { typedef typename boost::detail::array_inner::type T1; typedef typename boost::detail::array_base::type T2; T1* p1 = 0; T2* p2 = 0; std::size_t n1 = size * boost::detail::array_total::size; boost::detail::allocate_array_helper a1(allocator, n1, &p2); boost::detail::array_deleter d1(n1); boost::shared_ptr s1(p1, d1, a1); typedef boost::detail::array_deleter* D2; p1 = reinterpret_cast(p2); D2 d2 = static_cast(s1._internal_get_untyped_deleter()); d2->init(p2, boost::detail::sp_forward(args)...); return boost::shared_ptr(s1, p1); } template inline typename boost::detail::sp_if_size_array::type allocate_shared(const A& allocator, Args&&... args) { typedef typename boost::detail::array_inner::type T1; typedef typename boost::detail::array_base::type T2; enum { N = boost::detail::array_total::size }; T1* p1 = 0; T2* p2 = 0; boost::detail::allocate_array_helper a1(allocator, &p2); boost::detail::array_deleter d1; boost::shared_ptr s1(p1, d1, a1); typedef boost::detail::array_deleter* D2; p1 = reinterpret_cast(p2); D2 d2 = static_cast(s1._internal_get_untyped_deleter()); d2->init(p2, boost::detail::sp_forward(args)...); return boost::shared_ptr(s1, p1); } #endif #if !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX) template inline typename boost::detail::sp_if_size_array::type allocate_shared(const A& allocator, const T& list) { typedef typename boost::detail::array_inner::type T1; typedef typename boost::detail::array_base::type T2; typedef const T2 T3; enum { N = boost::detail::array_total::size }; T1* p1 = 0; T2* p2 = 0; T3* p3 = 0; boost::detail::allocate_array_helper a1(allocator, &p2); boost::detail::array_deleter d1; boost::shared_ptr s1(p1, d1, a1); typedef boost::detail::array_deleter* D2; p3 = reinterpret_cast(list); p1 = reinterpret_cast(p2); D2 d2 = static_cast(s1._internal_get_untyped_deleter()); d2->init_list(p2, p3); return boost::shared_ptr(s1, p1); } template inline typename boost::detail::sp_if_array::type allocate_shared(const A& allocator, std::size_t size, const typename boost::detail::array_inner::type& list) { typedef typename boost::detail::array_inner::type T1; typedef typename boost::detail::array_base::type T2; typedef const T2 T3; enum { M = boost::detail::array_total::size }; T1* p1 = 0; T2* p2 = 0; T3* p3 = 0; std::size_t n1 = M * size; boost::detail::allocate_array_helper a1(allocator, n1, &p2); boost::detail::array_deleter d1(n1); boost::shared_ptr s1(p1, d1, a1); typedef boost::detail::array_deleter* D2; p3 = reinterpret_cast(list); p1 = reinterpret_cast(p2); D2 d2 = static_cast(s1._internal_get_untyped_deleter()); d2->template init_list(p2, p3); return boost::shared_ptr(s1, p1); } template inline typename boost::detail::sp_if_size_array::type allocate_shared(const A& allocator, const typename boost::detail::array_inner::type& list) { typedef typename boost::detail::array_inner::type T1; typedef typename boost::detail::array_base::type T2; typedef const T2 T3; enum { M = boost::detail::array_total::size, N = boost::detail::array_total::size }; T1* p1 = 0; T2* p2 = 0; T3* p3 = 0; boost::detail::allocate_array_helper a1(allocator, &p2); boost::detail::array_deleter d1; boost::shared_ptr s1(p1, d1, a1); typedef boost::detail::array_deleter* D2; p3 = reinterpret_cast(list); p1 = reinterpret_cast(p2); D2 d2 = static_cast(s1._internal_get_untyped_deleter()); d2->template init_list(p2, p3); return boost::shared_ptr(s1, p1); } #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) template inline typename boost::detail::sp_if_array::type allocate_shared(const A& allocator, std::initializer_list::type> list) { typedef typename boost::detail::array_inner::type T1; typedef typename boost::detail::array_base::type T2; typedef const T2 T3; T1* p1 = 0; T2* p2 = 0; T3* p3 = 0; std::size_t n1 = list.size() * boost::detail::array_total::size; boost::detail::allocate_array_helper a1(allocator, n1, &p2); boost::detail::array_deleter d1(n1); boost::shared_ptr s1(p1, d1, a1); typedef boost::detail::array_deleter* D2; p3 = reinterpret_cast(list.begin()); p1 = reinterpret_cast(p2); D2 d2 = static_cast(s1._internal_get_untyped_deleter()); d2->init_list(p2, p3); return boost::shared_ptr(s1, p1); } #endif #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) template inline typename boost::detail::sp_if_array::type allocate_shared(const A& allocator, std::size_t size, typename boost::detail::array_base::type&& value) { typedef typename boost::detail::array_inner::type T1; typedef typename boost::detail::array_base::type T2; T1* p1 = 0; T2* p2 = 0; std::size_t n1 = size * boost::detail::array_total::size; boost::detail::allocate_array_helper a1(allocator, n1, &p2); boost::detail::array_deleter d1(n1); boost::shared_ptr s1(p1, d1, a1); typedef boost::detail::array_deleter* D2; p1 = reinterpret_cast(p2); D2 d2 = static_cast(s1._internal_get_untyped_deleter()); d2->init(p2, boost::detail::sp_forward(value)); return boost::shared_ptr(s1, p1); } template inline typename boost::detail::sp_if_size_array::type allocate_shared(const A& allocator, typename boost::detail::array_base::type&& value) { typedef typename boost::detail::array_inner::type T1; typedef typename boost::detail::array_base::type T2; enum { N = boost::detail::array_total::size }; T1* p1 = 0; T2* p2 = 0; boost::detail::allocate_array_helper a1(allocator, &p2); boost::detail::array_deleter d1; boost::shared_ptr s1(p1, d1, a1); typedef boost::detail::array_deleter* D2; p1 = reinterpret_cast(p2); D2 d2 = static_cast(s1._internal_get_untyped_deleter()); d2->init(p2, boost::detail::sp_forward(value)); return boost::shared_ptr(s1, p1); } #endif #endif template inline typename boost::detail::sp_if_array::type allocate_shared_noinit(const A& allocator, std::size_t size) { typedef typename boost::detail::array_inner::type T1; typedef typename boost::detail::array_base::type T2; T1* p1 = 0; T2* p2 = 0; std::size_t n1 = size * boost::detail::array_total::size; boost::detail::allocate_array_helper a1(allocator, n1, &p2); boost::detail::array_deleter d1(n1); boost::shared_ptr s1(p1, d1, a1); typedef boost::detail::array_deleter* D2; p1 = reinterpret_cast(p2); D2 d2 = static_cast(s1._internal_get_untyped_deleter()); d2->noinit(p2); return boost::shared_ptr(s1, p1); } template inline typename boost::detail::sp_if_size_array::type allocate_shared_noinit(const A& allocator) { typedef typename boost::detail::array_inner::type T1; typedef typename boost::detail::array_base::type T2; enum { N = boost::detail::array_total::size }; T1* p1 = 0; T2* p2 = 0; boost::detail::allocate_array_helper a1(allocator, &p2); boost::detail::array_deleter d1; boost::shared_ptr s1(p1, d1, a1); typedef boost::detail::array_deleter* D2; p1 = reinterpret_cast(p2); D2 d2 = static_cast(s1._internal_get_untyped_deleter()); d2->noinit(p2); return boost::shared_ptr(s1, p1); } } #endif