68 lines
1.3 KiB
Plaintext
Raw Normal View History

2020-02-01 23:06:01 +01:00
// -*-c++-*-
// vim: set ft=cpp:
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2021-09-14 00:13:48 +02:00
#pragma once
#include "cmSTL.hxx" // IWYU pragma: keep
2020-02-01 23:06:01 +01:00
#include <memory> // IWYU pragma: export
2021-09-14 00:13:48 +02:00
2020-02-01 23:06:01 +01:00
#if !defined(CMake_HAVE_CXX_MAKE_UNIQUE)
2020-08-30 11:54:41 +02:00
# include <cstddef>
# include <type_traits>
2020-02-01 23:06:01 +01:00
# include <utility>
#endif
namespace cm {
#if defined(CMake_HAVE_CXX_MAKE_UNIQUE)
using std::make_unique;
#else
2020-08-30 11:54:41 +02:00
namespace internals {
template <typename T>
struct make_unique_if
{
using single = std::unique_ptr<T>;
};
template <typename T>
struct make_unique_if<T[]>
{
using unbound_array = std::unique_ptr<T[]>;
};
template <typename T, std::size_t N>
struct make_unique_if<T[N]>
{
using bound_array = void;
};
}
2020-02-01 23:06:01 +01:00
template <typename T, typename... Args>
2020-08-30 11:54:41 +02:00
typename internals::make_unique_if<T>::single make_unique(Args&&... args)
2020-02-01 23:06:01 +01:00
{
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
2020-08-30 11:54:41 +02:00
template <typename T>
typename internals::make_unique_if<T>::unbound_array make_unique(std::size_t n)
{
using E = typename std::remove_extent<T>::type;
return std::unique_ptr<T>(new E[n]());
}
template <typename T, typename... Args>
typename internals::make_unique_if<T>::bound_array make_unique(Args&&...) =
delete;
2020-02-01 23:06:01 +01:00
#endif
} // namespace cm