5fd41c2127fdaa2acdd353898d6ea7cd55a6ec98
[gcc.git] / libstdc++-v3 / include / experimental / bits / fs_dir.h
1 // Filesystem directory utilities -*- C++ -*-
2
3 // Copyright (C) 2014-2016 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /** @file experimental/bits/fs_dir.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{experimental/filesystem}
28 */
29
30 #ifndef _GLIBCXX_EXPERIMENTAL_FS_DIR_H
31 #define _GLIBCXX_EXPERIMENTAL_FS_DIR_H 1
32
33 #if __cplusplus < 201103L
34 # include <bits/c++0x_warning.h>
35 #else
36 # include <typeinfo>
37 # include <ext/concurrence.h>
38 # include <bits/unique_ptr.h>
39 # include <bits/shared_ptr.h>
40
41 namespace std _GLIBCXX_VISIBILITY(default)
42 {
43 namespace experimental
44 {
45 namespace filesystem
46 {
47 inline namespace v1
48 {
49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50
51 /**
52 * @ingroup filesystem
53 * @{
54 */
55
56 class file_status
57 {
58 public:
59 // constructors
60 explicit
61 file_status(file_type __ft = file_type::none,
62 perms __prms = perms::unknown) noexcept
63 : _M_type(__ft), _M_perms(__prms) { }
64
65 file_status(const file_status&) noexcept = default;
66 file_status(file_status&&) noexcept = default;
67 ~file_status() = default;
68
69 file_status& operator=(const file_status&) noexcept = default;
70 file_status& operator=(file_status&&) noexcept = default;
71
72 // observers
73 file_type type() const noexcept { return _M_type; }
74 perms permissions() const noexcept { return _M_perms; }
75
76 // modifiers
77 void type(file_type __ft) noexcept { _M_type = __ft; }
78 void permissions(perms __prms) noexcept { _M_perms = __prms; }
79
80 private:
81 file_type _M_type;
82 perms _M_perms;
83 };
84
85 _GLIBCXX_BEGIN_NAMESPACE_CXX11
86
87 class directory_entry
88 {
89 public:
90 // constructors and destructor
91 directory_entry() noexcept = default;
92 directory_entry(const directory_entry&) = default;
93 directory_entry(directory_entry&&) noexcept = default;
94 explicit directory_entry(const filesystem::path& __p) : _M_path(__p) { }
95 ~directory_entry() = default;
96
97 // modifiers
98 directory_entry& operator=(const directory_entry&) = default;
99 directory_entry& operator=(directory_entry&&) noexcept = default;
100
101 void assign(const filesystem::path& __p) { _M_path = __p; }
102
103 void
104 replace_filename(const filesystem::path& __p)
105 { _M_path = _M_path.parent_path() / __p; }
106
107 // observers
108 const filesystem::path& path() const noexcept { return _M_path; }
109 operator const filesystem::path&() const noexcept { return _M_path; }
110
111 file_status
112 status() const
113 { return filesystem::status(_M_path); }
114
115 file_status
116 status(error_code& __ec) const noexcept
117 { return filesystem::status(_M_path, __ec); }
118
119 file_status
120 symlink_status() const
121 { return filesystem::symlink_status(_M_path); }
122
123 file_status
124 symlink_status(error_code& __ec) const noexcept
125 { return filesystem::symlink_status(_M_path, __ec); }
126
127 bool
128 operator< (const directory_entry& __rhs) const noexcept
129 { return _M_path < __rhs._M_path; }
130
131 bool
132 operator==(const directory_entry& __rhs) const noexcept
133 { return _M_path == __rhs._M_path; }
134
135 bool
136 operator!=(const directory_entry& __rhs) const noexcept
137 { return _M_path != __rhs._M_path; }
138
139 bool
140 operator<=(const directory_entry& __rhs) const noexcept
141 { return _M_path <= __rhs._M_path; }
142
143 bool
144 operator> (const directory_entry& __rhs) const noexcept
145 { return _M_path > __rhs._M_path; }
146
147 bool
148 operator>=(const directory_entry& __rhs) const noexcept
149 { return _M_path >= __rhs._M_path; }
150
151 private:
152 filesystem::path _M_path;
153 };
154
155 struct _Dir;
156 class directory_iterator;
157 class recursive_directory_iterator;
158
159 struct __directory_iterator_proxy
160 {
161 const directory_entry& operator*() const noexcept { return _M_entry; }
162
163 private:
164 friend class directory_iterator;
165 friend class recursive_directory_iterator;
166
167 explicit
168 __directory_iterator_proxy(const directory_entry& __e) : _M_entry(__e) { }
169
170 directory_entry _M_entry;
171 };
172
173 class directory_iterator
174 {
175 public:
176 typedef directory_entry value_type;
177 typedef ptrdiff_t difference_type;
178 typedef const directory_entry* pointer;
179 typedef const directory_entry& reference;
180 typedef input_iterator_tag iterator_category;
181
182 directory_iterator() noexcept = default;
183
184 explicit
185 directory_iterator(const path& __p)
186 : directory_iterator(__p, directory_options::none, nullptr) { }
187
188 directory_iterator(const path& __p, directory_options __options)
189 : directory_iterator(__p, __options, nullptr) { }
190
191 directory_iterator(const path& __p, error_code& __ec) noexcept
192 : directory_iterator(__p, directory_options::none, __ec) { }
193
194 directory_iterator(const path& __p,
195 directory_options __options,
196 error_code& __ec) noexcept
197 : directory_iterator(__p, __options, &__ec) { }
198
199 directory_iterator(const directory_iterator& __rhs) = default;
200
201 directory_iterator(directory_iterator&& __rhs) noexcept = default;
202
203 ~directory_iterator() = default;
204
205 directory_iterator&
206 operator=(const directory_iterator& __rhs) = default;
207
208 directory_iterator&
209 operator=(directory_iterator&& __rhs) noexcept = default;
210
211 const directory_entry& operator*() const;
212 const directory_entry* operator->() const { return &**this; }
213 directory_iterator& operator++();
214 directory_iterator& increment(error_code& __ec) noexcept;
215
216 __directory_iterator_proxy operator++(int)
217 {
218 __directory_iterator_proxy __pr{**this};
219 ++*this;
220 return __pr;
221 }
222
223 private:
224 directory_iterator(const path&, directory_options, error_code*);
225
226 friend bool
227 operator==(const directory_iterator& __lhs,
228 const directory_iterator& __rhs);
229
230 friend class recursive_directory_iterator;
231
232 std::shared_ptr<_Dir> _M_dir;
233 };
234
235 inline directory_iterator
236 begin(directory_iterator __iter) { return __iter; }
237
238 inline directory_iterator
239 end(directory_iterator) { return directory_iterator(); }
240
241 inline bool
242 operator==(const directory_iterator& __lhs, const directory_iterator& __rhs)
243 {
244 return !__rhs._M_dir.owner_before(__lhs._M_dir)
245 && !__lhs._M_dir.owner_before(__rhs._M_dir);
246 }
247
248 inline bool
249 operator!=(const directory_iterator& __lhs, const directory_iterator& __rhs)
250 { return !(__lhs == __rhs); }
251
252 class recursive_directory_iterator
253 {
254 public:
255 typedef directory_entry value_type;
256 typedef ptrdiff_t difference_type;
257 typedef const directory_entry* pointer;
258 typedef const directory_entry& reference;
259 typedef input_iterator_tag iterator_category;
260
261 recursive_directory_iterator() noexcept = default;
262
263 explicit
264 recursive_directory_iterator(const path& __p)
265 : recursive_directory_iterator(__p, directory_options::none, nullptr) { }
266
267 recursive_directory_iterator(const path& __p, directory_options __options)
268 : recursive_directory_iterator(__p, __options, nullptr) { }
269
270 recursive_directory_iterator(const path& __p,
271 directory_options __options,
272 error_code& __ec) noexcept
273 : recursive_directory_iterator(__p, __options, &__ec) { }
274
275 recursive_directory_iterator(const path& __p, error_code& __ec) noexcept
276 : recursive_directory_iterator(__p, directory_options::none, &__ec) { }
277
278 recursive_directory_iterator(
279 const recursive_directory_iterator&) = default;
280
281 recursive_directory_iterator(
282 recursive_directory_iterator&&) noexcept = default;
283
284 ~recursive_directory_iterator();
285
286 // observers
287 directory_options options() const { return _M_options; }
288 int depth() const;
289 bool recursion_pending() const { return _M_pending; }
290
291 const directory_entry& operator*() const;
292 const directory_entry* operator->() const { return &**this; }
293
294 // modifiers
295 recursive_directory_iterator&
296 operator=(const recursive_directory_iterator& __rhs) noexcept;
297 recursive_directory_iterator&
298 operator=(recursive_directory_iterator&& __rhs) noexcept;
299
300 recursive_directory_iterator& operator++();
301 recursive_directory_iterator& increment(error_code& __ec) noexcept;
302
303 __directory_iterator_proxy operator++(int)
304 {
305 __directory_iterator_proxy __pr{**this};
306 ++*this;
307 return __pr;
308 }
309
310 void pop();
311
312 void disable_recursion_pending() { _M_pending = false; }
313
314 private:
315 recursive_directory_iterator(const path&, directory_options, error_code*);
316
317 friend bool
318 operator==(const recursive_directory_iterator& __lhs,
319 const recursive_directory_iterator& __rhs);
320
321 struct _Dir_stack;
322 std::shared_ptr<_Dir_stack> _M_dirs;
323 directory_options _M_options = {};
324 bool _M_pending = false;
325 };
326
327 inline recursive_directory_iterator
328 begin(recursive_directory_iterator __iter) { return __iter; }
329
330 inline recursive_directory_iterator
331 end(recursive_directory_iterator) { return recursive_directory_iterator(); }
332
333 inline bool
334 operator==(const recursive_directory_iterator& __lhs,
335 const recursive_directory_iterator& __rhs)
336 {
337 return !__rhs._M_dirs.owner_before(__lhs._M_dirs)
338 && !__lhs._M_dirs.owner_before(__rhs._M_dirs);
339 }
340
341 inline bool
342 operator!=(const recursive_directory_iterator& __lhs,
343 const recursive_directory_iterator& __rhs)
344 { return !(__lhs == __rhs); }
345
346 _GLIBCXX_END_NAMESPACE_CXX11
347
348 // @} group filesystem
349 _GLIBCXX_END_NAMESPACE_VERSION
350 } // namespace v1
351 } // namespace filesystem
352 } // namespace experimental
353 } // namespace std
354
355 #endif // C++11
356
357 #endif // _GLIBCXX_EXPERIMENTAL_FS_DIR_H