2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3 // Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
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)
8 // Official repository: https://github.com/boostorg/json
11 #ifndef BOOST_JSON_VALUE_TO_HPP
12 #define BOOST_JSON_VALUE_TO_HPP
14 #include <boost/json/detail/config.hpp>
15 #include <boost/json/value.hpp>
16 #include <boost/json/detail/value_to.hpp>
20 /** Customization point tag type.
22 This tag type is used by the function
23 @ref value_to to select overloads
26 @note This type is empty; it has no members.
28 @see @ref value_from, @ref value_from_tag, @ref value_to,
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>
35 /** Convert a @ref value to an object of type `T`.
37 This function attempts to convert a @ref value
40 @li one of @ref value's accessors, or
42 @li a library-provided generic conversion, or
44 @li a user-provided overload of `tag_invoke`.
46 In all cases, the conversion is done by calling
47 an overload of `tag_invoke` found by argument-dependent
48 lookup. Its signature should be similar to:
51 T tag_invoke( value_to_tag<T>, value );
54 The object returned by the function call is
55 returned by @ref value_to as the result of the
60 ! std::is_reference< T >::value
66 @tparam T The type to convert to.
68 @returns `jv` converted to `T`.
70 @param jv The @ref value to convert.
72 @see @ref value_to_tag, @ref value_from,
73 <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1895r0.pdf">
74 tag_invoke: A general pattern for supporting customisable functions</a>
78 value_to(const value& jv)
80 BOOST_STATIC_ASSERT(! std::is_reference<T>::value);
81 return detail::value_to_impl(
82 value_to_tag<typename std::remove_cv<T>::type>(), jv);
85 /** Convert a @ref value to an object of type `T`.
87 This overload is **deleted** and participates in overload resolution only
88 when `U` is not @ref value. The overload exists to prevent unintented
89 creation of temporary @ref value instances, e.g.
92 auto flag = value_to<bool>(true);
95 template<class T, class U
96 #ifndef BOOST_JSON_DOCS
97 , class = typename std::enable_if<!std::is_same<U, value>::value>::type
101 value_to(U const& jv) = delete;
103 /** Determine a @ref value can be converted to `T`.
105 If @ref value can be converted to `T` via a
106 call to @ref value_to, the static data member `value`
107 is defined as `true`. Otherwise, `value` is
112 #ifdef BOOST_JSON_DOCS
114 using has_value_to = __see_below__;
116 template<class T, class>
118 : std::false_type { };
121 struct has_value_to<T, detail::void_t<decltype(
122 detail::value_to_impl(
123 value_to_tag<detail::remove_cvref<T>>(),
124 std::declval<const value&>())),
125 typename std::enable_if<
126 ! std::is_reference<T>::value>::type
127 > > : std::true_type { };