GCC Code Coverage Report


Directory: libs/json/include/boost/json/
File: detail/value_from.hpp
Date: 2025-12-23 17:38:58
Exec Total Coverage
Lines: 46 46 100.0%
Functions: 303 309 98.1%
Branches: 22 23 95.7%

Line Branch Exec Source
1 //
2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3 // Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
4 // Copyright (c) 2022 Dmitry Arkhipov (grisumbras@gmail.com)
5 //
6 // Distributed under the Boost Software License, Version 1.0. (See accompanying
7 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 //
9 // Official repository: https://github.com/boostorg/json
10 //
11
12 #ifndef BOOST_JSON_DETAIL_VALUE_FROM_HPP
13 #define BOOST_JSON_DETAIL_VALUE_FROM_HPP
14
15 #include <boost/json/conversion.hpp>
16 #include <boost/describe/enum_to_string.hpp>
17 #include <boost/mp11/algorithm.hpp>
18
19 #ifndef BOOST_NO_CXX17_HDR_OPTIONAL
20 # include <optional>
21 #endif
22
23 namespace boost {
24 namespace json {
25
26 namespace detail {
27
28 template< class Ctx, class T >
29 struct append_tuple_element {
30 array& arr;
31 Ctx const& ctx;
32 T&& t;
33
34 template<std::size_t I>
35 void
36 590 operator()(mp11::mp_size_t<I>) const
37 {
38 using std::get;
39
2/2
✓ Branch 2 taken 289 times.
✓ Branch 5 taken 285 times.
1180 arr.emplace_back(value_from(
40
1/1
✓ Branch 4 taken 4 times.
1200 get<I>(std::forward<T>(t)), ctx, arr.storage() ));
41 590 }
42 };
43
44 //----------------------------------------------------------
45 // User-provided conversion
46
47 template< class T, class Ctx >
48 void
49 65 value_from_impl( user_conversion_tag, value& jv, T&& from, Ctx const& )
50 {
51
1/1
✓ Branch 1 taken 24 times.
65 tag_invoke( value_from_tag(), jv, static_cast<T&&>(from) );
52 65 }
53
54 template< class T, class Ctx >
55 void
56 51 value_from_impl( context_conversion_tag, value& jv, T&& from, Ctx const& ctx)
57 {
58 using Sup = supported_context<Ctx, T, value_from_conversion>;
59
1/1
✓ Branch 2 taken 1 times.
51 tag_invoke( value_from_tag(), jv, static_cast<T&&>(from), Sup::get(ctx) );
60 51 }
61
62 template< class T, class Ctx >
63 void
64 2 value_from_impl(
65 full_context_conversion_tag, value& jv, T&& from, Ctx const& ctx)
66 {
67 using Sup = supported_context<Ctx, T, value_from_conversion>;
68 2 tag_invoke(
69 2 value_from_tag(), jv, static_cast<T&&>(from), Sup::get(ctx), ctx );
70 2 }
71
72 //----------------------------------------------------------
73 // Native conversion
74
75 template< class T, class Ctx >
76 void
77 13078 value_from_impl( native_conversion_tag, value& jv, T&& from, Ctx const& )
78 {
79 13078 jv = std::forward<T>(from);
80 13078 }
81
82 // null-like types
83 template< class T, class Ctx >
84 void
85 22 value_from_impl( null_like_conversion_tag, value& jv, T&&, Ctx const& )
86 {
87 // do nothing
88
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
22 BOOST_ASSERT(jv.is_null());
89 (void)jv;
90 22 }
91
92 // string-like types
93 template< class T, class Ctx >
94 void
95 137 value_from_impl( string_like_conversion_tag, value& jv, T&& from, Ctx const& )
96 {
97 137 auto sv = static_cast<string_view>(from);
98
1/1
✓ Branch 2 taken 75 times.
137 jv.emplace_string().assign(sv);
99 137 }
100
101 // map-like types
102 template< class T, class Ctx >
103 void
104 92 value_from_impl( map_like_conversion_tag, value& jv, T&& from, Ctx const& ctx )
105 {
106 using std::get;
107 92 object& obj = jv.emplace_object();
108 92 obj.reserve(detail::try_size(from, size_implementation<T>()));
109
2/2
✓ Branch 4 taken 99 times.
✓ Branch 5 taken 46 times.
290 for (auto&& elem : from)
110
3/3
✓ Branch 3 taken 99 times.
✓ Branch 7 taken 63 times.
✓ Branch 6 taken 18 times.
594 obj.emplace(
111 198 get<0>(elem),
112 198 value_from( get<1>(elem), ctx, obj.storage() ));
113 92 }
114
115 // ranges
116 template< class T, class Ctx >
117 void
118 194 value_from_impl( sequence_conversion_tag, value& jv, T&& from, Ctx const& ctx )
119 {
120 194 array& result = jv.emplace_array();
121 194 result.reserve(detail::try_size(from, size_implementation<T>()));
122 using ForwardedValue = forwarded_value<T&&>;
123
6/6
✓ Branch 4 taken 204 times.
✓ Branch 5 taken 75 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 4 times.
✓ Branch 0 taken 6024 times.
✓ Branch 1 taken 18 times.
12682 for (auto&& elem : from)
124
2/2
✓ Branch 3 taken 6244 times.
✓ Branch 6 taken 6178 times.
12824 result.emplace_back(
125 value_from(
126 // not a static_cast in order to appease clang < 4.0
127
1/1
✓ Branch 1 taken 60 times.
336 ForwardedValue(elem),
128 ctx,
129 result.storage() ));
130 194 }
131
132 // tuple-like types
133 template< class T, class Ctx >
134 void
135 278 value_from_impl( tuple_conversion_tag, value& jv, T&& from, Ctx const& ctx )
136 {
137 278 constexpr std::size_t n =
138 std::tuple_size<remove_cvref<T>>::value;
139 278 array& arr = jv.emplace_array();
140 278 arr.reserve(n);
141
1/1
✓ Branch 1 taken 139 times.
278 mp11::mp_for_each<mp11::mp_iota_c<n>>(
142 278 append_tuple_element< Ctx, T >{ arr, ctx, std::forward<T>(from) });
143 278 }
144
145 // no suitable conversion implementation
146 template< class T, class Ctx >
147 void
148 value_from_impl( no_conversion_tag, value&, T&&, Ctx const& )
149 {
150 static_assert(
151 !std::is_same<T, T>::value,
152 "No suitable tag_invoke overload found for the type");
153 }
154
155 template< class Ctx, class T >
156 struct from_described_member
157 {
158 static_assert(
159 uniquely_named_members< remove_cvref<T> >::value,
160 "The type has several described members with the same name.");
161
162 using Ds = described_members< remove_cvref<T> >;
163
164 object& obj;
165 Ctx const& ctx;
166 T&& from;
167
168 template< class I >
169 void
170 operator()(I) const
171 {
172 using D = mp11::mp_at<Ds, I>;
173 obj.emplace(
174 D::name,
175 value_from(
176 static_cast<T&&>(from).* D::pointer,
177 ctx,
178 obj.storage()));
179 }
180 };
181
182 // described classes
183 template< class T, class Ctx >
184 void
185 value_from_impl(
186 described_class_conversion_tag, value& jv, T&& from, Ctx const& ctx )
187 {
188 object& obj = jv.emplace_object();
189 from_described_member<Ctx, T> member_converter{
190 obj, ctx, static_cast<T&&>(from)};
191
192 using Ds = typename decltype(member_converter)::Ds;
193 constexpr std::size_t N = mp11::mp_size<Ds>::value;
194 obj.reserve(N);
195 mp11::mp_for_each< mp11::mp_iota_c<N> >(member_converter);
196 }
197
198 // described enums
199 template< class T, class Ctx >
200 void
201 value_from_impl(
202 described_enum_conversion_tag, value& jv, T from, Ctx const& )
203 {
204 (void)jv;
205 (void)from;
206 #ifdef BOOST_DESCRIBE_CXX14
207 char const* const name = describe::enum_to_string(from, nullptr);
208 if( name )
209 {
210 string& str = jv.emplace_string();
211 str.assign(name);
212 }
213 else
214 {
215 using Integer = typename std::underlying_type< remove_cvref<T> >::type;
216 jv = static_cast<Integer>(from);
217 }
218 #endif
219 }
220
221 // optionals
222 template< class T, class Ctx >
223 void
224 value_from_impl(
225 optional_conversion_tag, value& jv, T&& from, Ctx const& ctx )
226 {
227 if( from )
228 value_from( *from, ctx, jv );
229 else
230 jv = nullptr;
231 }
232
233 // variants
234 template< class Ctx >
235 struct value_from_visitor
236 {
237 value& jv;
238 Ctx const& ctx;
239
240 template<class T>
241 void
242 operator()(T&& t)
243 {
244 value_from( static_cast<T&&>(t), ctx, jv );
245 }
246 };
247
248 template< class Ctx, class T >
249 void
250 value_from_impl( variant_conversion_tag, value& jv, T&& from, Ctx const& ctx )
251 {
252 visit( value_from_visitor<Ctx>{ jv, ctx }, static_cast<T&&>(from) );
253 }
254
255 template< class Ctx, class T >
256 void
257 value_from_impl( path_conversion_tag, value& jv, T&& from, Ctx const& )
258 {
259 std::string s = from.generic_string();
260 string_view sv = s;
261 jv.emplace_string().assign(sv);
262 }
263
264 //----------------------------------------------------------
265 // Contextual conversions
266
267 template< class Ctx, class T >
268 using value_from_category = conversion_category<
269 Ctx, T, value_from_conversion >;
270
271 } // detail
272
273 #ifndef BOOST_NO_CXX17_HDR_OPTIONAL
274 inline
275 void
276 tag_invoke(
277 value_from_tag,
278 value& jv,
279 std::nullopt_t)
280 {
281 // do nothing
282 BOOST_ASSERT(jv.is_null());
283 (void)jv;
284 }
285 #endif
286
287 } // namespace json
288 } // namespace boost
289
290 #endif
291