mirror of
https://github.com/boostorg/smart_ptr.git
synced 2025-08-03 22:54:25 +02:00
Merged revision(s) 81748-81750,81752,81759,81782 from trunk:
Refactoring in detail array_deleter before adding support for special-casing trivially default-constructible construction and trivially destroyable destruction. ........ Special case array construction for trivially default-constructible types and array destruction for trivially-destroyable types. ........ Optimization in initialization overload of array_construct for compilers to optimize it into the equivalent of a memset ........ Correctly use r-value reference semantics for Args and T in array utilities ........ Change ordering of overload definitions in array_utility.hpp ........ Convert function parameter for inner array size into template parameter and make identifiers in array_deleter consistent with those in array_utility ........ [SVN r81800]
This commit is contained in:
@@ -13,7 +13,6 @@
|
|||||||
#include <boost/smart_ptr/detail/allocate_array_helper.hpp>
|
#include <boost/smart_ptr/detail/allocate_array_helper.hpp>
|
||||||
#include <boost/smart_ptr/detail/array_deleter.hpp>
|
#include <boost/smart_ptr/detail/array_deleter.hpp>
|
||||||
#include <boost/smart_ptr/detail/array_traits.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>
|
#include <boost/smart_ptr/detail/sp_if_array.hpp>
|
||||||
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
|
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
@@ -142,7 +141,7 @@ namespace boost {
|
|||||||
p3 = reinterpret_cast<T3*>(list);
|
p3 = reinterpret_cast<T3*>(list);
|
||||||
p1 = reinterpret_cast<T1*>(p2);
|
p1 = reinterpret_cast<T1*>(p2);
|
||||||
d2 = get_deleter<boost::detail::array_deleter<T2[]> >(s1);
|
d2 = get_deleter<boost::detail::array_deleter<T2[]> >(s1);
|
||||||
d2->construct_list(p2, p3, M);
|
d2->construct_list<M>(p2, p3);
|
||||||
return boost::shared_ptr<T>(s1, p1);
|
return boost::shared_ptr<T>(s1, p1);
|
||||||
}
|
}
|
||||||
template<typename T, typename A>
|
template<typename T, typename A>
|
||||||
@@ -166,7 +165,7 @@ namespace boost {
|
|||||||
p3 = reinterpret_cast<T3*>(list);
|
p3 = reinterpret_cast<T3*>(list);
|
||||||
p1 = reinterpret_cast<T1*>(p2);
|
p1 = reinterpret_cast<T1*>(p2);
|
||||||
d2 = get_deleter<boost::detail::array_deleter<T2[N]> >(s1);
|
d2 = get_deleter<boost::detail::array_deleter<T2[N]> >(s1);
|
||||||
d2->construct_list(p2, p3, M);
|
d2->construct_list<M>(p2, p3);
|
||||||
return boost::shared_ptr<T>(s1, p1);
|
return boost::shared_ptr<T>(s1, p1);
|
||||||
}
|
}
|
||||||
#if defined(BOOST_HAS_RVALUE_REFS)
|
#if defined(BOOST_HAS_RVALUE_REFS)
|
||||||
|
@@ -9,8 +9,8 @@
|
|||||||
#ifndef BOOST_SMART_PTR_DETAIL_ARRAY_DELETER_HPP
|
#ifndef BOOST_SMART_PTR_DETAIL_ARRAY_DELETER_HPP
|
||||||
#define BOOST_SMART_PTR_DETAIL_ARRAY_DELETER_HPP
|
#define BOOST_SMART_PTR_DETAIL_ARRAY_DELETER_HPP
|
||||||
|
|
||||||
#include <boost/config.hpp>
|
#include <boost/smart_ptr/detail/array_utility.hpp>
|
||||||
#include <cstddef>
|
#include <boost/smart_ptr/detail/sp_forward.hpp>
|
||||||
|
|
||||||
namespace boost {
|
namespace boost {
|
||||||
namespace detail {
|
namespace detail {
|
||||||
@@ -24,97 +24,47 @@ namespace boost {
|
|||||||
object(0) {
|
object(0) {
|
||||||
}
|
}
|
||||||
~array_deleter() {
|
~array_deleter() {
|
||||||
destroy(size);
|
if (object) {
|
||||||
|
array_destroy(object, size);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
void construct(T* memory) {
|
void construct(T* memory) {
|
||||||
std::size_t i = 0;
|
array_construct(memory, size);
|
||||||
try {
|
object = memory;
|
||||||
for (object = memory; i < size; i++) {
|
|
||||||
void* p1 = memory + i;
|
|
||||||
::new(p1) T();
|
|
||||||
}
|
|
||||||
} catch (...) {
|
|
||||||
destroy(i);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#if defined(BOOST_HAS_RVALUE_REFS)
|
#if defined(BOOST_HAS_RVALUE_REFS)
|
||||||
void construct(T* memory, T&& value) {
|
void construct(T* memory, T&& value) {
|
||||||
std::size_t i = 0;
|
array_construct(memory, size, sp_forward<T>(value));
|
||||||
try {
|
object = memory;
|
||||||
for (object = memory; i < size; i++) {
|
|
||||||
void* p1 = memory + i;
|
|
||||||
::new(p1) T(value);
|
|
||||||
}
|
|
||||||
} catch (...) {
|
|
||||||
destroy(i);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#if defined(BOOST_HAS_VARIADIC_TMPL)
|
#if defined(BOOST_HAS_VARIADIC_TMPL)
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
void construct(T* memory, Args&&... args) {
|
void construct(T* memory, Args&&... args) {
|
||||||
std::size_t i = 0;
|
array_construct(memory, size, sp_forward<Args>(args)...);
|
||||||
try {
|
object = memory;
|
||||||
for (object = memory; i < size; i++) {
|
|
||||||
void* p1 = memory + i;
|
|
||||||
::new(p1) T(args...);
|
|
||||||
}
|
|
||||||
} catch (...) {
|
|
||||||
destroy(i);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
void construct_list(T* memory, const T* list) {
|
void construct_list(T* memory, const T* list) {
|
||||||
std::size_t i = 0;
|
array_construct_list(memory, size, list);
|
||||||
try {
|
object = memory;
|
||||||
for (object = memory; i < size; i++) {
|
|
||||||
void* p1 = memory + i;
|
|
||||||
::new(p1) T(list[i]);
|
|
||||||
}
|
|
||||||
} catch (...) {
|
|
||||||
destroy(i);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
void construct_list(T* memory, const T* list, std::size_t n) {
|
template<std::size_t M>
|
||||||
std::size_t i = 0;
|
void construct_list(T* memory, const T* list) {
|
||||||
try {
|
array_construct_list<T, M>(memory, size, list);
|
||||||
for (object = memory; i < size; i++) {
|
object = memory;
|
||||||
void* p1 = memory + i;
|
|
||||||
::new(p1) T(list[i % n]);
|
|
||||||
}
|
|
||||||
} catch (...) {
|
|
||||||
destroy(i);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
void construct_noinit(T* memory) {
|
void construct_noinit(T* memory) {
|
||||||
std::size_t i = 0;
|
array_construct_noinit(memory, size);
|
||||||
try {
|
object = memory;
|
||||||
for (object = memory; i < size; i++) {
|
|
||||||
void* p1 = memory + i;
|
|
||||||
::new(p1) T;
|
|
||||||
}
|
|
||||||
} catch (...) {
|
|
||||||
destroy(i);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
void operator()(const void*) {
|
void operator()(const void*) {
|
||||||
destroy(size);
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
void destroy(std::size_t n) {
|
|
||||||
if (object) {
|
if (object) {
|
||||||
for (std::size_t i = n; i > 0; ) {
|
array_destroy(object, size);
|
||||||
object[--i].~T();
|
|
||||||
}
|
|
||||||
object = 0;
|
object = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
private:
|
||||||
std::size_t size;
|
std::size_t size;
|
||||||
T* object;
|
T* object;
|
||||||
};
|
};
|
||||||
@@ -125,97 +75,47 @@ namespace boost {
|
|||||||
: object(0) {
|
: object(0) {
|
||||||
}
|
}
|
||||||
~array_deleter() {
|
~array_deleter() {
|
||||||
destroy(N);
|
if (object) {
|
||||||
|
array_destroy(object, N);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
void construct(T* memory) {
|
void construct(T* memory) {
|
||||||
std::size_t i = 0;
|
array_construct(memory, N);
|
||||||
try {
|
object = memory;
|
||||||
for (object = memory; i < N; i++) {
|
|
||||||
void* p1 = memory + i;
|
|
||||||
::new(p1) T();
|
|
||||||
}
|
|
||||||
} catch (...) {
|
|
||||||
destroy(i);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#if defined(BOOST_HAS_RVALUE_REFS)
|
#if defined(BOOST_HAS_RVALUE_REFS)
|
||||||
void construct(T* memory, T&& value) {
|
void construct(T* memory, T&& value) {
|
||||||
std::size_t i = 0;
|
array_construct(memory, N, sp_forward<T>(value));
|
||||||
try {
|
object = memory;
|
||||||
for (object = memory; i < N; i++) {
|
|
||||||
void* p1 = memory + i;
|
|
||||||
::new(p1) T(value);
|
|
||||||
}
|
|
||||||
} catch (...) {
|
|
||||||
destroy(i);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#if defined(BOOST_HAS_VARIADIC_TMPL)
|
#if defined(BOOST_HAS_VARIADIC_TMPL)
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
void construct(T* memory, Args&&... args) {
|
void construct(T* memory, Args&&... args) {
|
||||||
std::size_t i = 0;
|
array_construct(memory, N, sp_forward<Args>(args)...);
|
||||||
try {
|
object = memory;
|
||||||
for (object = memory; i < N; i++) {
|
|
||||||
void* p1 = memory + i;
|
|
||||||
::new(p1) T(args...);
|
|
||||||
}
|
|
||||||
} catch (...) {
|
|
||||||
destroy(i);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
void construct_list(T* memory, const T* list) {
|
void construct_list(T* memory, const T* list) {
|
||||||
std::size_t i = 0;
|
array_construct_list(memory, N, list);
|
||||||
try {
|
object = memory;
|
||||||
for (object = memory; i < N; i++) {
|
|
||||||
void* p1 = memory + i;
|
|
||||||
::new(p1) T(list[i]);
|
|
||||||
}
|
|
||||||
} catch (...) {
|
|
||||||
destroy(i);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
void construct_list(T* memory, const T* list, std::size_t n) {
|
template<std::size_t M>
|
||||||
std::size_t i = 0;
|
void construct_list(T* memory, const T* list) {
|
||||||
try {
|
array_construct_list<T, M>(memory, N, list);
|
||||||
for (object = memory; i < N; i++) {
|
object = memory;
|
||||||
void* p1 = memory + i;
|
|
||||||
::new(p1) T(list[i % n]);
|
|
||||||
}
|
|
||||||
} catch (...) {
|
|
||||||
destroy(i);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
void construct_noinit(T* memory) {
|
void construct_noinit(T* memory) {
|
||||||
std::size_t i = 0;
|
array_construct_noinit(memory, N);
|
||||||
try {
|
object = memory;
|
||||||
for (object = memory; i < N; i++) {
|
|
||||||
void* p1 = memory + i;
|
|
||||||
::new(p1) T;
|
|
||||||
}
|
|
||||||
} catch (...) {
|
|
||||||
destroy(i);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
void operator()(const void*) {
|
void operator()(const void*) {
|
||||||
destroy(N);
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
void destroy(std::size_t n) {
|
|
||||||
if (object) {
|
if (object) {
|
||||||
for (std::size_t i = n; i > 0; ) {
|
array_destroy(object, N);
|
||||||
object[--i].~T();
|
|
||||||
}
|
|
||||||
object = 0;
|
object = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
private:
|
||||||
T* object;
|
T* object;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
136
include/boost/smart_ptr/detail/array_utility.hpp
Normal file
136
include/boost/smart_ptr/detail/array_utility.hpp
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
/*
|
||||||
|
* 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*, std::size_t, boost::true_type) {
|
||||||
|
}
|
||||||
|
template<typename T>
|
||||||
|
inline void array_destroy(T* memory, std::size_t size, boost::false_type) {
|
||||||
|
for (std::size_t i = size; i > 0; ) {
|
||||||
|
memory[--i].~T();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
template<typename T>
|
||||||
|
inline void array_destroy(T* memory, std::size_t size) {
|
||||||
|
boost::has_trivial_destructor<T> type;
|
||||||
|
array_destroy(memory, size, type);
|
||||||
|
}
|
||||||
|
template<typename T>
|
||||||
|
inline void array_construct(T* memory, std::size_t size, boost::true_type) {
|
||||||
|
for (std::size_t i = 0; i < size; i++) {
|
||||||
|
memory[i] = T();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
template<typename T>
|
||||||
|
inline void array_construct(T* memory, std::size_t size, boost::false_type) {
|
||||||
|
std::size_t i = 0;
|
||||||
|
try {
|
||||||
|
for (; i < size; i++) {
|
||||||
|
void* p1 = memory + i;
|
||||||
|
::new(p1) T();
|
||||||
|
}
|
||||||
|
} catch (...) {
|
||||||
|
array_destroy(memory, i);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
template<typename T>
|
||||||
|
inline void array_construct(T* memory, std::size_t size) {
|
||||||
|
boost::has_trivial_default_constructor<T> type;
|
||||||
|
array_construct(memory, size, type);
|
||||||
|
}
|
||||||
|
#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, std::size_t N>
|
||||||
|
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 % N]);
|
||||||
|
}
|
||||||
|
} catch (...) {
|
||||||
|
array_destroy(memory, i);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
template<typename T>
|
||||||
|
inline void array_construct_noinit(T*, std::size_t, boost::true_type) {
|
||||||
|
}
|
||||||
|
template<typename T>
|
||||||
|
inline void array_construct_noinit(T* memory, std::size_t size, boost::false_type) {
|
||||||
|
std::size_t i = 0;
|
||||||
|
try {
|
||||||
|
for (; i < size; i++) {
|
||||||
|
void* p1 = memory + i;
|
||||||
|
::new(p1) T;
|
||||||
|
}
|
||||||
|
} catch (...) {
|
||||||
|
array_destroy(memory, i);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
template<typename T>
|
||||||
|
inline void array_construct_noinit(T* memory, std::size_t size) {
|
||||||
|
boost::has_trivial_default_constructor<T> type;
|
||||||
|
array_construct_noinit(memory, size, type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@@ -13,7 +13,6 @@
|
|||||||
#include <boost/smart_ptr/detail/array_deleter.hpp>
|
#include <boost/smart_ptr/detail/array_deleter.hpp>
|
||||||
#include <boost/smart_ptr/detail/array_traits.hpp>
|
#include <boost/smart_ptr/detail/array_traits.hpp>
|
||||||
#include <boost/smart_ptr/detail/make_array_helper.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>
|
#include <boost/smart_ptr/detail/sp_if_array.hpp>
|
||||||
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
|
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
@@ -141,7 +140,7 @@ namespace boost {
|
|||||||
p3 = reinterpret_cast<T3*>(list);
|
p3 = reinterpret_cast<T3*>(list);
|
||||||
p1 = reinterpret_cast<T1*>(p2);
|
p1 = reinterpret_cast<T1*>(p2);
|
||||||
d2 = get_deleter<boost::detail::array_deleter<T2[]> >(s1);
|
d2 = get_deleter<boost::detail::array_deleter<T2[]> >(s1);
|
||||||
d2->construct_list(p2, p3, M);
|
d2->construct_list<M>(p2, p3);
|
||||||
return boost::shared_ptr<T>(s1, p1);
|
return boost::shared_ptr<T>(s1, p1);
|
||||||
}
|
}
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -164,7 +163,7 @@ namespace boost {
|
|||||||
p3 = reinterpret_cast<T3*>(list);
|
p3 = reinterpret_cast<T3*>(list);
|
||||||
p1 = reinterpret_cast<T1*>(p2);
|
p1 = reinterpret_cast<T1*>(p2);
|
||||||
d2 = get_deleter<boost::detail::array_deleter<T2[N]> >(s1);
|
d2 = get_deleter<boost::detail::array_deleter<T2[N]> >(s1);
|
||||||
d2->construct_list(p2, p3, M);
|
d2->construct_list<M>(p2, p3);
|
||||||
return boost::shared_ptr<T>(s1, p1);
|
return boost::shared_ptr<T>(s1, p1);
|
||||||
}
|
}
|
||||||
#if defined(BOOST_HAS_RVALUE_REFS)
|
#if defined(BOOST_HAS_RVALUE_REFS)
|
||||||
|
Reference in New Issue
Block a user