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_RANGES_CONSTRUCT_AT_H
11 #define _LIBCPP___MEMORY_RANGES_CONSTRUCT_AT_H
12 
13 #include <__concepts/destructible.h>
14 #include <__config>
15 #include <__iterator/incrementable_traits.h>
16 #include <__iterator/iterator_traits.h>
17 #include <__memory/concepts.h>
18 #include <__memory/construct_at.h>
19 #include <__ranges/access.h>
20 #include <__ranges/concepts.h>
21 #include <__ranges/dangling.h>
22 #include <__utility/declval.h>
23 #include <__utility/forward.h>
24 #include <__utility/move.h>
25 #include <new>
26 
27 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28 #  pragma GCC system_header
29 #endif
30 
31 _LIBCPP_PUSH_MACROS
32 #include <__undef_macros>
33 
34 _LIBCPP_BEGIN_NAMESPACE_STD
35 
36 #if _LIBCPP_STD_VER >= 20
37 namespace ranges {
38 
39 // construct_at
40 
41 namespace __construct_at {
42 
43 struct __fn {
44   template <class _Tp, class... _Args, class = decltype(::new(std::declval<void*>()) _Tp(std::declval<_Args>()...))>
operator__fn45   _LIBCPP_HIDE_FROM_ABI constexpr _Tp* operator()(_Tp* __location, _Args&&... __args) const {
46     return std::construct_at(__location, std::forward<_Args>(__args)...);
47   }
48 };
49 
50 } // namespace __construct_at
51 
52 inline namespace __cpo {
53 inline constexpr auto construct_at = __construct_at::__fn{};
54 } // namespace __cpo
55 
56 // destroy_at
57 
58 namespace __destroy_at {
59 
60 struct __fn {
61   template <destructible _Tp>
operator__fn62   _LIBCPP_HIDE_FROM_ABI constexpr void operator()(_Tp* __location) const noexcept {
63     std::destroy_at(__location);
64   }
65 };
66 
67 } // namespace __destroy_at
68 
69 inline namespace __cpo {
70 inline constexpr auto destroy_at = __destroy_at::__fn{};
71 } // namespace __cpo
72 
73 // destroy
74 
75 namespace __destroy {
76 
77 struct __fn {
78   template <__nothrow_input_iterator _InputIterator, __nothrow_sentinel_for<_InputIterator> _Sentinel>
79     requires destructible<iter_value_t<_InputIterator>>
operator__fn80   _LIBCPP_HIDE_FROM_ABI constexpr _InputIterator operator()(_InputIterator __first, _Sentinel __last) const noexcept {
81     return std::__destroy(std::move(__first), std::move(__last));
82   }
83 
84   template <__nothrow_input_range _InputRange>
85     requires destructible<range_value_t<_InputRange>>
operator__fn86   _LIBCPP_HIDE_FROM_ABI constexpr borrowed_iterator_t<_InputRange> operator()(_InputRange&& __range) const noexcept {
87     return (*this)(ranges::begin(__range), ranges::end(__range));
88   }
89 };
90 
91 } // namespace __destroy
92 
93 inline namespace __cpo {
94 inline constexpr auto destroy = __destroy::__fn{};
95 } // namespace __cpo
96 
97 // destroy_n
98 
99 namespace __destroy_n {
100 
101 struct __fn {
102   template <__nothrow_input_iterator _InputIterator>
103     requires destructible<iter_value_t<_InputIterator>>
104   _LIBCPP_HIDE_FROM_ABI constexpr _InputIterator
operator__fn105   operator()(_InputIterator __first, iter_difference_t<_InputIterator> __n) const noexcept {
106     return std::destroy_n(std::move(__first), __n);
107   }
108 };
109 
110 } // namespace __destroy_n
111 
112 inline namespace __cpo {
113 inline constexpr auto destroy_n = __destroy_n::__fn{};
114 } // namespace __cpo
115 
116 } // namespace ranges
117 
118 #endif // _LIBCPP_STD_VER >= 20
119 
120 _LIBCPP_END_NAMESPACE_STD
121 
122 _LIBCPP_POP_MACROS
123 
124 #endif // _LIBCPP___MEMORY_RANGES_CONSTRUCT_AT_H
125