memory 1.3 KB

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