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
13 #include <__cxx03/__assert>
14 #include <__cxx03/__config>
15 #include <__cxx03/__filesystem/directory_entry.h>
16 #include <__cxx03/__filesystem/directory_options.h>
17 #include <__cxx03/__filesystem/path.h>
18 #include <__cxx03/__iterator/default_sentinel.h>
19 #include <__cxx03/__iterator/iterator_traits.h>
20 #include <__cxx03/__memory/shared_ptr.h>
21 #include <__cxx03/__ranges/enable_borrowed_range.h>
22 #include <__cxx03/__ranges/enable_view.h>
23 #include <__cxx03/__system_error/error_code.h>
24 #include <__cxx03/__utility/move.h>
25 #include <__cxx03/cstddef>
27 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28 # pragma GCC system_header
32 #include <__cxx03/__undef_macros>
34 #if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
36 _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
38 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_PUSH
40 class _LIBCPP_HIDDEN __dir_stream
;
41 class directory_iterator
{
43 typedef directory_entry value_type
;
44 typedef ptrdiff_t difference_type
;
45 typedef value_type
const* pointer
;
46 typedef value_type
const& reference
;
47 typedef input_iterator_tag iterator_category
;
51 _LIBCPP_HIDE_FROM_ABI
directory_iterator() noexcept
{}
53 _LIBCPP_HIDE_FROM_ABI
explicit directory_iterator(const path
& __p
) : directory_iterator(__p
, nullptr) {}
55 _LIBCPP_HIDE_FROM_ABI
directory_iterator(const path
& __p
, directory_options __opts
)
56 : directory_iterator(__p
, nullptr, __opts
) {}
58 _LIBCPP_HIDE_FROM_ABI
directory_iterator(const path
& __p
, error_code
& __ec
) : directory_iterator(__p
, &__ec
) {}
60 _LIBCPP_HIDE_FROM_ABI
directory_iterator(const path
& __p
, directory_options __opts
, error_code
& __ec
)
61 : directory_iterator(__p
, &__ec
, __opts
) {}
63 _LIBCPP_HIDE_FROM_ABI
directory_iterator(const directory_iterator
&) = default;
64 _LIBCPP_HIDE_FROM_ABI
directory_iterator(directory_iterator
&&) = default;
65 _LIBCPP_HIDE_FROM_ABI directory_iterator
& operator=(const directory_iterator
&) = default;
67 _LIBCPP_HIDE_FROM_ABI directory_iterator
& operator=(directory_iterator
&& __o
) noexcept
{
68 // non-default implementation provided to support self-move assign.
70 __imp_
= std::move(__o
.__imp_
);
75 _LIBCPP_HIDE_FROM_ABI
~directory_iterator() = default;
77 _LIBCPP_HIDE_FROM_ABI
const directory_entry
& operator*() const {
78 // Note: this check duplicates a check in `__dereference()`.
79 _LIBCPP_ASSERT_NON_NULL(__imp_
, "The end iterator cannot be dereferenced");
80 return __dereference();
83 _LIBCPP_HIDE_FROM_ABI
const directory_entry
* operator->() const { return &**this; }
85 _LIBCPP_HIDE_FROM_ABI directory_iterator
& operator++() { return __increment(); }
87 _LIBCPP_HIDE_FROM_ABI __dir_element_proxy
operator++(int) {
88 __dir_element_proxy
__p(**this);
93 _LIBCPP_HIDE_FROM_ABI directory_iterator
& increment(error_code
& __ec
) { return __increment(&__ec
); }
95 # if _LIBCPP_STD_VER >= 20
97 _LIBCPP_HIDE_FROM_ABI
bool operator==(default_sentinel_t
) const noexcept
{ return *this == directory_iterator(); }
102 inline _LIBCPP_HIDE_FROM_ABI
friend bool
103 operator==(const directory_iterator
& __lhs
, const directory_iterator
& __rhs
) noexcept
;
105 // construct the dir_stream
106 _LIBCPP_EXPORTED_FROM_ABI
directory_iterator(const path
&, error_code
*, directory_options
= directory_options::none
);
108 _LIBCPP_EXPORTED_FROM_ABI directory_iterator
& __increment(error_code
* __ec
= nullptr);
110 _LIBCPP_EXPORTED_FROM_ABI
const directory_entry
& __dereference() const;
113 shared_ptr
<__dir_stream
> __imp_
;
116 inline _LIBCPP_HIDE_FROM_ABI
bool
117 operator==(const directory_iterator
& __lhs
, const directory_iterator
& __rhs
) noexcept
{
118 return __lhs
.__imp_
== __rhs
.__imp_
;
121 inline _LIBCPP_HIDE_FROM_ABI
bool
122 operator!=(const directory_iterator
& __lhs
, const directory_iterator
& __rhs
) noexcept
{
123 return !(__lhs
== __rhs
);
126 // enable directory_iterator range-based for statements
127 inline _LIBCPP_HIDE_FROM_ABI directory_iterator
begin(directory_iterator __iter
) noexcept
{ return __iter
; }
129 inline _LIBCPP_HIDE_FROM_ABI directory_iterator
end(directory_iterator
) noexcept
{ return directory_iterator(); }
131 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_POP
133 _LIBCPP_END_NAMESPACE_FILESYSTEM
135 # if _LIBCPP_STD_VER >= 20
138 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
inline constexpr bool
139 std::ranges::enable_borrowed_range
<std::filesystem::directory_iterator
> = true;
142 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
inline constexpr bool
143 std::ranges::enable_view
<std::filesystem::directory_iterator
> = true;
145 # endif // _LIBCPP_STD_VER >= 20
147 #endif // _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
151 #endif // _LIBCPP___FILESYSTEM_DIRECTORY_ITERATOR_H