| 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/smart_ptr/detail/requires_cxx11.hpp> |
| 12 | #include <boost/type_traits/enable_if.hpp> |
| 13 | #include <boost/type_traits/is_array.hpp> |
| 14 | #include <boost/type_traits/is_unbounded_array.hpp> |
| 15 | #include <boost/type_traits/remove_extent.hpp> |
| 16 | #include <boost/type_traits/remove_reference.hpp> |
| 17 | #include <memory> |
| 18 | #include <utility> |
| 19 | |
| 20 | namespace boost { |
| 21 | |
| 22 | template<class T> |
| 23 | inline typename enable_if_<!is_array<T>::value, std::unique_ptr<T> >::type |
| 24 | make_unique() |
| 25 | { |
| 26 | return std::unique_ptr<T>(new T()); |
| 27 | } |
| 28 | |
| 29 | #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) |
| 30 | template<class T, class... Args> |
| 31 | inline typename enable_if_<!is_array<T>::value, std::unique_ptr<T> >::type |
| 32 | make_unique(Args&&... args) |
| 33 | { |
| 34 | return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); |
| 35 | } |
| 36 | #endif |
| 37 | |
| 38 | template<class T> |
| 39 | inline typename enable_if_<!is_array<T>::value, std::unique_ptr<T> >::type |
| 40 | make_unique(typename remove_reference<T>::type&& value) |
| 41 | { |
| 42 | return std::unique_ptr<T>(new T(std::move(value))); |
| 43 | } |
| 44 | |
| 45 | template<class T> |
| 46 | inline typename enable_if_<!is_array<T>::value, std::unique_ptr<T> >::type |
| 47 | make_unique_noinit() |
| 48 | { |
| 49 | return std::unique_ptr<T>(new T); |
| 50 | } |
| 51 | |
| 52 | template<class T> |
| 53 | inline typename enable_if_<is_unbounded_array<T>::value, |
| 54 | std::unique_ptr<T> >::type |
| 55 | make_unique(std::size_t size) |
| 56 | { |
| 57 | return std::unique_ptr<T>(new typename remove_extent<T>::type[size]()); |
| 58 | } |
| 59 | |
| 60 | template<class T> |
| 61 | inline typename enable_if_<is_unbounded_array<T>::value, |
| 62 | std::unique_ptr<T> >::type |
| 63 | make_unique_noinit(std::size_t size) |
| 64 | { |
| 65 | return std::unique_ptr<T>(new typename remove_extent<T>::type[size]); |
| 66 | } |
| 67 | |
| 68 | } /* boost */ |
| 69 | |
| 70 | #endif |
| 71 |
