diff --git a/include/boost/smart_ptr/allocate_shared_array.hpp b/include/boost/smart_ptr/allocate_shared_array.hpp index d0123a6..ae44827 100644 --- a/include/boost/smart_ptr/allocate_shared_array.hpp +++ b/include/boost/smart_ptr/allocate_shared_array.hpp @@ -51,6 +51,23 @@ namespace boost { d2->construct(p2, n1, std::forward(args)...); return shared_ptr(s1, p1); } + template + inline typename detail::sp_if_size_array::type + allocate_shared(const A& allocator, Args&&... args) { + typedef typename detail::array_inner::type T1; + typedef typename detail::array_base::type T2; + T1* p1 = 0; + T2* p2 = 0; + size_t n1 = detail::array_size::size; + detail::allocate_array_helper a1(allocator, n1, &p2); + detail::array_deleter d1; + shared_ptr s1(p1, d1, a1); + detail::array_deleter* d2; + p1 = reinterpret_cast(p2); + d2 = get_deleter >(s1); + d2->construct(p2, n1, std::forward(args)...); + return shared_ptr(s1, p1); + } #endif #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) template diff --git a/include/boost/smart_ptr/detail/array_traits.hpp b/include/boost/smart_ptr/detail/array_traits.hpp index b95a598..d8f93ef 100644 --- a/include/boost/smart_ptr/detail/array_traits.hpp +++ b/include/boost/smart_ptr/detail/array_traits.hpp @@ -44,6 +44,10 @@ namespace boost { struct array_inner { typedef T type; }; + template + struct array_inner { + typedef T type; + }; #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) template struct array_list { diff --git a/include/boost/smart_ptr/detail/sp_if_array.hpp b/include/boost/smart_ptr/detail/sp_if_array.hpp index 4a838fc..96819b5 100644 --- a/include/boost/smart_ptr/detail/sp_if_array.hpp +++ b/include/boost/smart_ptr/detail/sp_if_array.hpp @@ -20,8 +20,11 @@ namespace boost { struct sp_if_array { typedef boost::shared_ptr type; }; + template + struct sp_if_size_array { + }; template - struct sp_if_array { + struct sp_if_size_array { typedef boost::shared_ptr type; }; } diff --git a/include/boost/smart_ptr/make_shared_array.hpp b/include/boost/smart_ptr/make_shared_array.hpp index 50a78fd..10d42a5 100644 --- a/include/boost/smart_ptr/make_shared_array.hpp +++ b/include/boost/smart_ptr/make_shared_array.hpp @@ -51,6 +51,23 @@ namespace boost { d2->construct(p2, n1, std::forward(args)...); return shared_ptr(s1, p1); } + template + inline typename detail::sp_if_size_array::type + make_shared(Args&&... args) { + typedef typename detail::array_inner::type T1; + typedef typename detail::array_base::type T2; + T1* p1 = 0; + T2* p2 = 0; + size_t n1 = detail::array_size::size; + detail::make_array_helper a1(n1, &p2); + detail::array_deleter d1; + shared_ptr s1(p1, d1, a1); + detail::array_deleter* d2; + p1 = reinterpret_cast(p2); + d2 = get_deleter >(s1); + d2->construct(p2, n1, std::forward(args)...); + return shared_ptr(s1, p1); + } #endif #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) template @@ -91,6 +108,23 @@ namespace boost { d2->construct_noinit(p2, n1); return shared_ptr(s1, p1); } + template + inline typename detail::sp_if_size_array::type + make_shared_noinit() { + typedef typename detail::array_inner::type T1; + typedef typename detail::array_base::type T2; + T1* p1 = 0; + T2* p2 = 0; + size_t n1 = detail::array_size::size; + detail::make_array_helper a1(n1, &p2); + detail::array_deleter d1; + shared_ptr s1(p1, d1, a1); + detail::array_deleter* d2; + p1 = reinterpret_cast(p2); + d2 = get_deleter >(s1); + d2->construct_noinit(p2, n1); + return shared_ptr(s1, p1); + } } #endif diff --git a/make_shared_array.html b/make_shared_array.html index e35350c..01bf71b 100644 --- a/make_shared_array.html +++ b/make_shared_array.html @@ -28,21 +28,38 @@

Synopsis

namespace boost {
     template<typename T>
-    shared_ptr<T> make_shared(size_t size);
+    shared_ptr<T[]> make_shared(size_t size);
 
     template<typename T, typename A>
-    shared_ptr<T> allocate_shared(const A& allocator, size_t size);
+    shared_ptr<T[]> allocate_shared(const A& allocator, size_t size);
     
 #if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS)
     template<typename T, typename... Args>
-    shared_ptr<T> make_shared(size_t size, Args&&... args);
+    shared_ptr<T[]> make_shared(size_t size, Args&&... args);
+    
+    template<typename T, typename... Args>
+    shared_ptr<T[N]> make_shared(Args&&... args);
     
