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 <__filesystem/path.h>
16 #include <__iterator/iterator_traits.h>
17 #include <__debug>
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 bidirectional_iterator_tag iterator_category;
42 
43   typedef path value_type;
44   typedef ptrdiff_t difference_type;
45   typedef const path* pointer;
46   typedef const path& reference;
47 
48   typedef void
49       __stashing_iterator_tag; // See reverse_iterator and __is_stashing_iterator
50 
51 public:
52   _LIBCPP_INLINE_VISIBILITY
53   iterator()
54       : __stashed_elem_(), __path_ptr_(nullptr), __entry_(),
55         __state_(_Singular) {}
56 
57   iterator(const iterator&) = default;
58   ~iterator() = default;
59 
60   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(__state_ != _Singular,
71                    "attempting to increment a singular iterator");
72     _LIBCPP_ASSERT(__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(__state_ != _Singular,
87                    "attempting to decrement a singular iterator");
88     _LIBCPP_ASSERT(__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 inline _LIBCPP_INLINE_VISIBILITY bool operator==(const path::iterator& __lhs,
116                                                  const path::iterator& __rhs) {
117   return __lhs.__path_ptr_ == __rhs.__path_ptr_ &&
118          __lhs.__entry_.data() == __rhs.__entry_.data();
119 }
120 
121 inline _LIBCPP_INLINE_VISIBILITY bool operator!=(const path::iterator& __lhs,
122                                                  const path::iterator& __rhs) {
123   return !(__lhs == __rhs);
124 }
125 
126 _LIBCPP_AVAILABILITY_FILESYSTEM_POP
127 
128 _LIBCPP_END_NAMESPACE_FILESYSTEM
129 
130 #endif // _LIBCPP_CXX03_LANG
131 
132 #endif // _LIBCPP___FILESYSTEM_PATH_ITERATOR_H
133