2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___FILESYSTEM_RECURSIVE_DIRECTORY_ITERATOR_H
11 #define _LIBCPP___FILESYSTEM_RECURSIVE_DIRECTORY_ITERATOR_H
13 #include <__availability>
15 #include <__filesystem/directory_entry.h>
16 #include <__filesystem/directory_options.h>
17 #include <__filesystem/path.h>
18 #include <__iterator/default_sentinel.h>
19 #include <__iterator/iterator_traits.h>
20 #include <__memory/shared_ptr.h>
21 #include <__ranges/enable_borrowed_range.h>
22 #include <__ranges/enable_view.h>
23 #include <__system_error/error_code.h>
24 #include <__utility/move.h>
27 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28 # pragma GCC system_header
31 #if !defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
33 _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
35 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_PUSH
37 class recursive_directory_iterator
{
39 using value_type
= directory_entry
;
40 using difference_type
= ptrdiff_t;
41 using pointer
= directory_entry
const*;
42 using reference
= directory_entry
const&;
43 using iterator_category
= input_iterator_tag
;
46 // constructors and destructor
47 _LIBCPP_INLINE_VISIBILITY
48 recursive_directory_iterator() noexcept
: __rec_(false) {}
50 _LIBCPP_INLINE_VISIBILITY
51 explicit recursive_directory_iterator(
52 const path
& __p
, directory_options __xoptions
= directory_options::none
)
53 : recursive_directory_iterator(__p
, __xoptions
, nullptr) {}
55 _LIBCPP_INLINE_VISIBILITY
56 recursive_directory_iterator(const path
& __p
, directory_options __xoptions
,
58 : recursive_directory_iterator(__p
, __xoptions
, &__ec
) {}
60 _LIBCPP_INLINE_VISIBILITY
61 recursive_directory_iterator(const path
& __p
, error_code
& __ec
)
62 : recursive_directory_iterator(__p
, directory_options::none
, &__ec
) {}
64 _LIBCPP_HIDE_FROM_ABI
recursive_directory_iterator(const recursive_directory_iterator
&) = default;
65 _LIBCPP_HIDE_FROM_ABI
recursive_directory_iterator(recursive_directory_iterator
&&) = default;
67 _LIBCPP_HIDE_FROM_ABI recursive_directory_iterator
&
68 operator=(const recursive_directory_iterator
&) = default;
70 _LIBCPP_INLINE_VISIBILITY
71 recursive_directory_iterator
&
72 operator=(recursive_directory_iterator
&& __o
) noexcept
{
73 // non-default implementation provided to support self-move assign.
75 __imp_
= _VSTD::move(__o
.__imp_
);
81 _LIBCPP_HIDE_FROM_ABI
~recursive_directory_iterator() = default;
83 _LIBCPP_INLINE_VISIBILITY
84 const directory_entry
& operator*() const { return __dereference(); }
86 _LIBCPP_INLINE_VISIBILITY
87 const directory_entry
* operator->() const { return &__dereference(); }
89 _LIBCPP_HIDE_FROM_ABI recursive_directory_iterator
& operator++() { return __increment(); }
91 _LIBCPP_INLINE_VISIBILITY
92 __dir_element_proxy
operator++(int) {
93 __dir_element_proxy
__p(**this);
98 _LIBCPP_INLINE_VISIBILITY
99 recursive_directory_iterator
& increment(error_code
& __ec
) {
100 return __increment(&__ec
);
103 _LIBCPP_EXPORTED_FROM_ABI directory_options
options() const;
104 _LIBCPP_EXPORTED_FROM_ABI
int depth() const;
106 _LIBCPP_INLINE_VISIBILITY
107 void pop() { __pop(); }
109 _LIBCPP_INLINE_VISIBILITY
110 void pop(error_code
& __ec
) { __pop(&__ec
); }
112 _LIBCPP_INLINE_VISIBILITY
113 bool recursion_pending() const { return __rec_
; }
115 _LIBCPP_INLINE_VISIBILITY
116 void disable_recursion_pending() { __rec_
= false; }
118 # if _LIBCPP_STD_VER >= 20
120 _LIBCPP_HIDE_FROM_ABI
bool operator==(default_sentinel_t
) const noexcept
{
121 return *this == recursive_directory_iterator();
127 _LIBCPP_EXPORTED_FROM_ABI
recursive_directory_iterator(const path
& __p
, directory_options __opt
, error_code
* __ec
);
128 _LIBCPP_EXPORTED_FROM_ABI
const directory_entry
& __dereference() const;
129 _LIBCPP_EXPORTED_FROM_ABI
bool __try_recursion(error_code
* __ec
);
130 _LIBCPP_EXPORTED_FROM_ABI
void __advance(error_code
* __ec
= nullptr);
131 _LIBCPP_EXPORTED_FROM_ABI recursive_directory_iterator
& __increment(error_code
* __ec
= nullptr);
132 _LIBCPP_EXPORTED_FROM_ABI
void __pop(error_code
* __ec
= nullptr);
134 inline _LIBCPP_INLINE_VISIBILITY
friend bool
135 operator==(const recursive_directory_iterator
&,
136 const recursive_directory_iterator
&) noexcept
;
138 struct _LIBCPP_HIDDEN __shared_imp
;
139 shared_ptr
<__shared_imp
> __imp_
;
141 }; // class recursive_directory_iterator
143 inline _LIBCPP_INLINE_VISIBILITY
bool
144 operator==(const recursive_directory_iterator
& __lhs
,
145 const recursive_directory_iterator
& __rhs
) noexcept
{
146 return __lhs
.__imp_
== __rhs
.__imp_
;
149 _LIBCPP_INLINE_VISIBILITY
150 inline bool operator!=(const recursive_directory_iterator
& __lhs
,
151 const recursive_directory_iterator
& __rhs
) noexcept
{
152 return !(__lhs
== __rhs
);
154 // enable recursive_directory_iterator range-based for statements
155 inline _LIBCPP_INLINE_VISIBILITY recursive_directory_iterator
156 begin(recursive_directory_iterator __iter
) noexcept
{
160 inline _LIBCPP_INLINE_VISIBILITY recursive_directory_iterator
161 end(recursive_directory_iterator
) noexcept
{
162 return recursive_directory_iterator();
165 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_POP
167 _LIBCPP_END_NAMESPACE_FILESYSTEM
169 #if _LIBCPP_STD_VER >= 20
172 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
173 inline constexpr bool _VSTD::ranges::enable_borrowed_range
<_VSTD_FS::recursive_directory_iterator
> = true;
176 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
177 inline constexpr bool _VSTD::ranges::enable_view
<_VSTD_FS::recursive_directory_iterator
> = true;
179 #endif // _LIBCPP_STD_VER >= 20
181 #endif // !defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
183 #endif // _LIBCPP___FILESYSTEM_RECURSIVE_DIRECTORY_ITERATOR_H