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