| 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_DIRECTORY_ITERATOR_H |
| 11 |
#define _LIBCPP___FILESYSTEM_DIRECTORY_ITERATOR_H |
| 12 |
|
| 13 |
#include <__assert> |
| 14 |
#include <__config> |
| 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> |
| 25 |
#include <cstddef> |
| 26 |
|
| 27 |
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 28 |
# pragma GCC system_header |
| 29 |
#endif |
| 30 |
|
| 31 |
_LIBCPP_PUSH_MACROS |
| 32 |
#include <__undef_macros> |
| 33 |
|
| 34 |
#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM) |
| 35 |
|
| 36 |
_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM |
| 37 |
|
| 38 |
_LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_PUSH |
| 39 |
|
| 40 |
class _LIBCPP_HIDDEN __dir_stream; |
| 41 |
class directory_iterator { |
| 42 |
public: |
| 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; |
| 48 |
|
| 49 |
public: |
| 50 |
// ctor & dtor |
| 51 |
_LIBCPP_HIDE_FROM_ABI directory_iterator() noexcept {} |
| 52 |
|
| 53 |
_LIBCPP_HIDE_FROM_ABI explicit directory_iterator(const path& __p) : directory_iterator(__p, nullptr) {} |
| 54 |
|
| 55 |
_LIBCPP_HIDE_FROM_ABI directory_iterator(const path& __p, directory_options __opts) |
| 56 |
: directory_iterator(__p, nullptr, __opts) {} |
| 57 |
|
| 58 |
_LIBCPP_HIDE_FROM_ABI directory_iterator(const path& __p, error_code& __ec) : directory_iterator(__p, &__ec) {} |
| 59 |
|
| 60 |
_LIBCPP_HIDE_FROM_ABI directory_iterator(const path& __p, directory_options __opts, error_code& __ec) |
| 61 |
: directory_iterator(__p, &__ec, __opts) {} |
| 62 |
|
| 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; |
| 66 |
|
| 67 |
_LIBCPP_HIDE_FROM_ABI directory_iterator& operator=(directory_iterator&& __o) noexcept { |
| 68 |
// non-default implementation provided to support self-move assign. |
| 69 |
if (this != &__o) { |
| 70 |
__imp_ = std::move(__o.__imp_); |
| 71 |
} |
| 72 |
return *this; |
| 73 |
} |
| 74 |
|
| 75 |
_LIBCPP_HIDE_FROM_ABI ~directory_iterator() = default; |
| 76 |
|
| 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(); |
| 81 |
} |
| 82 |
|
| 83 |
_LIBCPP_HIDE_FROM_ABI const directory_entry* operator->() const { return &**this; } |
| 84 |
|
| 85 |
_LIBCPP_HIDE_FROM_ABI directory_iterator& operator++() { return __increment(); } |
| 86 |
|
| 87 |
_LIBCPP_HIDE_FROM_ABI __dir_element_proxy operator++(int) { |
| 88 |
__dir_element_proxy __p(**this); |
| 89 |
__increment(); |
| 90 |
return __p; |
| 91 |
} |
| 92 |
|
| 93 |
_LIBCPP_HIDE_FROM_ABI directory_iterator& increment(error_code& __ec) { return __increment(&__ec); } |
| 94 |
|
| 95 |
# if _LIBCPP_STD_VER >= 20 |
| 96 |
|
| 97 |
_LIBCPP_HIDE_FROM_ABI bool operator==(default_sentinel_t) const noexcept { return *this == directory_iterator(); } |
| 98 |
|
| 99 |
# endif |
| 100 |
|
| 101 |
private: |
| 102 |
inline _LIBCPP_HIDE_FROM_ABI friend bool |
| 103 |
operator==(const directory_iterator& __lhs, const directory_iterator& __rhs) noexcept; |
| 104 |
|
| 105 |
// construct the dir_stream |
| 106 |
_LIBCPP_EXPORTED_FROM_ABI directory_iterator(const path&, error_code*, directory_options = directory_options::none); |
| 107 |
|
| 108 |
_LIBCPP_EXPORTED_FROM_ABI directory_iterator& __increment(error_code* __ec = nullptr); |
| 109 |
|
| 110 |
_LIBCPP_EXPORTED_FROM_ABI const directory_entry& __dereference() const; |
| 111 |
|
| 112 |
private: |
| 113 |
shared_ptr<__dir_stream> __imp_; |
| 114 |
}; |
| 115 |
|
| 116 |
inline _LIBCPP_HIDE_FROM_ABI bool |
| 117 |
operator==(const directory_iterator& __lhs, const directory_iterator& __rhs) noexcept { |
| 118 |
return __lhs.__imp_ == __rhs.__imp_; |
| 119 |
} |
| 120 |
|
| 121 |
inline _LIBCPP_HIDE_FROM_ABI bool |
| 122 |
operator!=(const directory_iterator& __lhs, const directory_iterator& __rhs) noexcept { |
| 123 |
return !(__lhs == __rhs); |
| 124 |
} |
| 125 |
|
| 126 |
// enable directory_iterator range-based for statements |
| 127 |
inline _LIBCPP_HIDE_FROM_ABI directory_iterator begin(directory_iterator __iter) noexcept { return __iter; } |
| 128 |
|
| 129 |
inline _LIBCPP_HIDE_FROM_ABI directory_iterator end(directory_iterator) noexcept { return directory_iterator(); } |
| 130 |
|
| 131 |
_LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_POP |
| 132 |
|
| 133 |
_LIBCPP_END_NAMESPACE_FILESYSTEM |
| 134 |
|
| 135 |
# if _LIBCPP_STD_VER >= 20 |
| 136 |
|
| 137 |
template <> |
| 138 |
_LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY inline constexpr bool |
| 139 |
std::ranges::enable_borrowed_range<std::filesystem::directory_iterator> = true; |
| 140 |
|
| 141 |
template <> |
| 142 |
_LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY inline constexpr bool |
| 143 |
std::ranges::enable_view<std::filesystem::directory_iterator> = true; |
| 144 |
|
| 145 |
# endif // _LIBCPP_STD_VER >= 20 |
| 146 |
|
| 147 |
#endif // _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM) |
| 148 |
|
| 149 |
_LIBCPP_POP_MACROS |
| 150 |
|
| 151 |
#endif // _LIBCPP___FILESYSTEM_DIRECTORY_ITERATOR_H |