path_iterator.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. class _LIBCPP_EXPORTED_FROM_ABI path::iterator {
  25. public:
  26. enum _ParserState : unsigned char {
  27. _Singular,
  28. _BeforeBegin,
  29. _InRootName,
  30. _InRootDir,
  31. _InFilenames,
  32. _InTrailingSep,
  33. _AtEnd
  34. };
  35. public:
  36. typedef input_iterator_tag iterator_category;
  37. typedef bidirectional_iterator_tag iterator_concept;
  38. typedef path value_type;
  39. typedef ptrdiff_t difference_type;
  40. typedef const path* pointer;
  41. typedef path reference;
  42. public:
  43. _LIBCPP_INLINE_VISIBILITY
  44. iterator()
  45. : __stashed_elem_(), __path_ptr_(nullptr), __entry_(),
  46. __state_(_Singular) {}
  47. _LIBCPP_HIDE_FROM_ABI iterator(const iterator&) = default;
  48. _LIBCPP_HIDE_FROM_ABI ~iterator() = default;
  49. _LIBCPP_HIDE_FROM_ABI iterator& operator=(const iterator&) = default;
  50. _LIBCPP_INLINE_VISIBILITY
  51. reference operator*() const { return __stashed_elem_; }
  52. _LIBCPP_INLINE_VISIBILITY
  53. pointer operator->() const { return &__stashed_elem_; }
  54. _LIBCPP_INLINE_VISIBILITY
  55. iterator& operator++() {
  56. _LIBCPP_ASSERT_UNCATEGORIZED(__state_ != _Singular,
  57. "attempting to increment a singular iterator");
  58. _LIBCPP_ASSERT_UNCATEGORIZED(__state_ != _AtEnd,
  59. "attempting to increment the end iterator");
  60. return __increment();
  61. }
  62. _LIBCPP_INLINE_VISIBILITY
  63. iterator operator++(int) {
  64. iterator __it(*this);
  65. this->operator++();
  66. return __it;
  67. }
  68. _LIBCPP_INLINE_VISIBILITY
  69. iterator& operator--() {
  70. _LIBCPP_ASSERT_UNCATEGORIZED(__state_ != _Singular,
  71. "attempting to decrement a singular iterator");
  72. _LIBCPP_ASSERT_UNCATEGORIZED(__entry_.data() != __path_ptr_->native().data(),
  73. "attempting to decrement the begin iterator");
  74. return __decrement();
  75. }
  76. _LIBCPP_INLINE_VISIBILITY
  77. iterator operator--(int) {
  78. iterator __it(*this);
  79. this->operator--();
  80. return __it;
  81. }
  82. private:
  83. friend class path;
  84. inline _LIBCPP_INLINE_VISIBILITY friend bool operator==(const iterator&,
  85. const iterator&);
  86. iterator& __increment();
  87. iterator& __decrement();
  88. path __stashed_elem_;
  89. const path* __path_ptr_;
  90. path::__string_view __entry_;
  91. _ParserState __state_;
  92. };
  93. _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
  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. _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
  100. inline _LIBCPP_INLINE_VISIBILITY bool operator!=(const path::iterator& __lhs,
  101. const path::iterator& __rhs) {
  102. return !(__lhs == __rhs);
  103. }
  104. _LIBCPP_END_NAMESPACE_FILESYSTEM
  105. #endif // _LIBCPP_CXX03_LANG
  106. #endif // _LIBCPP___FILESYSTEM_PATH_ITERATOR_H