forked from boostorg/smart_ptr
		
	Use const T (&)[N] for fixed size arrays instead of std::initializer<T> in overloads of make_shared and allocate_shared for arrays. ........ Use BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX instead for certain overloads of make_shared and allocate_shared ........ Code consistency: Use the same style of #if conditional compilation checks in allocate_shared_array.hpp and make_shared_array.hpp. ........ Change make_shared and allocate_shared array form overload for size and inner array initialization list to use const T(&)[N] instead of std::initializer_list<T>. ........ Move two tests for allocate_shared and make_shared within check for BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX ........ Make specializations of detail array_deleter consistent. ........ [SVN r81682]
		
			
				
	
	
		
			210 lines
		
	
	
		
			8.4 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			210 lines
		
	
	
		
			8.4 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|  * 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_MAKE_SHARED_ARRAY_HPP
 | |
| #define BOOST_SMART_PTR_MAKE_SHARED_ARRAY_HPP
 | |
| 
 | |
| #include <boost/smart_ptr/shared_ptr.hpp>
 | |
| #include <boost/smart_ptr/detail/array_deleter.hpp>
 | |
| #include <boost/smart_ptr/detail/array_traits.hpp>
 | |
| #include <boost/smart_ptr/detail/make_array_helper.hpp>
 | |
| #include <boost/smart_ptr/detail/sp_forward.hpp>
 | |
| #include <boost/smart_ptr/detail/sp_if_array.hpp>
 | |
| #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
 | |
| #include <initializer_list>
 | |
| #endif
 | |
| 
 | |
