basic_parsed_list.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_HTTP_DETAIL_BASIC_PARSED_LIST_HPP
  10. #define BOOST_BEAST_HTTP_DETAIL_BASIC_PARSED_LIST_HPP
  11. #include <boost/beast/core/string.hpp>
  12. #include <boost/core/empty_value.hpp>
  13. #include <cstddef>
  14. #include <iterator>
  15. namespace boost {
  16. namespace beast {
  17. namespace http {
  18. namespace detail {
  19. /** A list parser which presents the sequence as a container.
  20. */
  21. template<class Policy>
  22. class basic_parsed_list
  23. {
  24. string_view s_;
  25. public:
  26. /// The type of policy this list uses for parsing.
  27. using policy_type = Policy;
  28. /// The type of each element in the list.
  29. using value_type = typename Policy::value_type;
  30. /// A constant iterator to a list element.
  31. #if BOOST_BEAST_DOXYGEN
  32. using const_iterator = __implementation_defined__;
  33. #else
  34. class const_iterator;
  35. #endif
  36. class const_iterator
  37. : private boost::empty_value<Policy>
  38. {
  39. basic_parsed_list const* list_ = nullptr;
  40. char const* it_ = nullptr;
  41. typename Policy::value_type v_;
  42. bool error_ = false;
  43. public:
  44. using value_type =
  45. typename Policy::value_type;
  46. using reference = value_type const&;
  47. using pointer = value_type const*;
  48. using difference_type = std::ptrdiff_t;
  49. using iterator_category =
  50. std::forward_iterator_tag;
  51. const_iterator() = default;
  52. bool
  53. operator==(
  54. const_iterator const& other) const
  55. {
  56. return
  57. other.list_ == list_ &&
  58. other.it_ == it_;
  59. }
  60. bool
  61. operator!=(
  62. const_iterator const& other) const
  63. {
  64. return ! (*this == other);
  65. }
  66. reference
  67. operator*() const
  68. {
  69. return v_;
  70. }
  71. const_iterator&
  72. operator++()
  73. {
  74. increment();
  75. return *this;
  76. }
  77. const_iterator
  78. operator++(int)
  79. {
  80. auto temp = *this;
  81. ++(*this);
  82. return temp;
  83. }
  84. bool
  85. error() const
  86. {
  87. return error_;
  88. }
  89. private:
  90. friend class basic_parsed_list;
  91. const_iterator(
  92. basic_parsed_list const& list, bool at_end)
  93. : list_(&list)
  94. , it_(at_end ? nullptr :
  95. list.s_.data())
  96. {
  97. if(! at_end)
  98. increment();
  99. }
  100. void
  101. increment()
  102. {
  103. if(! this->get()(
  104. v_, it_, list_->s_))
  105. {
  106. it_ = nullptr;
  107. error_ = true;
  108. }
  109. }
  110. };
  111. /// Construct a list from a string
  112. explicit
  113. basic_parsed_list(string_view s)
  114. : s_(s)
  115. {
  116. }
  117. /// Return a const iterator to the beginning of the list
  118. const_iterator begin() const;
  119. /// Return a const iterator to the end of the list
  120. const_iterator end() const;
  121. /// Return a const iterator to the beginning of the list
  122. const_iterator cbegin() const;
  123. /// Return a const iterator to the end of the list
  124. const_iterator cend() const;
  125. };
  126. template<class Policy>
  127. inline
  128. auto
  129. basic_parsed_list<Policy>::
  130. begin() const ->
  131. const_iterator
  132. {
  133. return const_iterator{*this, false};
  134. }
  135. template<class Policy>
  136. inline
  137. auto
  138. basic_parsed_list<Policy>::
  139. end() const ->
  140. const_iterator
  141. {
  142. return const_iterator{*this, true};
  143. }
  144. template<class Policy>
  145. inline
  146. auto
  147. basic_parsed_list<Policy>::
  148. cbegin() const ->
  149. const_iterator
  150. {
  151. return const_iterator{*this, false};
  152. }
  153. template<class Policy>
  154. inline
  155. auto
  156. basic_parsed_list<Policy>::
  157. cend() const ->
  158. const_iterator
  159. {
  160. return const_iterator{*this, true};
  161. }
  162. } // detail
  163. } // http
  164. } // beast
  165. } // boost
  166. #endif