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_VALUE_TO_HPP
13 : #define BOOST_JSON_VALUE_TO_HPP
14 :
15 : #include <boost/json/detail/value_to.hpp>
16 :
17 : namespace boost {
18 : namespace json {
19 :
20 : /** Convert a @ref value to an object of type `T`.
21 :
22 : This function attempts to convert a @ref value
23 : to `T` using
24 :
25 : @li one of @ref value's accessors, or
26 :
27 : @li a library-provided generic conversion, or
28 :
29 : @li a user-provided overload of `tag_invoke`.
30 :
31 : Out of the box the function supports types satisfying
32 : <a href="https://en.cppreference.com/w/cpp/named_req/SequenceContainer"><em>SequenceContainer</em></a>,
33 : arrays, arithmetic types, `bool`, `std::tuple`, `std::pair`,
34 : `std::variant`, `std::optional`, `std::monostate`, and `std::nullopt_t`.
35 :
36 : Conversion of other types is done by calling an overload of `tag_invoke`
37 : found by argument-dependent lookup. Its signature should be similar to:
38 :
39 : @code
40 : template< class FullContext >
41 : T tag_invoke( value_to_tag<T>, const value&, const Context& , const FullContext& );
42 : @endcode
43 :
44 : or
45 :
46 : @code
47 : T tag_invoke( value_to_tag<T>, const value&, const Context& );
48 : @endcode
49 :
50 : or
51 :
52 : @code
53 : result<T> tag_invoke( value_to_tag<T>, const value& );
54 : @endcode
55 :
56 : The overloads are checked for existence in that order and the first that
57 : matches will be selected. <br>
58 :
59 : The object returned by the function call is returned by @ref value_to as
60 : the result of the conversion. <br>
61 :
62 : The `ctx` argument can be used either as a tag type to provide conversions
63 : for third-party types, or to pass extra data to the conversion function.
64 :
65 : @par Constraints
66 : @code
67 : ! std::is_reference< T >::value
68 : @endcode
69 :
70 : @par Exception Safety
71 : Strong guarantee.
72 :
73 : @tparam T The type to convert to.
74 :
75 : @tparam Context The type of context passed to the conversion function.
76 :
77 : @returns `jv` converted to `result<T>`.
78 :
79 : @param jv The @ref value to convert.
80 :
81 : @param ctx Context passed to the conversion function.
82 :
83 : @see @ref value_to_tag, @ref value_from,
84 : <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1895r0.pdf">
85 : tag_invoke: A general pattern for supporting customisable functions</a>
86 : */
87 : template< class T, class Context >
88 : T
89 447 : value_to( value const& jv, Context const& ctx )
90 : {
91 : BOOST_STATIC_ASSERT(! std::is_reference<T>::value);
92 :
93 : using Attrs = detail::value_to_attrs<Context, T>;
94 : using Rep = typename Attrs::representation;
95 : BOOST_STATIC_ASSERT(detail::conversion_round_trips<
96 : Context, Rep, detail::value_to_conversion>::value);
97 :
98 : using bare_T = detail::remove_cvref<T>;
99 222 : return static_cast<bare_T>(detail::value_to_impl(
100 405 : typename Attrs::category(), value_to_tag<Rep>(), jv, ctx ));
101 : }
102 :
103 : /** Convert a @ref value to an object of type `T`.
104 :
105 : This function attempts to convert a @ref value
106 : to `T` using
107 :
108 : @li one of @ref value's accessors, or
109 :
110 : @li a library-provided generic conversion, or
111 :
112 : @li a user-provided overload of `tag_invoke`.
113 :
114 : Out of the box the function supports types satisfying
115 : <a href="https://en.cppreference.com/w/cpp/named_req/SequenceContainer"><em>SequenceContainer</em></a>,
116 : arrays, arithmetic types, `bool`, `std::tuple`, `std::pair`,
117 : `std::variant`, `std::optional`, `std::monostate`, and `std::nullopt_t`.
118 :
119 : Conversion of other types is done by calling an overload of `tag_invoke`
120 : found by argument-dependent lookup. Its signature should be similar to:
121 :
122 : @code
123 : T tag_invoke( value_to_tag<T>, const value& );
124 : @endcode
125 :
126 : The object returned by the function call is
127 : returned by @ref value_to as the result of the
128 : conversion.
129 :
130 : @par Constraints
131 : @code
132 : ! std::is_reference< T >::value
133 : @endcode
134 :
135 : @par Exception Safety
136 : Strong guarantee.
137 :
138 : @tparam T The type to convert to.
139 :
140 : @returns `jv` converted to `T`.
141 :
142 : @param jv The @ref value to convert.
143 :
144 : @see @ref value_to_tag, @ref value_from,
145 : <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1895r0.pdf">
146 : tag_invoke: A general pattern for supporting customisable functions</a>
147 : */
148 : template<class T>
149 : T
150 65 : value_to(const value& jv)
151 : {
152 84 : return value_to<T>( jv, detail::no_context() );
153 : }
154 :
155 : /** Convert a @ref value to a `boost::system::result<T>`.
156 :
157 : This function attempts to convert a @ref value
158 : to `result<T>` using
159 :
160 : @li one of @ref value's accessors, or
161 :
162 : @li a library-provided generic conversion, or
163 :
164 : @li a user-provided overload of `tag_invoke`.
165 :
166 : Out of the box the function supports types satisfying
167 : <a href="https://en.cppreference.com/w/cpp/named_req/SequenceContainer"><em>SequenceContainer</em></a>,
168 : arrays, arithmetic types, `bool`, `std::tuple`, `std::pair`,
169 : `std::variant`, `std::optional`, `std::monostate`, and `std::nullopt_t`.
170 :
171 : Conversion of other types is done by calling an overload of `tag_invoke`
172 : found by argument-dependent lookup. Its signature should be similar to:
173 :
174 : @code
175 : template< class FullContext >
176 : result<T> tag_invoke( try_value_to_tag<T>, const value&, const Context& , const FullContext& );
177 : @endcode
178 :
179 : or
180 :
181 : @code
182 : result<T> tag_invoke( try_value_to_tag<T>, const value&, const Context& );
183 : @endcode
184 :
185 : or
186 :
187 : @code
188 : result<T> tag_invoke( try_value_to_tag<T>, const value& );
189 : @endcode
190 :
191 : The overloads are checked for existence in that order and the first that
192 : matches will be selected. <br>
193 :
194 : If an error occurs during conversion, the result will store the error code
195 : associated with the error. If an exception is thrown, the function will
196 : attempt to retrieve the associated error code and return it, otherwise it
197 : will return `error::exception`, unless the exception type is
198 : `std::bad_alloc`, which will be allowed to propagate. <br>
199 :
200 : The `ctx` argument can be used either as a tag type to provide conversions
201 : for third-party types, or to pass extra data to the conversion function.
202 :
203 : @par Constraints
204 : @code
205 : ! std::is_reference< T >::value
206 : @endcode
207 :
208 : @par Exception Safety
209 : Strong guarantee.
210 :
211 : @tparam T The type to convert to.
212 :
213 : @tparam Context The type of context passed to the conversion function.
214 :
215 : @param jv The @ref value to convert.
216 :
217 : @param ctx Context passed to the conversion function.
218 :
219 : @returns `jv` converted to `result<T>`.
220 :
221 : @see @ref value_to_tag, @ref value_to, @ref value_from,
222 : <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1895r0.pdf">
223 : tag_invoke: A general pattern for supporting customisable functions</a>
224 : */
225 : template< class T, class Context >
226 : typename result_for<T, value>::type
227 3817 : try_value_to( value const& jv, Context const& ctx )
228 : {
229 : BOOST_STATIC_ASSERT(! std::is_reference<T>::value);
230 :
231 : using Attrs = detail::value_to_attrs<Context, T>;
232 : using Rep = typename Attrs::representation;
233 : BOOST_STATIC_ASSERT(detail::conversion_round_trips<
234 : Context, Rep, detail::value_to_conversion>::value);
235 :
236 : using bare_T = detail::remove_cvref<T>;
237 280 : return detail::value_to_impl(
238 3330 : typename Attrs::category(), try_value_to_tag<Rep>(), jv, ctx )
239 7457 : & [](Rep&& rep) { return static_cast<bare_T>(rep); };
240 : }
241 :
242 : /** Convert a @ref value to a `boost::system::result<T>`.
243 :
244 : This function attempts to convert a @ref value
245 : to `result<T>` using
246 :
247 : @li one of @ref value's accessors, or
248 :
249 : @li a library-provided generic conversion, or
250 :
251 : @li a user-provided overload of `tag_invoke`.
252 :
253 : Out of the box the function supports types satisfying
254 : <a href="https://en.cppreference.com/w/cpp/named_req/SequenceContainer"><em>SequenceContainer</em></a>,
255 : arrays, arithmetic types, `bool`, `std::tuple`, `std::pair`,
256 : `std::variant`, `std::optional`, `std::monostate`, and `std::nullopt_t`.
257 :
258 : Conversion of other types is done by calling an overload of `tag_invoke`
259 : found by argument-dependent lookup. Its signature should be similar to:
260 :
261 : @code
262 : result<T> tag_invoke( try_value_to_tag<T>, const value& );
263 : @endcode
264 :
265 : If an error occurs during conversion, the result will store the error code
266 : associated with the error. If an exception is thrown, the function will
267 : attempt to retrieve the associated error code and return it, otherwise it
268 : will return `error::exception`, unless the exception type is
269 : `std::bad_alloc`, which will be allowed to propagate.
270 :
271 : @par Constraints
272 : @code
273 : ! std::is_reference< T >::value
274 : @endcode
275 :
276 : @par Exception Safety
277 : Strong guarantee.
278 :
279 : @tparam T The type to convert to.
280 :
281 : @param jv The @ref value to convert.
282 :
283 : @returns `jv` converted to `result<T>`.
284 :
285 : @see @ref value_to_tag, @ref value_to, @ref value_from,
286 : <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1895r0.pdf">
287 : tag_invoke: A general pattern for supporting customisable functions</a>
288 : */
289 : template<class T>
290 : typename result_for<T, value>::type
291 136 : try_value_to(const value& jv)
292 : {
293 169 : return try_value_to<T>( jv, detail::no_context() );
294 : }
295 :
296 : /** Convert a @ref value to an object of type `T`.
297 :
298 : This overload is **deleted** and participates in overload resolution only
299 : when `U` is not @ref value. The overload exists to prevent unintented
300 : creation of temporary @ref value instances, e.g.
301 :
302 : @code
303 : auto flag = value_to<bool>(true);
304 : @endcode
305 : */
306 : template<class T, class U
307 : #ifndef BOOST_JSON_DOCS
308 : , class = typename std::enable_if<!std::is_same<U, value>::value>::type
309 : #endif
310 : >
311 : T
312 : value_to(U const& jv) = delete;
313 :
314 : /** Determine a @ref value can be converted to `T`.
315 :
316 : If @ref value can be converted to `T` via a
317 : call to @ref value_to, the static data member `value`
318 : is defined as `true`. Otherwise, `value` is
319 : defined as `false`.
320 :
321 : @see @ref value_to
322 : */
323 : #ifdef BOOST_JSON_DOCS
324 : template<class T>
325 : using has_value_to = __see_below__;
326 : #else
327 : template<class T>
328 : using has_value_to = detail::can_convert<
329 : detail::remove_cvref<T>, detail::value_to_conversion>;
330 : #endif
331 :
332 : } // namespace json
333 : } // namespace boost
334 :
335 : #endif
|