construct_at.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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_UNCATEGORIZED(__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_UNCATEGORIZED(__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
  50. _ForwardIterator __destroy(_ForwardIterator, _ForwardIterator);
  51. template <class _Tp, __enable_if_t<!is_array<_Tp>::value, int> = 0>
  52. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  53. void __destroy_at(_Tp* __loc) {
  54. _LIBCPP_ASSERT_UNCATEGORIZED(__loc != nullptr, "null pointer given to destroy_at");
  55. __loc->~_Tp();
  56. }
  57. #if _LIBCPP_STD_VER >= 20
  58. template <class _Tp, __enable_if_t<is_array<_Tp>::value, int> = 0>
  59. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  60. void __destroy_at(_Tp* __loc) {
  61. _LIBCPP_ASSERT_UNCATEGORIZED(__loc != nullptr, "null pointer given to destroy_at");
  62. std::__destroy(std::begin(*__loc), std::end(*__loc));
  63. }
  64. #endif
  65. template <class _ForwardIterator>
  66. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  67. _ForwardIterator __destroy(_ForwardIterator __first, _ForwardIterator __last) {
  68. for (; __first != __last; ++__first)
  69. std::__destroy_at(std::addressof(*__first));
  70. return __first;
  71. }
  72. template <class _BidirectionalIterator>
  73. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  74. _BidirectionalIterator __reverse_destroy(_BidirectionalIterator __first, _BidirectionalIterator __last) {
  75. while (__last != __first) {
  76. --__last;
  77. std::__destroy_at(std::addressof(*__last));
  78. }
  79. return __last;
  80. }
  81. #if _LIBCPP_STD_VER >= 17
  82. template <class _Tp, enable_if_t<!is_array_v<_Tp>, int> = 0>
  83. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  84. void destroy_at(_Tp* __loc) {
  85. std::__destroy_at(__loc);
  86. }
  87. #if _LIBCPP_STD_VER >= 20
  88. template <class _Tp, enable_if_t<is_array_v<_Tp>, int> = 0>
  89. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  90. void destroy_at(_Tp* __loc) {
  91. std::__destroy_at(__loc);
  92. }
  93. #endif
  94. template <class _ForwardIterator>
  95. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  96. void destroy(_ForwardIterator __first, _ForwardIterator __last) {
  97. (void)std::__destroy(std::move(__first), std::move(__last));
  98. }
  99. template <class _ForwardIterator, class _Size>
  100. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  101. _ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
  102. for (; __n > 0; (void)++__first, --__n)
  103. std::__destroy_at(std::addressof(*__first));
  104. return __first;
  105. }
  106. #endif // _LIBCPP_STD_VER >= 17
  107. _LIBCPP_END_NAMESPACE_STD
  108. _LIBCPP_POP_MACROS
  109. #endif // _LIBCPP___MEMORY_CONSTRUCT_AT_H