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