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
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>
26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
27 # pragma GCC system_header
31 #include <__undef_macros>
33 #if _LIBCPP_STD_VER >= 17 && _LIBCPP_HAS_FILESYSTEM
35 _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
37 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_PUSH
39 class _LIBCPP_HIDDEN __dir_stream
;
40 class directory_iterator
{
42 typedef directory_entry value_type
;
43 typedef ptrdiff_t difference_type
;
44 typedef value_type
const* pointer
;
45 typedef value_type
const& reference
;
46 typedef input_iterator_tag iterator_category
;
50 _LIBCPP_HIDE_FROM_ABI
directory_iterator() noexcept
{}
52 _LIBCPP_HIDE_FROM_ABI
explicit directory_iterator(const path
& __p
) : directory_iterator(__p
, nullptr) {}
54 _LIBCPP_HIDE_FROM_ABI
directory_iterator(const path
& __p
, directory_options __opts
)
55 : directory_iterator(__p
, nullptr, __opts
) {}
57 _LIBCPP_HIDE_FROM_ABI
directory_iterator(const path
& __p
, error_code
& __ec
) : directory_iterator(__p
, &__ec
) {}
59 _LIBCPP_HIDE_FROM_ABI
directory_iterator(const path
& __p
, directory_options __opts
, error_code
& __ec
)
60 : directory_iterator(__p
, &__ec
, __opts
) {}
62 _LIBCPP_HIDE_FROM_ABI
directory_iterator(const directory_iterator
&) = default;
63 _LIBCPP_HIDE_FROM_ABI
directory_iterator(directory_iterator
&&) = default;
64 _LIBCPP_HIDE_FROM_ABI directory_iterator
& operator=(const directory_iterator
&) = default;
66 _LIBCPP_HIDE_FROM_ABI directory_iterator
& operator=(directory_iterator
&& __o
) noexcept
{
67 // non-default implementation provided to support self-move assign.
69 __imp_
= std::move(__o
.__imp_
);
74 _LIBCPP_HIDE_FROM_ABI
~directory_iterator() = default;
76 _LIBCPP_HIDE_FROM_ABI
const directory_entry
& operator*() const {
77 // Note: this check duplicates a check in `__dereference()`.
78 _LIBCPP_ASSERT_NON_NULL(__imp_
, "The end iterator cannot be dereferenced");
79 return __dereference();
82 _LIBCPP_HIDE_FROM_ABI
const directory_entry
* operator->() const { return &**this; }
84 _LIBCPP_HIDE_FROM_ABI directory_iterator
& operator++() { return __increment(); }
86 _LIBCPP_HIDE_FROM_ABI __dir_element_proxy
operator++(int) {
87 __dir_element_proxy
__p(**this);
92 _LIBCPP_HIDE_FROM_ABI directory_iterator
& increment(error_code
& __ec
) { return __increment(&__ec
); }
94 # if _LIBCPP_STD_VER >= 20
96 _LIBCPP_HIDE_FROM_ABI
bool operator==(default_sentinel_t
) const noexcept
{ return *this == directory_iterator(); }
101 inline _LIBCPP_HIDE_FROM_ABI
friend bool
102 operator==(const directory_iterator
& __lhs
, const directory_iterator
& __rhs
) noexcept
;
104 // construct the dir_stream
105 _LIBCPP_EXPORTED_FROM_ABI
directory_iterator(const path
&, error_code
*, directory_options
= directory_options::none
);
107 _LIBCPP_EXPORTED_FROM_ABI directory_iterator
& __increment(error_code
* __ec
= nullptr);
109 _LIBCPP_EXPORTED_FROM_ABI
const directory_entry
& __dereference() const;
112 shared_ptr
<__dir_stream
> __imp_
;
115 inline _LIBCPP_HIDE_FROM_ABI
bool
116 operator==(const directory_iterator
& __lhs
, const directory_iterator
& __rhs
) noexcept
{
117 return __lhs
.__imp_
== __rhs
.__imp_
;
120 inline _LIBCPP_HIDE_FROM_ABI
bool
121 operator!=(const directory_iterator
& __lhs
, const directory_iterator
& __rhs
) noexcept
{
122 return !(__lhs
== __rhs
);
125 // enable directory_iterator range-based for statements
126 inline _LIBCPP_HIDE_FROM_ABI directory_iterator
begin(directory_iterator __iter
) noexcept
{ return __iter
; }
128 inline _LIBCPP_HIDE_FROM_ABI directory_iterator
end(directory_iterator
) noexcept
{ return directory_iterator(); }
130 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_POP
132 _LIBCPP_END_NAMESPACE_FILESYSTEM
134 # if _LIBCPP_STD_VER >= 20
137 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
inline constexpr bool
138 std::ranges::enable_borrowed_range
<std::filesystem::directory_iterator
> = true;
141 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
inline constexpr bool
142 std::ranges::enable_view
<std::filesystem::directory_iterator
> = true;
144 # endif // _LIBCPP_STD_VER >= 20
146 #endif // _LIBCPP_STD_VER >= 17 && _LIBCPP_HAS_FILESYSTEM
150 #endif // _LIBCPP___FILESYSTEM_DIRECTORY_ITERATOR_H