Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r81236 - in trunk: boost/smart_ptr boost/smart_ptr/detail libs/smart_ptr/test
From: glenfe_at_[hidden]
Date: 2012-11-07 13:37:18


Author: glenfe
Date: 2012-11-07 13:37:17 EST (Wed, 07 Nov 2012)
New Revision: 81236
URL: http://svn.boost.org/trac/boost/changeset/81236

Log:
Update allocate_shared and make_shared to treat multidimensional array as single dimension. Remove detail array_helper. Add detail array traits. Update tests.
Added:
   trunk/boost/smart_ptr/detail/array_traits.hpp (contents, props changed)
Removed:
   trunk/boost/smart_ptr/detail/array_helper.hpp
Text files modified:
   trunk/boost/smart_ptr/allocate_shared_array.hpp | 38 ++++++++++++++++++------------
   trunk/boost/smart_ptr/detail/array_deleter.hpp | 26 +++++++++++---------
   trunk/boost/smart_ptr/make_shared_array.hpp | 50 ++++++++++++++++++++++++---------------
   trunk/libs/smart_ptr/test/Jamfile.v2 | 2 +
   4 files changed, 70 insertions(+), 46 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-07 13:37:17 EST (Wed, 07 Nov 2012)
@@ -12,36 +12,44 @@
 #include <boost/smart_ptr/shared_ptr.hpp>
 #include <boost/smart_ptr/detail/allocate_array_helper.hpp>
 #include <boost/smart_ptr/detail/array_deleter.hpp>
+#include <boost/smart_ptr/detail/array_traits.hpp>
 #include <boost/smart_ptr/detail/sp_if_array.hpp>
-#include <boost/type_traits/remove_cv.hpp>
 
 namespace boost {
     template<typename T, typename A>
     inline typename detail::sp_if_array<T>::type
     allocate_shared(const A& allocator, size_t size) {
- typedef typename remove_cv<typename shared_ptr<T>::element_type>::type T1;
+ typedef typename shared_ptr<T>::element_type T1;
+ typedef typename detail::array_type<T1>::type T2;
         T1* p1 = 0;
- detail::allocate_array_helper<A, T1> a1(allocator, size, &p1);
- detail::array_deleter<T1> d1;
+ T2* p2 = 0;
+ 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<T1>* d2;
- d2 = get_deleter<detail::array_deleter<T1> >(s1);
- d2->construct(p1, size);
+ detail::array_deleter<T2>* d2;
+ p1 = reinterpret_cast<T1*>(p2);
+ d2 = get_deleter<detail::array_deleter<T2> >(s1);
+ d2->construct(p2, n1);
         return shared_ptr<T>(s1, p1);
     }
 #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) {
- typedef typename remove_cv<typename shared_ptr<T>::element_type>::type T1;
+ typedef typename shared_ptr<T>::element_type T1;
+ typedef typename detail::array_type<T1>::type T2;
         T1* p1 = 0;
- detail::allocate_array_helper<A, T1> a1(allocator, size, &p1);
- detail::array_deleter<T1> d1;
- shared_ptr<T> s1(p1, d1, a1);
- detail::array_deleter<T1>* d2;
- d2 = get_deleter<detail::array_deleter<T1> >(s1);
- d2->construct(p1, size, std::forward<Args>(args)...);
- return shared_ptr<T>(s1, p1);
+ T2* p2 = 0;
+ 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(p2, d1, a1);
+ detail::array_deleter<T2>* d2;
+ p1 = reinterpret_cast<T1*>(p2);
+ d2 = get_deleter<detail::array_deleter<T2> >(s1);
+ d2->construct(p2, n1, std::forward<Args>(args)...);
+ return shared_ptr<T>(s1, p1)
     }
 #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-07 13:37:17 EST (Wed, 07 Nov 2012)
@@ -10,7 +10,6 @@
 #define BOOST_SMART_PTR_DETAIL_ARRAY_DELETER_HPP
 
 #include <boost/config.hpp>
