1 /*
2 Copyright 2012-2019 Glen Joseph Fernandes
3 (glenjofe@gmail.com)
4 
5 Distributed under the Boost Software License, Version 1.0.
6 (http://www.boost.org/LICENSE_1_0.txt)
7 */
8 #ifndef BOOST_SMART_PTR_MAKE_UNIQUE_HPP
9 #define BOOST_SMART_PTR_MAKE_UNIQUE_HPP
10 
11 #include <boost/type_traits/enable_if.hpp>
12 #include <boost/type_traits/is_array.hpp>
13 #include <boost/type_traits/is_unbounded_array.hpp>
14 #include <boost/type_traits/remove_extent.hpp>
15 #include <boost/type_traits/remove_reference.hpp>
16 #include <memory>
17 #include <utility>
18 
19 namespace boost {
20 
21 template<class T>
22 inline typename enable_if_<!is_array<T>::value, std::unique_ptr<T> >::type
make_unique()23 make_unique()
24 {
25     return std::unique_ptr<T>(new T());
26 }
27 
28 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
29 template<class T, class... Args>
30 inline typename enable_if_<!is_array<T>::value, std::unique_ptr<T> >::type
make_unique(Args &&...args)31 make_unique(Args&&... args)
32 {
33     return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
34 }
35 #endif
36 
37 template<class T>
38 inline typename enable_if_<!is_array<T>::value, std::unique_ptr<T> >::type
make_unique(typename remove_reference<T>::type && value)39 make_unique(typename remove_reference<T>::type&& value)
40 {
41     return std::unique_ptr<T>(new T(std::move(value)));
42 }
43 
44 template<class T>
45 inline typename enable_if_<!is_array<T>::value, std::unique_ptr<T> >::type
make_unique_noinit()46 make_unique_noinit()
47 {
48     return std::unique_ptr<T>(new T);
49 }
50 
51 template<class T>
52 inline typename enable_if_<is_unbounded_array<T>::value,
53     std::unique_ptr<T> >::type
make_unique(std::size_t size)54 make_unique(std::size_t size)
55 {
56     return std::unique_ptr<T>(new typename remove_extent<T>::type[size]());
57 }
58 
59 template<class T>
60 inline typename enable_if_<is_unbounded_array<T>::value,
61     std::unique_ptr<T> >::type
make_unique_noinit(std::size_t size)62 make_unique_noinit(std::size_t size)
63 {
64     return std::unique_ptr<T>(new typename remove_extent<T>::type[size]);
65 }
66 
67 } /* boost */
68 
69 #endif
70