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 <__config> 14 #include <__debug> 15 #include <__iterator/access.h> 16 #include <__memory/addressof.h> 17 #include <__utility/forward.h> 18 #include <type_traits> 19 #include <utility> 20 21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 22 #pragma GCC system_header 23 #endif 24 25 _LIBCPP_BEGIN_NAMESPACE_STD 26 27 // construct_at 28 29 #if _LIBCPP_STD_VER > 17 30 31 template<class _Tp, class ..._Args, class = decltype( 32 ::new (declval<void*>()) _Tp(declval<_Args>()...) 33 )> 34 _LIBCPP_INLINE_VISIBILITY 35 constexpr _Tp* construct_at(_Tp* __location, _Args&& ...__args) { 36 _LIBCPP_ASSERT(__location, "null pointer given to construct_at"); 37 return ::new ((void*)__location) _Tp(_VSTD::forward<_Args>(__args)...); 38 } 39 40 #endif 41 42 // destroy_at 43 44 #if _LIBCPP_STD_VER > 14 45 46 template <class _ForwardIterator> 47 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 48 void destroy(_ForwardIterator, _ForwardIterator); 49 50 template <class _Tp, enable_if_t<!is_array_v<_Tp>, int> = 0> 51 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 52 void destroy_at(_Tp* __loc) { 53 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at"); 54 __loc->~_Tp(); 55 } 56 57 #if _LIBCPP_STD_VER > 17 58 template <class _Tp, enable_if_t<is_array_v<_Tp>, int> = 0> 59 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 60 void destroy_at(_Tp* __loc) { 61 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at"); 62 _VSTD::destroy(_VSTD::begin(*__loc), _VSTD::end(*__loc)); 63 } 64 #endif 65 66 template <class _ForwardIterator> 67 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 68 void destroy(_ForwardIterator __first, _ForwardIterator __last) { 69 for (; __first != __last; ++__first) 70 _VSTD::destroy_at(_VSTD::addressof(*__first)); 71 } 72 73 template <class _ForwardIterator, class _Size> 74 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 75 _ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) { 76 for (; __n > 0; (void)++__first, --__n) 77 _VSTD::destroy_at(_VSTD::addressof(*__first)); 78 return __first; 79 } 80 81 #endif 82 83 _LIBCPP_END_NAMESPACE_STD 84 85 #endif // _LIBCPP___MEMORY_CONSTRUCT_AT_H 86