1  
//
1  
//
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3  
//
3  
//
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
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)
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
//
6  
//
7  
// Official repository: https://github.com/boostorg/json
7  
// Official repository: https://github.com/boostorg/json
8  
//
8  
//
9  

9  

10  
#ifndef BOOST_JSON_IMPL_VISIT_HPP
10  
#ifndef BOOST_JSON_IMPL_VISIT_HPP
11  
#define BOOST_JSON_IMPL_VISIT_HPP
11  
#define BOOST_JSON_IMPL_VISIT_HPP
12  

12  

13  
namespace boost {
13  
namespace boost {
14  
namespace json {
14  
namespace json {
15  

15  

16  

16  

17  
template<class Visitor>
17  
template<class Visitor>
18  
auto
18  
auto
19  
visit(
19  
visit(
20  
    Visitor&& v,
20  
    Visitor&& v,
21  
    value& jv) -> decltype(
21  
    value& jv) -> decltype(
22  
        static_cast<Visitor&&>(v)( std::declval<std::nullptr_t&>() ) )
22  
        static_cast<Visitor&&>(v)( std::declval<std::nullptr_t&>() ) )
23  
{
23  
{
24  
    switch(jv.kind())
24  
    switch(jv.kind())
25  
    {
25  
    {
26  
    default: // unreachable()?
26  
    default: // unreachable()?
27  
    case kind::string:  return static_cast<Visitor&&>(v)( jv.get_string() );
27  
    case kind::string:  return static_cast<Visitor&&>(v)( jv.get_string() );
28  
    case kind::array:   return static_cast<Visitor&&>(v)( jv.get_array() );
28  
    case kind::array:   return static_cast<Visitor&&>(v)( jv.get_array() );
29  
    case kind::object:  return static_cast<Visitor&&>(v)( jv.get_object() );
29  
    case kind::object:  return static_cast<Visitor&&>(v)( jv.get_object() );
30  
    case kind::bool_:   return static_cast<Visitor&&>(v)( jv.get_bool() );
30  
    case kind::bool_:   return static_cast<Visitor&&>(v)( jv.get_bool() );
31  
    case kind::int64:   return static_cast<Visitor&&>(v)( jv.get_int64() );
31  
    case kind::int64:   return static_cast<Visitor&&>(v)( jv.get_int64() );
32  
    case kind::uint64:  return static_cast<Visitor&&>(v)( jv.get_uint64() );
32  
    case kind::uint64:  return static_cast<Visitor&&>(v)( jv.get_uint64() );
33  
    case kind::double_: return static_cast<Visitor&&>(v)( jv.get_double() );
33  
    case kind::double_: return static_cast<Visitor&&>(v)( jv.get_double() );
34  
    case kind::null: {
34  
    case kind::null: {
35  
        auto np = nullptr;
35  
        auto np = nullptr;
36  
        return static_cast<Visitor&&>(v)(np) ;
36  
        return static_cast<Visitor&&>(v)(np) ;
37  
    }
37  
    }
38  
    }
38  
    }
39  
}
39  
}
40  

40  

41  
template<class Visitor>
41  
template<class Visitor>
42  
auto
42  
auto
43  
visit(
43  
visit(
44  
    Visitor&& v,
44  
    Visitor&& v,
45  
    value const& jv) -> decltype(
45  
    value const& jv) -> decltype(
46  
        static_cast<Visitor&&>(v)( std::declval<std::nullptr_t const&>() ) )
46  
        static_cast<Visitor&&>(v)( std::declval<std::nullptr_t const&>() ) )
47  
{
47  
{
48  
    switch(jv.kind())
48  
    switch(jv.kind())
49  
    {
49  
    {
50  
    default: // unreachable()?
50  
    default: // unreachable()?
51  
    case kind::string:  return static_cast<Visitor&&>(v)( jv.get_string() );
51  
    case kind::string:  return static_cast<Visitor&&>(v)( jv.get_string() );
52  
    case kind::array:   return static_cast<Visitor&&>(v)( jv.get_array() );
52  
    case kind::array:   return static_cast<Visitor&&>(v)( jv.get_array() );
53  
    case kind::object:  return static_cast<Visitor&&>(v)( jv.get_object() );
53  
    case kind::object:  return static_cast<Visitor&&>(v)( jv.get_object() );
54  
    case kind::bool_:   return static_cast<Visitor&&>(v)( jv.get_bool() );
54  
    case kind::bool_:   return static_cast<Visitor&&>(v)( jv.get_bool() );
55  
    case kind::int64:   return static_cast<Visitor&&>(v)( jv.get_int64() );
55  
    case kind::int64:   return static_cast<Visitor&&>(v)( jv.get_int64() );
56  
    case kind::uint64:  return static_cast<Visitor&&>(v)( jv.get_uint64() );
56  
    case kind::uint64:  return static_cast<Visitor&&>(v)( jv.get_uint64() );
57  
    case kind::double_: return static_cast<Visitor&&>(v)( jv.get_double() );
57  
    case kind::double_: return static_cast<Visitor&&>(v)( jv.get_double() );
58  
    case kind::null: {
58  
    case kind::null: {
59  
        auto const np = nullptr;
59  
        auto const np = nullptr;
60  
        return static_cast<Visitor&&>(v)(np) ;
60  
        return static_cast<Visitor&&>(v)(np) ;
61  
    }
61  
    }
62  
    }
62  
    }
63  
}
63  
}
64  

64  

65  

65  

66  
template<class Visitor>
66  
template<class Visitor>
67  
auto
67  
auto
68  
visit(
68  
visit(
69  
    Visitor&& v,
69  
    Visitor&& v,
70  
    value&& jv) -> decltype(
70  
    value&& jv) -> decltype(
71  
        static_cast<Visitor&&>(v)( std::declval<std::nullptr_t&&>() ) )
71  
        static_cast<Visitor&&>(v)( std::declval<std::nullptr_t&&>() ) )
72  
{
72  
{
73  
    switch(jv.kind())
73  
    switch(jv.kind())
74  
    {
74  
    {
75  
    default: // unreachable()?
75  
    default: // unreachable()?
76  
    case kind::string:  return static_cast<Visitor&&>(v)( std::move( jv.get_string() ) );
76  
    case kind::string:  return static_cast<Visitor&&>(v)( std::move( jv.get_string() ) );
77  
    case kind::array:   return static_cast<Visitor&&>(v)( std::move( jv.get_array() ) );
77  
    case kind::array:   return static_cast<Visitor&&>(v)( std::move( jv.get_array() ) );
78  
    case kind::object:  return static_cast<Visitor&&>(v)( std::move( jv.get_object() ) );
78  
    case kind::object:  return static_cast<Visitor&&>(v)( std::move( jv.get_object() ) );
79  
    case kind::bool_:   return static_cast<Visitor&&>(v)( std::move( jv.get_bool() ) );
79  
    case kind::bool_:   return static_cast<Visitor&&>(v)( std::move( jv.get_bool() ) );
80  
    case kind::int64:   return static_cast<Visitor&&>(v)( std::move( jv.get_int64() ) );
80  
    case kind::int64:   return static_cast<Visitor&&>(v)( std::move( jv.get_int64() ) );
81  
    case kind::uint64:  return static_cast<Visitor&&>(v)( std::move( jv.get_uint64() ) );
81  
    case kind::uint64:  return static_cast<Visitor&&>(v)( std::move( jv.get_uint64() ) );
82  
    case kind::double_: return static_cast<Visitor&&>(v)( std::move( jv.get_double() ) );
82  
    case kind::double_: return static_cast<Visitor&&>(v)( std::move( jv.get_double() ) );
83  
    case kind::null:    return static_cast<Visitor&&>(v)( std::nullptr_t() ) ;
83  
    case kind::null:    return static_cast<Visitor&&>(v)( std::nullptr_t() ) ;
84  
    }
84  
    }
85  
}
85  
}
86  

86  

87  
} // namespace json
87  
} // namespace json
88  
} // namespace boost
88  
} // namespace boost
89  

89  

90  
#endif
90  
#endif