| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // | ||
| 2 | // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.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/CPPAlliance/http_proto | ||
| 8 | // | ||
| 9 | |||
| 10 | #ifndef BOOST_HTTP_PROTO_IMPL_SERIALIZER_HPP | ||
| 11 | #define BOOST_HTTP_PROTO_IMPL_SERIALIZER_HPP | ||
| 12 | |||
| 13 | #include <boost/http_proto/detail/except.hpp> | ||
| 14 | #include <iterator> | ||
| 15 | #include <new> | ||
| 16 | #include <utility> | ||
| 17 | |||
| 18 | namespace boost { | ||
| 19 | namespace http_proto { | ||
| 20 | |||
| 21 | class serializer::buffers | ||
| 22 | { | ||
| 23 | std::size_t n_ = 0; | ||
| 24 | const_buffer const* p_ = nullptr; | ||
| 25 | |||
| 26 | friend class serializer; | ||
| 27 | |||
| 28 | ✗ | buffers( | |
| 29 | const_buffer const* p, | ||
| 30 | std::size_t n) noexcept | ||
| 31 | ✗ | : n_(n) | |
| 32 | ✗ | , p_(p) | |
| 33 | { | ||
| 34 | } | ||
| 35 | |||
| 36 | public: | ||
| 37 | using iterator = const_buffer const*; | ||
| 38 | using const_iterator = iterator; | ||
| 39 | using value_type = const_buffer; | ||
| 40 | using reference = const_buffer; | ||
| 41 | using const_reference = const_buffer; | ||
| 42 | using size_type = std::size_t; | ||
| 43 | using difference_type = std::ptrdiff_t; | ||
| 44 | |||
| 45 | buffers() = default; | ||
| 46 | buffers( | ||
| 47 | buffers const&) = default; | ||
| 48 | buffers& operator=( | ||
| 49 | buffers const&) = default; | ||
| 50 | |||
| 51 | iterator | ||
| 52 | begin() const noexcept | ||
| 53 | { | ||
| 54 | return p_; | ||
| 55 | } | ||
| 56 | |||
| 57 | iterator | ||
| 58 | end() const noexcept | ||
| 59 | { | ||
| 60 | return p_ + n_; | ||
| 61 | } | ||
| 62 | }; | ||
| 63 | |||
| 64 | //------------------------------------------------ | ||
| 65 | |||
| 66 | template<class Body> | ||
| 67 | void | ||
| 68 | serializer:: | ||
| 69 | set_body(Body&& body) | ||
| 70 | { | ||
| 71 | // can't set body twice | ||
| 72 | BOOST_ASSERT(! src_); | ||
| 73 | BOOST_ASSERT(cbn_ == 0); | ||
| 74 | |||
| 75 | set_body_impl( | ||
| 76 | std::forward<Body>(body), | ||
| 77 | std::is_base_of< | ||
| 78 | source, Body>{}); | ||
| 79 | } | ||
| 80 | |||
| 81 | template<class Body> | ||
| 82 | void | ||
| 83 | serializer:: | ||
| 84 | set_body_impl( | ||
| 85 | Body&& body, | ||
| 86 | std::true_type) | ||
| 87 | { | ||
| 88 | src_ = &ws_.push( | ||
| 89 | std::forward<Body>(body)); | ||
| 90 | cb_ = ws_.push_array( | ||
| 91 | 3, const_buffer{}); | ||
| 92 | } | ||
| 93 | |||
| 94 | template< | ||
| 95 | class Buffers> | ||
| 96 | void | ||
| 97 | serializer:: | ||
| 98 | set_body_impl( | ||
| 99 | Buffers&& bs0, | ||
| 100 | std::false_type) | ||
| 101 | { | ||
| 102 | auto n = std::distance( | ||
| 103 | bs0.begin(), bs0.end()); | ||
| 104 | auto& bs = ws_.push( | ||
| 105 | std::move(bs0)); | ||
| 106 | cb_ = ws_.push_array( | ||
| 107 | 1 + n, const_buffer{}); | ||
| 108 | cbn_ = 0; | ||
| 109 | cbi_ = 0; | ||
| 110 | for(const_buffer b : bs) | ||
| 111 | cb_[++cbn_] = b; | ||
| 112 | } | ||
| 113 | |||
| 114 | } // http_proto | ||
| 115 | } // boost | ||
| 116 | |||
| 117 | #endif | ||
| 118 |