value_from.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #ifndef BOOST_JSON_VALUE_FROM_HPP
  12. #define BOOST_JSON_VALUE_FROM_HPP
  13. #include <boost/json/detail/value_from.hpp>
  14. namespace boost {
  15. namespace json {
  16. /** Convert an object of type `T` to @ref value.
  17. This function attempts to convert an object
  18. of type `T` to @ref value using
  19. @li one of @ref value's constructors,
  20. @li a library-provided generic conversion, or
  21. @li a user-provided overload of `tag_invoke`.
  22. Out of the box the function supports types satisfying
  23. <a href="https://en.cppreference.com/w/cpp/named_req/SequenceContainer"><em>SequenceContainer</em></a>,
  24. arrays, arithmetic types, `bool`, `std::tuple`, `std::pair`,
  25. `std::variant`, `std::optional`, `std::monostate`, and `std::nullopt_t`.
  26. Conversion of other types is done by calling an overload of `tag_invoke`
  27. found by argument-dependent lookup. Its signature should be similar to:
  28. @code
  29. void tag_invoke( value_from_tag, value&, T );
  30. @endcode
  31. @par Exception Safety
  32. Strong guarantee.
  33. @tparam T The type of the object to convert.
  34. @param t The object to convert.
  35. @param jv @ref value out parameter.
  36. @see @ref value_from_tag, @ref value_to,
  37. <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1895r0.pdf">
  38. tag_invoke: A general pattern for supporting customisable functions</a>
  39. */
  40. template<class T>
  41. void
  42. value_from(
  43. T&& t,
  44. value& jv)
  45. {
  46. using bare_T = detail::remove_cvref<T>;
  47. BOOST_STATIC_ASSERT(detail::conversion_round_trips<
  48. bare_T, detail::value_from_conversion>::value);
  49. using impl = detail::value_from_implementation<bare_T>;
  50. detail::value_from_helper(jv, std::forward<T>(t), impl());
  51. }
  52. /** Convert an object of type `T` to @ref value.
  53. This function attempts to convert an object
  54. of type `T` to @ref value using
  55. @li one of @ref value's constructors,
  56. @li a library-provided generic conversion, or
  57. @li a user-provided overload of `tag_invoke`.
  58. Out of the box the function supports types satisfying
  59. <a href="https://en.cppreference.com/w/cpp/named_req/SequenceContainer"><em>SequenceContainer</em></a>,
  60. arrays, arithmetic types, `bool`, `std::tuple`, `std::pair`,
  61. `std::variant`, `std::optional`, `std::monostate`, and `std::nullopt_t`.
  62. Conversion of other types is done by calling an overload of `tag_invoke`
  63. found by argument-dependent lookup. Its signature should be similar to:
  64. @code
  65. void tag_invoke( value_from_tag, value&, T );
  66. @endcode
  67. A @ref value constructed
  68. with the @ref storage_ptr passed to @ref value_from is
  69. passed as the second argument to ensure that the memory
  70. resource is correctly propagated.
  71. @par Exception Safety
  72. Strong guarantee.
  73. @tparam T The type of the object to convert.
  74. @returns `t` converted to @ref value.
  75. @param t The object to convert.
  76. @param sp A storage pointer referring to the memory resource
  77. to use for the returned @ref value. The default argument for this
  78. parameter is `{}`.
  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. template<class T>
  84. value
  85. value_from(
  86. T&& t,
  87. storage_ptr sp = {})
  88. {
  89. value jv(std::move(sp));
  90. json::value_from(
  91. std::forward<T>(t), jv);
  92. return jv;
  93. }
  94. /** Determine if `T` can be converted to @ref value.
  95. If `T` can be converted to @ref value via a
  96. call to @ref value_from, the static data member `value`
  97. is defined as `true`. Otherwise, `value` is
  98. defined as `false`.
  99. @see @ref value_from
  100. */
  101. #ifdef BOOST_JSON_DOCS
  102. template<class T>
  103. using has_value_from = __see_below__;
  104. #else
  105. template<class T>
  106. using has_value_from = detail::can_convert<
  107. detail::remove_cvref<T>, detail::value_from_conversion>;
  108. #endif
  109. } // namespace json
  110. } // namespace boost
  111. #endif