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 <boost/buffers/iterators.hpp> | ||
15 | #include <iterator> | ||
16 | #include <new> | ||
17 | #include <utility> | ||
18 | |||
19 | namespace boost { | ||
20 | namespace http_proto { | ||
21 | |||
22 | class serializer:: | ||
23 | const_buffers_type | ||
24 | { | ||
25 | std::size_t n_ = 0; | ||
26 | buffers::const_buffer const* p_ = nullptr; | ||
27 | |||
28 | friend class serializer; | ||
29 | |||
30 | 12 | const_buffers_type( | |
31 | buffers::const_buffer const* p, | ||
32 | std::size_t n) noexcept | ||
33 | 12 | : n_(n) | |
34 | 12 | , p_(p) | |
35 | { | ||
36 | 12 | } | |
37 | |||
38 | public: | ||
39 | using iterator = buffers::const_buffer const*; | ||
40 | using const_iterator = iterator; | ||
41 | using value_type = buffers::const_buffer; | ||
42 | using reference = buffers::const_buffer; | ||
43 | using const_reference = buffers::const_buffer; | ||
44 | using size_type = std::size_t; | ||
45 | using difference_type = std::ptrdiff_t; | ||
46 | |||
47 | const_buffers_type() = default; | ||
48 | const_buffers_type( | ||
49 | const_buffers_type const&) = default; | ||
50 | const_buffers_type& operator=( | ||
51 | const_buffers_type const&) = default; | ||
52 | |||
53 | iterator | ||
54 | 23 | begin() const noexcept | |
55 | { | ||
56 | 23 | return p_; | |
57 | } | ||
58 | |||
59 | iterator | ||
60 | 23 | end() const noexcept | |
61 | { | ||
62 | 23 | return p_ + n_; | |
63 | } | ||
64 | }; | ||
65 | |||
66 | //------------------------------------------------ | ||
67 | |||
68 | template< | ||
69 | class P0, | ||
70 | class... Pn> | ||
71 | serializer:: | ||
72 | serializer( | ||
73 | std::size_t buffer_size, | ||
74 | P0&& p0, | ||
75 | Pn&&... pn) | ||
76 | : serializer(buffer_size) | ||
77 | { | ||
78 | apply_params( | ||
79 | std::forward<P0>(p0), | ||
80 | std::forward<Pn>(pn)...); | ||
81 | } | ||
82 | |||
83 | //------------------------------------------------ | ||
84 | |||
85 | template< | ||
86 | class ConstBufferSequence, | ||
87 | class> | ||
88 | void | ||
89 | 14 | serializer:: | |
90 | start( | ||
91 | message_view_base const& m, | ||
92 | ConstBufferSequence&& body) | ||
93 | { | ||
94 | 14 | start_init(m); | |
95 | 14 | auto const& bs = | |
96 | ws_.push(std::forward< | ||
97 | ConstBufferSequence>(body)); | ||
98 | 14 | std::size_t n = std::distance( | |
99 | buffers::begin(bs), | ||
100 | buffers::end(bs)); | ||
101 | 14 | buf_ = make_array(n); | |
102 | 14 | auto p = buf_.data(); | |
103 |
2/2✓ Branch 2 taken 7 times.
✓ Branch 3 taken 7 times.
|
28 | for(buffers::const_buffer b : bs) |
104 | 14 | *p++ = b; | |
105 | 14 | start_buffers(m); | |
106 | 14 | } | |
107 | |||
108 | template< | ||
109 | class Source, | ||
110 | class> | ||
111 | auto | ||
112 | 12 | serializer:: | |
113 | start( | ||
114 | message_view_base const& m, | ||
115 | Source&& src0) -> | ||
116 | typename std::decay< | ||
117 | Source>::type& | ||
118 | { | ||
119 | 12 | start_init(m); | |
120 | 12 | auto& src = ws_.push( | |
121 | std::forward< | ||
122 | Source>(src0)); | ||
123 | 12 | start_source( | |
124 | 12 | m, std::addressof(src)); | |
125 | 12 | return src; | |
126 | } | ||
127 | |||
128 | //------------------------------------------------ | ||
129 | |||
130 | inline | ||
131 | auto | ||
132 | 24 | serializer:: | |
133 | make_array(std::size_t n) -> | ||
134 | detail::array_of_const_buffers | ||
135 | { | ||
136 | return { | ||
137 | ws_.push_array(n, | ||
138 | 48 | buffers::const_buffer{}), | |
139 |
1/2✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
|
24 | n }; |
140 | } | ||
141 | |||
142 | inline | ||
143 | void | ||
144 | serializer:: | ||
145 | apply_params() noexcept | ||
146 | { | ||
147 | } | ||
148 | |||
149 | template< | ||
150 | class P0, | ||
151 | class... Pn> | ||
152 | void | ||
153 | serializer:: | ||
154 | apply_params( | ||
155 | P0&& p0, | ||
156 | Pn&&... pn) | ||
157 | { | ||
158 | // If you get an error here it means | ||
159 | // you passed an unknown parameter type. | ||
160 | apply_param(std::forward<P0>(p0)); | ||
161 | |||
162 | apply_params( | ||
163 | std::forward<Pn>(pn)...); | ||
164 | } | ||
165 | |||
166 | } // http_proto | ||
167 | } // boost | ||
168 | |||
169 | #endif | ||
170 |