Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r81275 - in trunk: boost/smart_ptr boost/smart_ptr/detail libs/smart_ptr libs/smart_ptr/test
From: glenfe_at_[hidden]
Date: 2012-11-09 20:33:31


Author: glenfe
Date: 2012-11-09 20:33:29 EST (Fri, 09 Nov 2012)
New Revision: 81275
URL: http://svn.boost.org/trac/boost/changeset/81275

Log:
Add final overload of make_shared and allocate_shared (array forms) for T[][N] with C++11 initializer lists.
Text files modified:
   trunk/boost/smart_ptr/allocate_shared_array.hpp | 39 ++++++++++++++++++++++++++++++---------
   trunk/boost/smart_ptr/detail/allocate_array_helper.hpp | 1 -
   trunk/boost/smart_ptr/detail/array_deleter.hpp | 8 +++++++-
   trunk/boost/smart_ptr/detail/array_traits.hpp | 7 +++++++
   trunk/boost/smart_ptr/detail/make_array_helper.hpp | 1 -
   trunk/boost/smart_ptr/make_shared_array.hpp | 39 ++++++++++++++++++++++++++++++---------
   trunk/libs/smart_ptr/make_shared_array.html | 18 ++++++++++++------
   trunk/libs/smart_ptr/test/allocate_shared_arrays_create_test.cpp | 14 ++++++++++++++
   trunk/libs/smart_ptr/test/make_shared_arrays_create_test.cpp | 15 +++++++++++++++
   9 files changed, 115 insertions(+), 27 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 20:33:29 EST (Fri, 09 Nov 2012)
