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_DIRECTORY_ITERATOR_H
11 #define _LIBCPP___FILESYSTEM_DIRECTORY_ITERATOR_H
14 #include <__availability>
16 #include <__filesystem/directory_entry.h>
17 #include <__filesystem/directory_options.h>
18 #include <__filesystem/path.h>
19 #include <__iterator/default_sentinel.h>
20 #include <__iterator/iterator_traits.h>
21 #include <__memory/shared_ptr.h>
22 #include <__ranges/enable_borrowed_range.h>
23 #include <__ranges/enable_view.h>
24 #include <__system_error/error_code.h>
25 #include <__utility/move.h>
28 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
29 # pragma GCC system_header
32 #if !defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
34 _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
36 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_PUSH
38 class _LIBCPP_HIDDEN __dir_stream
;
39 class directory_iterator
{
41 typedef directory_entry value_type
;
42 typedef ptrdiff_t difference_type
;
43 typedef value_type
const* pointer
;
44 typedef value_type
const& reference
;
45 typedef input_iterator_tag iterator_category
;
50 directory_iterator() noexcept
{}
53 explicit directory_iterator(const path
& __p
)
54 : directory_iterator(__p
, nullptr) {}
57 directory_iterator(const path
& __p
, directory_options __opts
)
58 : directory_iterator(__p
, nullptr, __opts
) {}
61 directory_iterator(const path
& __p
, error_code
& __ec
)
62 : directory_iterator(__p
, &__ec
) {}
65 directory_iterator(const path
& __p
, directory_options __opts
,
67 : directory_iterator(__p
, &__ec
, __opts
) {}
69 _LIBCPP_HIDE_FROM_ABI
directory_iterator(const directory_iterator
&) = default;
70 _LIBCPP_HIDE_FROM_ABI
directory_iterator(directory_iterator
&&) = default;
71 _LIBCPP_HIDE_FROM_ABI directory_iterator
& operator=(const directory_iterator
&) = default;
74 directory_iterator
& operator=(directory_iterator
&& __o
) noexcept
{
75 // non-default implementation provided to support self-move assign.
77 __imp_
= _VSTD::move(__o
.__imp_
);
82 _LIBCPP_HIDE_FROM_ABI
~directory_iterator() = default;
85 const directory_entry
& operator*() const {
86 _LIBCPP_ASSERT_UNCATEGORIZED(__imp_
, "The end iterator cannot be dereferenced");
87 return __dereference();
91 const directory_entry
* operator->() const { return &**this; }
94 directory_iterator
& operator++() { return __increment(); }
97 __dir_element_proxy
operator++(int) {
98 __dir_element_proxy
__p(**this);
103 _LIBCPP_HIDE_FROM_ABI
104 directory_iterator
& increment(error_code
& __ec
) { return __increment(&__ec
); }
106 # if _LIBCPP_STD_VER >= 20
108 _LIBCPP_HIDE_FROM_ABI
bool operator==(default_sentinel_t
) const noexcept
{ return *this == directory_iterator(); }
113 inline _LIBCPP_HIDE_FROM_ABI
friend bool
114 operator==(const directory_iterator
& __lhs
,
115 const directory_iterator
& __rhs
) noexcept
;
117 // construct the dir_stream
118 _LIBCPP_EXPORTED_FROM_ABI
directory_iterator(const path
&, error_code
*, directory_options
= directory_options::none
);
120 _LIBCPP_EXPORTED_FROM_ABI directory_iterator
& __increment(error_code
* __ec
= nullptr);
122 _LIBCPP_EXPORTED_FROM_ABI
const directory_entry
& __dereference() const;
125 shared_ptr
<__dir_stream
> __imp_
;
128 inline _LIBCPP_HIDE_FROM_ABI
bool
129 operator==(const directory_iterator
& __lhs
,
130 const directory_iterator
& __rhs
) noexcept
{
131 return __lhs
.__imp_
== __rhs
.__imp_
;
134 inline _LIBCPP_HIDE_FROM_ABI
bool
135 operator!=(const directory_iterator
& __lhs
,
136 const directory_iterator
& __rhs
) noexcept
{
137 return !(__lhs
== __rhs
);
140 // enable directory_iterator range-based for statements
141 inline _LIBCPP_HIDE_FROM_ABI directory_iterator
142 begin(directory_iterator __iter
) noexcept
{
146 inline _LIBCPP_HIDE_FROM_ABI directory_iterator
147 end(directory_iterator
) noexcept
{
148 return directory_iterator();
151 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_POP
153 _LIBCPP_END_NAMESPACE_FILESYSTEM
155 #if _LIBCPP_STD_VER >= 20
158 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
159 inline constexpr bool _VSTD::ranges::enable_borrowed_range
<_VSTD_FS::directory_iterator
> = true;
162 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
163 inline constexpr bool _VSTD::ranges::enable_view
<_VSTD_FS::directory_iterator
> = true;
165 #endif // _LIBCPP_STD_VER >= 20
167 #endif // !defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
169 #endif // _LIBCPP___FILESYSTEM_DIRECTORY_ITERATOR_H