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 _LIBCPP_AVAILABILITY_FILESYSTEM_PUSH
31 
32 class _LIBCPP_TYPE_VIS path::iterator {
33 public:
34   enum _ParserState : unsigned char {
35     _Singular,
36     _BeforeBegin,
37     _InRootName,
38     _InRootDir,
39     _InFilenames,
40     _InTrailingSep,
41     _AtEnd
42   };
43 
44 public:
45   typedef input_iterator_tag iterator_category;
46   typedef bidirectional_iterator_tag iterator_concept;
47 
48   typedef path value_type;
49   typedef ptrdiff_t difference_type;
50   typedef const path* pointer;
51   typedef path reference;
52 
53 public:
54   _LIBCPP_INLINE_VISIBILITY
55   iterator()
56       : __stashed_elem_(), __path_ptr_(nullptr), __entry_(),
57         __state_(_Singular) {}
58 
59   iterator(const iterator&) = default;
60   ~iterator() = default;
61 
62   iterator& operator=(const iterator&) = default;
63 
64   _LIBCPP_INLINE_VISIBILITY
65   reference operator*() const { return __stashed_elem_; }
66 
67   _LIBCPP_INLINE_VISIBILITY
68   pointer operator->() const { return &__stashed_elem_; }
69 
70   _LIBCPP_INLINE_VISIBILITY
71   iterator& operator++() {
72     _LIBCPP_ASSERT(__state_ != _Singular,
73                    "attempting to increment a singular iterator");
74     _LIBCPP_ASSERT(__state_ != _AtEnd,
75                    "attempting to increment the end iterator");
76     return __increment();
77   }
78 
79   _LIBCPP_INLINE_VISIBILITY
80   iterator operator++(int) {
81     iterator __it(*this);
82     this->operator++();
83     return __it;
84   }
85 
86   _LIBCPP_INLINE_VISIBILITY
87   iterator& operator--() {
88     _LIBCPP_ASSERT(__state_ != _Singular,
89                    "attempting to decrement a singular iterator");
90     _LIBCPP_ASSERT(__entry_.data() != __path_ptr_->native().data(),
91                    "attempting to decrement the begin iterator");
92     return __decrement();
93   }
94 
95   _LIBCPP_INLINE_VISIBILITY
96   iterator operator--(int) {
97     iterator __it(*this);
98     this->operator--();
99     return __it;
100   }
101 
102 private:
103   friend class path;
104 
105   inline _LIBCPP_INLINE_VISIBILITY friend bool operator==(const iterator&,
106                                                           const iterator&);
107 
108   iterator& __increment();
109   iterator& __decrement();
110 
111   path __stashed_elem_;
112   const path* __path_ptr_;
113   path::__string_view __entry_;
114   _ParserState __state_;
115 };
116 
117 inline _LIBCPP_INLINE_VISIBILITY bool operator==(const path::iterator& __lhs,
118                                                  const path::iterator& __rhs) {
119   return __lhs.__path_ptr_ == __rhs.__path_ptr_ &&
120          __lhs.__entry_.data() == __rhs.__entry_.data();
121 }
122 
123 inline _LIBCPP_INLINE_VISIBILITY bool operator!=(const path::iterator& __lhs,
124                                                  const path::iterator& __rhs) {
125   return !(__lhs == __rhs);
126 }
127 
128 _LIBCPP_AVAILABILITY_FILESYSTEM_POP
129 
130 _LIBCPP_END_NAMESPACE_FILESYSTEM
131 
132 #endif // _LIBCPP_CXX03_LANG
133 
134 #endif // _LIBCPP___FILESYSTEM_PATH_ITERATOR_H
135