Warning: This file is not a C or C++ file. It does not have highlighting.
| 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 | |
| 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 && _LIBCPP_HAS_FILESYSTEM |
| 34 | |
| 35 | _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM |
| 36 | |
| 37 | _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_PUSH |
| 38 | |
| 39 | class _LIBCPP_HIDDEN __dir_stream; |
| 40 | class directory_iterator { |
| 41 | public: |
| 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; |
| 47 | |
| 48 | public: |
| 49 | // ctor & dtor |
| 50 | _LIBCPP_HIDE_FROM_ABI directory_iterator() noexcept {} |
| 51 | |
| 52 | _LIBCPP_HIDE_FROM_ABI explicit directory_iterator(const path& __p) : directory_iterator(__p, nullptr) {} |
| 53 | |
| 54 | _LIBCPP_HIDE_FROM_ABI directory_iterator(const path& __p, directory_options __opts) |
| 55 | : directory_iterator(__p, nullptr, __opts) {} |
| 56 | |
| 57 | _LIBCPP_HIDE_FROM_ABI directory_iterator(const path& __p, error_code& __ec) : directory_iterator(__p, &__ec) {} |
| 58 | |
| 59 | _LIBCPP_HIDE_FROM_ABI directory_iterator(const path& __p, directory_options __opts, error_code& __ec) |
| 60 | : directory_iterator(__p, &__ec, __opts) {} |
| 61 | |
| 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; |
| 65 | |
| 66 | _LIBCPP_HIDE_FROM_ABI directory_iterator& operator=(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 | } |
| 71 | return *this; |
| 72 | } |
| 73 | |
| 74 | _LIBCPP_HIDE_FROM_ABI ~directory_iterator() = default; |
| 75 | |
| 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(); |
| 80 | } |
| 81 | |
| 82 | _LIBCPP_HIDE_FROM_ABI const directory_entry* operator->() const { return &**this; } |
| 83 | |
| 84 | _LIBCPP_HIDE_FROM_ABI directory_iterator& operator++() { return __increment(); } |
| 85 | |
| 86 | _LIBCPP_HIDE_FROM_ABI __dir_element_proxy operator++(int) { |
| 87 | __dir_element_proxy __p(**this); |
| 88 | __increment(); |
| 89 | return __p; |
| 90 | } |
| 91 | |
| 92 | _LIBCPP_HIDE_FROM_ABI directory_iterator& increment(error_code& __ec) { return __increment(&__ec); } |
| 93 | |
| 94 | # if _LIBCPP_STD_VER >= 20 |
| 95 | |
| 96 | _LIBCPP_HIDE_FROM_ABI bool operator==(default_sentinel_t) const noexcept { return *this == directory_iterator(); } |
| 97 | |
| 98 | # endif |
| 99 | |
| 100 | private: |
| 101 | inline _LIBCPP_HIDE_FROM_ABI friend bool |
| 102 | operator==(const directory_iterator& __lhs, const directory_iterator& __rhs) noexcept; |
| 103 | |
| 104 | // construct the dir_stream |
| 105 | _LIBCPP_EXPORTED_FROM_ABI directory_iterator(const path&, error_code*, directory_options = directory_options::none); |
| 106 | |
| 107 | _LIBCPP_EXPORTED_FROM_ABI directory_iterator& __increment(error_code* __ec = nullptr); |
| 108 | |
| 109 | _LIBCPP_EXPORTED_FROM_ABI const directory_entry& __dereference() const; |
| 110 | |
| 111 | private: |
| 112 | shared_ptr<__dir_stream> __imp_; |
| 113 | }; |
| 114 | |
| 115 | inline _LIBCPP_HIDE_FROM_ABI bool |
| 116 | operator==(const directory_iterator& __lhs, const directory_iterator& __rhs) noexcept { |
| 117 | return __lhs.__imp_ == __rhs.__imp_; |
| 118 | } |
| 119 | |
| 120 | inline _LIBCPP_HIDE_FROM_ABI bool |
| 121 | operator!=(const directory_iterator& __lhs, const directory_iterator& __rhs) noexcept { |
| 122 | return !(__lhs == __rhs); |
| 123 | } |
| 124 | |
| 125 | // enable directory_iterator range-based for statements |
| 126 | inline _LIBCPP_HIDE_FROM_ABI directory_iterator begin(directory_iterator __iter) noexcept { return __iter; } |
| 127 | |
| 128 | inline _LIBCPP_HIDE_FROM_ABI directory_iterator end(directory_iterator) noexcept { return directory_iterator(); } |
| 129 | |
| 130 | _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_POP |
| 131 | |
| 132 | _LIBCPP_END_NAMESPACE_FILESYSTEM |
| 133 | |
| 134 | # if _LIBCPP_STD_VER >= 20 |
| 135 | |
| 136 | template <> |
| 137 | _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY inline constexpr bool |
| 138 | std::ranges::enable_borrowed_range<std::filesystem::directory_iterator> = true; |
| 139 | |
| 140 | template <> |
| 141 | _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY inline constexpr bool |
| 142 | std::ranges::enable_view<std::filesystem::directory_iterator> = true; |
| 143 | |
| 144 | # endif // _LIBCPP_STD_VER >= 20 |
| 145 | |
| 146 | #endif // _LIBCPP_STD_VER >= 17 && _LIBCPP_HAS_FILESYSTEM |
| 147 | |
| 148 | _LIBCPP_POP_MACROS |
| 149 | |
| 150 | #endif // _LIBCPP___FILESYSTEM_DIRECTORY_ITERATOR_H |
| 151 |
Warning: This file is not a C or C++ file. It does not have highlighting.
