1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
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
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef _LIBCPP___FILESYSTEM_RECURSIVE_DIRECTORY_ITERATOR_H
11 #define _LIBCPP___FILESYSTEM_RECURSIVE_DIRECTORY_ITERATOR_H
12
13 #include <__config>
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>
24 #include <cstddef>
25
26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
27 # pragma GCC system_header
28 #endif
29
30 _LIBCPP_PUSH_MACROS
31 #include <__undef_macros>
32
33 #if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
34
35 _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
36
37 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_PUSH
38
39 class recursive_directory_iterator {
40 public:
41 using value_type = directory_entry;
42 using difference_type = ptrdiff_t;
43 using pointer = directory_entry const*;
44 using reference = directory_entry const&;
45 using iterator_category = input_iterator_tag;
46
47 public:
48 // constructors and destructor
recursive_directory_iterator()49 _LIBCPP_HIDE_FROM_ABI recursive_directory_iterator() noexcept : __rec_(false) {}
50
51 _LIBCPP_HIDE_FROM_ABI explicit recursive_directory_iterator(
52 const path& __p, directory_options __xoptions = directory_options::none)
recursive_directory_iterator(__p,__xoptions,nullptr)53 : recursive_directory_iterator(__p, __xoptions, nullptr) {}
54
recursive_directory_iterator(const path & __p,directory_options __xoptions,error_code & __ec)55 _LIBCPP_HIDE_FROM_ABI recursive_directory_iterator(const path& __p, directory_options __xoptions, error_code& __ec)
56 : recursive_directory_iterator(__p, __xoptions, &__ec) {}
57
recursive_directory_iterator(const path & __p,error_code & __ec)58 _LIBCPP_HIDE_FROM_ABI recursive_directory_iterator(const path& __p, error_code& __ec)
59 : recursive_directory_iterator(__p, directory_options::none, &__ec) {}
60
61 _LIBCPP_HIDE_FROM_ABI recursive_directory_iterator(const recursive_directory_iterator&) = default;
62 _LIBCPP_HIDE_FROM_ABI recursive_directory_iterator(recursive_directory_iterator&&) = default;
63
64 _LIBCPP_HIDE_FROM_ABI recursive_directory_iterator& operator=(const recursive_directory_iterator&) = default;
65
66 _LIBCPP_HIDE_FROM_ABI recursive_directory_iterator& operator=(recursive_directory_iterator&& __o) noexcept {
67 // non-default implementation provided to support self-move assign.
68 if (this != &__o) {
69 __imp_ = std::move(__o.__imp_);
70 __rec_ = __o.__rec_;
71 }
72 return *this;
73 }
74
75 _LIBCPP_HIDE_FROM_ABI ~recursive_directory_iterator() = default;
76
77 _LIBCPP_HIDE_FROM_ABI const directory_entry& operator*() const { return __dereference(); }
78
79 _LIBCPP_HIDE_FROM_ABI const directory_entry* operator->() const { return &__dereference(); }
80
81 _LIBCPP_HIDE_FROM_ABI recursive_directory_iterator& operator++() { return __increment(); }
82
83 _LIBCPP_HIDE_FROM_ABI __dir_element_proxy operator++(int) {
84 __dir_element_proxy __p(**this);
85 __increment();
86 return __p;
87 }
88
increment(error_code & __ec)89 _LIBCPP_HIDE_FROM_ABI recursive_directory_iterator& increment(error_code& __ec) { return __increment(&__ec); }
90
91 _LIBCPP_EXPORTED_FROM_ABI directory_options options() const;
92 _LIBCPP_EXPORTED_FROM_ABI int depth() const;
93
pop()94 _LIBCPP_HIDE_FROM_ABI void pop() { __pop(); }
95
pop(error_code & __ec)96 _LIBCPP_HIDE_FROM_ABI void pop(error_code& __ec) { __pop(&__ec); }
97
recursion_pending()98 _LIBCPP_HIDE_FROM_ABI bool recursion_pending() const { return __rec_; }
99
disable_recursion_pending()100 _LIBCPP_HIDE_FROM_ABI void disable_recursion_pending() { __rec_ = false; }
101
102 # if _LIBCPP_STD_VER >= 20
103
104 _LIBCPP_HIDE_FROM_ABI bool operator==(default_sentinel_t) const noexcept {
105 return *this == recursive_directory_iterator();
106 }
107
108 # endif
109
110 private:
111 _LIBCPP_EXPORTED_FROM_ABI recursive_directory_iterator(const path& __p, directory_options __opt, error_code* __ec);
112 _LIBCPP_EXPORTED_FROM_ABI const directory_entry& __dereference() const;
113 _LIBCPP_EXPORTED_FROM_ABI bool __try_recursion(error_code* __ec);
114 _LIBCPP_EXPORTED_FROM_ABI void __advance(error_code* __ec = nullptr);
115 _LIBCPP_EXPORTED_FROM_ABI recursive_directory_iterator& __increment(error_code* __ec = nullptr);
116 _LIBCPP_EXPORTED_FROM_ABI void __pop(error_code* __ec = nullptr);
117
118 inline _LIBCPP_HIDE_FROM_ABI friend bool
119 operator==(const recursive_directory_iterator&, const recursive_directory_iterator&) noexcept;
120
121 struct _LIBCPP_HIDDEN __shared_imp;
122 shared_ptr<__shared_imp> __imp_;
123 bool __rec_;
124 }; // class recursive_directory_iterator
125
126 inline _LIBCPP_HIDE_FROM_ABI bool
127 operator==(const recursive_directory_iterator& __lhs, const recursive_directory_iterator& __rhs) noexcept {
128 return __lhs.__imp_ == __rhs.__imp_;
129 }
130
131 _LIBCPP_HIDE_FROM_ABI inline bool
132 operator!=(const recursive_directory_iterator& __lhs, const recursive_directory_iterator& __rhs) noexcept {
133 return !(__lhs == __rhs);
134 }
135 // enable recursive_directory_iterator range-based for statements
begin(recursive_directory_iterator __iter)136 inline _LIBCPP_HIDE_FROM_ABI recursive_directory_iterator begin(recursive_directory_iterator __iter) noexcept {
137 return __iter;
138 }
139
end(recursive_directory_iterator)140 inline _LIBCPP_HIDE_FROM_ABI recursive_directory_iterator end(recursive_directory_iterator) noexcept {
141 return recursive_directory_iterator();
142 }
143
144 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_POP
145
146 _LIBCPP_END_NAMESPACE_FILESYSTEM
147
148 # if _LIBCPP_STD_VER >= 20
149
150 template <>
151 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY inline constexpr bool
152 std::ranges::enable_borrowed_range<std::filesystem::recursive_directory_iterator> = true;
153
154 template <>
155 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY inline constexpr bool
156 std::ranges::enable_view<std::filesystem::recursive_directory_iterator> = true;
157
158 # endif // _LIBCPP_STD_VER >= 20
159
160 #endif // _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
161
162 _LIBCPP_POP_MACROS
163
164 #endif // _LIBCPP___FILESYSTEM_RECURSIVE_DIRECTORY_ITERATOR_H
165