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 #ifndef _LIBCPP_CXX03_LANG
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_INLINE_VISIBILITY
53   iterator()
54       : __stashed_elem_(), __path_ptr_(nullptr), __entry_(),
55         __state_(_Singular) {}
56 
57   _LIBCPP_HIDE_FROM_ABI iterator(const iterator&) = default;
58   _LIBCPP_HIDE_FROM_ABI ~iterator() = default;
59 
60   _LIBCPP_HIDE_FROM_ABI iterator& operator=(const iterator&) = default;
61 
62   _LIBCPP_INLINE_VISIBILITY
63   reference operator*() const { return __stashed_elem_; }
64 
65   _LIBCPP_INLINE_VISIBILITY
66   pointer operator->() const { return &__stashed_elem_; }
67 
68   _LIBCPP_INLINE_VISIBILITY
69   iterator& operator++() {
70     _LIBCPP_ASSERT_UNCATEGORIZED(__state_ != _Singular,
71                                  "attempting to increment a singular iterator");
72     _LIBCPP_ASSERT_UNCATEGORIZED(__state_ != _AtEnd,
73                                  "attempting to increment the end iterator");
74     return __increment();
75   }
76 
77   _LIBCPP_INLINE_VISIBILITY
78   iterator operator++(int) {
79     iterator __it(*this);
80     this->operator++();
81     return __it;
82   }
83 
84   _LIBCPP_INLINE_VISIBILITY
85   iterator& operator--() {
86     _LIBCPP_ASSERT_UNCATEGORIZED(__state_ != _Singular,
87                                  "attempting to decrement a singular iterator");
88     _LIBCPP_ASSERT_UNCATEGORIZED(__entry_.data() != __path_ptr_->native().data(),
89                                  "attempting to decrement the begin iterator");
90     return __decrement();
91   }
92 
93   _LIBCPP_INLINE_VISIBILITY
94   iterator operator--(int) {
95     iterator __it(*this);
96     this->operator--();
97     return __it;
98   }
99 
100 private:
101   friend class path;
102 
103   inline _LIBCPP_INLINE_VISIBILITY friend bool operator==(const iterator&,
104                                                           const iterator&);
105 
106   iterator& __increment();
107   iterator& __decrement();
108 
109   path __stashed_elem_;
110   const path* __path_ptr_;
111   path::__string_view __entry_;
112   _ParserState __state_;
113 };
114 
115 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
116 inline _LIBCPP_INLINE_VISIBILITY bool operator==(const path::iterator& __lhs,
117                                                  const path::iterator& __rhs) {
118   return __lhs.__path_ptr_ == __rhs.__path_ptr_ &&
119          __lhs.__entry_.data() == __rhs.__entry_.data();
120 }
121 
122 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
123 inline _LIBCPP_INLINE_VISIBILITY bool operator!=(const path::iterator& __lhs,
124                                                  const path::iterator& __rhs) {
125   return !(__lhs == __rhs);
126 }
127 
128 _LIBCPP_END_NAMESPACE_FILESYSTEM
129 
130 #endif // _LIBCPP_CXX03_LANG
131 
132 #endif // _LIBCPP___FILESYSTEM_PATH_ITERATOR_H
133