1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef _LIBCPP___MEMORY_DESTROY_H
10 #define _LIBCPP___MEMORY_DESTROY_H
11
12 #include <__config>
13 #include <__memory/addressof.h>
14 #include <__memory/allocator_traits.h>
15 #include <__memory/construct_at.h>
16 #include <__utility/move.h>
17
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 # pragma GCC system_header
20 #endif
21
22 _LIBCPP_PUSH_MACROS
23 #include <__undef_macros>
24
25 _LIBCPP_BEGIN_NAMESPACE_STD
26
27 template <class _ForwardIterator>
28 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
__destroy(_ForwardIterator __first,_ForwardIterator __last)29 __destroy(_ForwardIterator __first, _ForwardIterator __last) {
30 for (; __first != __last; ++__first)
31 std::__destroy_at(std::addressof(*__first));
32 return __first;
33 }
34
35 template <class _BidirectionalIterator>
36 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _BidirectionalIterator
__reverse_destroy(_BidirectionalIterator __first,_BidirectionalIterator __last)37 __reverse_destroy(_BidirectionalIterator __first, _BidirectionalIterator __last) {
38 while (__last != __first) {
39 --__last;
40 std::__destroy_at(std::addressof(*__last));
41 }
42 return __last;
43 }
44
45 // Destroy all elements in [__first, __last) from left to right using allocator destruction.
46 template <class _Alloc, class _Iter, class _Sent>
47 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
__allocator_destroy(_Alloc & __alloc,_Iter __first,_Sent __last)48 __allocator_destroy(_Alloc& __alloc, _Iter __first, _Sent __last) {
49 for (; __first != __last; ++__first)
50 allocator_traits<_Alloc>::destroy(__alloc, std::addressof(*__first));
51 }
52
53 #if _LIBCPP_STD_VER >= 17
54 template <class _ForwardIterator>
destroy(_ForwardIterator __first,_ForwardIterator __last)55 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void destroy(_ForwardIterator __first, _ForwardIterator __last) {
56 (void)std::__destroy(std::move(__first), std::move(__last));
57 }
58
59 template <class _ForwardIterator, class _Size>
destroy_n(_ForwardIterator __first,_Size __n)60 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
61 for (; __n > 0; (void)++__first, --__n)
62 std::__destroy_at(std::addressof(*__first));
63 return __first;
64 }
65 #endif
66
67 _LIBCPP_END_NAMESPACE_STD
68
69 _LIBCPP_POP_MACROS
70
71 #endif // _LIBCPP___MEMORY_DESTROY_H
72