segments_base.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
  3. // Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.com)
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // Official repository: https://github.com/boostorg/url
  9. //
  10. #ifndef BOOST_URL_IMPL_SEGMENTS_BASE_HPP
  11. #define BOOST_URL_IMPL_SEGMENTS_BASE_HPP
  12. #include <boost/url/detail/segments_iter_impl.hpp>
  13. #include <boost/assert.hpp>
  14. #include <iterator>
  15. namespace boost {
  16. namespace urls {
  17. namespace detail {
  18. struct segments_iter_access;
  19. }
  20. class segments_base::iterator
  21. {
  22. detail::segments_iter_impl it_;
  23. friend class segments_base;
  24. friend class segments_ref;
  25. friend struct detail::segments_iter_access;
  26. iterator(detail::path_ref const&) noexcept;
  27. iterator(detail::path_ref const&, int) noexcept;
  28. iterator(
  29. detail::segments_iter_impl const& it) noexcept
  30. : it_(it)
  31. {
  32. }
  33. public:
  34. using value_type = segments_base::value_type;
  35. using reference = segments_base::reference;
  36. using pointer = reference;
  37. using difference_type =
  38. segments_base::difference_type;
  39. using iterator_category =
  40. std::bidirectional_iterator_tag;
  41. iterator() = default;
  42. iterator(iterator const&) = default;
  43. iterator& operator=(
  44. iterator const&) noexcept = default;
  45. BOOST_URL_DECL
  46. reference
  47. operator*() const;
  48. // the return value is too expensive
  49. pointer operator->() const = delete;
  50. iterator&
  51. operator++() noexcept
  52. {
  53. it_.increment();
  54. return *this;
  55. }
  56. iterator&
  57. operator--() noexcept
  58. {
  59. it_.decrement();
  60. return *this;
  61. }
  62. iterator
  63. operator++(int) noexcept
  64. {
  65. auto tmp = *this;
  66. ++*this;
  67. return tmp;
  68. }
  69. iterator
  70. operator--(int) noexcept
  71. {
  72. auto tmp = *this;
  73. --*this;
  74. return tmp;
  75. }
  76. bool
  77. operator==(
  78. iterator const& other) const noexcept
  79. {
  80. return it_.equal(other.it_);
  81. }
  82. bool
  83. operator!=(
  84. iterator const& other) const noexcept
  85. {
  86. return ! it_.equal(other.it_);
  87. }
  88. };
  89. //------------------------------------------------
  90. inline
  91. std::string
  92. segments_base::
  93. front() const noexcept
  94. {
  95. BOOST_ASSERT(! empty());
  96. return *begin();
  97. }
  98. inline
  99. std::string
  100. segments_base::
  101. back() const noexcept
  102. {
  103. BOOST_ASSERT(! empty());
  104. return *--end();
  105. }
  106. } // urls
  107. } // boost
  108. #endif