ゴミ箱
vector_body.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2016-2017 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_VECTOR_BODY_HPP
11 #define BOOST_BEAST_HTTP_VECTOR_BODY_HPP
12 
13 #include <boost/beast/config.hpp>
17 #include <boost/asio/buffer.hpp>
18 #include <boost/optional.hpp>
19 #include <cstdint>
20 #include <limits>
21 #include <memory>
22 #include <stdexcept>
23 #include <string>
24 #include <utility>
25 
26 namespace boost {
27 namespace beast {
28 namespace http {
29 
36 template<class T, class Allocator = std::allocator<T>>
38 {
39 private:
40  static_assert(sizeof(T) == 1 &&
41  std::is_integral<T>::value,
42  "T requirements not met");
43 
44 public:
50  using value_type = std::vector<T, Allocator>;
51 
58  static
59  std::uint64_t
61  {
62  return body.size();
63  }
64 
69 #if BOOST_BEAST_DOXYGEN
70  using reader = implementation_defined;
71 #else
72  class reader
73  {
74  value_type const& body_;
75 
76  public:
77  using const_buffers_type =
78  boost::asio::const_buffers_1;
79 
80  template<bool isRequest, class Fields>
81  explicit
82  reader(message<isRequest,
83  vector_body, Fields> const& msg)
84  : body_(msg.body)
85  {
86  }
87 
88  void
90  {
91  ec.assign(0, ec.category());
92  }
93 
94  boost::optional<std::pair<const_buffers_type, bool>>
95  get(error_code& ec)
96  {
97  ec.assign(0, ec.category());
98  return {{const_buffers_type{
99  body_.data(), body_.size()}, false}};
100  }
101  };
102 #endif
103 
108 #if BOOST_BEAST_DOXYGEN
109  using writer = implementation_defined;
110 #else
111  class writer
112  {
113  value_type& body_;
114 
115  public:
116  template<bool isRequest, class Fields>
117  explicit
118  writer(message<isRequest,
119  vector_body, Fields>& m)
120  : body_(m.body)
121  {
122  }
123 
124  void
126  std::uint64_t> const& length, error_code& ec)
127  {
128  if(length)
129  {
130  if(*length > (
131  std::numeric_limits<std::size_t>::max)())
132  {
134  return;
135  }
136  try
137  {
138  body_.reserve(
139  static_cast<std::size_t>(*length));
140  }
141  catch(std::exception const&)
142  {
144  return;
145  }
146  }
147  ec.assign(0, ec.category());
148  }
149 
150  template<class ConstBufferSequence>
151  std::size_t
153  error_code& ec)
154  {
155  using boost::asio::buffer_size;
156  using boost::asio::buffer_copy;
157  auto const n = buffer_size(buffers);
158  auto const len = body_.size();
159  try
160  {
161  body_.resize(len + n);
162  }
163  catch(std::exception const&)
164  {
166  return 0;
167  }
168  ec.assign(0, ec.category());
169  return buffer_copy(boost::asio::buffer(
170  &body_[0] + len, n), buffers);
171  }
172 
173  void
175  {
176  ec.assign(0, ec.category());
177  }
178  };
179 #endif
180 };
181 
182 } // http
183 } // beast
184 } // boost
185 
186 #endif
BufferSequence< boost::asio::const_buffer > ConstBufferSequence
Definition: type_traits.hpp:280
reader(message< isRequest, vector_body, Fields > const &msg)
Definition: vector_body.hpp:82
void init(boost::optional< std::uint64_t > const &length, error_code &ec)
Definition: vector_body.hpp:125
Definition: async_result.hpp:20
Definition: type_traits.hpp:25
detail::buffers_helper< ConstBufferSequence > buffers(ConstBufferSequence const &b)
Definition: ostream.hpp:50
Definition: beast_common.hpp:6
Definition: vector_body.hpp:37
void finish(error_code &ec)
Definition: vector_body.hpp:174
void init(error_code &ec)
Definition: vector_body.hpp:89
Definition: vector_body.hpp:72
boost::system::error_code error_code
The type of error code used by the library.
Definition: error.hpp:21
Definition: vector_body.hpp:111
std::vector< T, Allocator > value_type
Definition: vector_body.hpp:50
std::size_t put(ConstBufferSequence const &buffers, error_code &ec)
Definition: vector_body.hpp:152
static std::uint64_t size(value_type const &body)
Definition: vector_body.hpp:60
writer(message< isRequest, vector_body, Fields > &m)
Definition: vector_body.hpp:118
boost::asio::const_buffers_1 const_buffers_type
Definition: vector_body.hpp:78