LCOV - code coverage report
Current view: top level - json - value_from.hpp (source / functions) Coverage Total Hit Missed
Test: coverage_remapped.info Lines: 92.3 % 13 12 1
Test Date: 2026-06-16 12:19:39 Functions: 98.5 % 535 527 8

           TLA  Line data    Source code
       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                 : 
      12                 : #ifndef BOOST_JSON_VALUE_FROM_HPP
      13                 : #define BOOST_JSON_VALUE_FROM_HPP
      14                 : 
      15                 : #include <boost/core/detail/static_assert.hpp>
      16                 : #include <boost/json/detail/value_from.hpp>
      17                 : 
      18                 : namespace boost {
      19                 : namespace json {
      20                 : 
      21                 : /** Convert an object of type `T` to @ref value.
      22                 : 
      23                 :     This function attempts to convert an object
      24                 :     of type `T` to @ref value using
      25                 : 
      26                 :     @li one of @ref value's constructors,
      27                 : 
      28                 :     @li a library-provided generic conversion, or
      29                 : 
      30                 :     @li a user-provided overload of `tag_invoke`.
      31                 : 
      32                 :     Out of the function supports default constructible types satisfying
      33                 :     {req_SequenceContainer}, arrays, arithmetic types, `bool`, `std::tuple`,
      34                 :     `std::pair`, `std::optional`, `std::variant`, `std::nullptr_t`, and structs
      35                 :     and enums described using Boost.Describe.
      36                 : 
      37                 :     Conversion of other types is done by calling an overload of `tag_invoke`
      38                 :     found by argument-dependent lookup. Its signature should be similar to:
      39                 : 
      40                 :     @code
      41                 :     template< class FullContext >
      42                 :     void tag_invoke( value_from_tag, value&, T, const Context&, const FullContext& );
      43                 :     @endcode
      44                 : 
      45                 :     or
      46                 : 
      47                 :     @code
      48                 :     void tag_invoke( value_from_tag, value&, T, const Context& );
      49                 :     @endcode
      50                 : 
      51                 :     or
      52                 : 
      53                 :     @code
      54                 :     void tag_invoke( value_from_tag, value&, T );
      55                 :     @endcode
      56                 : 
      57                 :     The overloads are checked for existence in that order and the first that
      58                 :     matches will be selected. <br>
      59                 : 
      60                 :     The `ctx` argument can be used either as a tag type to provide conversions
      61                 :     for third-party types, or to pass extra data to the conversion function.
      62                 : 
      63                 :     Overloads **(2)** and **(4)** construct their return value using the
      64                 :     @ref storage_ptr `sp`, which ensures that the memory resource is correctly
      65                 :     propagated.
      66                 : 
      67                 :     @par Exception Safety
      68                 :     Strong guarantee.
      69                 : 
      70                 :     @tparam T The type of the object to convert.
      71                 : 
      72                 :     @tparam Context The type of context passed to the conversion function.
      73                 : 
      74                 :     @param t The object to convert.
      75                 : 
      76                 :     @param ctx Context passed to the conversion function.
      77                 : 
      78                 :     @param jv @ref value out parameter.
      79                 : 
      80                 :     @see @ref value_from_tag, @ref value_to,
      81                 :     <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1895r0.pdf">
      82                 :         tag_invoke: A general pattern for supporting customisable functions</a>
      83                 : */
      84                 : /// @{
      85                 : template< class T, class Context >
      86                 : void
      87 HIT        7056 : value_from(
      88                 :     T&& t,
      89                 :     Context const& ctx,
      90                 :     value& jv)
      91                 : {
      92                 :     using bare_T = detail::remove_cvref<T>;
      93                 :     using cat = detail::value_from_category<Context, bare_T>;
      94            7056 :     detail::value_from_impl( cat(), jv, std::forward<T>(t), ctx );
      95            7056 : }
      96                 : 
      97                 : /** Overload
      98                 :    @param t
      99                 :    @param ctx
     100                 :    @param sp A storage pointer referring to the memory resource to use for the
     101                 :    returned @ref value.
     102                 : 
     103                 :    @return Overloads **(2)** and **(4)** return `t` converted to @ref value.
     104                 :    Overloads **(1)** and **3** return `void` instead and pass their result via
     105                 :    the out parameter `jv`.
     106                 : */
     107                 : template< class T, class Context >
     108                 : #ifndef BOOST_JSON_DOCS
     109                 : typename std::enable_if<
     110                 :     !std::is_same< detail::remove_cvref<Context>, storage_ptr >::value &&
     111                 :     !std::is_same< detail::remove_cvref<Context>, value >::value,
     112                 :     value >::type
     113                 : #else
     114                 : value
     115                 : #endif
     116            7037 : value_from(
     117                 :     T&& t,
     118                 :     Context const& ctx,
     119                 :     storage_ptr sp = {})
     120                 : {
     121            7037 :     value jv(std::move(sp));
     122            7037 :     value_from( static_cast<T&&>(t), ctx, jv );
     123            7037 :     return jv;
     124 MIS           0 : }
     125                 : 
     126                 : /// Overload
     127                 : template<class T>
     128                 : void
     129 HIT          19 : value_from(
     130                 :     T&& t,
     131                 :     value& jv)
     132                 : {
     133              19 :    value_from( static_cast<T&&>(t), detail::no_context(), jv );
     134              19 : }
     135                 : 
     136                 : /// Overload
     137                 : template<class T>
     138                 : value
     139             297 : value_from(
     140                 :     T&& t,
     141                 :     storage_ptr sp = {})
     142                 : {
     143                 :    return value_from(
     144             297 :            static_cast<T&&>(t), detail::no_context(), std::move(sp) );
     145                 : }
     146                 : /// @}
     147                 : 
     148                 : /** Determine if `T` can be converted to @ref value.
     149                 : 
     150                 :     If `T` can be converted to @ref value via a call to @ref value_from, the
     151                 :     static data member `value` is defined as `true`. Otherwise, `value` is
     152                 :     defined as `false`.
     153                 : 
     154                 :     @see @ref value_from.
     155                 : */
     156                 : #ifdef BOOST_JSON_DOCS
     157                 : template<class T>
     158                 : using has_value_from = __see_below__;
     159                 : #else
     160                 : template<class T>
     161                 : using has_value_from = detail::can_convert<
     162                 :     detail::remove_cvref<T>, detail::value_from_conversion>;
     163                 : #endif
     164                 : 
     165                 : } // namespace json
     166                 : } // namespace boost
     167                 : 
     168                 : #endif
        

Generated by: LCOV version 2.3