memory 1.3 KB

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