| namespace boost {
 | |
|     template<typename T>
 | |
|     inline typename boost::detail::sp_if_array<T>::type
 | |
|     make_shared(std::size_t size) {
 | |
|         typedef typename boost::detail::array_inner<T>::type T1;
 | |
|         typedef typename boost::detail::array_base<T1>::type T2;
 | |
|         T1* p1 = 0;
 | |
|         T2* p2 = 0;
 | |
|         std::size_t n1 = size * boost::detail::array_total<T1>::size;
 | |
|         boost::detail::make_array_helper<T2[]> a1(n1, &p2);
 | |
|         boost::detail::array_deleter<T2[]> d1(n1);
 | |
|         boost::shared_ptr<T> s1(p1, d1, a1);
 | |
|         boost::detail::array_deleter<T2[]>* d2;
 | |
|         p1 = reinterpret_cast<T1*>(p2);        
 | |
|         d2 = get_deleter<boost::detail::array_deleter<T2[]> >(s1);
 | |
|         d2->construct(p2);
 | |
|         return boost::shared_ptr<T>(s1, p1);
 | |
|     }
 | |
| #if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS)
 | |
|     template<typename T, typename... Args>
 | |
|     inline typename boost::detail::sp_if_array<T>::type
 | |
|     make_shared(std::size_t size, Args&&... args) {
 | |
|         typedef typename boost::detail::array_inner<T>::type T1;
 | |
|         typedef typename boost::detail::array_base<T1>::type T2;
 | |
|         T1* p1 = 0;
 | |
|         T2* p2 = 0;
 | |
|         std::size_t n1 = size * boost::detail::array_total<T1>::size;
 | |
|         boost::detail::make_array_helper<T2[]> a1(n1, &p2);
 | |
|         boost::detail::array_deleter<T2[]> d1(n1);
 | |
|         boost::shared_ptr<T> s1(p1, d1, a1);
 | |
|         boost::detail::array_deleter<T2[]>* d2;
 | |
|         p1 = reinterpret_cast<T1*>(p2);        
 | |
|         d2 = get_deleter<boost::detail::array_deleter<T2[]> >(s1);
 | |
|         d2->construct(p2, boost::detail::sp_forward<Args>(args)...);
 | |
|         return boost::shared_ptr<T>(s1, p1);
 | |
|     }
 | |
|     template<typename T, typename... Args>
 | |
|     inline typename boost::detail::sp_if_size_array<T>::type
 | |
|     make_shared(Args&&... args) {
 | |
|         typedef typename boost::detail::array_inner<T>::type T1;
 | |
|         typedef typename boost::detail::array_base<T1>::type T2;
 | |
|         enum { 
 | |
|             N = boost::detail::array_total<T>::size 
 | |
|         };
 | |
|         T1* p1 = 0;
 | |
|         T2* p2 = 0;
 | |
|         boost::detail::make_array_helper<T2[N]> a1(&p2);
 | |
|         boost::detail::array_deleter<T2[N]> d1;
 | |
|         boost::shared_ptr<T> s1(p1, d1, a1);
 | |
|         boost::detail::array_deleter<T2[N]>* d2;
 | |
|         p1 = reinterpret_cast<T1*>(p2);        
 | |
|         d2 = get_deleter<boost::detail::array_deleter<T2[N]> >(s1);
 | |
|         d2->construct(p2, boost::detail::sp_forward<Args>(args)...);
 | |
|         return boost::shared_ptr<T>(s1, p1);
 | |
|     }
 | |
| #endif
 | |
| #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
 | |
|     template<typename T>
 | |
|     inline typename boost::detail::sp_if_array<T>::type
 | |
|     make_shared(std::initializer_list<typename boost::detail::array_inner<T>::type> list) {
 | |
|         typedef typename boost::detail::array_inner<T>::type T1;
 | |
|         typedef typename boost::detail::array_base<T1>::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<T1>::size;
 | |
|         boost::detail::make_array_helper<T2[]> a1(n1, &p2);
 | |
|         boost::detail::array_deleter<T2[]> d1(n1);
 | |
|         boost::shared_ptr<T> s1(p1, d1, a1);
 | |
|         boost::detail::array_deleter<T2[]>* d2;        
 | |
|         p3 = reinterpret_cast<T3*>(list.begin());
 | |
|         p1 = reinterpret_cast<T1*>(p2);
 | |
|         d2 = get_deleter<boost::detail::array_deleter<T2[]> >(s1);
 | |
|         d2->construct_list(p2, p3);
 | |
|         return boost::shared_ptr<T>(s1, p1);
 | |
|     }    
 | |
| #endif
 | |
| #if !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX)
 | |
|     template<typename T>
 | |
|     inline typename boost::detail::sp_if_size_array<T>::type
 | |
|     make_shared(const T& list) {
 | |
|         typedef typename boost::detail::array_inner<T>::type T1;
 | |
|         typedef typename boost::detail::array_base<T1>::type T2;
 | |
|         typedef const T2 T3;
 | |
|         enum { 
 | |
|             N = boost::detail::array_total<T>::size 
 | |
|         };
 | |
|         T1* p1 = 0;
 | |
|         T2* p2 = 0;
 | |
|         T3* p3 = 0;
 | |
|         boost::detail::make_array_helper<T2[N]> a1(&p2);
 | |
|         boost::detail::array_deleter<T2[N]> d1;
 | |
|         boost::shared_ptr<T> s1(p1, d1, a1);
 | |
|         boost::detail::array_deleter<T2[N]>* d2;        
 | |
|         p3 = reinterpret_cast<T3*>(list);
 | |
|         p1 = reinterpret_cast<T1*>(p2);
 | |
|         d2 = get_deleter<boost::detail::array_deleter<T2[N]> >(s1);
 | |
|         d2->construct_list(p2, p3);
 | |
|         return boost::shared_ptr<T>(s1, p1);
 | |
|     }
 | |
|     template<typename T>
 | |
|     inline typename boost::detail::sp_if_array<T>::type
 | |
|     make_shared(std::size_t size, 
 | |
|         const typename boost::detail::array_inner<T>::type& list) {
 | |
|         typedef typename boost::detail::array_inner<T>::type T1;
 | |
|         typedef typename boost::detail::array_base<T1>::type T2;
 | |
|         typedef const T2 T3;
 | |
|         enum { 
 | |
|             M = boost::detail::array_total<T1>::size 
 | |
|         };
 | |
|         T1* p1 = 0;
 | |
|         T2* p2 = 0;
 | |
|         T3* p3 = 0;
 | |
|         std::size_t n1 = M * size;
 | |
|         boost::detail::make_array_helper<T2[]> a1(n1, &p2);
 | |
|         boost::detail::array_deleter<T2[]> d1(n1);
 | |
|         boost::shared_ptr<T> s1(p1, d1, a1);
 | |
|         boost::detail::array_deleter<T2[]>* d2;        
 | |
|         p3 = reinterpret_cast<T3*>(list);
 | |
|         p1 = reinterpret_cast<T1*>(p2);
 | |
|         d2 = get_deleter<boost::detail::array_deleter<T2[]> >(s1);
 | |
|         d2->construct_list(p2, p3, M);
 | |
|         return boost::shared_ptr<T>(s1, p1);
 | |
|     }
 | |
|     template<typename T>
 | |
|     inline typename boost::detail::sp_if_size_array<T>::type
 | |
|     make_shared(const typename boost::detail::array_inner<T>::type& list) {
 | |
|         typedef typename boost::detail::array_inner<T>::type T1;
 | |
|         typedef typename boost::detail::array_base<T1>::type T2;
 | |
|         typedef const T2 T3;
 | |
|         enum { 
 | |
|             M = boost::detail::array_total<T1>::size,
 | |
|             N = boost::detail::array_total<T>::size 
 | |
|         };
 | |
|         T1* p1 = 0;
 | |
|         T2* p2 = 0;
 | |
|         T3* p3 = 0;
 | |
|         boost::detail::make_array_helper<T2[N]> a1(&p2);
 | |
|         boost::detail::array_deleter<T2[N]> d1;
 | |
|         boost::shared_ptr<T> s1(p1, d1, a1);
 | |
|         boost::detail::array_deleter<T2[N]>* d2;        
 | |
|         p3 = reinterpret_cast<T3*>(list);
 | |
|         p1 = reinterpret_cast<T1*>(p2);
 | |
|         d2 = get_deleter<boost::detail::array_deleter<T2[N]> >(s1);
 | |
|         d2->construct_list(p2, p3, M);
 | |
|         return boost::shared_ptr<T>(s1, p1);
 | |
|     }
 | |
| #endif
 | |
|     template<typename T>
 | |
|     inline typename boost::detail::sp_if_array<T>::type
 | |
|     make_shared_noinit(std::size_t size) {
 | |
|         typedef typename boost::detail::array_inner<T>::type T1;
 | |
|         typedef typename boost::detail::array_base<T1>::type T2;
 | |
|         T1* p1 = 0;
 | |
|         T2* p2 = 0;
 | |
|         std::size_t n1 = size * boost::detail::array_total<T1>::size;
 | |
|         boost::detail::make_array_helper<T2[]> a1(n1, &p2);
 | |
|         boost::detail::array_deleter<T2[]> d1(n1);
 | |
|         boost::shared_ptr<T> s1(p1, d1, a1);
 | |
|         boost::detail::array_deleter<T2[]>* d2;
 | |
|         p1 = reinterpret_cast<T1*>(p2);        
 | |
|         d2 = get_deleter<boost::detail::array_deleter<T2[]> >(s1);
 | |
|         d2->construct_noinit(p2);
 | |
|         return boost::shared_ptr<T>(s1, p1);
 | |
|     }
 | |
|     template<typename T>
 | |
|     inline typename boost::detail::sp_if_size_array<T>::type
 | |
|     make_shared_noinit() {
 | |
|         typedef typename boost::detail::array_inner<T>::type T1;
 | |
|         typedef typename boost::detail::array_base<T1>::type T2;
 | |
|         enum { 
 | |
|             N = boost::detail::array_total<T>::size 
 | |
|         };
 | |
|         T1* p1 = 0;
 | |
|         T2* p2 = 0;
 | |
|         boost::detail::make_array_helper<T2[N]> a1(&p2);
 | |
|         boost::detail::array_deleter<T2[N]> d1;
 | |
|         boost::shared_ptr<T> s1(p1, d1, a1);
 | |
|         boost::detail::array_deleter<T2[N]>* d2;
 | |
|         p1 = reinterpret_cast<T1*>(p2);     
 | |
|         d2 = get_deleter<boost::detail::array_deleter<T2[N]> >(s1);
 | |
|         d2->construct_noinit(p2);
 | |
|         return boost::shared_ptr<T>(s1, p1);
 | |
|     }
 | |
| }
 | |
| 
 | |
| #endif
 |