forked from boostorg/smart_ptr
Add C++11 initializer list support for make_shared and allocate_shared array forms.
[SVN r81257]
This commit is contained in:
@@ -52,6 +52,28 @@ namespace boost {
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
#endif
|
||||
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
|
||||
template<typename T, typename A>
|
||||
inline typename detail::sp_if_array<T>::type
|
||||
allocate_shared(const A& allocator, size_t size, typename detail::array_list<T>::type list) {
|
||||
typedef typename shared_ptr<T>::element_type T1;
|
||||
typedef typename detail::array_type<T1>::type T2;
|
||||
typedef const T1* L1;
|
||||
typedef const T2* L2;
|
||||
T1* p1 = 0;
|
||||
T2* p2 = 0;
|
||||
L1 l1 = list.begin();
|
||||
size_t n1 = size * detail::array_size<T1>::size;
|
||||
detail::allocate_array_helper<A, T2> a1(allocator, n1, &p2);
|
||||
detail::array_deleter<T2> d1;
|
||||
shared_ptr<T> s1(p1, d1, a1);
|
||||
detail::array_deleter<T2>* d2;
|
||||
p1 = reinterpret_cast<T1*>(p2);
|
||||
d2 = get_deleter<detail::array_deleter<T2> >(s1);
|
||||
d2->construct(p2, n1, L2(l1));
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@@ -37,6 +37,14 @@ namespace boost {
|
||||
::new(p1) T(args...);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
|
||||
void construct(T* memory, std::size_t count, const T* list) {
|
||||
for (object = memory; size < count; size++) {
|
||||
void* p1 = object + size;
|
||||
::new(p1) T(list[size]);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
void construct_noinit(T* memory, std::size_t count) {
|
||||
for (object = memory; size < count; size++) {
|
||||
|
@@ -10,6 +10,9 @@
|
||||
#define BOOST_SMART_PTR_DETAIL_ARRAY_TRAITS_HPP
|
||||
|
||||
#include <boost/type_traits/remove_cv.hpp>
|
||||
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
|
||||
#include <initializer_list>
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
namespace detail {
|
||||
@@ -17,22 +20,37 @@ namespace boost {
|
||||
struct array_type {
|
||||
typedef typename boost::remove_cv<T>::type type;
|
||||
};
|
||||
#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
|
||||
template<typename T, size_t N>
|
||||
struct array_type<T[N]> {
|
||||
typedef typename array_type<T>::type type;
|
||||
};
|
||||
#endif
|
||||
template<typename T>
|
||||
struct array_size {
|
||||
enum {
|
||||
size = 1
|
||||
};
|
||||
};
|
||||
#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
|
||||
template<typename T, size_t N>
|
||||
struct array_size<T[N]> {
|
||||
enum {
|
||||
size = N * array_size<T>::size
|
||||
};
|
||||
};
|
||||
#endif
|
||||
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
|
||||
template<typename T>
|
||||
struct array_list {
|
||||
};
|
||||
#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
|
||||
template<typename T>
|
||||
struct array_list<T[]> {
|
||||
typedef std::initializer_list<T> type;
|
||||
};
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -51,6 +51,28 @@ namespace boost {
|
||||
d2->construct(p2, n1, std::forward<Args>(args)...);
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
#endif
|
||||
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
|
||||
template<typename T>
|
||||
inline typename detail::sp_if_array<T>::type
|
||||
make_shared(std::size_t size, typename detail::array_list<T>::type list) {
|
||||
typedef typename shared_ptr<T>::element_type T1;
|
||||
typedef typename detail::array_type<T1>::type T2;
|
||||
typedef const T1* L1;
|
||||
typedef const T2* L2;
|
||||
T1* p1 = 0;
|
||||
T2* p2 = 0;
|
||||
L1 l1 = list.begin();
|
||||
size_t n1 = size * detail::array_size<T1>::size;
|
||||
detail::make_array_helper<T2> a1(n1, &p2);
|
||||
detail::array_deleter<T2> d1;
|
||||
shared_ptr<T> s1(p1, d1, a1);
|
||||
detail::array_deleter<T2>* d2;
|
||||
p1 = reinterpret_cast<T1*>(p2);
|
||||
d2 = get_deleter<detail::array_deleter<T2> >(s1);
|
||||
d2->construct(p2, n1, L2(l1));
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
#endif
|
||||
template<typename T>
|
||||
inline typename detail::sp_if_array<T>::type
|
||||
|
@@ -73,6 +73,22 @@ int main() {
|
||||
BOOST_TEST(a1[0][1][0][0].f == 0);
|
||||
BOOST_TEST(a1[1][0][0][0].i == 0);
|
||||
}
|
||||
#endif
|
||||
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
|
||||
{
|
||||
boost::shared_ptr<int[]> a1 = boost::allocate_shared<int[]>(std::allocator<int>(), 4, { 0, 1, 2, 3 });
|
||||
BOOST_TEST(a1[0] == 0);
|
||||
BOOST_TEST(a1[1] == 1);
|
||||
BOOST_TEST(a1[2] == 2);
|
||||
BOOST_TEST(a1[3] == 3);
|
||||
}
|
||||
{
|
||||
boost::shared_ptr<int[][2]> a1 = boost::allocate_shared<int[][2]>(std::allocator<int>(), 2, { {0, 1}, {2, 3} });
|
||||
BOOST_TEST(a1[0][0] == 0);
|
||||
BOOST_TEST(a1[0][1] == 1);
|
||||
BOOST_TEST(a1[1][0] == 2);
|
||||
BOOST_TEST(a1[1][1] == 3);
|
||||
}
|
||||
#endif
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
@@ -73,6 +73,22 @@ int main() {
|
||||
BOOST_TEST(a1[0][1][0][0].f == 0);
|
||||
BOOST_TEST(a1[1][0][0][0].i == 0);
|
||||
}
|
||||
#endif
|
||||
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
|
||||
{
|
||||
boost::shared_ptr<int[]> a1 = boost::make_shared<int[]>(4, { 0, 1, 2, 3 });
|
||||
BOOST_TEST(a1[0] == 0);
|
||||
BOOST_TEST(a1[1] == 1);
|
||||
BOOST_TEST(a1[2] == 2);
|
||||
BOOST_TEST(a1[3] == 3);
|
||||
}
|
||||
{
|
||||
boost::shared_ptr<int[][2]> a1 = boost::make_shared<int[][2]>(2, { {0, 1}, {2, 3} });
|
||||
BOOST_TEST(a1[0][0] == 0);
|
||||
BOOST_TEST(a1[0][1] == 1);
|
||||
BOOST_TEST(a1[1][0] == 2);
|
||||
BOOST_TEST(a1[1][1] == 3);
|
||||
}
|
||||
#endif
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
Reference in New Issue
Block a user