xref: /freebsd/contrib/llvm-project/libcxx/include/__filesystem/path_iterator.h (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
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_PATH_ITERATOR_H
11 #define _LIBCPP___FILESYSTEM_PATH_ITERATOR_H
12 
13 #include <__assert>
14 #include <__availability>
15 #include <__config>
16 #include <__filesystem/path.h>
17 #include <__iterator/iterator_traits.h>
18 #include <cstddef>
19 #include <string>
20 #include <string_view>
21 
22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23 #  pragma GCC system_header
24 #endif
25 
26 #if _LIBCPP_STD_VER >= 17
27 
28 _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
29 
30 class _LIBCPP_EXPORTED_FROM_ABI path::iterator {
31 public:
32   enum _ParserState : unsigned char {
33     _Singular,
34     _BeforeBegin,
35     _InRootName,
36     _InRootDir,
37     _InFilenames,
38     _InTrailingSep,
39     _AtEnd
40   };
41 
42 public:
43   typedef input_iterator_tag iterator_category;
44   typedef bidirectional_iterator_tag iterator_concept;
45 
46   typedef path value_type;
47   typedef ptrdiff_t difference_type;
48   typedef const path* pointer;
49   typedef path reference;
50 
51 public:
52   _LIBCPP_HIDE_FROM_ABI iterator() : __stashed_elem_(), __path_ptr_(nullptr), __entry_(), __state_(_Singular) {}
53 
54   _LIBCPP_HIDE_FROM_ABI iterator(const iterator&) = default;
55   _LIBCPP_HIDE_FROM_ABI ~iterator()               = default;
56 
57   _LIBCPP_HIDE_FROM_ABI iterator& operator=(const iterator&) = default;
58 
59   _LIBCPP_HIDE_FROM_ABI reference operator*() const { return __stashed_elem_; }
60 
61   _LIBCPP_HIDE_FROM_ABI pointer operator->() const { return &__stashed_elem_; }
62 
63   _LIBCPP_HIDE_FROM_ABI iterator& operator++() {
64     _LIBCPP_ASSERT_NON_NULL(__state_ != _Singular, "attempting to increment a singular iterator");
65     _LIBCPP_ASSERT_UNCATEGORIZED(__state_ != _AtEnd, "attempting to increment the end iterator");
66     return __increment();
67   }
68 
69   _LIBCPP_HIDE_FROM_ABI iterator operator++(int) {
70     iterator __it(*this);
71     this->operator++();
72     return __it;
73   }
74 
75   _LIBCPP_HIDE_FROM_ABI iterator& operator--() {
76     _LIBCPP_ASSERT_NON_NULL(__state_ != _Singular, "attempting to decrement a singular iterator");
77     _LIBCPP_ASSERT_UNCATEGORIZED(
78         __entry_.data() != __path_ptr_->native().data(), "attempting to decrement the begin iterator");
79     return __decrement();
80   }
81 
82   _LIBCPP_HIDE_FROM_ABI iterator operator--(int) {
83     iterator __it(*this);
84     this->operator--();
85     return __it;
86   }
87 
88 private:
89   friend class path;
90 
91   inline _LIBCPP_HIDE_FROM_ABI friend bool operator==(const iterator&, const iterator&);
92 
93   iterator& __increment();
94   iterator& __decrement();
95 
96   path __stashed_elem_;
97   const path* __path_ptr_;
98   path::__string_view __entry_;
99   _ParserState __state_;
100 };
101 
102 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
103 inline _LIBCPP_HIDE_FROM_ABI bool operator==(const path::iterator& __lhs, const path::iterator& __rhs) {
104   return __lhs.__path_ptr_ == __rhs.__path_ptr_ && __lhs.__entry_.data() == __rhs.__entry_.data();
105 }
106 
107 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
108 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const path::iterator& __lhs, const path::iterator& __rhs) {
109   return !(__lhs == __rhs);
110 }
111 
112 _LIBCPP_END_NAMESPACE_FILESYSTEM
113 
114 #endif // _LIBCPP_STD_VER >= 17
115 
116 #endif // _LIBCPP___FILESYSTEM_PATH_ITERATOR_H
117