result_for.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
  3. // Copyright (c) 2024 Dmitry Arkhipov (grisumbras@yandex.ru)
  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_RESULT_FOR_HPP
  11. #define BOOST_JSON_RESULT_FOR_HPP
  12. #include <boost/json/detail/config.hpp>
  13. #include <boost/json/fwd.hpp>
  14. #include <boost/assert/source_location.hpp>
  15. #include <boost/system/result.hpp>
  16. namespace boost {
  17. namespace json {
  18. /**
  19. Helper trait that returns @ref boost::system::result.
  20. The primary template is an incomplete type. The library provides a partial
  21. specialisation `result_for<T1, value>`, that has nested type alias `type`
  22. that aliases the type `result<T1>`.
  23. The purpose of this trait is to let users provide non-throwing conversions
  24. for their types without creating a physical dependency on Boost.Json. For
  25. example:
  26. @code
  27. namespace boost {
  28. namespace json {
  29. template<class T>
  30. struct value_to_tag;
  31. template<class T1, class T2>
  32. struct result_for;
  33. }
  34. }
  35. namespace mine
  36. {
  37. class my_class;
  38. ...
  39. template<class JsonValue>
  40. boost::json::result_for<my_class, JsonValue>
  41. tag_invoke(boost::json::try_value_to_tag<my_class>, const JsonValue& jv)
  42. { ... }
  43. }
  44. @endcode
  45. @see @ref try_value_to, @ref try_value_to_tag
  46. */
  47. template <class T1, class T2>
  48. struct result_for;
  49. /** Create @ref boost::system::result containing a portable error code.
  50. This function constructs a `boost::system::result<T>` that stores
  51. `error_code` with `value()` equal to `e` and `category()` equal to
  52. @ref boost::system::generic_category().
  53. The main use for this function is in implementation of functions returning
  54. `boost::system::result`, without including `boost/json/system_error.hpp` or
  55. even `<system_error>`. In particular, it may be useful for customizations
  56. of @ref try_value_to without creating a physical dependency on Boost.JSON.
  57. For example:
  58. @code
  59. #include <cerrno>
  60. #include <boost/assert/source_location.hpp>
  61. namespace boost {
  62. namespace json {
  63. class value;
  64. template<class T>
  65. struct try_value_to_tag;
  66. template<class T1, class T2>
  67. struct result_for;
  68. template <class T>
  69. typename result_for<T, value>::type
  70. result_from_errno(int e, boost::source_location const* loc) noexcept
  71. }
  72. }
  73. namespace mine {
  74. class my_class;
  75. ...
  76. template<class JsonValue>
  77. boost::json::result_for<my_class, JsonValue>
  78. tag_invoke(boost::json::try_value_to_tag<my_class>, const JsonValue& jv)
  79. {
  80. BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION;
  81. if( !jv.is_null() )
  82. return boost::json::result_from_errno<my_class>(EINVAL, &loc);
  83. return my_class();
  84. }
  85. }
  86. @endcode
  87. @par Exception Safety
  88. Does not throw exceptions.
  89. @tparam T The value type of returned `result`.
  90. @param e The error value.
  91. @param loc The error location.
  92. @returns A @ref boost::system::result containing an error.
  93. @see @ref try_value_to_tag, @ref try_value_to, @ref result_for.
  94. */
  95. template <class T>
  96. typename result_for<T, value>::type
  97. result_from_errno(int e, boost::source_location const* loc) noexcept
  98. {
  99. system::error_code ec(e, system::generic_category(), loc);
  100. return {system::in_place_error, ec};
  101. }
  102. } // namespace json
  103. } // namespace boost
  104. #endif // BOOST_JSON_RESULT_FOR_HPP