@@ -18,12 +18,12 @@
 namespace boost {
     template<typename T, typename A>
     inline typename detail::sp_if_array<T>::type
- allocate_shared(const A& allocator, size_t size) {
+ allocate_shared(const A& allocator, std::size_t size) {
         typedef typename detail::array_inner<T>::type T1;
         typedef typename detail::array_base<T1>::type T2;
         T1* p1 = 0;
         T2* p2 = 0;
- size_t n1 = size * detail::array_total<T1>::size;
+ std::size_t n1 = size * detail::array_total<T1>::size;
         detail::allocate_array_helper<A, T2> a1(allocator, n1, &p2);
         detail::array_deleter<T2> d1;
         shared_ptr<T> s1(p1, d1, a1);
@@ -36,12 +36,12 @@
 #if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS)
     template<typename T, typename A, typename... Args>
     inline typename detail::sp_if_array<T>::type
- allocate_shared(const A& allocator, size_t size, Args&&... args) {
+ allocate_shared(const A& allocator, std::size_t size, Args&&... args) {
         typedef typename detail::array_inner<T>::type T1;
         typedef typename detail::array_base<T1>::type T2;
         T1* p1 = 0;
         T2* p2 = 0;
- size_t n1 = size * detail::array_total<T1>::size;
+ std::size_t n1 = size * detail::array_total<T1>::size;
         detail::allocate_array_helper<A, T2> a1(allocator, n1, &p2);
         detail::array_deleter<T2> d1;
         shared_ptr<T> s1(p1, d1, a1);
@@ -58,7 +58,7 @@
         typedef typename detail::array_base<T1>::type T2;
         T1* p1 = 0;
         T2* p2 = 0;
- size_t n1 = detail::array_total<T>::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);
@@ -79,7 +79,7 @@
         T1* p1 = 0;
         T2* p2 = 0;
         T3* p3 = 0;
- size_t n1 = list.size() * detail::array_total<T1>::size;
+ std::size_t n1 = list.size() * detail::array_total<T1>::size;
         detail::allocate_array_helper<A, T2> a1(allocator, n1, &p2);
         detail::array_deleter<T2> d1;
         shared_ptr<T> s1(p1, d1, a1);
@@ -87,7 +87,7 @@
         p3 = reinterpret_cast<T3*>(list.begin());
         p1 = reinterpret_cast<T1*>(p2);
         d2 = get_deleter<detail::array_deleter<T2> >(s1);
- d2->construct(p2, n1, p3);
+ d2->construct_list(p2, n1, p3);
         return shared_ptr<T>(s1, p1);
     }
     template<typename T, typename A>
@@ -100,7 +100,7 @@
         T1* p1 = 0;
         T2* p2 = 0;
         T3* p3 = 0;
- size_t n1 = detail::array_total<T>::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);
@@ -108,7 +108,28 @@
         p3 = reinterpret_cast<T3*>(list.begin());
         p1 = reinterpret_cast<T1*>(p2);
         d2 = get_deleter<detail::array_deleter<T2> >(s1);
- d2->construct(p2, n1, p3);
+ d2->construct_list(p2, n1, p3);
+ return shared_ptr<T>(s1, p1);
+ }
+ template<typename T, typename A>
+ inline typename detail::sp_if_array<T>::type
+ allocate_shared(const A& allocator, std::size_t size, typename detail::inner_list<T>::type list) {
+ typedef typename detail::array_inner<T>::type T1;
+ typedef typename detail::array_base<T1>::type T2;
+ typedef const T2 T3;
+ T1* p1 = 0;
+ T2* p2 = 0;
+ T3* p3 = 0;
+ std::size_t n0 = detail::array_total<T1>::size;
+ std::size_t n1 = n0 * list.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

Modified: trunk/boost/smart_ptr/detail/allocate_array_helper.hpp
==============================================================================
--- trunk/boost/smart_ptr/detail/allocate_array_helper.hpp (original)
+++ trunk/boost/smart_ptr/detail/allocate_array_helper.hpp 2012-11-09 20:33:29 EST (Fri, 09 Nov 2012)
@@ -10,7 +10,6 @@
 #define BOOST_SMART_PTR_DETAIL_ALLOCATE_ARRAY_HELPER_HPP
 
 #include <boost/type_traits/alignment_of.hpp>
-#include <cstddef>
 
 namespace boost {
     namespace detail {

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 20:33:29 EST (Fri, 09 Nov 2012)
@@ -39,12 +39,18 @@
             }
 #endif
 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
- void construct(T* memory, std::size_t count, const T* list) {
+ void construct_list(T* memory, std::size_t count, const T* list) {
                 for (object = memory; size < count; size++) {
                     void* p1 = object + size;
                     ::new(p1) T(list[size]);
                 }
             }
+ void construct_list(T* memory, std::size_t count, const T* list, std::size_t n) {
+ for (object = memory; size < count; size++) {
+ void* p1 = object + size;
+ ::new(p1) T(list[size % n]);
+ }
+ }
 #endif
             void construct_noinit(T* memory, std::size_t count) {
                 for (object = memory; size < count; 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 20:33:29 EST (Fri, 09 Nov 2012)
@@ -68,6 +68,13 @@
         struct array_list<T[N]> {
             typedef std::initializer_list<T> type;
         };
+ template<typename T>
+ struct inner_list {
+ };
+ template<typename T, size_t N>
+ struct inner_list<T[][N]> {
+ typedef std::initializer_list<T> type;
+ };
 #endif
     }
 }

Modified: trunk/boost/smart_ptr/detail/make_array_helper.hpp
==============================================================================
--- trunk/boost/smart_ptr/detail/make_array_helper.hpp (original)
+++ trunk/boost/smart_ptr/detail/make_array_helper.hpp 2012-11-09 20:33:29 EST (Fri, 09 Nov 2012)
@@ -10,7 +10,6 @@
 #define BOOST_SMART_PTR_DETAIL_MAKE_ARRAY_HELPER_HPP
 
 #include <boost/type_traits/alignment_of.hpp>
-#include <cstddef>
 
 namespace boost {
     namespace detail {

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 20:33:29 EST (Fri, 09 Nov 2012)
@@ -23,7 +23,7 @@
         typedef typename detail::array_base<T1>::type T2;
         T1* p1 = 0;
         T2* p2 = 0;
- size_t n1 = size * detail::array_total<T1>::size;
+ std::size_t n1 = size * detail::array_total<T1>::size;
         detail::make_array_helper<T2> a1(n1, &p2);
         detail::array_deleter<T2> d1;
         shared_ptr<T> s1(p1, d1, a1);
@@ -41,7 +41,7 @@
         typedef typename detail::array_base<T1>::type T2;
         T1* p1 = 0;
         T2* p2 = 0;
- size_t n1 = size * detail::array_total<T1>::size;
+ std::size_t n1 = size * detail::array_total<T1>::size;
         detail::make_array_helper<T2> a1(n1, &p2);
         detail::array_deleter<T2> d1;
         shared_ptr<T> s1(p1, d1, a1);
@@ -58,7 +58,7 @@
         typedef typename detail::array_base<T1>::type T2;
         T1* p1 = 0;
         T2* p2 = 0;
- size_t n1 = detail::array_total<T>::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);
@@ -79,7 +79,7 @@
         T1* p1 = 0;
         T2* p2 = 0;
         T3* p3 = 0;
- size_t n1 = list.size() * detail::array_total<T1>::size;
+ std::size_t n1 = list.size() * detail::array_total<T1>::size;
         detail::make_array_helper<T2> a1(n1, &p2);
         detail::array_deleter<T2> d1;
         shared_ptr<T> s1(p1, d1, a1);
@@ -87,7 +87,7 @@
         p3 = reinterpret_cast<T3*>(list.begin());
         p1 = reinterpret_cast<T1*>(p2);
         d2 = get_deleter<detail::array_deleter<T2> >(s1);
- d2->construct(p2, n1, p3);
+ d2->construct_list(p2, n1, p3);
         return shared_ptr<T>(s1, p1);
     }
     template<typename T>
@@ -100,7 +100,7 @@
         T1* p1 = 0;
         T2* p2 = 0;
         T3* p3 = 0;
- size_t n1 = detail::array_total<T>::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);
@@ -108,7 +108,28 @@
         p3 = reinterpret_cast<T3*>(list.begin());
         p1 = reinterpret_cast<T1*>(p2);
         d2 = get_deleter<detail::array_deleter<T2> >(s1);
- d2->construct(p2, n1, p3);
+ d2->construct_list(p2, n1, p3);
+ return shared_ptr<T>(s1, p1);
+ }
+ template<typename T>
+ inline typename detail::sp_if_array<T>::type
+ make_shared(std::size_t size, typename detail::inner_list<T>::type list) {
+ typedef typename detail::array_inner<T>::type T1;
+ typedef typename detail::array_base<T1>::type T2;
+ typedef const T2 T3;
+ T1* p1 = 0;
+ T2* p2 = 0;
+ T3* p3 = 0;
+ std::size_t n0 = detail::array_total<T1>::size;
+ std::size_t n1 = n0 * 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
@@ -119,7 +140,7 @@
         typedef typename detail::array_base<T1>::type T2;
         T1* p1 = 0;
         T2* p2 = 0;
- size_t n1 = size * detail::array_total<T1>::size;
+ std::size_t n1 = size * detail::array_total<T1>::size;
         detail::make_array_helper<T2> a1(n1, &p2);
         detail::array_deleter<T2> d1;
         shared_ptr<T> s1(p1, d1, a1);
@@ -136,7 +157,7 @@
         typedef typename detail::array_base<T1>::type T2;
         T1* p1 = 0;
         T2* p2 = 0;
- size_t n1 = detail::array_total<T>::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);

Modified: trunk/libs/smart_ptr/make_shared_array.html
==============================================================================
--- trunk/libs/smart_ptr/make_shared_array.html (original)
+++ trunk/libs/smart_ptr/make_shared_array.html 2012-11-09 20:33:29 EST (Fri, 09 Nov 2012)
@@ -49,16 +49,22 @@
 
 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
     template&lt;typename T, typename... Args&gt;
- shared_ptr&lt;T[]&gt; make_shared(std::initializer_list&lt;T&gt; list);
+ shared_ptr&lt;T[]&gt; make_shared(initializer_list&lt;T&gt; list);
     
     template&lt;typename T, typename... Args&gt;
- shared_ptr&lt;T[N]&gt; make_shared(std::initializer_list&lt;T&gt; list);
+ shared_ptr&lt;T[N]&gt; make_shared(initializer_list&lt;T&gt; list);
+
+ template&lt;typename T, typename... Args&gt;
+ shared_ptr&lt;T[][N]&gt; make_shared(size_t size, initializer_list&lt;T&gt; list);
         
     template&lt;typename T, typename A, typename... Args&gt;
- shared_ptr&lt;T[]&gt; allocate_shared(const A&amp; allocator, std::initializer_list&lt;T&gt; list);
+ shared_ptr&lt;T[]&gt; allocate_shared(const A&amp; allocator, initializer_list&lt;T&gt; list);
+
+ template&lt;typename T, typename A, typename... Args&gt;
+ shared_ptr&lt;T[N]&gt; allocate_shared(const A&amp; allocator, initializer_list&lt;T&gt; list);
     
     template&lt;typename T, typename A, typename... Args&gt;
- shared_ptr&lt;T[N]&gt; allocate_shared(const A&amp; allocator, std::initializer_list&lt;T&gt; list);
+ shared_ptr&lt;T[][N]&gt; allocate_shared(const A&amp; allocator, size_t size, initializer_list&lt;T&gt; list);
 #endif
 
     template&lt;typename T&gt;
@@ -74,7 +80,7 @@
     shared_ptr&lt;T&gt; allocate_shared(const A&amp; allocator, size_t size, Args&amp;&amp;... args);</pre>
     <blockquote>
       <p><b>Requires:</b> The expression
- <code>new(pointer) T(std::forward&lt;Args&gt;(args)...)</code>, where
+ <code>new(pointer) T(forward&lt;Args&gt;(args)...)</code>, where
         <code>pointer</code> is a <code>void*</code> pointing to storage
         suitable to hold an object of type <code>T</code>, shall be
         well-formed. <code>A</code> shall be an <em>Allocator</em>, as
@@ -85,7 +91,7 @@
         <code>T</code> and size <code>size</code> and constructs an array
         of objects in it via the placement new expression
         <code>new(pointer) T()</code> or
- <code>new(pointer) T(std::forward&lt;Args&gt;(args)...)</code>.
+ <code>new(pointer) T(forward&lt;Args&gt;(args)...)</code>.
         <code>allocate_shared</code> uses a copy of
         <code>allocator</code> to allocate memory. If an exception is thrown,
         has no effect.</p>

Modified: trunk/libs/smart_ptr/test/allocate_shared_arrays_create_test.cpp
==============================================================================
--- trunk/libs/smart_ptr/test/allocate_shared_arrays_create_test.cpp (original)
+++ trunk/libs/smart_ptr/test/allocate_shared_arrays_create_test.cpp 2012-11-09 20:33:29 EST (Fri, 09 Nov 2012)
@@ -25,6 +25,20 @@
         BOOST_TEST(a1[1][0] == 2);
         BOOST_TEST(a1[1][1] == 3);
     }
+ {
+ boost::shared_ptr<int[][2]> a1 = boost::allocate_shared<int[][2]>(std::allocator<int>(), 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][2]> a1 = boost::allocate_shared<int[][2][2]>(std::allocator<int>(), 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();
 }

Modified: trunk/libs/smart_ptr/test/make_shared_arrays_create_test.cpp
==============================================================================
--- trunk/libs/smart_ptr/test/make_shared_arrays_create_test.cpp (original)
+++ trunk/libs/smart_ptr/test/make_shared_arrays_create_test.cpp 2012-11-09 20:33:29 EST (Fri, 09 Nov 2012)
@@ -8,6 +8,7 @@
  */
 #include <boost/detail/lightweight_test.hpp>
 #include <boost/smart_ptr/make_shared_array.hpp>
+#include <string>
 
 int main() {
 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
@@ -25,6 +26,20 @@
         BOOST_TEST(a1[1][0] == 2);
         BOOST_TEST(a1[1][1] == 3);
     }
+ {
+ boost::shared_ptr<int[][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][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();
 }


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