Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r81748 - in trunk/boost/smart_ptr: . detail
From: glenfe_at_[hidden]
Date: 2012-12-06 20:53:36


Author: glenfe
Date: 2012-12-06 20:53:35 EST (Thu, 06 Dec 2012)
New Revision: 81748
URL: http://svn.boost.org/trac/boost/changeset/81748

Log:
Refactoring in detail array_deleter before adding support for special-casing trivially default-constructible construction and trivially destroyable destruction.
Added:
   trunk/boost/smart_ptr/detail/array_utility.hpp (contents, props changed)
Text files modified:
   trunk/boost/smart_ptr/allocate_shared_array.hpp | 1
   trunk/boost/smart_ptr/detail/array_deleter.hpp | 174 ++++++++-------------------------------
   trunk/boost/smart_ptr/make_shared_array.hpp | 1
   3 files changed, 36 insertions(+), 140 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-12-06 20:53:35 EST (Thu, 06 Dec 2012)
@@ -13,7 +13,6 @@
 #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_forward.hpp>
 #include <boost/smart_ptr/detail/sp_if_array.hpp>
 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
 #include <initializer_list>

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-12-06 20:53:35 EST (Thu, 06 Dec 2012)
@@ -9,8 +9,8 @@
 #ifndef BOOST_SMART_PTR_DETAIL_ARRAY_DELETER_HPP
 #define BOOST_SMART_PTR_DETAIL_ARRAY_DELETER_HPP
 
-#include <boost/config.hpp>
-#include <cstddef>
+#include <boost/smart_ptr/detail/array_utility.hpp>
+#include <boost/smart_ptr/detail/sp_forward.hpp>
 
 namespace boost {
     namespace detail {
@@ -24,97 +24,46 @@
                   object(0) {
             }
             ~array_deleter() {
- destroy(size);
+ if (object) {
+ array_destroy(object, size);
+ }
             }
             void construct(T* memory) {
- std::size_t i = 0;
- try {
- for (object = memory; i < size; i++) {
- void* p1 = memory + i;
- ::new(p1) T();
- }
- } catch (...) {
- destroy(i);
- throw;
- }
+ array_construct(memory, size);
+ object = memory;
             }
 #if defined(BOOST_HAS_RVALUE_REFS)
             void construct(T* memory, T&& value) {
- std::size_t i = 0;
- try {
- for (object = memory; i < size; i++) {
- void* p1 = memory + i;
- ::new(p1) T(value);
- }
- } catch (...) {
- destroy(i);
- throw;
- }
+ array_construct(memory, size, value);
+ object = memory;
             }
 #if defined(BOOST_HAS_VARIADIC_TMPL)
             template<typename... Args>
             void construct(T* memory, Args&&... args) {
- std::size_t i = 0;
- try {
- for (object = memory; i < size; i++) {
- void* p1 = memory + i;
- ::new(p1) T(args...);
- }
- } catch (...) {
- destroy(i);
- throw;
- }
+ array_construct(memory, size, args...);
+ object = memory;
             }
 #endif
 #endif
             void construct_list(T* memory, const T* list) {
- std::size_t i = 0;
- try {
- for (object = memory; i < size; i++) {
- void* p1 = memory + i;
- ::new(p1) T(list[i]);
- }
- } catch (...) {
- destroy(i);
- throw;
- }
+ array_construct_list(memory, size, list);
+ object = memory;
             }
             void construct_list(T* memory, const T* list, std::size_t n) {
- std::size_t i = 0;
- try {
- for (object = memory; i < size; i++) {
- void* p1 = memory + i;
- ::new(p1) T(list[i % n]);
- }
- } catch (...) {
- destroy(i);
- throw;
- }
+ array_construct_list(memory, size, list, n);
+ object = memory;
             }
             void construct_noinit(T* memory) {
- std::size_t i = 0;
- try {
- for (object = memory; i < size; i++) {
- void* p1 = memory + i;
- ::new(p1) T;
- }
- } catch (...) {
- destroy(i);
- throw;
- }
+ array_construct_noinit(memory, size);
+ object = memory;
             }
             void operator()(const void*) {
- destroy(size);
- }
- private:
- void destroy(std::size_t n) {
                 if (object) {
- for (std::size_t i = n; i > 0; ) {
- object[--i].~T();
- }
+ array_destroy(object, size);
                     object = 0;
                 }
             }
+ private:
             std::size_t size;
             T* object;
         };
@@ -125,97 +74,46 @@
                 : object(0) {
             }
             ~array_deleter() {
- destroy(N);
+ if (object) {
+ array_destroy(object, N);
+ }
             }
             void construct(T* memory) {
- std::size_t i = 0;
- try {
- for (object = memory; i < N; i++) {
- void* p1 = memory + i;
- ::new(p1) T();
- }
- } catch (...) {
- destroy(i);
- throw;
- }
+ array_construct(memory, N);
+ object = memory;
             }
 #if defined(BOOST_HAS_RVALUE_REFS)
             void construct(T* memory, T&& value) {
- std::size_t i = 0;
- try {
- for (object = memory; i < N; i++) {
- void* p1 = memory + i;
- ::new(p1) T(value);
- }
- } catch (...) {
- destroy(i);
- throw;
- }
+ array_construct(memory, N, value);
+ object = memory;
             }
 #if defined(BOOST_HAS_VARIADIC_TMPL)
             template<typename... Args>
             void construct(T* memory, Args&&... args) {
- std::size_t i = 0;
- try {
- for (object = memory; i < N; i++) {
- void* p1 = memory + i;
- ::new(p1) T(args...);
- }
- } catch (...) {
- destroy(i);
- throw;
- }
+ array_construct(memory, N, args...);
+ object = memory;
             }
 #endif
 #endif
             void construct_list(T* memory, const T* list) {
- std::size_t i = 0;
- try {
- for (object = memory; i < N; i++) {
- void* p1 = memory + i;
- ::new(p1) T(list[i]);
- }
- } catch (...) {
- destroy(i);
- throw;
- }
+ array_construct_list(memory, N, list);
+ object = memory;
             }
             void construct_list(T* memory, const T* list, std::size_t n) {
- std::size_t i = 0;
- try {
- for (object = memory; i < N; i++) {
- void* p1 = memory + i;
- ::new(p1) T(list[i % n]);
- }
- } catch (...) {
- destroy(i);
- throw;
- }
+ array_construct_list(memory, N, list, n);
+ object = memory;
             }
             void construct_noinit(T* memory) {
- std::size_t i = 0;
- try {
- for (object = memory; i < N; i++) {
- void* p1 = memory + i;
- ::new(p1) T;
- }
- } catch (...) {
- destroy(i);
- throw;
- }
+ array_construct_noinit(memory, N);
+ object = memory;
             }
             void operator()(const void*) {
- destroy(N);
- }
- private:
- void destroy(std::size_t n) {
                 if (object) {
- for (std::size_t i = n; i > 0; ) {
- object[--i].~T();
- }
+ array_destroy(object, N);
                     object = 0;
                 }
             }
+ private:
             T* object;
         };
     }