     template<typename T, typename A, typename... Args>
     shared_ptr<T> allocate_shared(const A& allocator, size_t size, Args&&... args);
+    
+    template<typename T, typename A, typename... Args>
+    shared_ptr<T[N]> allocate_shared(const A& allocator, Args&&... args);
+#endif
+
+#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
+    template<typename T, typename... Args>
+    shared_ptr<T[]> make_shared(std::initializer_list<T> list);
+        
+    template<typename T, typename A, typename... Args>
+    shared_ptr<T[]> allocate_shared(const A& allocator, std::initializer_list<T> list);
 #endif
 
     template<typename T>
     shared_ptr<T> make_shared_noinit(size_t size);
+    
+    template<typename T>
+    shared_ptr<T[N]> make_shared_noinit();
 }

Free Functions

template<typename T, typename... Args>
diff --git a/test/allocate_shared_array_create_test.cpp b/test/allocate_shared_array_create_test.cpp
index 9758c78..9959838 100644
--- a/test/allocate_shared_array_create_test.cpp
+++ b/test/allocate_shared_array_create_test.cpp
@@ -47,6 +47,15 @@ int main() {
         BOOST_TEST(a1[1].i == 9);
     }
     BOOST_TEST(type::instances == 0);
+    {
+        boost::shared_ptr a1 = boost::allocate_shared(std::allocator(), 1, 2, 3, 4, 5, 6, 7, 8, 9);
+        BOOST_TEST(type::instances == 2);
+        BOOST_TEST(a1[0].a == 1);
+        BOOST_TEST(a1[0].d == 4);
+        BOOST_TEST(a1[1].f == 6);
+        BOOST_TEST(a1[1].i == 9);
+    }
+    BOOST_TEST(type::instances == 0);
     {
         boost::shared_ptr a1 = boost::allocate_shared(std::allocator(), 2, 1, 2, 3, 4, 5, 6, 7);
         BOOST_TEST(type::instances == 4);
@@ -56,6 +65,15 @@ int main() {
         BOOST_TEST(a1[1][1].i == 0);
     }
     BOOST_TEST(type::instances == 0);
+    {
+        boost::shared_ptr a1 = boost::allocate_shared(std::allocator(), 1, 2, 3, 4, 5, 6, 7);
+        BOOST_TEST(type::instances == 4);
+        BOOST_TEST(a1[0][0].a == 1);
+        BOOST_TEST(a1[0][1].d == 4);
+        BOOST_TEST(a1[1][0].f == 6);
+        BOOST_TEST(a1[1][1].i == 0);
+    }
+    BOOST_TEST(type::instances == 0);
     {
         boost::shared_ptr a1 = boost::allocate_shared(std::allocator(), 2, 1, 2, 3, 4, 5);
         BOOST_TEST(type::instances == 8);
@@ -65,6 +83,15 @@ int main() {
         BOOST_TEST(a1[1][1][1].i == 0);
     }
     BOOST_TEST(type::instances == 0);
+    {
+        boost::shared_ptr a1 = boost::allocate_shared(std::allocator(), 1, 2, 3, 4, 5);
+        BOOST_TEST(type::instances == 8);
+        BOOST_TEST(a1[0][0][0].a == 1);
+        BOOST_TEST(a1[0][1][0].c == 3);
+        BOOST_TEST(a1[1][0][1].e == 5);
+        BOOST_TEST(a1[1][1][1].i == 0);
+    }
+    BOOST_TEST(type::instances == 0);
     {
         boost::shared_ptr a1 = boost::allocate_shared(std::allocator(), 2, 1, 2, 3);
         BOOST_TEST(type::instances == 16);
@@ -73,6 +100,15 @@ int main() {
         BOOST_TEST(a1[0][1][0][0].f == 0);
         BOOST_TEST(a1[1][0][0][0].i == 0);
     }
+    BOOST_TEST(type::instances == 0);
+    {
+        boost::shared_ptr a1 = boost::allocate_shared(std::allocator(), 1, 2, 3);
+        BOOST_TEST(type::instances == 16);
+        BOOST_TEST(a1[0][0][0][1].a == 1);
+        BOOST_TEST(a1[0][0][1][0].c == 3);
+        BOOST_TEST(a1[0][1][0][0].f == 0);
+        BOOST_TEST(a1[1][0][0][0].i == 0);
+    }
 #endif
 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
     {
diff --git a/test/make_shared_array_create_test.cpp b/test/make_shared_array_create_test.cpp
index 869d21c..cc47dd6 100644
--- a/test/make_shared_array_create_test.cpp
+++ b/test/make_shared_array_create_test.cpp
@@ -47,6 +47,15 @@ int main() {
         BOOST_TEST(a1[1].i == 9);
     }
     BOOST_TEST(type::instances == 0);
+    {
+        boost::shared_ptr a1 = boost::make_shared(1, 2, 3, 4, 5, 6, 7, 8, 9);
+        BOOST_TEST(type::instances == 2);
+        BOOST_TEST(a1[0].a == 1);
+        BOOST_TEST(a1[0].d == 4);
+        BOOST_TEST(a1[1].f == 6);
+        BOOST_TEST(a1[1].i == 9);
+    }
+    BOOST_TEST(type::instances == 0);
     {
         boost::shared_ptr a1 = boost::make_shared(2, 1, 2, 3, 4, 5, 6, 7);
         BOOST_TEST(type::instances == 4);
@@ -56,6 +65,15 @@ int main() {
         BOOST_TEST(a1[1][1].i == 0);
     }
     BOOST_TEST(type::instances == 0);
+    {
+        boost::shared_ptr a1 = boost::make_shared(1, 2, 3, 4, 5, 6, 7);
+        BOOST_TEST(type::instances == 4);
+        BOOST_TEST(a1[0][0].a == 1);
+        BOOST_TEST(a1[0][1].d == 4);
+        BOOST_TEST(a1[1][0].f == 6);
+        BOOST_TEST(a1[1][1].i == 0);
+    }    
+    BOOST_TEST(type::instances == 0);
     {
         boost::shared_ptr a1 = boost::make_shared(2, 1, 2, 3, 4, 5);
         BOOST_TEST(type::instances == 8);
@@ -65,6 +83,15 @@ int main() {
         BOOST_TEST(a1[1][1][1].i == 0);
     }
     BOOST_TEST(type::instances == 0);
+    {
+        boost::shared_ptr a1 = boost::make_shared(1, 2, 3, 4, 5);
+        BOOST_TEST(type::instances == 8);
+        BOOST_TEST(a1[0][0][0].a == 1);
+        BOOST_TEST(a1[0][1][0].c == 3);
+        BOOST_TEST(a1[1][0][1].e == 5);
+        BOOST_TEST(a1[1][1][1].i == 0);
+    }
+    BOOST_TEST(type::instances == 0);
     {
         boost::shared_ptr a1 = boost::make_shared(2, 1, 2, 3);
         BOOST_TEST(type::instances == 16);
@@ -73,6 +100,15 @@ int main() {
         BOOST_TEST(a1[0][1][0][0].f == 0);
         BOOST_TEST(a1[1][0][0][0].i == 0);
     }
+    BOOST_TEST(type::instances == 0);
+    {
+        boost::shared_ptr a1 = boost::make_shared(1, 2, 3);
+        BOOST_TEST(type::instances == 16);
+        BOOST_TEST(a1[0][0][0][1].a == 1);
+        BOOST_TEST(a1[0][0][1][0].c == 3);
+        BOOST_TEST(a1[0][1][0][0].f == 0);
+        BOOST_TEST(a1[1][0][0][0].i == 0);
+    }
 #endif
 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
     {
diff --git a/test/make_shared_arrays_test.cpp b/test/make_shared_arrays_test.cpp
index 22fe447..e4c3d48 100644
--- a/test/make_shared_arrays_test.cpp
+++ b/test/make_shared_arrays_test.cpp
@@ -75,11 +75,21 @@ int main() {
         boost::shared_ptr a1 = boost::make_shared_noinit(2);
         BOOST_TEST(a1.get() != 0);
         BOOST_TEST(a1.use_count() == 1);
+    }
+    {
+        boost::shared_ptr a1 = boost::make_shared_noinit();
+        BOOST_TEST(a1.get() != 0);
+        BOOST_TEST(a1.use_count() == 1);
     }    
     {
         boost::shared_ptr a1 = boost::make_shared_noinit(2);
         BOOST_TEST(a1.get() != 0);
         BOOST_TEST(a1.use_count() == 1);
+    }
+    {
+        boost::shared_ptr a1 = boost::make_shared_noinit();
+        BOOST_TEST(a1.get() != 0);
+        BOOST_TEST(a1.use_count() == 1);
     }    
     BOOST_TEST(type::instances == 0);
     {
@@ -91,11 +101,27 @@ int main() {
         BOOST_TEST(type::instances == 0);
     }
     BOOST_TEST(type::instances == 0);
+    {
+        boost::shared_ptr a1 = boost::make_shared_noinit();
+        BOOST_TEST(a1.get() != 0);
+        BOOST_TEST(a1.use_count() == 1);
+        BOOST_TEST(type::instances == 8);
+        a1.reset();
+        BOOST_TEST(type::instances == 0);
+    }
+    BOOST_TEST(type::instances == 0);
     {
         boost::shared_ptr a1 = boost::make_shared_noinit(2);
         BOOST_TEST(a1.get() != 0);
         BOOST_TEST(a1.use_count() == 1);
         BOOST_TEST(type::instances == 8);
     }
+    BOOST_TEST(type::instances == 0);
+    {
+        boost::shared_ptr a1 = boost::make_shared_noinit();
+        BOOST_TEST(a1.get() != 0);
+        BOOST_TEST(a1.use_count() == 1);
+        BOOST_TEST(type::instances == 8);
+    }
     return boost::report_errors();
 }