value_to.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #ifndef BOOST_JSON_VALUE_TO_HPP
  11. #define BOOST_JSON_VALUE_TO_HPP
  12. #include <boost/json/detail/config.hpp>
  13. #include <boost/json/value.hpp>
  14. #include <boost/json/detail/value_to.hpp>
  15. BOOST_JSON_NS_BEGIN
  16. /** Customization point tag type.
  17. This tag type is used by the function
  18. @ref value_to to select overloads
  19. of `tag_invoke`.
  20. @note This type is empty; it has no members.
  21. @see @ref value_from, @ref value_from_tag, @ref value_to,
  22. <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1895r0.pdf">
  23. tag_invoke: A general pattern for supporting customisable functions</a>
  24. */
  25. template<class T>
  26. struct value_to_tag;
  27. /** Convert a @ref value to an object of type `T`.
  28. This function attempts to convert a @ref value
  29. to `T` using
  30. @li one of @ref value's accessors, or
  31. @li a library-provided generic conversion, or
  32. @li a user-provided overload of `tag_invoke`.
  33. In all cases, the conversion is done by calling
  34. an overload of `tag_invoke` found by argument-dependent
  35. lookup. Its signature should be similar to:
  36. @code
  37. T tag_invoke( value_to_tag<T>, value );
  38. @endcode
  39. The object returned by the function call is
  40. returned by @ref value_to as the result of the
  41. conversion.
  42. @par Constraints
  43. @code
  44. ! std::is_reference< T >::value
  45. @endcode
  46. @par Exception Safety
  47. Strong guarantee.
  48. @tparam T The type to convert to.
  49. @returns `jv` converted to `T`.
  50. @param jv The @ref value to convert.
  51. @see @ref value_to_tag, @ref value_from,
  52. <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1895r0.pdf">
  53. tag_invoke: A general pattern for supporting customisable functions</a>
  54. */
  55. #ifdef BOOST_JSON_DOCS
  56. template<class T>
  57. T
  58. value_to(const value& jv);
  59. #else
  60. template<class T, class U
  61. , class = typename std::enable_if<
  62. ! std::is_reference<T>::value &&
  63. std::is_same<U, value>::value>::type
  64. >
  65. T
  66. value_to(const U& jv)
  67. {
  68. return detail::value_to_impl(
  69. value_to_tag<typename std::remove_cv<T>::type>(), jv);
  70. }
  71. #endif
  72. /** Determine a @ref value can be converted to `T`.
  73. If @ref value can be converted to `T` via a
  74. call to @ref value_to, the static data member `value`
  75. is defined as `true`. Otherwise, `value` is
  76. defined as `false`.
  77. @see @ref value_to
  78. */
  79. #ifdef BOOST_JSON_DOCS
  80. template<class T>
  81. using has_value_to = __see_below__;
  82. #else
  83. template<class T, class>
  84. struct has_value_to
  85. : std::false_type { };
  86. template<class T>
  87. struct has_value_to<T, detail::void_t<decltype(
  88. detail::value_to_impl(
  89. value_to_tag<detail::remove_cvref<T>>(),
  90. std::declval<const value&>())),
  91. typename std::enable_if<
  92. ! std::is_reference<T>::value>::type
  93. > > : std::true_type { };
  94. #endif
  95. BOOST_JSON_NS_END
  96. #endif