Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r81257 - in trunk: boost/smart_ptr boost/smart_ptr/detail libs/smart_ptr/test
From: glenfe_at_[hidden]
Date: 2012-11-09 01:17:07


Author: glenfe
Date: 2012-11-09 01:17:05 EST (Fri, 09 Nov 2012)
New Revision: 81257
URL: http://svn.boost.org/trac/boost/changeset/81257

Log:
Add C++11 initializer list support for make_shared and allocate_shared array forms.

Text files modified:
   trunk/boost/smart_ptr/allocate_shared_array.hpp | 22 ++++++++++++++++++++++
   trunk/boost/smart_ptr/detail/array_deleter.hpp | 8 ++++++++
   trunk/boost/smart_ptr/detail/array_traits.hpp | 18 ++++++++++++++++++
   trunk/boost/smart_ptr/make_shared_array.hpp | 22 ++++++++++++++++++++++
   trunk/libs/smart_ptr/test/allocate_shared_array_create_test.cpp | 16 ++++++++++++++++
   trunk/libs/smart_ptr/test/make_shared_array_create_test.cpp | 16 ++++++++++++++++
   6 files changed, 102 insertions(+), 0 deletions(-)

Modified: trunk/boost/smart_ptr/allocate_shared_array.hpp
==============================================================================
--- trunk/boost/smart_ptr/allocate_shared_array.hpp (original)
+++ trunk/boost/smart_ptr/allocate_shared_array.hpp 2012-11-09 01:17:05 EST (Fri, 09 Nov 2012)
@@ -52,6 +52,28 @@
         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

Modified: trunk/boost/smart_ptr/detail/array_deleter.hpp
==============================================================================
--- trunk/boost/smart_ptr/detail/array_deleter.hpp (original)
+++ trunk/boost/smart_ptr/detail/array_deleter.hpp 2012-11-09 01:17:05 EST (Fri, 09 Nov 2012)
@@ -38,6 +38,14 @@
                 }
             }
 #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++) {
                     void* p1 = object + size;

Modified: trunk/boost/smart_ptr/detail/array_traits.hpp
==============================================================================
--- trunk/boost/smart_ptr/detail/array_traits.hpp (original)
+++ trunk/boost/smart_ptr/detail/array_traits.hpp 2012-11-09 01:17:05 EST (Fri, 09 Nov 2012)
@@ -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 @@
         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
     }
 }
 

Modified: trunk/boost/smart_ptr/make_shared_array.hpp
==============================================================================
--- trunk/boost/smart_ptr/make_shared_array.hpp (original)
+++ trunk/boost/smart_ptr/make_shared_array.hpp 2012-11-09 01:17:05 EST (Fri, 09 Nov 2012)
@@ -52,6 +52,28 @@
         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
     make_shared_noinit(std::size_t size) {

Modified: trunk/libs/smart_ptr/test/allocate_shared_array_create_test.cpp
==============================================================================
--- trunk/libs/smart_ptr/test/allocate_shared_array_create_test.cpp (original)
+++ trunk/libs/smart_ptr/test/allocate_shared_array_create_test.cpp 2012-11-09 01:17:05 EST (Fri, 09 Nov 2012)
@@ -74,5 +74,21 @@
         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();
 }

Modified: trunk/libs/smart_ptr/test/make_shared_array_create_test.cpp
==============================================================================
--- trunk/libs/smart_ptr/test/make_shared_array_create_test.cpp (original)
+++ trunk/libs/smart_ptr/test/make_shared_array_create_test.cpp 2012-11-09 01:17:05 EST (Fri, 09 Nov 2012)
@@ -74,5 +74,21 @@
         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();
 }


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk