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