| 1 |
// -*-c++-*- |
| 2 |
// vim: set ft=cpp: |
| 3 |
|
| 4 |
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
| 5 |
file LICENSE.rst or https://cmake.org/licensing for details. */ |
| 6 |
#pragma once |
| 7 |
|
| 8 |
#include "cmSTL.hxx" // IWYU pragma: keep |
| 9 |
|
| 10 |
#include <memory> // IWYU pragma: export |
| 11 |
|
| 12 |
#if !defined(CMake_HAVE_CXX_MAKE_UNIQUE) |
| 13 |
# include <cstddef> |
| 14 |
# include <type_traits> |
| 15 |
# include <utility> |
| 16 |
#endif |
| 17 |
|
| 18 |
namespace cm { |
| 19 |
|
| 20 |
#if defined(CMake_HAVE_CXX_MAKE_UNIQUE) |
| 21 |
|
| 22 |
using std::make_unique; |
| 23 |
|
| 24 |
#else |
| 25 |
|
| 26 |
namespace internals { |
| 27 |
|
| 28 |
template <typename T> |
| 29 |
struct make_unique_if |
| 30 |
{ |
| 31 |
using single = std::unique_ptr<T>; |
| 32 |
}; |
| 33 |
|
| 34 |
template <typename T> |
| 35 |
struct make_unique_if<T[]> |
| 36 |
{ |
| 37 |
using unbound_array = std::unique_ptr<T[]>; |
| 38 |
}; |
| 39 |
|
| 40 |
template <typename T, std::size_t N> |
| 41 |
struct make_unique_if<T[N]> |
| 42 |
{ |
| 43 |
using bound_array = void; |
| 44 |
}; |
| 45 |
} |
| 46 |
|
| 47 |
template <typename T, typename... Args> |
| 48 |
typename internals::make_unique_if<T>::single make_unique(Args&&... args) |
| 49 |
{ |
| 50 |
return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); |
| 51 |
} |
| 52 |
|
| 53 |
template <typename T> |
| 54 |
typename internals::make_unique_if<T>::unbound_array make_unique(std::size_t n) |
| 55 |
{ |
| 56 |
using E = typename std::remove_extent<T>::type; |
| 57 |
|
| 58 |
return std::unique_ptr<T>(new E[n]()); |
| 59 |
} |
| 60 |
|
| 61 |
template <typename T, typename... Args> |
| 62 |
typename internals::make_unique_if<T>::bound_array make_unique(Args&&...) = |
| 63 |
delete; |
| 64 |
|
| 65 |
#endif |
| 66 |
|
| 67 |
} // namespace cm |