-#include <boost/smart_ptr/detail/array_helper.hpp>
 #include <cstddef>
 
 namespace boost {
@@ -24,31 +23,34 @@
             ~array_deleter() {
                 destroy();
             }
- void construct(T* memory, std::size_t count) {
- for (object = memory; size < count; size++) {
- array_helper<T>::create(object[size]);
+ void construct(void* memory, std::size_t count) {
+ for (object = static_cast<T*>(memory); size < count; size++) {
+ void* p1 = object + size;
+ ::new(p1) T();
                 }
             }
 #if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS)
             template<typename... Args>
- void construct(T* memory, std::size_t count, Args&&... args) {
- for (object = memory; size < count; size++) {
- array_helper<T>::create(object[size], args...);
+ void construct(void* memory, std::size_t count, Args&&... args) {
+ for (object = static_cast<T*>(memory); size < count; size++) {
+ void* p1 = object + size;
+ ::new(p1) T(args...);
                 }
             }
 #endif
- void construct_noinit(T* memory, std::size_t count) {
- for (object = memory; size < count; size++) {
- array_helper<T>::create_noinit(object[size]);
+ void construct_noinit(void* memory, std::size_t count) {
+ for (object = static_cast<T*>(memory); size < count; size++) {
+ void* p1 = object + size;
+ ::new(p1) T;
                 }
             }
- void operator()(T*) {
+ void operator()(const void*) {
                 destroy();
             }
         private:
             void destroy() {
                 while (size > 0) {
- array_helper<T>::destroy(object[--size]);
+ object[--size].~T();
                 }
             }
             std::size_t size;

Deleted: trunk/boost/smart_ptr/detail/array_helper.hpp
==============================================================================
--- trunk/boost/smart_ptr/detail/array_helper.hpp 2012-11-07 13:37:17 EST (Wed, 07 Nov 2012)
+++ (empty file)
@@ -1,76 +0,0 @@
-/*
- * Copyright (c) 2012 Glen Joseph Fernandes
- * glenfe at live dot com
- *
- * Distributed under the Boost Software License,
- * Version 1.0. (See accompanying file LICENSE_1_0.txt
- * or copy at http://boost.org/LICENSE_1_0.txt)
- */
-#ifndef BOOST_SMART_PTR_DETAIL_ARRAY_HELPER_HPP
-#define BOOST_SMART_PTR_DETAIL_ARRAY_HELPER_HPP
-
-namespace boost {
- namespace detail {
- template<typename T>
- struct array_helper {
- static void create(T& value) {
- void* p1 = &value;
- ::new(p1) T();
- }
- static void create_noinit(T& value) {
- void* p1 = &value;
- ::new(p1) T;
- }
- static void destroy(T& value) {
- value.~T();
- }
-#if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS)
- template<typename... Args>
- static void create(T& value, Args... args) {
- void* p1 = &value;
- ::new(p1) T(args...);
- }
-#endif
- };
-#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
- template<typename T, size_t N>
- struct array_helper<T[N]> {
- static void create(T value[N]) {
- array_helper<T[N-1]>::create(value);
- array_helper<T>::create(value[N-1]);
- }
- static void create_noinit(T value[N]) {
- array_helper<T[N-1]>::create_noinit(value);
- array_helper<T>::create_noinit(value[N-1]);
- }
- static void destroy(T value[N]) {
- array_helper<T>::destroy(value[N-1]);
- array_helper<T[N-1]>::destroy(value);
- }
-#if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS)
- template<typename... Args>
- static void create(T value[N], Args... args) {
- array_helper<T[N-1]>::create(value, args...);
- array_helper<T>::create(value[N-1], args...);
- }
-#endif
- };
- template<typename T>
- struct array_helper<T[0]> {
- static void create(T[]) {
- }
- static void create_noinit(T[]) {
- }
- static void destroy(T[]) {
- }
-#if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS)
- template<typename... Args>
- static void create(T[], Args...) {
- }
-#endif
- };
-#endif
- }
-}
-
-#endif

Added: trunk/boost/smart_ptr/detail/array_traits.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/smart_ptr/detail/array_traits.hpp 2012-11-07 13:37:17 EST (Wed, 07 Nov 2012)
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2012 Glen Joseph Fernandes
+ * glenfe at live dot com
+ *
+ * Distributed under the Boost Software License,
+ * Version 1.0. (See accompanying file LICENSE_1_0.txt
+ * or copy at http://boost.org/LICENSE_1_0.txt)
+ */
+#ifndef BOOST_SMART_PTR_DETAIL_ARRAY_TRAITS_HPP
+#define BOOST_SMART_PTR_DETAIL_ARRAY_TRAITS_HPP
+
+#include <boost/type_traits/remove_cv.hpp>
+
+namespace boost {
+ namespace detail {
+ template<typename T>
+ struct array_type {
+ typedef typename boost::remove_cv<T>::type type;
+ };
+ template<typename T, size_t N>
+ struct array_type<T[N]> {
+ typedef typename array_type<T>::type type;
+ };
+ template<typename T>
+ struct array_size {
+ enum {
+ size = 1
+ };
+ };
+ template<typename T, size_t N>
+ struct array_size<T[N]> {
+ enum {
+ size = N * array_size<T>::size
+ };
+ };
+ }
+}
+
+#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-07 13:37:17 EST (Wed, 07 Nov 2012)
@@ -11,50 +11,62 @@
 
 #include <boost/smart_ptr/shared_ptr.hpp>
 #include <boost/smart_ptr/detail/array_deleter.hpp>
+#include <boost/smart_ptr/detail/array_traits.hpp>
 #include <boost/smart_ptr/detail/make_array_helper.hpp>
 #include <boost/smart_ptr/detail/sp_if_array.hpp>
-#include <boost/type_traits/remove_cv.hpp>
 
 namespace boost {
     template<typename T>
     inline typename detail::sp_if_array<T>::type
     make_shared(std::size_t size) {
- typedef typename remove_cv<typename shared_ptr<T>::element_type>::type T1;
+ typedef typename shared_ptr<T>::element_type T1;
+ typedef typename detail::array_type<T1>::type T2;
         T1* p1 = 0;
- detail::make_array_helper<T1> a1(size, &p1);
- detail::array_deleter<T1> d1;
+ T2* p2 = 0;
+ 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<T1>* d2;
- d2 = get_deleter<detail::array_deleter<T1> >(s1);
- d2->construct(p1, size);
+ detail::array_deleter<T2>* d2;
+ p1 = reinterpret_cast<T1*>(p2);
+ d2 = get_deleter<detail::array_deleter<T2> >(s1);
+ d2->construct(p2, n1);
         return shared_ptr<T>(s1, p1);
     }
 #if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS)
     template<typename T, typename... Args>
     inline typename detail::sp_if_array<T>::type
     make_shared(std::size_t size, Args&&... args) {
- typedef typename remove_cv<typename shared_ptr<T>::element_type>::type T1;
+ typedef typename shared_ptr<T>::element_type T1;
+ typedef typename detail::array_type<T1>::type T2;
         T1* p1 = 0;
- detail::make_array_helper<T1> a1(size, &p1);
- detail::array_deleter<T1> d1;
+ T2* p2 = 0;
+ 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<T1>* d2;
- d2 = get_deleter<detail::array_deleter<T1> >(s1);
- d2->construct(p1, size, std::forward<Args>(args)...);
+ detail::array_deleter<T2>* d2;
+ p1 = reinterpret_cast<T1*>(p2);
+ d2 = get_deleter<detail::array_deleter<T2> >(s1);
+ d2->construct(p2, n1, std::forward<Args>(args)...);
         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) {
- typedef typename remove_cv<typename shared_ptr<T>::element_type>::type T1;
+ typedef typename shared_ptr<T>::element_type T1;
+ typedef typename detail::array_type<T1>::type T2;
         T1* p1 = 0;
- detail::make_array_helper<T1> a1(size, &p1);
- detail::array_deleter<T1> d1;
+ T2* p2 = 0;
+ 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<T1>* d2;
- d2 = get_deleter<detail::array_deleter<T1> >(s1);
- d2->construct_noinit(p1, size);
+ detail::array_deleter<T2>* d2;
+ p1 = reinterpret_cast<T1*>(p2);
+ d2 = get_deleter<detail::array_deleter<T2> >(s1);
+ d2->construct_noinit(p2, n1);
         return shared_ptr<T>(s1, p1);
     }
 }

Modified: trunk/libs/smart_ptr/test/Jamfile.v2
==============================================================================
--- trunk/libs/smart_ptr/test/Jamfile.v2 (original)
+++ trunk/libs/smart_ptr/test/Jamfile.v2 2012-11-07 13:37:17 EST (Wed, 07 Nov 2012)
@@ -131,10 +131,12 @@
           [ compile-fail array_fail_array_access.cpp ]
           
           [ run make_shared_array_test.cpp ]
+ [ run make_shared_arrays_test.cpp ]
           [ run make_shared_array_throws_test.cpp ]
           [ run make_shared_array_esft_test.cpp ]
           [ run make_shared_array_args_test.cpp ]
           [ run allocate_shared_array_test.cpp ]
+ [ run allocate_shared_arrays_test.cpp ]
           [ run allocate_shared_array_throws_test.cpp ]
           [ run allocate_shared_array_esft_test.cpp ]
         ;


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