Line data Source code
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 <boost/buffers/range.hpp> 15 : #include <utility> 16 : 17 : namespace boost { 18 : namespace http_proto { 19 : 20 : class serializer:: 21 : const_buffers_type 22 : { 23 : std::size_t n_ = 0; 24 : buffers::const_buffer const* p_ = nullptr; 25 : 26 : friend class serializer; 27 : 28 30 : const_buffers_type( 29 : buffers::const_buffer const* p, 30 : std::size_t n) noexcept 31 30 : : n_(n) 32 30 : , p_(p) 33 : { 34 30 : } 35 : 36 : public: 37 : using iterator = buffers::const_buffer const*; 38 : using const_iterator = iterator; 39 : using value_type = buffers::const_buffer; 40 : using reference = buffers::const_buffer; 41 : using const_reference = buffers::const_buffer; 42 : using size_type = std::size_t; 43 : using difference_type = std::ptrdiff_t; 44 : 45 : const_buffers_type() = default; 46 : const_buffers_type( 47 : const_buffers_type const&) = default; 48 : const_buffers_type& operator=( 49 : const_buffers_type const&) = default; 50 : 51 : iterator 52 59 : begin() const noexcept 53 : { 54 59 : return p_; 55 : } 56 : 57 : iterator 58 59 : end() const noexcept 59 : { 60 59 : return p_ + n_; 61 : } 62 : }; 63 : 64 : //------------------------------------------------ 65 : 66 : template< 67 : class ConstBufferSequence, 68 : class> 69 : void 70 7 : serializer:: 71 : start( 72 : message_view_base const& m, 73 : ConstBufferSequence&& body) 74 : { 75 7 : start_init(m); 76 7 : auto const& bs = 77 : ws_.emplace<ConstBufferSequence>( 78 : std::forward<ConstBufferSequence>(body)); 79 7 : std::size_t n = std::distance( 80 : buffers::begin(bs), 81 : buffers::end(bs)); 82 7 : buf_ = make_array(n); 83 7 : auto p = buf_.data(); 84 14 : for(buffers::const_buffer b : 85 7 : buffers::range(bs)) 86 7 : *p++ = b; 87 7 : start_buffers(m); 88 7 : } 89 : 90 : template< 91 : class Source, 92 : class... Args, 93 : class> 94 : Source& 95 8 : serializer:: 96 : start( 97 : message_view_base const& m, 98 : Args&&... args) 99 : { 100 : static_assert( 101 : std::is_constructible<Source, Args...>::value || 102 : std::is_constructible<Source, buffered_base::allocator&, Args...>::value, 103 : "The Source cannot be constructed with the given arguments"); 104 : 105 8 : start_init(m); 106 8 : auto& src = construct_source<Source>( 107 : std::forward<Args>(args)...); 108 8 : start_source(m, std::addressof(src)); 109 8 : return src; 110 : } 111 : 112 : //------------------------------------------------ 113 : 114 : inline 115 : auto 116 26 : serializer:: 117 : make_array(std::size_t n) -> 118 : detail::array_of_const_buffers 119 : { 120 : return { 121 : ws_.push_array(n, 122 52 : buffers::const_buffer{}), 123 26 : n }; 124 : } 125 : 126 : } // http_proto 127 : } // boost 128 : 129 : #endif