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_FROM_HPP
13 : #define BOOST_JSON_VALUE_FROM_HPP
14 :
15 : #include <boost/json/detail/value_from.hpp>
16 :
17 : namespace boost {
18 : namespace json {
19 :
20 : /** Convert an object of type `T` to @ref value.
21 :
22 : This function attempts to convert an object
23 : of type `T` to @ref value using
24 :
25 : @li one of @ref value's constructors,
26 :
27 : @li a library-provided generic conversion, or
28 :
29 : @li a user-provided overload of `tag_invoke`.
30 :
31 : Out of the function supports default constructible types satisfying
32 : {req_SequenceContainer}, arrays, arithmetic types, `bool`, `std::tuple`,
33 : `std::pair`, `std::optional`, `std::variant`, `std::nullptr_t`, and structs
34 : and enums described using Boost.Describe.
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 : void tag_invoke( value_from_tag, value&, T, const Context&, const FullContext& );
42 : @endcode
43 :
44 : or
45 :
46 : @code
47 : void tag_invoke( value_from_tag, value&, T, const Context& );
48 : @endcode
49 :
50 : or
51 :
52 : @code
53 : void tag_invoke( value_from_tag, value&, T );
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 `ctx` argument can be used either as a tag type to provide conversions
60 : for third-party types, or to pass extra data to the conversion function.
61 :
62 : Overloads **(2)** and **(4)** construct their return value using the
63 : @ref storage_ptr `sp`, which ensures that the memory resource is correctly
64 : propagated.
65 :
66 : @par Exception Safety
67 : Strong guarantee.
68 :
69 : @tparam T The type of the object to convert.
70 :
71 : @tparam Context The type of context passed to the conversion function.
72 :
73 : @param t The object to convert.
74 :
75 : @param ctx Context passed to the conversion function.
76 :
77 : @param jv @ref value out parameter.
78 :
79 : @see @ref value_from_tag, @ref value_to,
80 : <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1895r0.pdf">
81 : tag_invoke: A general pattern for supporting customisable functions</a>
82 : */
83 : /// @{
84 : template< class T, class Context >
85 : void
86 6970 : value_from(
87 : T&& t,
88 : Context const& ctx,
89 : value& jv)
90 : {
91 : using bare_T = detail::remove_cvref<T>;
92 : BOOST_STATIC_ASSERT(detail::conversion_round_trips<
93 : Context, bare_T, detail::value_from_conversion>::value);
94 : using cat = detail::value_from_category<Context, bare_T>;
95 6970 : detail::value_from_impl( cat(), jv, std::forward<T>(t), ctx );
96 6970 : }
97 :
98 : /** Overload
99 : @param t
100 : @param ctx
101 : @param sp A storage pointer referring to the memory resource to use for the
102 : returned @ref value.
103 :
104 : @return Overloads **(2)** and **(4)** return `t` converted to @ref value.
105 : Overloads **(1)** and **3** return `void` instead and pass their result via
106 : the out parameter `jv`.
107 : */
108 : template< class T, class Context >
109 : #ifndef BOOST_JSON_DOCS
110 : typename std::enable_if<
111 : !std::is_same< detail::remove_cvref<Context>, storage_ptr >::value &&
112 : !std::is_same< detail::remove_cvref<Context>, value >::value,
113 : value >::type
114 : #else
115 : value
116 : #endif
117 6951 : value_from(
118 : T&& t,
119 : Context const& ctx,
120 : storage_ptr sp = {})
121 : {
122 6951 : value jv(std::move(sp));
123 6951 : value_from( static_cast<T&&>(t), ctx, jv );
124 6951 : return jv;
125 0 : }
126 :
127 : /// Overload
128 : template<class T>
129 : void
130 19 : value_from(
131 : T&& t,
132 : value& jv)
133 : {
134 19 : value_from( static_cast<T&&>(t), detail::no_context(), jv );
135 19 : }
136 :
137 : /// Overload
138 : template<class T>
139 : value
140 222 : value_from(
141 : T&& t,
142 : storage_ptr sp = {})
143 : {
144 : return value_from(
145 222 : static_cast<T&&>(t), detail::no_context(), std::move(sp) );
146 : }
147 : /// @}
148 :
149 : /** Determine if `T` can be converted to @ref value.
150 :
151 : If `T` can be converted to @ref value via a call to @ref value_from, the
152 : static data member `value` is defined as `true`. Otherwise, `value` is
153 : defined as `false`.
154 :
155 : @see @ref value_from.
156 : */
157 : #ifdef BOOST_JSON_DOCS
158 : template<class T>
159 : using has_value_from = __see_below__;
160 : #else
161 : template<class T>
162 : using has_value_from = detail::can_convert<
163 : detail::remove_cvref<T>, detail::value_from_conversion>;
164 : #endif
165 :
166 : } // namespace json
167 : } // namespace boost
168 :
169 : #endif
|