2012-11-06 14:17:32 +00:00
|
|
|
/*
|
2014-01-23 20:40:46 -08:00
|
|
|
* Copyright (c) 2012-2014 Glen Joseph Fernandes
|
2012-11-06 14:17:32 +00:00
|
|
|
* glenfe at live dot com
|
|
|
|
*
|
2012-12-11 20:51:05 +00:00
|
|
|
* Distributed under the Boost Software License,
|
|
|
|
* Version 1.0. (See accompanying file LICENSE_1_0.txt
|
2012-11-06 14:17:32 +00:00
|
|
|
* 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-18 02:51:06 +00:00
|
|
|
#include <boost/smart_ptr/detail/sp_if_array.hpp>
|
2012-11-06 14:17:32 +00:00
|
|
|
|
|
|
|
namespace boost {
|
|
|
|
template<typename T, typename A>
|
2012-12-11 20:51:05 +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);
|
2012-12-02 22:05:31 +00:00
|
|
|
boost::detail::array_deleter<T2[]> d1(n1);
|
2012-11-18 02:51:06 +00:00
|
|
|
boost::shared_ptr<T> s1(p1, d1, a1);
|
2012-12-11 20:51:05 +00:00
|
|
|
typedef boost::detail::array_deleter<T2[]>* D2;
|
2012-11-07 18:37:17 +00:00
|
|
|
p1 = reinterpret_cast<T1*>(p2);
|
2012-12-11 20:51:05 +00:00
|
|
|
D2 d2 = static_cast<D2>(s1._internal_get_untyped_deleter());
|
2012-12-13 04:04:23 +00:00
|
|
|
d2->init(p2);
|
2012-11-18 02:51:06 +00:00
|
|
|
return boost::shared_ptr<T>(s1, p1);
|
2012-11-06 14:17:32 +00:00
|
|
|
}
|
2014-01-23 20:40:46 -08:00
|
|
|
|
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
|
2014-01-23 20:40:46 -08:00
|
|
|
allocate_shared(const A& allocator) {
|
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-11 20:51:05 +00:00
|
|
|
enum {
|
|
|
|
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;
|
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-11 20:51:05 +00:00
|
|
|
typedef boost::detail::array_deleter<T2[N]>* D2;
|
2012-11-09 17:30:07 +00:00
|
|
|
p1 = reinterpret_cast<T1*>(p2);
|
2012-12-11 20:51:05 +00:00
|
|
|
D2 d2 = static_cast<D2>(s1._internal_get_untyped_deleter());
|
2014-01-23 20:40:46 -08:00
|
|
|
d2->init(p2);
|
2012-11-18 02:51:06 +00:00
|
|
|
return boost::shared_ptr<T>(s1, p1);
|
2012-11-10 01:33:29 +00:00
|
|
|
}
|
2014-01-23 20:40:46 -08:00
|
|
|
|
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
|
2012-12-11 20:51:05 +00:00
|
|
|
allocate_shared(const A& allocator, std::size_t size,
|
2012-12-01 22:43:57 +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-10 01:33:29 +00:00
|
|
|
typedef const T2 T3;
|
2012-12-11 20:51:05 +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);
|
2012-12-02 22:05:31 +00:00
|
|
|
boost::detail::array_deleter<T2[]> d1(n1);
|
2012-11-18 02:51:06 +00:00
|
|
|
boost::shared_ptr<T> s1(p1, d1, a1);
|
2012-12-11 20:51:05 +00:00
|
|
|
typedef boost::detail::array_deleter<T2[]>* D2;
|
2014-01-23 20:40:46 -08:00
|
|
|
p3 = reinterpret_cast<T3*>(&list);
|
2012-11-10 01:33:29 +00:00
|
|
|
p1 = reinterpret_cast<T1*>(p2);
|
2012-12-11 20:51:05 +00:00
|
|
|
D2 d2 = static_cast<D2>(s1._internal_get_untyped_deleter());
|
2014-01-23 20:40:46 -08:00
|
|
|
d2->template init<M>(p2, p3);
|
2012-11-18 02:51:06 +00:00
|
|
|
return boost::shared_ptr<T>(s1, p1);
|
2012-11-09 17:30:07 +00:00
|
|
|
}
|
2014-01-23 20:40:46 -08: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-12-11 20:51:05 +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-11 20:51:05 +00:00
|
|
|
enum {
|
|
|
|
M = boost::detail::array_total<T1>::size,
|
|
|
|
N = boost::detail::array_total<T>::size
|
2012-12-01 04:36:41 +00:00
|
|
|
};
|
2012-11-14 15:18:50 +00:00
|
|
|
T1* p1 = 0;
|
|
|
|
T2* p2 = 0;
|
2012-12-11 20:51:05 +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-12-11 20:51:05 +00:00
|
|
|
typedef boost::detail::array_deleter<T2[N]>* D2;
|
2014-01-23 20:40:46 -08:00
|
|
|
p3 = reinterpret_cast<T3*>(&list);
|
2012-12-04 06:06:23 +00:00
|
|
|
p1 = reinterpret_cast<T1*>(p2);
|
2012-12-11 20:51:05 +00:00
|
|
|
D2 d2 = static_cast<D2>(s1._internal_get_untyped_deleter());
|
2014-01-23 20:40:46 -08:00
|
|
|
d2->template init<M>(p2, p3);
|
2012-12-04 06:06:23 +00:00
|
|
|
return boost::shared_ptr<T>(s1, p1);
|
|
|
|
}
|
2014-01-23 20:40:46 -08:00
|
|
|
|
2012-12-11 17:42:47 +00:00
|
|
|
template<typename T, typename A>
|
|
|
|
inline typename boost::detail::sp_if_array<T>::type
|
|
|
|
allocate_shared_noinit(const A& allocator, 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::allocate_array_helper<A, T2[]> a1(allocator, n1, &p2);
|
|
|
|
boost::detail::array_deleter<T2[]> d1(n1);
|
|
|
|
boost::shared_ptr<T> s1(p1, d1, a1);
|
2012-12-11 20:51:05 +00:00
|
|
|
typedef boost::detail::array_deleter<T2[]>* D2;
|
|
|
|
p1 = reinterpret_cast<T1*>(p2);
|
|
|
|
D2 d2 = static_cast<D2>(s1._internal_get_untyped_deleter());
|
2012-12-13 04:04:23 +00:00
|
|
|
d2->noinit(p2);
|
2012-12-11 17:42:47 +00:00
|
|
|
return boost::shared_ptr<T>(s1, p1);
|
|
|
|
}
|
2014-01-23 20:40:46 -08:00
|
|
|
|
2012-12-11 17:42:47 +00:00
|
|
|
template<typename T, typename A>
|
|
|
|
inline typename boost::detail::sp_if_size_array<T>::type
|
|
|
|
allocate_shared_noinit(const A& allocator) {
|
|
|
|
typedef typename boost::detail::array_inner<T>::type T1;
|
|
|
|
typedef typename boost::detail::array_base<T1>::type T2;
|
2012-12-11 20:51:05 +00:00
|
|
|
enum {
|
|
|
|
N = boost::detail::array_total<T>::size
|
2012-12-11 17:42:47 +00:00
|
|
|
};
|
|
|
|
T1* p1 = 0;
|
|
|
|
T2* p2 = 0;
|
|
|
|
boost::detail::allocate_array_helper<A, T2[N]> a1(allocator, &p2);
|
|
|
|
boost::detail::array_deleter<T2[N]> d1;
|
|
|
|
boost::shared_ptr<T> s1(p1, d1, a1);
|
2012-12-11 20:51:05 +00:00
|
|
|
typedef boost::detail::array_deleter<T2[N]>* D2;
|
|
|
|
p1 = reinterpret_cast<T1*>(p2);
|
|
|
|
D2 d2 = static_cast<D2>(s1._internal_get_untyped_deleter());
|
2012-12-13 04:04:23 +00:00
|
|
|
d2->noinit(p2);
|
2012-12-11 17:42:47 +00:00
|
|
|
return boost::shared_ptr<T>(s1, p1);
|
|
|
|
}
|
2012-11-06 14:17:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|