segments_base.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. class segments_base::iterator
  18. {
  19. detail::segments_iter_impl it_;
  20. friend class segments_base;
  21. friend class segments_ref;
  22. iterator(detail::path_ref const&) noexcept;
  23. iterator(detail::path_ref const&, int) noexcept;
  24. iterator(
  25. detail::segments_iter_impl const& it) noexcept
  26. : it_(it)
  27. {
  28. }
  29. public:
  30. using value_type = segments_base::value_type;
  31. using reference = segments_base::reference;
  32. using pointer = reference;
  33. using difference_type =
  34. segments_base::difference_type;
  35. using iterator_category =
  36. std::bidirectional_iterator_tag;
  37. iterator() = default;
  38. iterator(iterator const&) = default;
  39. iterator& operator=(
  40. iterator const&) noexcept = default;
  41. BOOST_URL_DECL
  42. reference
  43. operator*() const;
  44. // the return value is too expensive
  45. pointer operator->() const = delete;
  46. iterator&
  47. operator++() noexcept
  48. {
  49. it_.increment();
  50. return *this;
  51. }
  52. iterator&
  53. operator--() noexcept
  54. {
  55. it_.decrement();
  56. return *this;
  57. }
  58. iterator
  59. operator++(int) noexcept
  60. {
  61. auto tmp = *this;
  62. ++*this;
  63. return tmp;
  64. }
  65. iterator
  66. operator--(int) noexcept
  67. {
  68. auto tmp = *this;
  69. --*this;
  70. return tmp;
  71. }
  72. bool
  73. operator==(
  74. iterator const& other) const noexcept
  75. {
  76. return it_.equal(other.it_);
  77. }
  78. bool
  79. operator!=(
  80. iterator const& other) const noexcept
  81. {
  82. return ! it_.equal(other.it_);
  83. }
  84. };
  85. //------------------------------------------------
  86. inline
  87. std::string
  88. segments_base::
  89. front() const noexcept
  90. {
  91. BOOST_ASSERT(! empty());
  92. return *begin();
  93. }
  94. inline
  95. std::string
  96. segments_base::
  97. back() const noexcept
  98. {
  99. BOOST_ASSERT(! empty());
  100. return *--end();
  101. }
  102. } // urls
  103. } // boost
  104. #endif