Refactor make_unique implementations

Before the pending refactor of make_shared/allocate_shared for arrays.
This commit is contained in:
Glen Fernandes
2015-11-08 00:38:22 -05:00
parent 3f17244225
commit 7af503d3bb
14 changed files with 202 additions and 229 deletions

View File

@ -1,11 +1,11 @@
/* /*
* Copyright (c) 2014 Glen Joseph Fernandes (c) 2014 Glen Joseph Fernandes
* glenfe at live dot com <glenjofe -at- gmail.com>
*
* Distributed under the Boost Software License, Distributed under the Boost Software
* Version 1.0. (See accompanying file LICENSE_1_0.txt License, Version 1.0.
* or copy at http://boost.org/LICENSE_1_0.txt) http://boost.org/LICENSE_1_0.txt
*/ */
#ifndef BOOST_MAKE_UNIQUE_HPP_INCLUDED #ifndef BOOST_MAKE_UNIQUE_HPP_INCLUDED
#define BOOST_MAKE_UNIQUE_HPP_INCLUDED #define BOOST_MAKE_UNIQUE_HPP_INCLUDED

View File

@ -1,26 +0,0 @@
/*
* Copyright (c) 2014 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_UP_IF_ARRAY_HPP
#define BOOST_SMART_PTR_DETAIL_UP_IF_ARRAY_HPP
#include <memory>
namespace boost {
namespace detail {
template<class T>
struct up_if_array;
template<class T>
struct up_if_array<T[]> {
typedef std::unique_ptr<T[]> type;
};
}
}
#endif

View File

@ -1,31 +0,0 @@
/*
* Copyright (c) 2014 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_UP_IF_NOT_ARRAY_HPP
#define BOOST_SMART_PTR_DETAIL_UP_IF_NOT_ARRAY_HPP
#include <memory>
namespace boost {
namespace detail {
template<class T>
struct up_if_not_array {
typedef std::unique_ptr<T> type;
};
template<class T>
struct up_if_not_array<T[]> {
};
template<class T, std::size_t N>
struct up_if_not_array<T[N]> {
};
}
}
#endif

View File

@ -1,15 +1,105 @@
/* /*
* Copyright (c) 2014 Glen Joseph Fernandes (c) 2014-2015 Glen Joseph Fernandes
* glenfe at live dot com <glenjofe -at- gmail.com>
*
* Distributed under the Boost Software License, Distributed under the Boost Software
* Version 1.0. (See accompanying file LICENSE_1_0.txt License, Version 1.0.
* or copy at http://boost.org/LICENSE_1_0.txt) http://boost.org/LICENSE_1_0.txt
*/ */
#ifndef BOOST_SMART_PTR_MAKE_UNIQUE_HPP #ifndef BOOST_SMART_PTR_MAKE_UNIQUE_HPP
#define BOOST_SMART_PTR_MAKE_UNIQUE_HPP #define BOOST_SMART_PTR_MAKE_UNIQUE_HPP
#include <boost/smart_ptr/make_unique_array.hpp> #include <boost/config.hpp>
#include <boost/smart_ptr/make_unique_object.hpp> #include <memory>
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
#include <utility>
#endif
namespace boost {
namespace detail {
template<class T>
struct up_if_object {
typedef std::unique_ptr<T> type;
};
template<class T>
struct up_if_object<T[]> {
};
template<class T, std::size_t N>
struct up_if_object<T[N]> {
};
template<class T>
struct up_if_array {
};
template<class T>
struct up_if_array<T[]> {
typedef std::unique_ptr<T[]> type;
};
template<class T>
struct up_value {
typedef T&& type;
};
template<class T>
struct up_element {
};
template<class T>
struct up_element<T[]> {
typedef T type;
};
} /* detail */
template<class T>
inline typename detail::up_if_object<T>::type
make_unique()
{
return std::unique_ptr<T>(new T());
}
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
template<class T, class... Args>
inline typename detail::up_if_object<T>::type
make_unique(Args&&... args)
{
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
#endif
template<class T>
inline typename detail::up_if_object<T>::type
make_unique(typename detail::up_value<T>::type value)
{
return std::unique_ptr<T>(new T(std::move(value)));
}
template<class T>
inline typename detail::up_if_object<T>::type
make_unique_noinit()
{
return std::unique_ptr<T>(new T);
}
template<class T>
inline typename detail::up_if_array<T>::type
make_unique(std::size_t n)
{
return std::unique_ptr<T>(new
typename detail::up_element<T>::type[n]());
}
template<class T>
inline typename detail::up_if_array<T>::type
make_unique_noinit(std::size_t n)
{
return std::unique_ptr<T>(new
typename detail::up_element<T>::type[n]);
}
} /* boost */
#endif #endif

View File

@ -1,31 +0,0 @@
/*
* Copyright (c) 2014 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_MAKE_UNIQUE_ARRAY_HPP
#define BOOST_SMART_PTR_MAKE_UNIQUE_ARRAY_HPP
#include <boost/smart_ptr/detail/up_if_array.hpp>
#include <boost/smart_ptr/detail/array_traits.hpp>
namespace boost {
template<class T>
inline typename boost::detail::up_if_array<T>::type
make_unique(std::size_t size) {
typedef typename boost::detail::array_inner<T>::type U;
return std::unique_ptr<T>(new U[size]());
}
template<class T>
inline typename boost::detail::up_if_array<T>::type
make_unique_noinit(std::size_t size) {
typedef typename boost::detail::array_inner<T>::type U;
return std::unique_ptr<T>(new U[size]);
}
}
#endif

View File

@ -1,45 +0,0 @@
/*
* Copyright (c) 2014 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_MAKE_UNIQUE_OBJECT_HPP
#define BOOST_SMART_PTR_MAKE_UNIQUE_OBJECT_HPP
#include <boost/config.hpp>
#include <boost/smart_ptr/detail/up_if_not_array.hpp>
#include <boost/type_traits/add_rvalue_reference.hpp>
#include <utility>
namespace boost {
template<class T>
inline typename boost::detail::up_if_not_array<T>::type
make_unique() {
return std::unique_ptr<T>(new T());
}
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
template<class T, class... Args>
inline typename boost::detail::up_if_not_array<T>::type
make_unique(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
#endif
template<class T>
inline typename boost::detail::up_if_not_array<T>::type
make_unique(typename add_rvalue_reference<T>::type value) {
return std::unique_ptr<T>(new T(std::move(value)));
}
template<class T>
inline typename boost::detail::up_if_not_array<T>::type
make_unique_noinit() {
return std::unique_ptr<T>(new T);
}
}
#endif

View File

@ -1,15 +1,15 @@
/* /*
* Copyright (c) 2014 Glen Joseph Fernandes (c) 2014 Glen Joseph Fernandes
* glenfe at live dot com <glenjofe -at- gmail.com>
*
* Distributed under the Boost Software License, Distributed under the Boost Software
* Version 1.0. (See accompanying file LICENSE_1_0.txt License, Version 1.0.
* or copy at http://boost.org/LICENSE_1_0.txt) http://boost.org/LICENSE_1_0.txt
*/ */
#include <boost/config.hpp> #include <boost/config.hpp>
#if !defined(BOOST_NO_CXX11_SMART_PTR) #if !defined(BOOST_NO_CXX11_SMART_PTR)
#include <boost/detail/lightweight_test.hpp> #include <boost/detail/lightweight_test.hpp>
#include <boost/smart_ptr/make_unique_object.hpp> #include <boost/smart_ptr/make_unique.hpp>
class type { class type {
public: public:
@ -41,7 +41,8 @@ private:
unsigned int type::instances = 0; unsigned int type::instances = 0;
int main() { int main()
{
BOOST_TEST(type::instances == 0); BOOST_TEST(type::instances == 0);
{ {
std::unique_ptr<type> a1 = boost::make_unique<type>(); std::unique_ptr<type> a1 = boost::make_unique<type>();
@ -139,7 +140,8 @@ int main() {
} }
#else #else
int main() { int main()
{
return 0; return 0;
} }

View File

@ -1,15 +1,15 @@
/* /*
* Copyright (c) 2012-2014 Glen Joseph Fernandes (c) 2014 Glen Joseph Fernandes
* glenfe at live dot com <glenjofe -at- gmail.com>
*
* Distributed under the Boost Software License, Distributed under the Boost Software
* Version 1.0. (See accompanying file LICENSE_1_0.txt License, Version 1.0.
* or copy at http://boost.org/LICENSE_1_0.txt) http://boost.org/LICENSE_1_0.txt
*/ */
#include <boost/config.hpp> #include <boost/config.hpp>
#if !defined(BOOST_NO_CXX11_SMART_PTR) #if !defined(BOOST_NO_CXX11_SMART_PTR)
#include <boost/detail/lightweight_test.hpp> #include <boost/detail/lightweight_test.hpp>
#include <boost/smart_ptr/make_unique_array.hpp> #include <boost/smart_ptr/make_unique.hpp>
class type { class type {
public: public:
@ -30,7 +30,8 @@ private:
unsigned int type::instances = 0; unsigned int type::instances = 0;
int main() { int main()
{
{ {
std::unique_ptr<int[]> a1 = boost::make_unique_noinit<int[]>(3); std::unique_ptr<int[]> a1 = boost::make_unique_noinit<int[]>(3);
BOOST_TEST(a1.get() != 0); BOOST_TEST(a1.get() != 0);
@ -81,7 +82,8 @@ int main() {
} }
#else #else
int main() { int main()
{
return 0; return 0;
} }

View File

@ -1,15 +1,15 @@
/* /*
* Copyright (c) 2014 Glen Joseph Fernandes (c) 2014 Glen Joseph Fernandes
* glenfe at live dot com <glenjofe -at- gmail.com>
*
* Distributed under the Boost Software License, Distributed under the Boost Software
* Version 1.0. (See accompanying file LICENSE_1_0.txt License, Version 1.0.
* or copy at http://boost.org/LICENSE_1_0.txt) http://boost.org/LICENSE_1_0.txt
*/ */
#include <boost/config.hpp> #include <boost/config.hpp>
#if !defined(BOOST_NO_CXX11_SMART_PTR) #if !defined(BOOST_NO_CXX11_SMART_PTR)
#include <boost/detail/lightweight_test.hpp> #include <boost/detail/lightweight_test.hpp>
#include <boost/smart_ptr/make_unique_array.hpp> #include <boost/smart_ptr/make_unique.hpp>
class type { class type {
public: public:
@ -30,7 +30,8 @@ private:
unsigned int type::instances = 0; unsigned int type::instances = 0;
int main() { int main()
{
{ {
std::unique_ptr<int[]> a1 = boost::make_unique<int[]>(3); std::unique_ptr<int[]> a1 = boost::make_unique<int[]>(3);
BOOST_TEST(a1.get() != 0); BOOST_TEST(a1.get() != 0);
@ -105,7 +106,8 @@ int main() {
} }
#else #else
int main() { int main()
{
return 0; return 0;
} }

View File

@ -1,15 +1,15 @@
/* /*
* Copyright (c) 2014 Glen Joseph Fernandes (c) 2014 Glen Joseph Fernandes
* glenfe at live dot com <glenjofe -at- gmail.com>
*
* Distributed under the Boost Software License, Distributed under the Boost Software
* Version 1.0. (See accompanying file LICENSE_1_0.txt License, Version 1.0.
* or copy at http://boost.org/LICENSE_1_0.txt) http://boost.org/LICENSE_1_0.txt
*/ */
#include <boost/config.hpp> #include <boost/config.hpp>
#if !defined(BOOST_NO_CXX11_SMART_PTR) #if !defined(BOOST_NO_CXX11_SMART_PTR)
#include <boost/detail/lightweight_test.hpp> #include <boost/detail/lightweight_test.hpp>
#include <boost/smart_ptr/make_unique_array.hpp> #include <boost/smart_ptr/make_unique.hpp>
class type { class type {
public: public:
@ -33,7 +33,8 @@ private:
unsigned int type::instances = 0; unsigned int type::instances = 0;
int main() { int main()
{
BOOST_TEST(type::instances == 0); BOOST_TEST(type::instances == 0);
try { try {
boost::make_unique<type[]>(6); boost::make_unique<type[]>(6);
@ -70,7 +71,8 @@ int main() {
} }
#else #else
int main() { int main()
{
return 0; return 0;
} }

View File

@ -1,15 +1,15 @@
/* /*
* Copyright (c) 2012-2014 Glen Joseph Fernandes (c) 2014 Glen Joseph Fernandes
* glenfe at live dot com <glenjofe -at- gmail.com>
*
* Distributed under the Boost Software License, Distributed under the Boost Software
* Version 1.0. (See accompanying file LICENSE_1_0.txt License, Version 1.0.
* or copy at http://boost.org/LICENSE_1_0.txt) http://boost.org/LICENSE_1_0.txt
*/ */
#include <boost/config.hpp> #include <boost/config.hpp>
#if !defined(BOOST_NO_CXX11_SMART_PTR) #if !defined(BOOST_NO_CXX11_SMART_PTR)
#include <boost/detail/lightweight_test.hpp> #include <boost/detail/lightweight_test.hpp>
#include <boost/smart_ptr/make_unique_object.hpp> #include <boost/smart_ptr/make_unique.hpp>
class type { class type {
public: public:
@ -30,7 +30,8 @@ private:
unsigned int type::instances = 0; unsigned int type::instances = 0;
int main() { int main()
{
{ {
std::unique_ptr<int> a1 = boost::make_unique_noinit<int>(); std::unique_ptr<int> a1 = boost::make_unique_noinit<int>();
BOOST_TEST(a1.get() != 0); BOOST_TEST(a1.get() != 0);
@ -58,7 +59,8 @@ int main() {
} }
#else #else
int main() { int main()
{
return 0; return 0;
} }

View File

@ -1,15 +1,15 @@
/* /*
* Copyright (c) 2014 Glen Joseph Fernandes (c) 2014 Glen Joseph Fernandes
* glenfe at live dot com <glenjofe -at- gmail.com>
*
* Distributed under the Boost Software License, Distributed under the Boost Software
* Version 1.0. (See accompanying file LICENSE_1_0.txt License, Version 1.0.
* or copy at http://boost.org/LICENSE_1_0.txt) http://boost.org/LICENSE_1_0.txt
*/ */
#include <boost/config.hpp> #include <boost/config.hpp>
#if !defined(BOOST_NO_CXX11_SMART_PTR) #if !defined(BOOST_NO_CXX11_SMART_PTR)
#include <boost/detail/lightweight_test.hpp> #include <boost/detail/lightweight_test.hpp>
#include <boost/smart_ptr/make_unique_object.hpp> #include <boost/smart_ptr/make_unique.hpp>
class type { class type {
public: public:
@ -30,7 +30,8 @@ private:
unsigned int type::instances = 0; unsigned int type::instances = 0;
int main() { int main()
{
{ {
std::unique_ptr<int> a1 = boost::make_unique<int>(); std::unique_ptr<int> a1 = boost::make_unique<int>();
BOOST_TEST(a1.get() != 0); BOOST_TEST(a1.get() != 0);
@ -65,7 +66,8 @@ int main() {
} }
#else #else
int main() { int main()
{
return 0; return 0;
} }

View File

@ -1,15 +1,15 @@
/* /*
* Copyright (c) 2014 Glen Joseph Fernandes (c) 2014 Glen Joseph Fernandes
* glenfe at live dot com <glenjofe -at- gmail.com>
*
* Distributed under the Boost Software License, Distributed under the Boost Software
* Version 1.0. (See accompanying file LICENSE_1_0.txt License, Version 1.0.
* or copy at http://boost.org/LICENSE_1_0.txt) http://boost.org/LICENSE_1_0.txt
*/ */
#include <boost/config.hpp> #include <boost/config.hpp>
#if !defined(BOOST_NO_CXX11_SMART_PTR) #if !defined(BOOST_NO_CXX11_SMART_PTR)
#include <boost/detail/lightweight_test.hpp> #include <boost/detail/lightweight_test.hpp>
#include <boost/smart_ptr/make_unique_object.hpp> #include <boost/smart_ptr/make_unique.hpp>
class type { class type {
public: public:
@ -33,7 +33,8 @@ private:
unsigned int type::instances = 0; unsigned int type::instances = 0;
int main() { int main()
{
BOOST_TEST(type::instances == 0); BOOST_TEST(type::instances == 0);
try { try {
boost::make_unique<type>(); boost::make_unique<type>();
@ -46,7 +47,8 @@ int main() {
} }
#else #else
int main() { int main()
{
return 0; return 0;
} }

View File

@ -1,22 +1,23 @@
/* /*
* Copyright (c) 2014 Glen Joseph Fernandes (c) 2014 Glen Joseph Fernandes
* glenfe at live dot com <glenjofe -at- gmail.com>
*
* Distributed under the Boost Software License, Distributed under the Boost Software
* Version 1.0. (See accompanying file LICENSE_1_0.txt License, Version 1.0.
* or copy at http://boost.org/LICENSE_1_0.txt) http://boost.org/LICENSE_1_0.txt
*/ */
#include <boost/config.hpp> #include <boost/config.hpp>
#if !defined(BOOST_NO_CXX11_SMART_PTR) #if !defined(BOOST_NO_CXX11_SMART_PTR)
#include <boost/detail/lightweight_test.hpp> #include <boost/detail/lightweight_test.hpp>
#include <boost/smart_ptr/make_unique_object.hpp> #include <boost/smart_ptr/make_unique.hpp>
struct type { struct type {
int x; int x;
int y; int y;
}; };
int main() { int main()
{
{ {
std::unique_ptr<type> a1 = boost::make_unique<type>(); std::unique_ptr<type> a1 = boost::make_unique<type>();
BOOST_TEST(a1.get() != 0); BOOST_TEST(a1.get() != 0);
@ -51,7 +52,8 @@ int main() {
} }
#else #else
int main() { int main()
{
return 0; return 0;
} }