Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / include / __filesystem / directory_iterator.h
blob632c2c806369b807a3c020a0fc94a586c62ea2ea
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 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___FILESYSTEM_DIRECTORY_ITERATOR_H
11 #define _LIBCPP___FILESYSTEM_DIRECTORY_ITERATOR_H
13 #include <__assert>
14 #include <__availability>
15 #include <__config>
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>
26 #include <cstddef>
28 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
29 # pragma GCC system_header
30 #endif
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 {
40 public:
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;
47 public:
48 //ctor & dtor
49 _LIBCPP_HIDE_FROM_ABI
50 directory_iterator() noexcept {}
52 _LIBCPP_HIDE_FROM_ABI
53 explicit directory_iterator(const path& __p)
54 : directory_iterator(__p, nullptr) {}
56 _LIBCPP_HIDE_FROM_ABI
57 directory_iterator(const path& __p, directory_options __opts)
58 : directory_iterator(__p, nullptr, __opts) {}
60 _LIBCPP_HIDE_FROM_ABI
61 directory_iterator(const path& __p, error_code& __ec)
62 : directory_iterator(__p, &__ec) {}
64 _LIBCPP_HIDE_FROM_ABI
65 directory_iterator(const path& __p, directory_options __opts,
66 error_code& __ec)
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;
73 _LIBCPP_HIDE_FROM_ABI
74 directory_iterator& operator=(directory_iterator&& __o) noexcept {
75 // non-default implementation provided to support self-move assign.
76 if (this != &__o) {
77 __imp_ = _VSTD::move(__o.__imp_);
79 return *this;
82 _LIBCPP_HIDE_FROM_ABI ~directory_iterator() = default;
84 _LIBCPP_HIDE_FROM_ABI
85 const directory_entry& operator*() const {
86 _LIBCPP_ASSERT_UNCATEGORIZED(__imp_, "The end iterator cannot be dereferenced");
87 return __dereference();
90 _LIBCPP_HIDE_FROM_ABI
91 const directory_entry* operator->() const { return &**this; }
93 _LIBCPP_HIDE_FROM_ABI
94 directory_iterator& operator++() { return __increment(); }
96 _LIBCPP_HIDE_FROM_ABI
97 __dir_element_proxy operator++(int) {
98 __dir_element_proxy __p(**this);
99 __increment();
100 return __p;
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(); }
110 # endif
112 private:
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;
124 private:
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 {
143 return __iter;
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
157 template <>
158 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
159 inline constexpr bool _VSTD::ranges::enable_borrowed_range<_VSTD_FS::directory_iterator> = true;
161 template <>
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