From 6b2556edfbc6645d598f3d2eeba3b311dbceaae9 Mon Sep 17 00:00:00 2001 From: Glen Fernandes Date: Wed, 14 Nov 2012 15:18:50 +0000 Subject: [PATCH] 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] --- .../boost/smart_ptr/allocate_shared_array.hpp | 25 ++++++++++++++++++- .../boost/smart_ptr/detail/array_traits.hpp | 10 +++++--- include/boost/smart_ptr/make_shared_array.hpp | 24 +++++++++++++++++- make_shared_array.html | 18 +++++++++++-- test/allocate_shared_arrays_create_test.cpp | 14 +++++++++++ test/make_shared_arrays_create_test.cpp | 14 +++++++++++ 6 files changed, 98 insertions(+), 7 deletions(-) diff --git a/include/boost/smart_ptr/allocate_shared_array.hpp b/include/boost/smart_ptr/allocate_shared_array.hpp index b2aa826..3f428c3 100644 --- a/include/boost/smart_ptr/allocate_shared_array.hpp +++ b/include/boost/smart_ptr/allocate_shared_array.hpp @@ -98,10 +98,10 @@ namespace boost { inline typename detail::sp_if_size_array::type allocate_shared(const A& allocator, std::initializer_list::type> list) { - BOOST_ASSERT(list.size() == detail::array_size::size); typedef typename detail::array_inner::type T1; typedef typename detail::array_base::type T2; typedef const T2 T3; + BOOST_ASSERT(list.size() == detail::array_size::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(s1, p1); } + template + inline typename detail::sp_if_size_array::type + allocate_shared(const A& allocator, + std::initializer_list::type> list) { + typedef typename detail::array_inner::type T1; + typedef typename detail::array_base::type T2; + typedef const T2 T3; + BOOST_ASSERT(list.size() == detail::array_size::size); + T1* p1 = 0; + T2* p2 = 0; + T3* p3 = 0; + std::size_t n0 = detail::array_total::size; + std::size_t n1 = detail::array_total::size; + detail::allocate_array_helper a1(allocator, n1, &p2); + detail::array_deleter d1; + shared_ptr s1(p1, d1, a1); + detail::array_deleter* d2; + p3 = reinterpret_cast(list.begin()); + p1 = reinterpret_cast(p2); + d2 = get_deleter >(s1); + d2->construct_list(p2, n1, p3, n0); + return shared_ptr(s1, p1); + } #endif } diff --git a/include/boost/smart_ptr/detail/array_traits.hpp b/include/boost/smart_ptr/detail/array_traits.hpp index 068629a..3b80f46 100644 --- a/include/boost/smart_ptr/detail/array_traits.hpp +++ b/include/boost/smart_ptr/detail/array_traits.hpp @@ -24,7 +24,7 @@ namespace boost { template struct array_size { }; - template + template struct array_size { enum { size = N @@ -49,17 +49,21 @@ namespace boost { struct array_inner { typedef T type; }; - template + template struct array_inner { typedef T type; }; template struct arrays_inner { }; - template + template struct arrays_inner { typedef T type; }; + template + struct arrays_inner { + typedef T type; + }; } } diff --git a/include/boost/smart_ptr/make_shared_array.hpp b/include/boost/smart_ptr/make_shared_array.hpp index f9a39ff..2c0fd8d 100644 --- a/include/boost/smart_ptr/make_shared_array.hpp +++ b/include/boost/smart_ptr/make_shared_array.hpp @@ -96,10 +96,10 @@ namespace boost { template inline typename detail::sp_if_size_array::type make_shared(std::initializer_list::type> list) { - BOOST_ASSERT(list.size() == detail::array_size::size); typedef typename detail::array_inner::type T1; typedef typename detail::array_base::type T2; typedef const T2 T3; + BOOST_ASSERT(list.size() == detail::array_size::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(s1, p1); } + template + inline typename detail::sp_if_size_array::type + make_shared(std::initializer_list::type> list) { + typedef typename detail::array_inner::type T1; + typedef typename detail::array_base::type T2; + typedef const T2 T3; + BOOST_ASSERT(list.size() == detail::array_size::size); + T1* p1 = 0; + T2* p2 = 0; + T3* p3 = 0; + std::size_t n0 = detail::array_total::size; + std::size_t n1 = detail::array_total::size; + detail::make_array_helper a1(n1, &p2); + detail::array_deleter d1; + shared_ptr s1(p1, d1, a1); + detail::array_deleter* d2; + p3 = reinterpret_cast(list.begin()); + p1 = reinterpret_cast(p2); + d2 = get_deleter >(s1); + d2->construct_list(p2, n1, p3, n0); + return shared_ptr(s1, p1); + } #endif template inline typename detail::sp_if_array::type diff --git a/make_shared_array.html b/make_shared_array.html index 4739b3a..9c46926 100644 --- a/make_shared_array.html +++ b/make_shared_array.html @@ -56,7 +56,10 @@ template<typename T, typename... Args> shared_ptr<T[][N]> make_shared(size_t size, initializer_list<T> list); - + + template<typename T, typename... Args> + shared_ptr<T[M][N]> make_shared(initializer_list<T> list); + template<typename T, typename A, typename... Args> shared_ptr<T[]> allocate_shared(const A& allocator, initializer_list<T> list); @@ -65,6 +68,9 @@ template<typename T, typename A, typename... Args> shared_ptr<T[][N]> allocate_shared(const A& allocator, size_t size, initializer_list<T> list); + + template<typename T, typename A, typename... Args> + shared_ptr<T[M][N]> allocate_shared(const A& allocator, initializer_list<T> list); #endif template<typename T> @@ -117,7 +123,15 @@ template<typename T, typename A, typename... Args> constructor of T for each array element.

Example

-
boost::shared_ptr<int[]> array = boost::make_shared<int[]>(size);
+
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]>();

$Date: 2012-10-30 10:12:25 -0800 (Tue, 30 Oct 2012) $

Copyright 2012 Glen Fernandes. Distributed under the Boost diff --git a/test/allocate_shared_arrays_create_test.cpp b/test/allocate_shared_arrays_create_test.cpp index fd44aa0..03a2d7e 100644 --- a/test/allocate_shared_arrays_create_test.cpp +++ b/test/allocate_shared_arrays_create_test.cpp @@ -25,6 +25,13 @@ int main() { BOOST_TEST(a1[1][0] == 2); BOOST_TEST(a1[1][1] == 3); } + { + boost::shared_ptr a1 = boost::allocate_shared(std::allocator(), {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 a1 = boost::allocate_shared(std::allocator(), 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 a1 = boost::allocate_shared(std::allocator(), { {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(); } diff --git a/test/make_shared_arrays_create_test.cpp b/test/make_shared_arrays_create_test.cpp index b5b96d5..e468378 100644 --- a/test/make_shared_arrays_create_test.cpp +++ b/test/make_shared_arrays_create_test.cpp @@ -26,6 +26,13 @@ int main() { BOOST_TEST(a1[1][0] == 2); BOOST_TEST(a1[1][1] == 3); } + { + boost::shared_ptr a1 = boost::make_shared({ 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 a1 = boost::make_shared(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 a1 = boost::make_shared({ {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(); }