impl/stream_parser.ipp

100.0% Lines (67/67) 100.0% List of functions (13/13)
f(x) Functions (13)
Line TLA Hits 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/boostorg/json
8 //
9
10 #ifndef BOOST_JSON_IMPL_STREAM_PARSER_IPP
11 #define BOOST_JSON_IMPL_STREAM_PARSER_IPP
12
13 #include <boost/json/stream_parser.hpp>
14 #include <boost/json/basic_parser_impl.hpp>
15 #include <boost/json/error.hpp>
16 #include <cstring>
17 #include <stdexcept>
18 #include <utility>
19
20 namespace boost {
21 namespace json {
22
23 22x stream_parser::
24 stream_parser(
25 storage_ptr sp,
26 parse_options const& opt,
27 unsigned char* buffer,
28 22x std::size_t size) noexcept
29 22x : p_(
30 opt,
31 22x std::move(sp),
32 buffer,
33 size)
34 {
35 22x reset();
36 22x }
37
38 75304x stream_parser::
39 stream_parser(
40 storage_ptr sp,
41 75304x parse_options const& opt) noexcept
42 75304x : p_(
43 opt,
44 75304x std::move(sp),
45 150608x nullptr,
46 75304x 0)
47 {
48 75304x reset();
49 75304x }
50
51 void
52 150055x stream_parser::
53 reset(storage_ptr sp) noexcept
54 {
55 150055x p_.reset();
56 150055x p_.handler().st.reset(sp);
57 150055x }
58
59 std::size_t
60 130421x stream_parser::
61 write_some(
62 char const* data,
63 std::size_t size,
64 system::error_code& ec)
65 {
66 130421x return p_.write_some(
67 130280x true, data, size, ec);
68 }
69
70 std::size_t
71 2x stream_parser::
72 write_some(
73 char const* data,
74 std::size_t size,
75 std::error_code& ec)
76 {
77 2x system::error_code jec;
78 2x std::size_t const result = write_some(data, size, jec);
79 2x ec = jec;
80 2x return result;
81 }
82
83 std::size_t
84 6x stream_parser::
85 write_some(
86 char const* data,
87 std::size_t size)
88 {
89 6x system::error_code ec;
90 6x auto const n = write_some(
91 data, size, ec);
92 6x if(ec)
93 1x detail::throw_system_error( ec );
94 5x return n;
95 }
96
97 std::size_t
98 130392x stream_parser::
99 write(
100 char const* data,
101 std::size_t size,
102 system::error_code& ec)
103 {
104 130392x auto const n = write_some(
105 data, size, ec);
106 130251x if(! ec && n < size)
107 {
108 6x BOOST_JSON_FAIL(ec, error::extra_data);
109 6x p_.fail(ec);
110 }
111 130251x return n;
112 }
113
114 std::size_t
115 2x stream_parser::
116 write(
117 char const* data,
118 std::size_t size,
119 std::error_code& ec)
120 {
121 2x system::error_code jec;
122 2x std::size_t const result = write(data, size, jec);
123 2x ec = jec;
124 2x return result;
125 }
126
127 std::size_t
128 11x stream_parser::
129 write(
130 char const* data,
131 std::size_t size)
132 {
133 11x system::error_code ec;
134 11x auto const n = write(
135 data, size, ec);
136 11x if(ec)
137 1x detail::throw_system_error( ec );
138 10x return n;
139 }
140
141 void
142 75152x stream_parser::
143 finish(system::error_code& ec)
144 {
145 75152x p_.write_some(false, nullptr, 0, ec);
146 75152x }
147
148 void
149 8x stream_parser::
150 finish()
151 {
152 8x system::error_code ec;
153 8x finish(ec);
154 8x if(ec)
155 6x detail::throw_system_error( ec );
156 2x }
157
158 void
159 2x stream_parser::
160 finish(std::error_code& ec)
161 {
162 2x system::error_code jec;
163 2x finish(jec);
164 2x ec = jec;
165 2x }
166
167 value
168 75151x stream_parser::
169 release()
170 {
171 75151x if(! p_.done())
172 {
173 // prevent undefined behavior
174 4x finish();
175 }
176 75148x return p_.handler().st.release();
177 }
178
179 } // namespace json
180 } // namespace boost
181
182 #endif
183