Added: trunk/boost/smart_ptr/detail/array_utility.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/smart_ptr/detail/array_utility.hpp 2012-12-06 20:53:35 EST (Thu, 06 Dec 2012)
@@ -0,0 +1,109 @@
+/*
+ * 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_UTILITY_HPP
+#define BOOST_SMART_PTR_DETAIL_ARRAY_UTILITY_HPP
+
+#include <boost/config.hpp>
+#include <boost/type_traits/has_trivial_constructor.hpp>
+#include <boost/type_traits/has_trivial_destructor.hpp>
+
+namespace boost {
+ namespace detail {
+ template<typename T>
+ inline void array_destroy(T* memory, std::size_t size) {
+ for (std::size_t i = size; i > 0; ) {
+ memory[--i].~T();
+ }
+ }
+ template<typename T>
+ inline void array_construct(T* memory, std::size_t size) {
+ std::size_t i = 0;
+ try {
+ for (; i < size; i++) {
+ void* p1 = memory + i;
+ ::new(p1) T();
+ }
+ } catch (...) {
+ array_destroy(memory, i);
+ throw;
+ }
+ }
+#if defined(BOOST_HAS_RVALUE_REFS)
+ template<typename T>
+ inline void array_construct(T* memory, std::size_t size, T value) {
+ std::size_t i = 0;
+ try {
+ for (; i < size; i++) {
+ void* p1 = memory + i;
+ ::new(p1) T(value);
+ }
+ } catch (...) {
+ array_destroy(memory, i);
+ throw;
+ }
+ }
+#if defined(BOOST_HAS_VARIADIC_TMPL)
+ template<typename T, typename... Args>
+ inline void array_construct(T* memory, std::size_t size, Args... args) {
+ std::size_t i = 0;
+ try {
+ for (; i < size; i++) {
+ void* p1 = memory + i;
+ ::new(p1) T(args...);
+ }
+ } catch (...) {
+ array_destroy(memory, i);
+ throw;
+ }
+ }
+#endif
+#endif
+ template<typename T>
+ inline void array_construct_list(T* memory, std::size_t size, const T* list) {
+ std::size_t i = 0;
+ try {
+ for (; i < size; i++) {
+ void* p1 = memory + i;
+ ::new(p1) T(list[i]);
+ }
+ } catch (...) {
+ array_destroy(memory, i);
+ throw;
+ }
+ }
+ template<typename T>
+ inline void array_construct_list(T* memory, std::size_t size, const T* list, std::size_t n) {
+ std::size_t i = 0;
+ try {
+ for (; i < size; i++) {
+ void* p1 = memory + i;
+ ::new(p1) T(list[i % n]);
+ }
+ } catch (...) {
+ array_destroy(memory, i);
+ throw;
+ }
+ }
+ template<typename T>
+ inline void array_construct_noinit(T* memory, std::size_t size) {
+ std::size_t i = 0;
+ try {
+ for (; i < size; i++) {
+ void* p1 = memory + i;
+ ::new(p1) T;
+ }
+ } catch (...) {
+ array_destroy(memory, i);
+ throw;
+ }
+ }
+ }
+}
+
+#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-12-06 20:53:35 EST (Thu, 06 Dec 2012)
@@ -13,7 +13,6 @@
 #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_forward.hpp>
 #include <boost/smart_ptr/detail/sp_if_array.hpp>
 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
 #include <initializer_list>


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