Add final overload of make_shared and allocate_shared (array forms) for T[][N] with C++11 initializer lists.

[SVN r81275]
This commit is contained in:
Glen Fernandes
2012-11-10 01:33:29 +00:00
parent 5bdde37414
commit 980070e63f
9 changed files with 115 additions and 27 deletions

View File

@ -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 @@ namespace boost {
#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 @@ namespace boost {
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 @@ namespace boost {
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 @@ namespace boost {
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 @@ namespace boost {
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 @@ namespace boost {
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

View File

@ -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 {

View File

@ -39,12 +39,18 @@ namespace boost {
}
#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++) {

View File

@ -68,6 +68,13 @@ namespace boost {
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
}
}

View File

@ -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 {

View File

@ -23,7 +23,7 @@ namespace boost {
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 @@ namespace boost {
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 @@ namespace boost {
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 @@ namespace boost {
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 @@ namespace boost {
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 @@ namespace boost {
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 @@ namespace boost {
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 @@ namespace boost {
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 @@ namespace boost {
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);

View File

@ -49,16 +49,22 @@
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
template&lt;typename T, typename... Args&gt;
shared_ptr&lt;T[]&gt; <a href="#functions">make_shared</a>(std::initializer_list&lt;T&gt; list);
shared_ptr&lt;T[]&gt; <a href="#functions">make_shared</a>(initializer_list&lt;T&gt; list);
template&lt;typename T, typename... Args&gt;
shared_ptr&lt;T[N]&gt; <a href="#functions">make_shared</a>(std::initializer_list&lt;T&gt; list);
shared_ptr&lt;T[N]&gt; <a href="#functions">make_shared</a>(initializer_list&lt;T&gt; list);
template&lt;typename T, typename... Args&gt;
shared_ptr&lt;T[][N]&gt; <a href="#functions">make_shared</a>(size_t size, initializer_list&lt;T&gt; list);
template&lt;typename T, typename A, typename... Args&gt;
shared_ptr&lt;T[]&gt; <a href="#functions">allocate_shared</a>(const A&amp; allocator, std::initializer_list&lt;T&gt; list);
shared_ptr&lt;T[]&gt; <a href="#functions">allocate_shared</a>(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; <a href="#functions">allocate_shared</a>(const A&amp; allocator, std::initializer_list&lt;T&gt; list);
shared_ptr&lt;T[N]&gt; <a href="#functions">allocate_shared</a>(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; <a href="#functions">allocate_shared</a>(const A&amp; allocator, size_t size, initializer_list&lt;T&gt; list);
#endif
template&lt;typename T&gt;
@ -74,7 +80,7 @@ template&lt;typename T, typename A, typename... Args&gt;
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 @@ template&lt;typename T, typename A, typename... Args&gt;
<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>

View File

@ -25,6 +25,20 @@ int main() {
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();
}

View File

@ -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 @@ int main() {
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();
}