construct_at.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___MEMORY_CONSTRUCT_AT_H
  10. #define _LIBCPP___MEMORY_CONSTRUCT_AT_H
  11. #include <__assert>
  12. #include <__config>
  13. #include <__iterator/access.h>
  14. #include <__memory/addressof.h>
  15. #include <__memory/voidify.h>
  16. #include <__type_traits/enable_if.h>
  17. #include <__type_traits/is_array.h>
  18. #include <__utility/declval.h>
  19. #include <__utility/forward.h>
  20. #include <__utility/move.h>
  21. #include <new>
  22. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  23. # pragma GCC system_header
  24. #endif
  25. _LIBCPP_PUSH_MACROS
  26. #include <__undef_macros>
  27. _LIBCPP_BEGIN_NAMESPACE_STD
  28. // construct_at
  29. #if _LIBCPP_STD_VER >= 20
  30. template <class _Tp, class... _Args, class = decltype(::new(std::declval<void*>()) _Tp(std::declval<_Args>()...))>
  31. _LIBCPP_HIDE_FROM_ABI constexpr _Tp* construct_at(_Tp* __location, _Args&&... __args) {
  32. _LIBCPP_ASSERT_NON_NULL(__location != nullptr, "null pointer given to construct_at");
  33. return ::new (std::__voidify(*__location)) _Tp(std::forward<_Args>(__args)...);
  34. }
  35. #endif
  36. template <class _Tp, class... _Args, class = decltype(::new(std::declval<void*>()) _Tp(std::declval<_Args>()...))>
  37. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp* __construct_at(_Tp* __location, _Args&&... __args) {
  38. #if _LIBCPP_STD_VER >= 20
  39. return std::construct_at(__location, std::forward<_Args>(__args)...);
  40. #else
  41. return _LIBCPP_ASSERT_NON_NULL(__location != nullptr, "null pointer given to construct_at"),
  42. ::new (std::__voidify(*__location)) _Tp(std::forward<_Args>(__args)...);
  43. #endif
  44. }
  45. // destroy_at
  46. // The internal functions are available regardless of the language version (with the exception of the `__destroy_at`
  47. // taking an array).
  48. template <class _ForwardIterator>
  49. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator __destroy(_ForwardIterator, _ForwardIterator);
  50. template <class _Tp, __enable_if_t<!is_array<_Tp>::value, int> = 0>
  51. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy_at(_Tp* __loc) {
  52. _LIBCPP_ASSERT_NON_NULL(__loc != nullptr, "null pointer given to destroy_at");
  53. __loc->~_Tp();
  54. }
  55. #if _LIBCPP_STD_VER >= 20
  56. template <class _Tp, __enable_if_t<is_array<_Tp>::value, int> = 0>
  57. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy_at(_Tp* __loc) {
  58. _LIBCPP_ASSERT_NON_NULL(__loc != nullptr, "null pointer given to destroy_at");
  59. std::__destroy(std::begin(*__loc), std::end(*__loc));
  60. }
  61. #endif
  62. template <class _ForwardIterator>
  63. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
  64. __destroy(_ForwardIterator __first, _ForwardIterator __last) {
  65. for (; __first != __last; ++__first)
  66. std::__destroy_at(std::addressof(*__first));
  67. return __first;
  68. }
  69. template <class _BidirectionalIterator>
  70. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _BidirectionalIterator
  71. __reverse_destroy(_BidirectionalIterator __first, _BidirectionalIterator __last) {
  72. while (__last != __first) {
  73. --__last;
  74. std::__destroy_at(std::addressof(*__last));
  75. }
  76. return __last;
  77. }
  78. #if _LIBCPP_STD_VER >= 17
  79. template <class _Tp, enable_if_t<!is_array_v<_Tp>, int> = 0>
  80. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void destroy_at(_Tp* __loc) {
  81. std::__destroy_at(__loc);
  82. }
  83. # if _LIBCPP_STD_VER >= 20
  84. template <class _Tp, enable_if_t<is_array_v<_Tp>, int> = 0>
  85. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void destroy_at(_Tp* __loc) {
  86. std::__destroy_at(__loc);
  87. }
  88. # endif
  89. template <class _ForwardIterator>
  90. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void destroy(_ForwardIterator __first, _ForwardIterator __last) {
  91. (void)std::__destroy(std::move(__first), std::move(__last));
  92. }
  93. template <class _ForwardIterator, class _Size>
  94. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
  95. for (; __n > 0; (void)++__first, --__n)
  96. std::__destroy_at(std::addressof(*__first));
  97. return __first;
  98. }
  99. #endif // _LIBCPP_STD_VER >= 17
  100. _LIBCPP_END_NAMESPACE_STD
  101. _LIBCPP_POP_MACROS
  102. #endif // _LIBCPP___MEMORY_CONSTRUCT_AT_H