Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:35

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___CXX03___MEMORY_CONSTRUCT_AT_H
0011 #define _LIBCPP___CXX03___MEMORY_CONSTRUCT_AT_H
0012 
0013 #include <__cxx03/__assert>
0014 #include <__cxx03/__config>
0015 #include <__cxx03/__iterator/access.h>
0016 #include <__cxx03/__memory/addressof.h>
0017 #include <__cxx03/__memory/voidify.h>
0018 #include <__cxx03/__type_traits/enable_if.h>
0019 #include <__cxx03/__type_traits/is_array.h>
0020 #include <__cxx03/__utility/declval.h>
0021 #include <__cxx03/__utility/forward.h>
0022 #include <__cxx03/__utility/move.h>
0023 #include <__cxx03/new>
0024 
0025 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0026 #  pragma GCC system_header
0027 #endif
0028 
0029 _LIBCPP_PUSH_MACROS
0030 #include <__cxx03/__undef_macros>
0031 
0032 _LIBCPP_BEGIN_NAMESPACE_STD
0033 
0034 // construct_at
0035 
0036 #if _LIBCPP_STD_VER >= 20
0037 
0038 template <class _Tp, class... _Args, class = decltype(::new(std::declval<void*>()) _Tp(std::declval<_Args>()...))>
0039 _LIBCPP_HIDE_FROM_ABI constexpr _Tp* construct_at(_Tp* __location, _Args&&... __args) {
0040   _LIBCPP_ASSERT_NON_NULL(__location != nullptr, "null pointer given to construct_at");
0041   return ::new (std::__voidify(*__location)) _Tp(std::forward<_Args>(__args)...);
0042 }
0043 
0044 #endif
0045 
0046 template <class _Tp, class... _Args, class = decltype(::new(std::declval<void*>()) _Tp(std::declval<_Args>()...))>
0047 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp* __construct_at(_Tp* __location, _Args&&... __args) {
0048 #if _LIBCPP_STD_VER >= 20
0049   return std::construct_at(__location, std::forward<_Args>(__args)...);
0050 #else
0051   return _LIBCPP_ASSERT_NON_NULL(__location != nullptr, "null pointer given to construct_at"),
0052          ::new (std::__voidify(*__location)) _Tp(std::forward<_Args>(__args)...);
0053 #endif
0054 }
0055 
0056 // destroy_at
0057 
0058 // The internal functions are available regardless of the language version (with the exception of the `__destroy_at`
0059 // taking an array).
0060 
0061 template <class _ForwardIterator>
0062 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator __destroy(_ForwardIterator, _ForwardIterator);
0063 
0064 template <class _Tp, __enable_if_t<!is_array<_Tp>::value, int> = 0>
0065 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy_at(_Tp* __loc) {
0066   _LIBCPP_ASSERT_NON_NULL(__loc != nullptr, "null pointer given to destroy_at");
0067   __loc->~_Tp();
0068 }
0069 
0070 #if _LIBCPP_STD_VER >= 20
0071 template <class _Tp, __enable_if_t<is_array<_Tp>::value, int> = 0>
0072 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy_at(_Tp* __loc) {
0073   _LIBCPP_ASSERT_NON_NULL(__loc != nullptr, "null pointer given to destroy_at");
0074   std::__destroy(std::begin(*__loc), std::end(*__loc));
0075 }
0076 #endif
0077 
0078 template <class _ForwardIterator>
0079 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
0080 __destroy(_ForwardIterator __first, _ForwardIterator __last) {
0081   for (; __first != __last; ++__first)
0082     std::__destroy_at(std::addressof(*__first));
0083   return __first;
0084 }
0085 
0086 template <class _BidirectionalIterator>
0087 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _BidirectionalIterator
0088 __reverse_destroy(_BidirectionalIterator __first, _BidirectionalIterator __last) {
0089   while (__last != __first) {
0090     --__last;
0091     std::__destroy_at(std::addressof(*__last));
0092   }
0093   return __last;
0094 }
0095 
0096 #if _LIBCPP_STD_VER >= 17
0097 
0098 template <class _Tp, enable_if_t<!is_array_v<_Tp>, int> = 0>
0099 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void destroy_at(_Tp* __loc) {
0100   std::__destroy_at(__loc);
0101 }
0102 
0103 #  if _LIBCPP_STD_VER >= 20
0104 template <class _Tp, enable_if_t<is_array_v<_Tp>, int> = 0>
0105 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void destroy_at(_Tp* __loc) {
0106   std::__destroy_at(__loc);
0107 }
0108 #  endif
0109 
0110 template <class _ForwardIterator>
0111 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void destroy(_ForwardIterator __first, _ForwardIterator __last) {
0112   (void)std::__destroy(std::move(__first), std::move(__last));
0113 }
0114 
0115 template <class _ForwardIterator, class _Size>
0116 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
0117   for (; __n > 0; (void)++__first, --__n)
0118     std::__destroy_at(std::addressof(*__first));
0119   return __first;
0120 }
0121 
0122 #endif // _LIBCPP_STD_VER >= 17
0123 
0124 _LIBCPP_END_NAMESPACE_STD
0125 
0126 _LIBCPP_POP_MACROS
0127 
0128 #endif // _LIBCPP___CXX03___MEMORY_CONSTRUCT_AT_H