forked from boostorg/smart_ptr
Add overloads to support fixed size arrays, T[N], to allocate_shared (variadic) and make_shared (variadic) and make_shared_noinit.
[SVN r81265]
This commit is contained in:
@ -51,6 +51,23 @@ namespace boost {
|
||||
d2->construct(p2, n1, std::forward<Args>(args)...);
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
template<typename T, typename A, typename... Args>
|
||||
inline typename detail::sp_if_size_array<T>::type
|
||||
allocate_shared(const A& allocator, 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 = detail::array_size<T>::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;
|
||||
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
|
||||
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
|
||||
template<typename T, typename A>
|
||||
|
@ -44,6 +44,10 @@ namespace boost {
|
||||
struct array_inner<T[]> {
|
||||
typedef T type;
|
||||
};
|
||||
template<typename T, size_t N>
|
||||
struct array_inner<T[N]> {
|
||||
typedef T type;
|
||||
};
|
||||
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
|
||||
template<typename T>
|
||||
struct array_list {
|
||||
|
@ -20,8 +20,11 @@ namespace boost {
|
||||
struct sp_if_array<T[]> {
|
||||
typedef boost::shared_ptr<T[]> type;
|
||||
};
|
||||
template<typename T>
|
||||
struct sp_if_size_array {
|
||||
};
|
||||
template<typename T, size_t N>
|
||||
struct sp_if_array<T[N]> {
|
||||
struct sp_if_size_array<T[N]> {
|
||||
typedef boost::shared_ptr<T[N]> type;
|
||||
};
|
||||
}
|
||||
|
@ -51,6 +51,23 @@ namespace boost {
|
||||
d2->construct(p2, n1, std::forward<Args>(args)...);
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
template<typename T, typename... Args>
|
||||
inline typename detail::sp_if_size_array<T>::type
|
||||
make_shared(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 = detail::array_size<T>::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;
|
||||
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
|
||||
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
|
||||
template<typename T>
|
||||
@ -91,6 +108,23 @@ namespace boost {
|
||||
d2->construct_noinit(p2, n1);
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
template<typename T>
|
||||
inline typename detail::sp_if_size_array<T>::type
|
||||
make_shared_noinit() {
|
||||
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 = detail::array_size<T>::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;
|
||||
p1 = reinterpret_cast<T1*>(p2);
|
||||
d2 = get_deleter<detail::array_deleter<T2> >(s1);
|
||||
d2->construct_noinit(p2, n1);
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -28,21 +28,38 @@
|
||||
<h2><a name="Synopsis">Synopsis</a></h2>
|
||||
<pre>namespace boost {
|
||||
template<typename T>
|
||||
shared_ptr<T> <a href="#functions">make_shared</a>(size_t size);
|
||||
shared_ptr<T[]> <a href="#functions">make_shared</a>(size_t size);
|
||||
|
||||
template<typename T, typename A>
|
||||
shared_ptr<T> <a href="#functions">allocate_shared</a>(const A& allocator, size_t size);
|
||||
shared_ptr<T[]> <a href="#functions">allocate_shared</a>(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> <a href="#functions">make_shared</a>(size_t size, Args&&... args);
|
||||
shared_ptr<T[]> <a href="#functions">make_shared</a>(size_t size, Args&&... args);
|
||||
|
||||
template<typename T, typename... Args>
|
||||
shared_ptr<T[N]> <a href="#functions">make_shared</a>(Args&&... args);
|
||||
|
||||
template<typename T, typename A, typename... Args>
|
||||
shared_ptr<T> <a href="#functions">allocate_shared</a>(const A& allocator, size_t size, Args&&... args);
|
||||
|
||||
template<typename T, typename A, typename... Args>
|
||||
shared_ptr<T[N]> <a href="#functions">allocate_shared</a>(const A& allocator, Args&&... args);
|
||||
#endif
|
||||
|
||||
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
|
||||
template<typename T, typename... Args>
|
||||
shared_ptr<T[]> <a href="#functions">make_shared</a>(std::initializer_list<T> list);
|
||||
|
||||
template<typename T, typename A, typename... Args>
|
||||
shared_ptr<T[]> <a href="#functions">allocate_shared</a>(const A& allocator, std::initializer_list<T> list);
|
||||
#endif
|
||||
|
||||
template<typename T>
|
||||
shared_ptr<T> <a href="#functions">make_shared_noinit</a>(size_t size);
|
||||
|
||||
template<typename T>
|
||||
shared_ptr<T[N]> <a href="#functions">make_shared_noinit</a>();
|
||||
}</pre>
|
||||
<h2><a name="functions">Free Functions</a></h2>
|
||||
<pre>template<typename T, typename... Args>
|
||||
|
@ -47,6 +47,15 @@ int main() {
|
||||
BOOST_TEST(a1[1].i == 9);
|
||||
}
|
||||
BOOST_TEST(type::instances == 0);
|
||||
{
|
||||
boost::shared_ptr<type[2]> a1 = boost::allocate_shared<type[2]>(std::allocator<type>(), 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<type[][2]> a1 = boost::allocate_shared<type[][2]>(std::allocator<type>(), 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<type[2][2]> a1 = boost::allocate_shared<type[2][2]>(std::allocator<type>(), 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<type[][2][2]> a1 = boost::allocate_shared<type[][2][2]>(std::allocator<type>(), 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<type[2][2][2]> a1 = boost::allocate_shared<type[2][2][2]>(std::allocator<type>(), 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<type[][2][2][2]> a1 = boost::allocate_shared<type[][2][2][2]>(std::allocator<type>(), 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<type[2][2][2][2]> a1 = boost::allocate_shared<type[2][2][2][2]>(std::allocator<type>(), 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)
|
||||
{
|
||||
|
@ -47,6 +47,15 @@ int main() {
|
||||
BOOST_TEST(a1[1].i == 9);
|
||||
}
|
||||
BOOST_TEST(type::instances == 0);
|
||||
{
|
||||
boost::shared_ptr<type[2]> a1 = boost::make_shared<type[2]>(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<type[][2]> a1 = boost::make_shared<type[][2]>(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<type[2][2]> a1 = boost::make_shared<type[2][2]>(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<type[][2][2]> a1 = boost::make_shared<type[][2][2]>(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<type[2][2][2]> a1 = boost::make_shared<type[2][2][2]>(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<type[][2][2][2]> a1 = boost::make_shared<type[][2][2][2]>(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<type[2][2][2][2]> a1 = boost::make_shared<type[2][2][2][2]>(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)
|
||||
{
|
||||
|
@ -75,11 +75,21 @@ int main() {
|
||||
boost::shared_ptr<int[][2][2]> a1 = boost::make_shared_noinit<int[][2][2]>(2);
|
||||
BOOST_TEST(a1.get() != 0);
|
||||
BOOST_TEST(a1.use_count() == 1);
|
||||
}
|
||||
{
|
||||
boost::shared_ptr<int[2][2][2]> a1 = boost::make_shared_noinit<int[2][2][2]>();
|
||||
BOOST_TEST(a1.get() != 0);
|
||||
BOOST_TEST(a1.use_count() == 1);
|
||||
}
|
||||
{
|
||||
boost::shared_ptr<const int[][2][2]> a1 = boost::make_shared_noinit<const int[][2][2]>(2);
|
||||
BOOST_TEST(a1.get() != 0);
|
||||
BOOST_TEST(a1.use_count() == 1);
|
||||
}
|
||||
{
|
||||
boost::shared_ptr<const int[2][2][2]> a1 = boost::make_shared_noinit<const int[2][2][2]>();
|
||||
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<type[2][2][2]> a1 = boost::make_shared_noinit<type[2][2][2]>();
|
||||
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<const type[][2][2]> a1 = boost::make_shared_noinit<const type[][2][2]>(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<const type[2][2][2]> a1 = boost::make_shared_noinit<const type[2][2][2]>();
|
||||
BOOST_TEST(a1.get() != 0);
|
||||
BOOST_TEST(a1.use_count() == 1);
|
||||
BOOST_TEST(type::instances == 8);
|
||||
}
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
Reference in New Issue
Block a user