1// -*-c++-*-
2// vim: set ft=cpp:
3
4/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
5   file Copyright.txt 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
18namespace cm {
19
20#if defined(CMake_HAVE_CXX_MAKE_UNIQUE)
21
22using std::make_unique;
23
24#else
25
26namespace internals {
27
28template <typename T>
29struct make_unique_if
30{
31  using single = std::unique_ptr<T>;
32};
33
34template <typename T>
35struct make_unique_if<T[]>
36{
37  using unbound_array = std::unique_ptr<T[]>;
38};
39
40template <typename T, std::size_t N>
41struct make_unique_if<T[N]>
42{
43  using bound_array = void;
44};
45}
46
47template <typename T, typename... Args>
48typename 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
53template <typename T>
54typename 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
61template <typename T, typename... Args>
62typename internals::make_unique_if<T>::bound_array make_unique(Args&&...) =
63  delete;
64
65#endif
66
67} // namespace cm
68