value_from.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_FROM_HPP
  11. #define BOOST_JSON_VALUE_FROM_HPP
  12. #include <boost/json/detail/config.hpp>
  13. #include <boost/json/value.hpp>
  14. #include <boost/json/detail/value_from.hpp>
  15. BOOST_JSON_NS_BEGIN
  16. /** Customization point tag.
  17. This tag type is used by the function
  18. @ref value_from to select overloads
  19. of `tag_invoke`.
  20. @note This type is empty; it has no members.
  21. @see @ref value_from, @ref value_to, @ref value_to_tag,
  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. #ifndef BOOST_JSON_DOCS
  26. struct value_from_tag;
  27. #else
  28. // VFALCO Doc toolchain doesn't like
  29. // forward declared ordinary classes.
  30. struct value_from_tag {};
  31. #endif
  32. /** Convert an object of type `T` to @ref value.
  33. This function attempts to convert an object
  34. of type `T` to @ref value using
  35. @li one of @ref value's constructors,
  36. @li a library-provided generic conversion, or
  37. @li a user-provided overload of `tag_invoke`.
  38. In all cases, the conversion is done by calling
  39. an overload of `tag_invoke` found by argument-dependent
  40. lookup. Its signature should be similar to:
  41. @code
  42. void tag_invoke( value_from_tag, value&, T );
  43. @endcode
  44. A @ref value constructed
  45. with the @ref storage_ptr passed to @ref value_from is
  46. passed as the second argument to ensure that the memory
  47. resource is correctly propagated.
  48. @par Exception Safety
  49. Strong guarantee.
  50. @tparam T The type of the object to convert.
  51. @returns `t` converted to @ref value.
  52. @param t The object to convert.
  53. @param sp A storage pointer referring to the memory resource
  54. to use for the returned @ref value. The default argument for this
  55. parameter is `{}`.
  56. @see @ref value_from_tag, @ref value_to,
  57. <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1895r0.pdf">
  58. tag_invoke: A general pattern for supporting customisable functions</a>
  59. */
  60. template<class T>
  61. value
  62. value_from(
  63. T&& t,
  64. storage_ptr sp = {})
  65. {
  66. return detail::value_from_impl(
  67. std::forward<T>(t), std::move(sp));
  68. }
  69. /** Determine if `T` can be converted to @ref value.
  70. If `T` can be converted to @ref value via a
  71. call to @ref value_from, the static data member `value`
  72. is defined as `true`. Otherwise, `value` is
  73. defined as `false`.
  74. @see @ref value_from
  75. */
  76. #ifdef BOOST_JSON_DOCS
  77. template<class T>
  78. using has_value_from = __see_below__;
  79. #else
  80. template<class T, class>
  81. struct has_value_from : std::false_type { };
  82. template<class T>
  83. struct has_value_from<T, detail::void_t<
  84. decltype(detail::value_from_impl(std::declval<T&&>(),
  85. std::declval<storage_ptr>()))>>
  86. : std::true_type { };
  87. #endif
  88. BOOST_JSON_NS_END
  89. #endif