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
27template <class _ForwardIterator>
28_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
29__destroy(_ForwardIterator __first, _ForwardIterator __last) {
30 for (; __first != __last; ++__first)
31 std::__destroy_at(std::addressof(*__first));
32 return __first;
33}
34
35template <class _BidirectionalIterator>
36_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _BidirectionalIterator
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.
46template <class _Alloc, class _Iter, class _Sent>
47_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
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
54template <class _ForwardIterator>
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
59template <class _ForwardIterator, class _Size>
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