2012-11-06 14:17:32 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
|
2012-11-07 14:42:10 +00:00
|
|
|
#include <boost/smart_ptr/shared_ptr.hpp>
|
2012-11-06 14:17:32 +00:00
|
|
|
#include <boost/smart_ptr/detail/allocate_array_helper.hpp>
|
|
|
|
#include <boost/smart_ptr/detail/array_deleter.hpp>
|
2012-11-07 18:37:17 +00:00
|
|
|
#include <boost/smart_ptr/detail/array_traits.hpp>
|
2012-11-17 16:21:41 +00:00
|
|
|
#include <boost/smart_ptr/detail/sp_forward.hpp>
|
2012-11-18 02:51:06 +00:00
|
|
|
#include <boost/smart_ptr/detail/sp_if_array.hpp>
|
2012-11-10 02:17:02 +00:00
|
|
|
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
|
|
|
|
#include <initializer_list>
|
|
|
|
#endif
|
2012-11-06 14:17:32 +00:00
|
|
|
|
|
|
|
namespace boost {
|
|
|
|
template<typename T, typename A>
|
2012-11-18 02:51:06 +00:00
|
|
|
inline typename boost::detail::sp_if_array<T>::type
|
2012-11-10 01:33:29 +00:00
|
|
|
allocate_shared(const A& allocator, std::size_t size) {
|
2012-11-18 02:51:06 +00:00
|
|
|
typedef typename boost::detail::array_inner<T>::type T1;
|
|
|
|
typedef typename boost::detail::array_base<T1>::type T2;
|
2012-11-06 14:17:32 +00:00
|
|
|
T1* p1 = 0;
|
2012-11-07 18:37:17 +00:00
|
|
|
T2* p2 = 0;
|
2012-11-18 02:51:06 +00:00
|
|
|
std::size_t n1 = size * boost::detail::array_total<T1>::size;
|
2012-11-28 07:32:30 +00:00
|
|
|
boost::detail::allocate_array_helper<A, T2[]> a1(allocator, n1, &p2);
|
|
|
|
boost::detail::array_deleter<T2[]> d1;
|
2012-11-18 02:51:06 +00:00
|
|
|
boost::shared_ptr<T> s1(p1, d1, a1);
|
2012-11-28 07:32:30 +00:00
|
|
|
boost::detail::array_deleter<T2[]>* d2;
|
2012-11-07 18:37:17 +00:00
|
|
|
p1 = reinterpret_cast<T1*>(p2);
|
2012-11-28 07:32:30 +00:00
|
|
|
d2 = get_deleter<boost::detail::array_deleter<T2[]> >(s1);
|
2012-11-07 18:37:17 +00:00
|
|
|
d2->construct(p2, n1);
|
2012-11-18 02:51:06 +00:00
|
|
|
return boost::shared_ptr<T>(s1, p1);
|
2012-11-06 14:17:32 +00:00
|
|
|
}
|
|
|
|
#if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS)
|
|
|
|
template<typename T, typename A, typename... Args>
|
2012-11-18 02:51:06 +00:00
|
|
|
inline typename boost::detail::sp_if_array<T>::type
|
2012-11-10 01:33:29 +00:00
|
|
|
allocate_shared(const A& allocator, std::size_t size, Args&&... args) {
|
2012-11-18 02:51:06 +00:00
|
|
|
typedef typename boost::detail::array_inner<T>::type T1;
|
|
|
|
typedef typename boost::detail::array_base<T1>::type T2;
|
2012-11-06 14:17:32 +00:00
|
|
|
T1* p1 = 0;
|
2012-11-07 18:37:17 +00:00
|
|
|
T2* p2 = 0;
|
2012-11-18 02:51:06 +00:00
|
|
|
std::size_t n1 = size * boost::detail::array_total<T1>::size;
|
2012-11-28 07:32:30 +00:00
|
|
|
boost::detail::allocate_array_helper<A, T2[]> a1(allocator, n1, &p2);
|
|
|
|
boost::detail::array_deleter<T2[]> d1;
|
2012-11-18 02:51:06 +00:00
|
|
|
boost::shared_ptr<T> s1(p1, d1, a1);
|
2012-11-28 07:32:30 +00:00
|
|
|
boost::detail::array_deleter<T2[]>* d2;
|
2012-11-07 18:37:17 +00:00
|
|
|
p1 = reinterpret_cast<T1*>(p2);
|
2012-11-28 07:32:30 +00:00
|
|
|
d2 = get_deleter<boost::detail::array_deleter<T2[]> >(s1);
|
2012-11-18 02:51:06 +00:00
|
|
|
d2->construct(p2, n1, boost::detail::sp_forward<Args>(args)...);
|
|
|
|
return boost::shared_ptr<T>(s1, p1);
|
2012-11-09 17:12:56 +00:00
|
|
|
}
|
|
|
|
template<typename T, typename A, typename... Args>
|
2012-11-18 02:51:06 +00:00
|
|
|
inline typename boost::detail::sp_if_size_array<T>::type
|
2012-11-09 17:12:56 +00:00
|
|
|
allocate_shared(const A& allocator, Args&&... args) {
|
2012-11-18 02:51:06 +00:00
|
|
|
typedef typename boost::detail::array_inner<T>::type T1;
|
|
|
|
typedef typename boost::detail::array_base<T1>::type T2;
|
2012-12-01 04:36:41 +00:00
|
|
|
enum {
|
|
|
|
N = boost::detail::array_total<T>::size
|
|
|
|
};
|
2012-11-09 17:12:56 +00:00
|
|
|
T1* p1 = 0;
|
|
|
|
T2* p2 = 0;
|
2012-11-28 06:07:45 +00:00
|
|
|
boost::detail::allocate_array_helper<A, T2[N]> a1(allocator, &p2);
|
|
|
|
boost::detail::array_deleter<T2[N]> d1;
|
2012-11-18 02:51:06 +00:00
|
|
|
boost::shared_ptr<T> s1(p1, d1, a1);
|
2012-11-28 06:07:45 +00:00
|
|
|
boost::detail::array_deleter<T2[N]>* d2;
|
2012-11-09 17:12:56 +00:00
|
|
|
p1 = reinterpret_cast<T1*>(p2);
|
2012-11-28 06:07:45 +00:00
|
|
|
d2 = get_deleter<boost::detail::array_deleter<T2[N]> >(s1);
|
|
|
|
d2->construct(p2, boost::detail::sp_forward<Args>(args)...);
|
2012-11-18 02:51:06 +00:00
|
|
|
return boost::shared_ptr<T>(s1, p1);
|
2012-11-06 14:17:32 +00:00
|
|
|
}
|
|
|
|
#endif
|
2012-11-09 06:17:05 +00:00
|
|
|
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
|
|
|
|
template<typename T, typename A>
|
2012-11-18 02:51:06 +00:00
|
|
|
inline typename boost::detail::sp_if_array<T>::type
|
2012-11-10 02:33:48 +00:00
|
|
|
allocate_shared(const A& allocator,
|
2012-11-18 02:51:06 +00:00
|
|
|
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;
|
2012-11-09 09:14:23 +00:00
|
|
|
typedef const T2 T3;
|
2012-11-09 06:17:05 +00:00
|
|
|
T1* p1 = 0;
|
|
|
|
T2* p2 = 0;
|
2012-11-09 09:14:23 +00:00
|
|
|
T3* p3 = 0;
|
2012-11-18 02:51:06 +00:00
|
|
|
std::size_t n1 = list.size() * boost::detail::array_total<T1>::size;
|
2012-11-28 07:32:30 +00:00
|
|
|
boost::detail::allocate_array_helper<A, T2[]> a1(allocator, n1, &p2);
|
|
|
|
boost::detail::array_deleter<T2[]> d1;
|
2012-11-18 02:51:06 +00:00
|
|
|
boost::shared_ptr<T> s1(p1, d1, a1);
|
2012-11-28 07:32:30 +00:00
|
|
|
boost::detail::array_deleter<T2[]>* d2;
|
2012-11-09 09:14:23 +00:00
|
|
|
p3 = reinterpret_cast<T3*>(list.begin());
|
2012-11-09 06:17:05 +00:00
|
|
|
p1 = reinterpret_cast<T1*>(p2);
|
2012-11-28 07:32:30 +00:00
|
|
|
d2 = get_deleter<boost::detail::array_deleter<T2[]> >(s1);
|
2012-11-10 01:33:29 +00:00
|
|
|
d2->construct_list(p2, n1, p3);
|
2012-11-18 02:51:06 +00:00
|
|
|
return boost::shared_ptr<T>(s1, p1);
|
2012-11-09 06:17:05 +00:00
|
|
|
}
|
2012-12-01 22:43:57 +00:00
|
|
|
#endif
|
|
|
|
#if !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX)
|
2012-11-09 17:30:07 +00:00
|
|
|
template<typename T, typename A>
|
2012-12-01 22:43:57 +00:00
|
|
|
inline typename boost::detail::sp_if_size_array<T>::type
|
|
|
|
allocate_shared(const A& allocator, const T& list) {
|
2012-11-18 02:51:06 +00:00
|
|
|
typedef typename boost::detail::array_inner<T>::type T1;
|
|
|
|
typedef typename boost::detail::array_base<T1>::type T2;
|
2012-11-09 17:30:07 +00:00
|
|
|
typedef const T2 T3;
|
2012-12-01 04:36:41 +00:00
|
|
|
enum {
|
2012-12-01 22:43:57 +00:00
|
|
|
N = boost::detail::array_total<T>::size
|
2012-12-01 04:36:41 +00:00
|
|
|
};
|
2012-11-09 17:30:07 +00:00
|
|
|
T1* p1 = 0;
|
|
|
|
T2* p2 = 0;
|
|
|
|
T3* p3 = 0;
|
2012-12-01 22:43:57 +00:00
|
|
|
boost::detail::allocate_array_helper<A, T2[N]> a1(allocator, &p2);
|
|
|
|
boost::detail::array_deleter<T2[N]> d1;
|
2012-11-18 02:51:06 +00:00
|
|
|
boost::shared_ptr<T> s1(p1, d1, a1);
|
2012-12-01 22:43:57 +00:00
|
|
|
boost::detail::array_deleter<T2[N]>* d2;
|
|
|
|
p3 = reinterpret_cast<T3*>(list);
|
2012-11-09 17:30:07 +00:00
|
|
|
p1 = reinterpret_cast<T1*>(p2);
|
2012-12-01 22:43:57 +00:00
|
|
|
d2 = get_deleter<boost::detail::array_deleter<T2[N]> >(s1);
|
|
|
|
d2->construct_list(p2, p3);
|
2012-11-18 02:51:06 +00:00
|
|
|
return boost::shared_ptr<T>(s1, p1);
|
2012-11-10 01:33:29 +00:00
|
|
|
}
|
|
|
|
template<typename T, typename A>
|
2012-12-01 22:43:57 +00:00
|
|
|
inline typename boost::detail::sp_if_array<T>::type
|
|
|
|
allocate_shared(const A& allocator, std::size_t size,
|
|
|
|
const typename boost::detail::array_inner<T>::type& list) {
|
2012-11-18 02:51:06 +00:00
|
|
|
typedef typename boost::detail::array_inner<T>::type T1;
|
|
|
|
typedef typename boost::detail::array_base<T1>::type T2;
|
2012-11-10 01:33:29 +00:00
|
|
|
typedef const T2 T3;
|
2012-12-01 04:36:41 +00:00
|
|
|
enum {
|
2012-12-01 22:43:57 +00:00
|
|
|
M = boost::detail::array_total<T1>::size
|
2012-12-01 04:36:41 +00:00
|
|
|
};
|
2012-11-10 01:33:29 +00:00
|
|
|
T1* p1 = 0;
|
|
|
|
T2* p2 = 0;
|
|
|
|
T3* p3 = 0;
|
2012-12-01 22:43:57 +00:00
|
|
|
std::size_t n1 = M * size;
|
|
|
|
boost::detail::allocate_array_helper<A, T2[]> a1(allocator, n1, &p2);
|
|
|
|
boost::detail::array_deleter<T2[]> d1;
|
2012-11-18 02:51:06 +00:00
|
|
|
boost::shared_ptr<T> s1(p1, d1, a1);
|
2012-12-01 22:43:57 +00:00
|
|
|
boost::detail::array_deleter<T2[]>* d2;
|
2012-12-01 05:23:37 +00:00
|
|
|
p3 = reinterpret_cast<T3*>(list);
|
2012-11-10 01:33:29 +00:00
|
|
|
p1 = reinterpret_cast<T1*>(p2);
|
2012-12-01 22:43:57 +00:00
|
|
|
d2 = get_deleter<boost::detail::array_deleter<T2[]> >(s1);
|
|
|
|
d2->construct_list(p2, n1, p3, M);
|
2012-11-18 02:51:06 +00:00
|
|
|
return boost::shared_ptr<T>(s1, p1);
|
2012-11-09 17:30:07 +00:00
|
|
|
}
|
2012-11-14 15:18:50 +00:00
|
|
|
template<typename T, typename A>
|
2012-11-18 02:51:06 +00:00
|
|
|
inline typename boost::detail::sp_if_size_array<T>::type
|
2012-11-14 15:18:50 +00:00
|
|
|
allocate_shared(const A& allocator,
|
2012-12-01 04:36:41 +00:00
|
|
|
const typename boost::detail::array_inner<T>::type& list) {
|
2012-11-18 02:51:06 +00:00
|
|
|
typedef typename boost::detail::array_inner<T>::type T1;
|
|
|
|
typedef typename boost::detail::array_base<T1>::type T2;
|
2012-11-14 15:18:50 +00:00
|
|
|
typedef const T2 T3;
|
2012-12-01 04:36:41 +00:00
|
|
|
enum {
|
|
|
|
M = boost::detail::array_total<T1>::size,
|
|
|
|
N = boost::detail::array_total<T>::size
|
|
|
|
};
|
2012-11-14 15:18:50 +00:00
|
|
|
T1* p1 = 0;
|
|
|
|
T2* p2 = 0;
|
2012-12-01 04:36:41 +00:00
|
|
|
T3* p3 = 0;
|
2012-11-28 06:07:45 +00:00
|
|
|
boost::detail::allocate_array_helper<A, T2[N]> a1(allocator, &p2);
|
|
|
|
boost::detail::array_deleter<T2[N]> d1;
|
2012-11-18 02:51:06 +00:00
|
|
|
boost::shared_ptr<T> s1(p1, d1, a1);
|
2012-11-28 06:07:45 +00:00
|
|
|
boost::detail::array_deleter<T2[N]>* d2;
|
2012-12-01 04:36:41 +00:00
|
|
|
p3 = reinterpret_cast<T3*>(list);
|
2012-11-14 15:18:50 +00:00
|
|
|
p1 = reinterpret_cast<T1*>(p2);
|
2012-11-28 06:07:45 +00:00
|
|
|
d2 = get_deleter<boost::detail::array_deleter<T2[N]> >(s1);
|
|
|
|
d2->construct_list(p2, p3, M);
|
2012-11-18 02:51:06 +00:00
|
|
|
return boost::shared_ptr<T>(s1, p1);
|
2012-11-14 15:18:50 +00:00
|
|
|
}
|
2012-11-09 06:17:05 +00:00
|
|
|
#endif
|
2012-11-06 14:17:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|