GCC Code Coverage Report


Directory: libs/json/include/boost/json/
File: detail/value_from.hpp
Date: 2025-12-23 17:15:02
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_category, 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_category, 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_category, 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_category, 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_category, 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(unknown_category, 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(described_class_category, value& jv, T&& from, Ctx const& ctx)
186 {
187 object& obj = jv.emplace_object();
188 from_described_member<Ctx, T> member_converter{
189 obj, ctx, static_cast<T&&>(from)};
190
191 using Ds = typename decltype(member_converter)::Ds;
192 constexpr std::size_t N = mp11::mp_size<Ds>::value;
193 obj.reserve(N);
194 mp11::mp_for_each< mp11::mp_iota_c<N> >(member_converter);
195 }
196
197 // described enums
198 template< class T, class Ctx >
199 void
200 value_from_impl(described_enum_category, value& jv, T from, Ctx const&)
201 {
202 (void)jv;
203 (void)from;
204 #ifdef BOOST_DESCRIBE_CXX14
205 char const* const name = describe::enum_to_string(from, nullptr);
206 if( name )
207 {
208 string& str = jv.emplace_string();
209 str.assign(name);
210 }
211 else
212 {
213 using Integer = typename std::underlying_type< remove_cvref<T> >::type;
214 jv = static_cast<Integer>(from);
215 }
216 #endif
217 }
218
219 // optionals
220 template< class T, class Ctx >
221 void
222 value_from_impl(optional_category, value& jv, T&& from, Ctx const& ctx)
223 {
224 if( from )
225 value_from( *from, ctx, jv );
226 else
227 jv = nullptr;
228 }
229
230 // variants
231 template< class Ctx >
232 struct value_from_visitor
233 {
234 value& jv;
235 Ctx const& ctx;
236
237 template<class T>
238 void
239 operator()(T&& t)
240 {
241 value_from( static_cast<T&&>(t), ctx, jv );
242 }
243 };
244
245 template< class Ctx, class T >
246 void
247 value_from_impl(variant_category, value& jv, T&& from, Ctx const& ctx)
248 {
249 visit( value_from_visitor<Ctx>{ jv, ctx }, static_cast<T&&>(from) );
250 }
251
252 template< class Ctx, class T >
253 void
254 value_from_impl(path_category, value& jv, T&& from, Ctx const&)
255 {
256 std::string s = from.generic_string();
257 string_view sv = s;
258 jv.emplace_string().assign(sv);
259 }
260
261 //----------------------------------------------------------
262 // Contextual conversions
263
264 template< class Ctx, class T >
265 using value_from_category = conversion_category<
266 Ctx, T, value_from_conversion >;
267
268 } // detail
269
270 #ifndef BOOST_NO_CXX17_HDR_OPTIONAL
271 inline
272 void
273 tag_invoke(
274 value_from_tag,
275 value& jv,
276 std::nullopt_t)
277 {
278 // do nothing
279 BOOST_ASSERT(jv.is_null());
280 (void)jv;
281 }
282 #endif
283
284 } // namespace json
285 } // namespace boost
286
287 #endif
288