// // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com) // Copyright (c) 2024 Dmitry Arkhipov (grisumbras@yandex.ru) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // // Official repository: https://github.com/boostorg/json // #ifndef BOOST_JSON_RESULT_FOR_HPP #define BOOST_JSON_RESULT_FOR_HPP #include #include #include #include namespace boost { namespace json { /** Helper trait that returns @ref result The primary template is an incomplete type. The library provides a partial specialisation `result_for`, that has nested type alias `type` that aliases the type `result`. The purpose of this trait is to let users provide non-throwing conversions for their types without creating a physical dependency on Boost.Json. For example: @code namespace boost { namespace json { template struct value_to_tag; template struct result_for; } } namespace mine { class my_class; ... template boost::json::result_for tag_invoke(boost::json::try_value_to_tag, const JsonValue& jv) { ... } } @endcode @see @ref try_value_to, @ref try_value_to_tag */ template struct result_for; /** Create @ref result storing a portable error code This function constructs a `boost::system::result` that stores `boost::system::error_code` with `value()` equal to `e` and `category()` equal to `boost::system::generic_category()`.
The main use for this function is in implementation of functions returning @ref result, without including `boost/json/system_error.hpp` or even ``. In particular, it may be useful for customizations of @ref try_value_to without creating a physical dependency on Boost.JSON. For example: @code #include #include namespace boost { namespace json { class value; template struct try_value_to_tag; template struct result_for; template typename result_for::type result_from_errno(int e, boost::source_location const* loc) noexcept } } namespace mine { class my_class; ... template boost::json::result_for tag_invoke(boost::json::try_value_to_tag, const JsonValue& jv) { BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION; if( !jv.is_null() ) return boost::json::result_from_errno(EINVAL, &loc); return my_class(); } } @endcode @par Exception Safety Does not throw exceptions. @tparam T The value type of returned `result`. @param e The error value. @param loc The error location. @returns `boost::system::error_code` with `value()` equal to `e` and `category()` equal to `boost::system::generic_category()`. @see @ref try_value_to_tag, @ref try_value_to, @ref result_for, `boost::system::generic_category`, `boost::source_location`. */ template typename result_for::type result_from_errno(int e, boost::source_location const* loc) noexcept { system::error_code ec(e, system::generic_category(), loc); return {system::in_place_error, ec}; } } // namespace json } // namespace boost #endif // BOOST_JSON_RESULT_FOR_HPP