Line data Source code
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 295 : operator()(mp11::mp_size_t<I>) const
37 : {
38 : using std::get;
39 590 : arr.emplace_back(value_from(
40 600 : get<I>(std::forward<T>(t)), ctx, arr.storage() ));
41 295 : }
42 : };
43 :
44 : //----------------------------------------------------------
45 : // User-provided conversion
46 :
47 : template< class T, class Ctx >
48 : void
49 35 : value_from_impl( user_conversion_tag, value& jv, T&& from, Ctx const& )
50 : {
51 35 : tag_invoke( value_from_tag(), jv, static_cast<T&&>(from) );
52 35 : }
53 :
54 : template< class T, class Ctx >
55 : void
56 26 : 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 26 : tag_invoke( value_from_tag(), jv, static_cast<T&&>(from), Sup::get(ctx) );
60 26 : }
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 6539 : value_from_impl( native_conversion_tag, value& jv, T&& from, Ctx const& )
78 : {
79 6539 : jv = std::forward<T>(from);
80 6539 : }
81 :
82 : // null-like types
83 : template< class T, class Ctx >
84 : void
85 11 : value_from_impl(null_category, value& jv, T&&, Ctx const&)
86 : {
87 : // do nothing
88 11 : BOOST_ASSERT(jv.is_null());
89 : (void)jv;
90 11 : }
91 :
92 : // string-like types
93 : template< class T, class Ctx >
94 : void
95 75 : value_from_impl(string_category, value& jv, T&& from, Ctx const&)
96 : {
97 75 : auto sv = static_cast<string_view>(from);
98 75 : jv.emplace_string().assign(sv);
99 75 : }
100 :
101 : // map-like types
102 : template< class T, class Ctx >
103 : void
104 46 : value_from_impl(map_category, value& jv, T&& from, Ctx const& ctx)
105 : {
106 : using std::get;
107 46 : object& obj = jv.emplace_object();
108 46 : obj.reserve(detail::try_size(from, size_implementation<T>()));
109 145 : for (auto&& elem : from)
110 297 : obj.emplace(
111 99 : get<0>(elem),
112 99 : value_from( get<1>(elem), ctx, obj.storage() ));
113 46 : }
114 :
115 : // ranges
116 : template< class T, class Ctx >
117 : void
118 97 : value_from_impl(sequence_category, value& jv, T&& from, Ctx const& ctx)
119 : {
120 97 : array& result = jv.emplace_array();
121 97 : result.reserve(detail::try_size(from, size_implementation<T>()));
122 : using ForwardedValue = forwarded_value<T&&>;
123 6341 : for (auto&& elem : from)
124 6412 : result.emplace_back(
125 : value_from(
126 : // not a static_cast in order to appease clang < 4.0
127 168 : ForwardedValue(elem),
128 : ctx,
129 : result.storage() ));
130 97 : }
131 :
132 : // tuple-like types
133 : template< class T, class Ctx >
134 : void
135 139 : value_from_impl(tuple_category, value& jv, T&& from, Ctx const& ctx)
136 : {
137 139 : constexpr std::size_t n =
138 : std::tuple_size<remove_cvref<T>>::value;
139 139 : array& arr = jv.emplace_array();
140 139 : arr.reserve(n);
141 139 : mp11::mp_for_each<mp11::mp_iota_c<n>>(
142 139 : append_tuple_element< Ctx, T >{ arr, ctx, std::forward<T>(from) });
143 139 : }
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
|