Coverage Report

Created: 2026-06-21 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boost/boost/json/impl/visit.hpp
Line
Count
Source
1
//
2
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3
//
4
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
//
7
// Official repository: https://github.com/boostorg/json
8
//
9
10
#ifndef BOOST_JSON_IMPL_VISIT_HPP
11
#define BOOST_JSON_IMPL_VISIT_HPP
12
13
namespace boost {
14
namespace json {
15
16
namespace detail {
17
18
extern
19
BOOST_JSON_DECL
20
std::nullptr_t stable_np;
21
22
} // namespace detail
23
24
template<class Visitor>
25
auto
26
visit(
27
    Visitor&& v,
28
    value& jv) -> decltype(
29
        static_cast<Visitor&&>(v)( std::declval<std::nullptr_t&>() ) )
30
{
31
    switch(jv.kind())
32
    {
33
    default: // unreachable()?
34
    case kind::string:  return static_cast<Visitor&&>(v)( jv.get_string() );
35
    case kind::array:   return static_cast<Visitor&&>(v)( jv.get_array() );
36
    case kind::object:  return static_cast<Visitor&&>(v)( jv.get_object() );
37
    case kind::bool_:   return static_cast<Visitor&&>(v)( jv.get_bool() );
38
    case kind::int64:   return static_cast<Visitor&&>(v)( jv.get_int64() );
39
    case kind::uint64:  return static_cast<Visitor&&>(v)( jv.get_uint64() );
40
    case kind::double_: return static_cast<Visitor&&>(v)( jv.get_double() );
41
    case kind::null:    return static_cast<Visitor&&>(v)( detail::stable_np ) ;
42
    }
43
}
44
45
template<class Visitor>
46
auto
47
visit(
48
    Visitor&& v,
49
    value const& jv) -> decltype(
50
        static_cast<Visitor&&>(v)( std::declval<std::nullptr_t const&>() ) )
51
0
{
52
0
    detail::scalar const& sc = detail::access::get_scalar(jv);
53
0
    switch(jv.kind())
54
0
    {
55
0
    default: // unreachable()?
56
0
    case kind::string:  return static_cast<Visitor&&>(v)( jv.get_string() );
57
0
    case kind::array:   return static_cast<Visitor&&>(v)( jv.get_array() );
58
0
    case kind::object:  return static_cast<Visitor&&>(v)( jv.get_object() );
59
    // local variables work around a bug in older clangs
60
0
    case kind::bool_: {
61
0
        bool const& b = sc.b;
62
0
        return static_cast<Visitor&&>(v)(b);
63
0
    }
64
0
    case kind::int64: {
65
0
        std::int64_t const& i = sc.i;
66
0
        return static_cast<Visitor&&>(v)(i);
67
0
    }
68
0
    case kind::uint64: {
69
0
        std::uint64_t const& u =  sc.u;
70
0
        return static_cast<Visitor&&>(v)(u);
71
0
    }
72
0
    case kind::double_: {
73
0
        double const& d = sc.d;
74
0
        return static_cast<Visitor&&>(v)(d);
75
0
    }
76
0
    case kind::null: {
77
0
        auto const& np = detail::stable_np;
78
0
        return static_cast<Visitor&&>(v)(np) ;
79
0
    }
80
0
    }
81
0
}
82
83
84
template<class Visitor>
85
auto
86
visit(
87
    Visitor&& v,
88
    value&& jv) -> decltype(
89
        static_cast<Visitor&&>(v)( std::declval<std::nullptr_t&&>() ) )
90
{
91
    switch(jv.kind())
92
    {
93
    default: // unreachable()?
94
    case kind::string:  return static_cast<Visitor&&>(v)(std::move( jv.get_string() ));
95
    case kind::array:   return static_cast<Visitor&&>(v)(std::move( jv.get_array() ));
96
    case kind::object:  return static_cast<Visitor&&>(v)(std::move( jv.get_object() ));
97
    case kind::bool_:   return static_cast<Visitor&&>(v)(std::move( detail::access::get_scalar(jv).b ));
98
    case kind::int64:   return static_cast<Visitor&&>(v)(std::move( detail::access::get_scalar(jv).i ));
99
    case kind::uint64:  return static_cast<Visitor&&>(v)(std::move( detail::access::get_scalar(jv).u ));
100
    case kind::double_: return static_cast<Visitor&&>(v)(std::move( detail::access::get_scalar(jv).d ));
101
    case kind::null:    return static_cast<Visitor&&>(v)(std::move( detail::stable_np )) ;
102
    }
103
}
104
105
} // namespace json
106
} // namespace boost
107
108
#endif