construct_at.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 <__utility/forward.h>
  17. #include <__utility/move.h>
  18. #include <type_traits>
  19. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  20. # pragma GCC system_header
  21. #endif
  22. _LIBCPP_BEGIN_NAMESPACE_STD
  23. // construct_at
  24. #if _LIBCPP_STD_VER > 17
  25. template <class _Tp, class... _Args, class = decltype(::new(declval<void*>()) _Tp(declval<_Args>()...))>
  26. _LIBCPP_HIDE_FROM_ABI constexpr _Tp* construct_at(_Tp* __location, _Args&&... __args) {
  27. _LIBCPP_ASSERT(__location != nullptr, "null pointer given to construct_at");
  28. return ::new (_VSTD::__voidify(*__location)) _Tp(_VSTD::forward<_Args>(__args)...);
  29. }
  30. #endif
  31. template <class _Tp, class... _Args, class = decltype(::new(declval<void*>()) _Tp(declval<_Args>()...))>
  32. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp* __construct_at(_Tp* __location, _Args&&... __args) {
  33. #if _LIBCPP_STD_VER > 17
  34. return std::construct_at(__location, std::forward<_Args>(__args)...);
  35. #else
  36. return ::new (std::__voidify(*__location)) _Tp(std::forward<_Args>(__args)...);
  37. #endif
  38. }
  39. // destroy_at
  40. // The internal functions are available regardless of the language version (with the exception of the `__destroy_at`
  41. // taking an array).
  42. template <class _ForwardIterator>
  43. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
  44. _ForwardIterator __destroy(_ForwardIterator, _ForwardIterator);
  45. template <class _Tp, typename enable_if<!is_array<_Tp>::value, int>::type = 0>
  46. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
  47. void __destroy_at(_Tp* __loc) {
  48. _LIBCPP_ASSERT(__loc != nullptr, "null pointer given to destroy_at");
  49. __loc->~_Tp();
  50. }
  51. #if _LIBCPP_STD_VER > 17
  52. template <class _Tp, typename enable_if<is_array<_Tp>::value, int>::type = 0>
  53. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
  54. void __destroy_at(_Tp* __loc) {
  55. _LIBCPP_ASSERT(__loc != nullptr, "null pointer given to destroy_at");
  56. _VSTD::__destroy(_VSTD::begin(*__loc), _VSTD::end(*__loc));
  57. }
  58. #endif
  59. template <class _ForwardIterator>
  60. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
  61. _ForwardIterator __destroy(_ForwardIterator __first, _ForwardIterator __last) {
  62. for (; __first != __last; ++__first)
  63. _VSTD::__destroy_at(_VSTD::addressof(*__first));
  64. return __first;
  65. }
  66. #if _LIBCPP_STD_VER > 14
  67. template <class _Tp, enable_if_t<!is_array_v<_Tp>, int> = 0>
  68. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
  69. void destroy_at(_Tp* __loc) {
  70. _VSTD::__destroy_at(__loc);
  71. }
  72. #if _LIBCPP_STD_VER > 17
  73. template <class _Tp, enable_if_t<is_array_v<_Tp>, int> = 0>
  74. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
  75. void destroy_at(_Tp* __loc) {
  76. _VSTD::__destroy_at(__loc);
  77. }
  78. #endif
  79. template <class _ForwardIterator>
  80. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
  81. void destroy(_ForwardIterator __first, _ForwardIterator __last) {
  82. (void)_VSTD::__destroy(_VSTD::move(__first), _VSTD::move(__last));
  83. }
  84. template <class _ForwardIterator, class _Size>
  85. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
  86. _ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
  87. for (; __n > 0; (void)++__first, --__n)
  88. _VSTD::__destroy_at(_VSTD::addressof(*__first));
  89. return __first;
  90. }
  91. #endif // _LIBCPP_STD_VER > 14
  92. _LIBCPP_END_NAMESPACE_STD
  93. #endif // _LIBCPP___MEMORY_CONSTRUCT_AT_H