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