construct_at.h 3.3 KB

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