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
14 #include <__filesystem/directory_entry.h>
15 #include <__filesystem/directory_options.h>
16 #include <__filesystem/path.h>
17 #include <__iterator/default_sentinel.h>
18 #include <__iterator/iterator_traits.h>
19 #include <__memory/shared_ptr.h>
20 #include <__ranges/enable_borrowed_range.h>
21 #include <__ranges/enable_view.h>
22 #include <__system_error/error_code.h>
23 #include <__utility/move.h>
25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26 # pragma GCC system_header
30 #include <__undef_macros>
32 #if _LIBCPP_STD_VER >= 17 && _LIBCPP_HAS_FILESYSTEM
34 _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
36 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_PUSH
38 class recursive_directory_iterator
{
40 using value_type
= directory_entry
;
41 using difference_type
= ptrdiff_t;
42 using pointer
= directory_entry
const*;
43 using reference
= directory_entry
const&;
44 using iterator_category
= input_iterator_tag
;
47 // constructors and destructor
48 _LIBCPP_HIDE_FROM_ABI
recursive_directory_iterator() noexcept
: __rec_(false) {}
50 _LIBCPP_HIDE_FROM_ABI
explicit recursive_directory_iterator(
51 const path
& __p
, directory_options __xoptions
= directory_options::none
)
52 : recursive_directory_iterator(__p
, __xoptions
, nullptr) {}
54 _LIBCPP_HIDE_FROM_ABI
recursive_directory_iterator(const path
& __p
, directory_options __xoptions
, error_code
& __ec
)
55 : recursive_directory_iterator(__p
, __xoptions
, &__ec
) {}
57 _LIBCPP_HIDE_FROM_ABI
recursive_directory_iterator(const path
& __p
, error_code
& __ec
)
58 : recursive_directory_iterator(__p
, directory_options::none
, &__ec
) {}
60 _LIBCPP_HIDE_FROM_ABI
recursive_directory_iterator(const recursive_directory_iterator
&) = default;
61 _LIBCPP_HIDE_FROM_ABI
recursive_directory_iterator(recursive_directory_iterator
&&) = default;
63 _LIBCPP_HIDE_FROM_ABI recursive_directory_iterator
& operator=(const recursive_directory_iterator
&) = default;
65 _LIBCPP_HIDE_FROM_ABI recursive_directory_iterator
& operator=(recursive_directory_iterator
&& __o
) noexcept
{
66 // non-default implementation provided to support self-move assign.
68 __imp_
= std::move(__o
.__imp_
);
74 _LIBCPP_HIDE_FROM_ABI
~recursive_directory_iterator() = default;
76 _LIBCPP_HIDE_FROM_ABI
const directory_entry
& operator*() const { return __dereference(); }
78 _LIBCPP_HIDE_FROM_ABI
const directory_entry
* operator->() const { return &__dereference(); }
80 _LIBCPP_HIDE_FROM_ABI recursive_directory_iterator
& operator++() { return __increment(); }
82 _LIBCPP_HIDE_FROM_ABI __dir_element_proxy
operator++(int) {
83 __dir_element_proxy
__p(**this);
88 _LIBCPP_HIDE_FROM_ABI recursive_directory_iterator
& increment(error_code
& __ec
) { return __increment(&__ec
); }
90 _LIBCPP_EXPORTED_FROM_ABI directory_options
options() const;
91 _LIBCPP_EXPORTED_FROM_ABI
int depth() const;
93 _LIBCPP_HIDE_FROM_ABI
void pop() { __pop(); }
95 _LIBCPP_HIDE_FROM_ABI
void pop(error_code
& __ec
) { __pop(&__ec
); }
97 _LIBCPP_HIDE_FROM_ABI
bool recursion_pending() const { return __rec_
; }
99 _LIBCPP_HIDE_FROM_ABI
void disable_recursion_pending() { __rec_
= false; }
101 # if _LIBCPP_STD_VER >= 20
103 _LIBCPP_HIDE_FROM_ABI
bool operator==(default_sentinel_t
) const noexcept
{
104 return *this == recursive_directory_iterator();
110 _LIBCPP_EXPORTED_FROM_ABI
recursive_directory_iterator(const path
& __p
, directory_options __opt
, error_code
* __ec
);
111 _LIBCPP_EXPORTED_FROM_ABI
const directory_entry
& __dereference() const;
112 _LIBCPP_EXPORTED_FROM_ABI
bool __try_recursion(error_code
* __ec
);
113 _LIBCPP_EXPORTED_FROM_ABI
void __advance(error_code
* __ec
= nullptr);
114 _LIBCPP_EXPORTED_FROM_ABI recursive_directory_iterator
& __increment(error_code
* __ec
= nullptr);
115 _LIBCPP_EXPORTED_FROM_ABI
void __pop(error_code
* __ec
= nullptr);
117 inline _LIBCPP_HIDE_FROM_ABI
friend bool
118 operator==(const recursive_directory_iterator
&, const recursive_directory_iterator
&) noexcept
;
120 struct _LIBCPP_HIDDEN __shared_imp
;
121 shared_ptr
<__shared_imp
> __imp_
;
123 }; // class recursive_directory_iterator
125 inline _LIBCPP_HIDE_FROM_ABI
bool
126 operator==(const recursive_directory_iterator
& __lhs
, const recursive_directory_iterator
& __rhs
) noexcept
{
127 return __lhs
.__imp_
== __rhs
.__imp_
;
130 _LIBCPP_HIDE_FROM_ABI
inline bool
131 operator!=(const recursive_directory_iterator
& __lhs
, const recursive_directory_iterator
& __rhs
) noexcept
{
132 return !(__lhs
== __rhs
);
134 // enable recursive_directory_iterator range-based for statements
135 inline _LIBCPP_HIDE_FROM_ABI recursive_directory_iterator
begin(recursive_directory_iterator __iter
) noexcept
{
139 inline _LIBCPP_HIDE_FROM_ABI recursive_directory_iterator
end(recursive_directory_iterator
) noexcept
{
140 return recursive_directory_iterator();
143 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_POP
145 _LIBCPP_END_NAMESPACE_FILESYSTEM
147 # if _LIBCPP_STD_VER >= 20
150 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
inline constexpr bool
151 std::ranges::enable_borrowed_range
<std::filesystem::recursive_directory_iterator
> = true;
154 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
inline constexpr bool
155 std::ranges::enable_view
<std::filesystem::recursive_directory_iterator
> = true;
157 # endif // _LIBCPP_STD_VER >= 20
159 #endif // _LIBCPP_STD_VER >= 17 && _LIBCPP_HAS_FILESYSTEM
163 #endif // _LIBCPP___FILESYSTEM_RECURSIVE_DIRECTORY_ITERATOR_H