forked from boostorg/smart_ptr
Add additional overload for allocate_shared and make_shared array forms that take initializer list of T for the array types T[M][N]
[SVN r81341]
This commit is contained in:
@ -98,10 +98,10 @@ namespace boost {
|
||||
inline typename detail::sp_if_size_array<T>::type
|
||||
allocate_shared(const A& allocator,
|
||||
std::initializer_list<typename detail::array_inner<T>::type> list) {
|
||||
BOOST_ASSERT(list.size() == detail::array_size<T>::size);
|
||||
typedef typename detail::array_inner<T>::type T1;
|
||||
typedef typename detail::array_base<T1>::type T2;
|
||||
typedef const T2 T3;
|
||||
BOOST_ASSERT(list.size() == detail::array_size<T>::size);
|
||||
T1* p1 = 0;
|
||||
T2* p2 = 0;
|
||||
T3* p3 = 0;
|
||||
@ -138,6 +138,29 @@ namespace boost {
|
||||
d2->construct_list(p2, n1, p3, n0);
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
template<typename T, typename A>
|
||||
inline typename detail::sp_if_size_array<T>::type
|
||||
allocate_shared(const A& allocator,
|
||||
std::initializer_list<typename detail::arrays_inner<T>::type> list) {
|
||||
typedef typename detail::array_inner<T>::type T1;
|
||||
typedef typename detail::array_base<T1>::type T2;
|
||||
typedef const T2 T3;
|
||||
BOOST_ASSERT(list.size() == detail::array_size<T1>::size);
|
||||
T1* p1 = 0;
|
||||
T2* p2 = 0;
|
||||
T3* p3 = 0;
|
||||
std::size_t n0 = detail::array_total<T1>::size;
|
||||
std::size_t n1 = detail::array_total<T>::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;
|
||||
p3 = reinterpret_cast<T3*>(list.begin());
|
||||
p1 = reinterpret_cast<T1*>(p2);
|
||||
d2 = get_deleter<detail::array_deleter<T2> >(s1);
|
||||
d2->construct_list(p2, n1, p3, n0);
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -24,7 +24,7 @@ namespace boost {
|
||||
template<typename T>
|
||||
struct array_size {
|
||||
};
|
||||
template<typename T, size_t N>
|
||||
template<typename T, std::size_t N>
|
||||
struct array_size<T[N]> {
|
||||
enum {
|
||||
size = N
|
||||
@ -49,17 +49,21 @@ namespace boost {
|
||||
struct array_inner<T[]> {
|
||||
typedef T type;
|
||||
};
|
||||
template<typename T, size_t N>
|
||||
template<typename T, std::size_t N>
|
||||
struct array_inner<T[N]> {
|
||||
typedef T type;
|
||||
};
|
||||
template<typename T>
|
||||
struct arrays_inner {
|
||||
};
|
||||
template<typename T, size_t N>
|
||||
template<typename T, std::size_t N>
|
||||
struct arrays_inner<T[][N]> {
|
||||
typedef T type;
|
||||
};
|
||||
template<typename T, std::size_t M, std::size_t N>
|
||||
struct arrays_inner<T[M][N]> {
|
||||
typedef T type;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -96,10 +96,10 @@ namespace boost {
|
||||
template<typename T>
|
||||
inline typename detail::sp_if_size_array<T>::type
|
||||
make_shared(std::initializer_list<typename detail::array_inner<T>::type> list) {
|
||||
BOOST_ASSERT(list.size() == detail::array_size<T>::size);
|
||||
typedef typename detail::array_inner<T>::type T1;
|
||||
typedef typename detail::array_base<T1>::type T2;
|
||||
typedef const T2 T3;
|
||||
BOOST_ASSERT(list.size() == detail::array_size<T>::size);
|
||||
T1* p1 = 0;
|
||||
T2* p2 = 0;
|
||||
T3* p3 = 0;
|
||||
@ -136,6 +136,28 @@ namespace boost {
|
||||
d2->construct_list(p2, n1, p3, n0);
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
template<typename T>
|
||||
inline typename detail::sp_if_size_array<T>::type
|
||||
make_shared(std::initializer_list<typename detail::arrays_inner<T>::type> list) {
|
||||
typedef typename detail::array_inner<T>::type T1;
|
||||
typedef typename detail::array_base<T1>::type T2;
|
||||
typedef const T2 T3;
|
||||
BOOST_ASSERT(list.size() == detail::array_size<T1>::size);
|
||||
T1* p1 = 0;
|
||||
T2* p2 = 0;
|
||||
T3* p3 = 0;
|
||||
std::size_t n0 = detail::array_total<T1>::size;
|
||||
std::size_t n1 = detail::array_total<T>::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;
|
||||
p3 = reinterpret_cast<T3*>(list.begin());
|
||||
p1 = reinterpret_cast<T1*>(p2);
|
||||
d2 = get_deleter<detail::array_deleter<T2> >(s1);
|
||||
d2->construct_list(p2, n1, p3, n0);
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
#endif
|
||||
template<typename T>
|
||||
inline typename detail::sp_if_array<T>::type
|
||||
|
@ -56,7 +56,10 @@
|
||||
|
||||
template<typename T, typename... Args>
|
||||
shared_ptr<T[][N]> <a href="#functions">make_shared</a>(size_t size, initializer_list<T> list);
|
||||
|
||||
|
||||
template<typename T, typename... Args>
|
||||
shared_ptr<T[M][N]> <a href="#functions">make_shared</a>(initializer_list<T> list);
|
||||
|
||||
template<typename T, typename A, typename... Args>
|
||||
shared_ptr<T[]> <a href="#functions">allocate_shared</a>(const A& allocator, initializer_list<T> list);
|
||||
|
||||
@ -65,6 +68,9 @@
|
||||
|
||||
template<typename T, typename A, typename... Args>
|
||||
shared_ptr<T[][N]> <a href="#functions">allocate_shared</a>(const A& allocator, size_t size, initializer_list<T> list);
|
||||
|
||||
template<typename T, typename A, typename... Args>
|
||||
shared_ptr<T[M][N]> <a href="#functions">allocate_shared</a>(const A& allocator, initializer_list<T> list);
|
||||
#endif
|
||||
|
||||
template<typename T>
|
||||
@ -117,7 +123,15 @@ template<typename T, typename A, typename... Args>
|
||||
constructor of <code>T</code> for each array element.</p>
|
||||
</blockquote>
|
||||
<h2><a name="example">Example</a></h2>
|
||||
<pre>boost::shared_ptr<int[]> array = boost::make_shared<int[]>(size);</pre>
|
||||
<pre>boost::shared_ptr<int[]> a1 = boost::make_shared<int[]>(size);
|
||||
boost::shared_ptr<point[5]> a2 = boost::make_shared<point[5]>(x, y);
|
||||
boost::shared_ptr<int[5]> a3 = boost::make_shared<int[5]>();
|
||||
boost::shared_ptr<int[]> a4 = boost::make_shared<int[]>({1, 2, 3});
|
||||
boost::shared_ptr<int[3]> a5 = boost::make_shared<int[3]>({1, 2, 3});
|
||||
boost::shared_ptr<int[][3]> a6 = boost::make_shared<int[][3]>(size, {1, 2, 3});
|
||||
boost::shared_ptr<int[5][3]> a7 = boost::make_shared<int[5][3]>({1, 2, 3});
|
||||
boost::shared_ptr<int[]> a8 = boost::make_shared_noinit<int[]>(size);
|
||||
boost::shared_ptr<int[5]> a9 = boost::make_shared_noinit<int[5]>();</pre>
|
||||
<hr>
|
||||
<p>$Date: 2012-10-30 10:12:25 -0800 (Tue, 30 Oct 2012) $</p>
|
||||
<p><small>Copyright 2012 Glen Fernandes. Distributed under the Boost
|
||||
|
@ -25,6 +25,13 @@ int main() {
|
||||
BOOST_TEST(a1[1][0] == 2);
|
||||
BOOST_TEST(a1[1][1] == 3);
|
||||
}
|
||||
{
|
||||
boost::shared_ptr<int[2][2]> a1 = boost::allocate_shared<int[2][2]>(std::allocator<int>(), {0, 1});
|
||||
BOOST_TEST(a1[0][0] == 0);
|
||||
BOOST_TEST(a1[0][1] == 1);
|
||||
BOOST_TEST(a1[1][0] == 0);
|
||||
BOOST_TEST(a1[1][1] == 1);
|
||||
}
|
||||
{
|
||||
boost::shared_ptr<int[][2]> a1 = boost::allocate_shared<int[][2]>(std::allocator<int>(), 2, {0, 1});
|
||||
BOOST_TEST(a1[0][0] == 0);
|
||||
@ -39,6 +46,13 @@ int main() {
|
||||
BOOST_TEST(a1[1][1][0] == 2);
|
||||
BOOST_TEST(a1[1][1][1] == 3);
|
||||
}
|
||||
{
|
||||
boost::shared_ptr<int[2][2][2]> a1 = boost::allocate_shared<int[2][2][2]>(std::allocator<int>(), { {0, 1}, {2, 3} });
|
||||
BOOST_TEST(a1[0][0][0] == 0);
|
||||
BOOST_TEST(a1[0][0][1] == 1);
|
||||
BOOST_TEST(a1[1][1][0] == 2);
|
||||
BOOST_TEST(a1[1][1][1] == 3);
|
||||
}
|
||||
#endif
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
@ -26,6 +26,13 @@ int main() {
|
||||
BOOST_TEST(a1[1][0] == 2);
|
||||
BOOST_TEST(a1[1][1] == 3);
|
||||
}
|
||||
{
|
||||
boost::shared_ptr<int[2][2]> a1 = boost::make_shared<int[2][2]>({ 0, 1 });
|
||||
BOOST_TEST(a1[0][0] == 0);
|
||||
BOOST_TEST(a1[0][1] == 1);
|
||||
BOOST_TEST(a1[1][0] == 0);
|
||||
BOOST_TEST(a1[1][1] == 1);
|
||||
}
|
||||
{
|
||||
boost::shared_ptr<int[][2]> a1 = boost::make_shared<int[][2]>(2, {0, 1});
|
||||
BOOST_TEST(a1[0][0] == 0);
|
||||
@ -40,6 +47,13 @@ int main() {
|
||||
BOOST_TEST(a1[1][1][0] == 2);
|
||||
BOOST_TEST(a1[1][1][1] == 3);
|
||||
}
|
||||
{
|
||||
boost::shared_ptr<int[2][2][2]> a1 = boost::make_shared<int[2][2][2]>({ {0, 1}, {2, 3} });
|
||||
BOOST_TEST(a1[0][0][0] == 0);
|
||||
BOOST_TEST(a1[0][0][1] == 1);
|
||||
BOOST_TEST(a1[1][1][0] == 2);
|
||||
BOOST_TEST(a1[1][1][1] == 3);
|
||||
}
|
||||
#endif
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
Reference in New Issue
Block a user