path_iterator.h 3.5 KB

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