]>
Commit | Line | Data |
---|---|---|
20effc67 TL |
1 | // |
2 | // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com) | |
3 | // Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com) | |
4 | // | |
5 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
6 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
7 | // | |
8 | // Official repository: https://github.com/boostorg/json | |
9 | // | |
10 | ||
11 | #ifndef BOOST_JSON_VALUE_FROM_HPP | |
12 | #define BOOST_JSON_VALUE_FROM_HPP | |
13 | ||
14 | #include <boost/json/detail/config.hpp> | |
15 | #include <boost/json/value.hpp> | |
16 | #include <boost/json/detail/value_from.hpp> | |
17 | ||
18 | BOOST_JSON_NS_BEGIN | |
19 | ||
20 | /** Customization point tag. | |
1e59de90 | 21 | |
20effc67 TL |
22 | This tag type is used by the function |
23 | @ref value_from to select overloads | |
24 | of `tag_invoke`. | |
25 | ||
26 | @note This type is empty; it has no members. | |
27 | ||
28 | @see @ref value_from, @ref value_to, @ref value_to_tag, | |
29 | <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1895r0.pdf"> | |
30 | tag_invoke: A general pattern for supporting customisable functions</a> | |
31 | */ | |
32 | #ifndef BOOST_JSON_DOCS | |
33 | struct value_from_tag; | |
34 | #else | |
35 | // VFALCO Doc toolchain doesn't like | |
36 | // forward declared ordinary classes. | |
37 | struct value_from_tag {}; | |
38 | #endif | |
39 | ||
40 | /** Convert an object of type `T` to @ref value. | |
41 | ||
42 | This function attempts to convert an object | |
43 | of type `T` to @ref value using | |
44 | ||
45 | @li one of @ref value's constructors, | |
46 | ||
47 | @li a library-provided generic conversion, or | |
48 | ||
49 | @li a user-provided overload of `tag_invoke`. | |
1e59de90 | 50 | |
20effc67 TL |
51 | In all cases, the conversion is done by calling |
52 | an overload of `tag_invoke` found by argument-dependent | |
53 | lookup. Its signature should be similar to: | |
1e59de90 | 54 | |
20effc67 TL |
55 | @code |
56 | void tag_invoke( value_from_tag, value&, T ); | |
57 | @endcode | |
58 | ||
59 | A @ref value constructed | |
60 | with the @ref storage_ptr passed to @ref value_from is | |
61 | passed as the second argument to ensure that the memory | |
62 | resource is correctly propagated. | |
63 | ||
64 | @par Exception Safety | |
65 | Strong guarantee. | |
66 | ||
67 | @tparam T The type of the object to convert. | |
1e59de90 | 68 | |
20effc67 TL |
69 | @returns `t` converted to @ref value. |
70 | ||
71 | @param t The object to convert. | |
72 | ||
73 | @param sp A storage pointer referring to the memory resource | |
74 | to use for the returned @ref value. The default argument for this | |
75 | parameter is `{}`. | |
76 | ||
77 | @see @ref value_from_tag, @ref value_to, | |
78 | <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1895r0.pdf"> | |
79 | tag_invoke: A general pattern for supporting customisable functions</a> | |
80 | */ | |
81 | template<class T> | |
82 | value | |
83 | value_from( | |
84 | T&& t, | |
85 | storage_ptr sp = {}) | |
86 | { | |
87 | return detail::value_from_impl( | |
88 | std::forward<T>(t), std::move(sp)); | |
89 | } | |
90 | ||
1e59de90 TL |
91 | /** Convert an object of type `T` to @ref value. |
92 | ||
93 | This function attempts to convert an object | |
94 | of type `T` to @ref value using | |
95 | ||
96 | @li one of @ref value's constructors, | |
97 | ||
98 | @li a library-provided generic conversion, or | |
99 | ||
100 | @li a user-provided overload of `tag_invoke`. | |
101 | ||
102 | In all cases, the conversion is done by calling | |
103 | an overload of `tag_invoke` found by argument-dependent | |
104 | lookup. Its signature should be similar to: | |
105 | ||
106 | @code | |
107 | void tag_invoke( value_from_tag, value&, T ); | |
108 | @endcode | |
109 | ||
110 | @par Exception Safety | |
111 | Strong guarantee. | |
112 | ||
113 | @tparam T The type of the object to convert. | |
114 | ||
115 | @param t The object to convert. | |
116 | ||
117 | @param jv @ref value out parameter. | |
118 | ||
119 | @see @ref value_from_tag, @ref value_to, | |
120 | <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1895r0.pdf"> | |
121 | tag_invoke: A general pattern for supporting customisable functions</a> | |
122 | */ | |
123 | template<class T> | |
124 | void | |
125 | value_from( | |
126 | T&& t, | |
127 | value& jv) | |
128 | { | |
129 | jv = detail::value_from_impl( | |
130 | std::forward<T>(t), jv.storage()); | |
131 | } | |
132 | ||
20effc67 TL |
133 | /** Determine if `T` can be converted to @ref value. |
134 | ||
1e59de90 | 135 | If `T` can be converted to @ref value via a |
20effc67 TL |
136 | call to @ref value_from, the static data member `value` |
137 | is defined as `true`. Otherwise, `value` is | |
138 | defined as `false`. | |
139 | ||
140 | @see @ref value_from | |
141 | */ | |
142 | #ifdef BOOST_JSON_DOCS | |
143 | template<class T> | |
144 | using has_value_from = __see_below__; | |
145 | #else | |
146 | template<class T, class> | |
147 | struct has_value_from : std::false_type { }; | |
148 | ||
149 | template<class T> | |
150 | struct has_value_from<T, detail::void_t< | |
151 | decltype(detail::value_from_impl(std::declval<T&&>(), | |
152 | std::declval<storage_ptr>()))>> | |
153 | : std::true_type { }; | |
154 | #endif | |
155 | ||
156 | BOOST_JSON_NS_END | |
157 | ||
1e59de90 | 158 | #endif |