| 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 | |
| 10 | #ifndef BOOST_BEAST_HTTP_DETAIL_BASIC_PARSED_LIST_HPP |
| 11 | #define BOOST_BEAST_HTTP_DETAIL_BASIC_PARSED_LIST_HPP |
| 12 | |
| 13 | #include <boost/beast/core/string.hpp> |
| 14 | #include <boost/core/empty_value.hpp> |
| 15 | #include <cstddef> |
| 16 | #include <iterator> |
| 17 | |
| 18 | namespace boost { |
| 19 | namespace beast { |
| 20 | namespace http { |
| 21 | namespace detail { |
| 22 | |
| 23 | /** A list parser which presents the sequence as a container. |
| 24 | */ |
| 25 | template<class Policy> |
| 26 | class basic_parsed_list |
| 27 | { |
| 28 | string_view s_; |
| 29 | |
| 30 | public: |
| 31 | /// The type of policy this list uses for parsing. |
| 32 | using policy_type = Policy; |
| 33 | |
| 34 | /// The type of each element in the list. |
| 35 | using value_type = typename Policy::value_type; |
| 36 | |
| 37 | /// A constant iterator to a list element. |
| 38 | #if BOOST_BEAST_DOXYGEN |
| 39 | using const_iterator = __implementation_defined__; |
| 40 | #else |
| 41 | class const_iterator; |
| 42 | #endif |
| 43 | |
| 44 | class const_iterator |
| 45 | : private boost::empty_value<Policy> |
| 46 | { |
| 47 | basic_parsed_list const* list_ = nullptr; |
| 48 | char const* it_ = nullptr; |
| 49 | typename Policy::value_type v_; |
| 50 | bool error_ = false; |
| 51 | |
| 52 | public: |
| 53 | using value_type = |
| 54 | typename Policy::value_type; |
| 55 | using reference = value_type const&; |
| 56 | using pointer = value_type const*; |
| 57 | using difference_type = std::ptrdiff_t; |
| 58 | using iterator_category = |
| 59 | std::forward_iterator_tag; |
| 60 | |
| 61 | const_iterator() = default; |
| 62 | |
| 63 | bool |
| 64 | operator==( |
| 65 | const_iterator const& other) const |
| 66 | { |
| 67 | return |
| 68 | other.list_ == list_ && |
| 69 | other.it_ == it_; |
| 70 | } |
| 71 | |
| 72 | bool |
| 73 | operator!=( |
| 74 | const_iterator const& other) const |
| 75 | { |
| 76 | return ! (*this == other); |
| 77 | } |
| 78 | |
| 79 | reference |
| 80 | operator*() const |
| 81 | { |
| 82 | return v_; |
| 83 | } |
| 84 | |
| 85 | const_iterator& |
| 86 | operator++() |
| 87 | { |
| 88 | increment(); |
| 89 | return *this; |
| 90 | } |
| 91 | |
| 92 | const_iterator |
| 93 | operator++(int) |
| 94 | { |
| 95 | auto temp = *this; |
| 96 | ++(*this); |
| 97 | return temp; |
| 98 | } |
| 99 | |
| 100 | bool |
| 101 | error() const |
| 102 | { |
| 103 | return error_; |
| 104 | } |
| 105 | |
| 106 | private: |
| 107 | friend class basic_parsed_list; |
| 108 | |
| 109 | const_iterator( |
| 110 | basic_parsed_list const& list, bool at_end) |
| 111 | : list_(&list) |
| 112 | , it_(at_end ? nullptr : |
| 113 | list.s_.data()) |
| 114 | { |
| 115 | if(! at_end) |
| 116 | increment(); |
| 117 | } |
| 118 | |
| 119 | void |
| 120 | increment() |
| 121 | { |
| 122 | if(! this->get()( |
| 123 | v_, it_, list_->s_)) |
| 124 | { |
| 125 | it_ = nullptr; |
| 126 | error_ = true; |
| 127 | } |
| 128 | } |
| 129 | }; |
| 130 | |
| 131 | /// Construct a list from a string |
| 132 | explicit |
| 133 | basic_parsed_list(string_view s) |
| 134 | : s_(s) |
| 135 | { |
| 136 | } |
| 137 | |
| 138 | /// Return a const iterator to the beginning of the list |
| 139 | const_iterator begin() const; |
| 140 | |
| 141 | /// Return a const iterator to the end of the list |
| 142 | const_iterator end() const; |
| 143 | |
| 144 | /// Return a const iterator to the beginning of the list |
| 145 | const_iterator cbegin() const; |
| 146 | |
| 147 | /// Return a const iterator to the end of the list |
| 148 | const_iterator cend() const; |
| 149 | }; |
| 150 | |
| 151 | template<class Policy> |
| 152 | inline |
| 153 | auto |
| 154 | basic_parsed_list<Policy>:: |
| 155 | begin() const -> |
| 156 | const_iterator |
| 157 | { |
| 158 | return const_iterator{*this, false}; |
| 159 | } |
| 160 | |
| 161 | template<class Policy> |
| 162 | inline |
| 163 | auto |
| 164 | basic_parsed_list<Policy>:: |
| 165 | end() const -> |
| 166 | const_iterator |
| 167 | { |
| 168 | return const_iterator{*this, true}; |
| 169 | } |
| 170 | |
| 171 | template<class Policy> |
| 172 | inline |
| 173 | auto |
| 174 | basic_parsed_list<Policy>:: |
| 175 | cbegin() const -> |
| 176 | const_iterator |
| 177 | { |
| 178 | return const_iterator{*this, false}; |
| 179 | } |
| 180 | |
| 181 | template<class Policy> |
| 182 | inline |
| 183 | auto |
| 184 | basic_parsed_list<Policy>:: |
| 185 | cend() const -> |
| 186 | const_iterator |
| 187 | { |
| 188 | return const_iterator{*this, true}; |
| 189 | } |
| 190 | |
| 191 | } // detail |
| 192 | } // http |
| 193 | } // beast |
| 194 | } // boost |
| 195 | |
| 196 | #endif |
| 197 | |
| 198 | |