visit.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // Copyright (c) 2019 Vinnie Falco ([email protected])
  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. #ifndef BOOST_JSON_IMPL_VISIT_HPP
  10. #define BOOST_JSON_IMPL_VISIT_HPP
  11. namespace boost {
  12. namespace json {
  13. template<class Visitor>
  14. auto
  15. visit(
  16. Visitor&& v,
  17. value& jv) -> decltype(
  18. static_cast<Visitor&&>(v)( std::declval<std::nullptr_t&>() ) )
  19. {
  20. switch(jv.kind())
  21. {
  22. default: // unreachable()?
  23. case kind::string: return static_cast<Visitor&&>(v)( jv.get_string() );
  24. case kind::array: return static_cast<Visitor&&>(v)( jv.get_array() );
  25. case kind::object: return static_cast<Visitor&&>(v)( jv.get_object() );
  26. case kind::bool_: return static_cast<Visitor&&>(v)( jv.get_bool() );
  27. case kind::int64: return static_cast<Visitor&&>(v)( jv.get_int64() );
  28. case kind::uint64: return static_cast<Visitor&&>(v)( jv.get_uint64() );
  29. case kind::double_: return static_cast<Visitor&&>(v)( jv.get_double() );
  30. case kind::null: {
  31. auto np = nullptr;
  32. return static_cast<Visitor&&>(v)(np) ;
  33. }
  34. }
  35. }
  36. template<class Visitor>
  37. auto
  38. visit(
  39. Visitor&& v,
  40. value const& jv) -> decltype(
  41. static_cast<Visitor&&>(v)( std::declval<std::nullptr_t const&>() ) )
  42. {
  43. switch(jv.kind())
  44. {
  45. default: // unreachable()?
  46. case kind::string: return static_cast<Visitor&&>(v)( jv.get_string() );
  47. case kind::array: return static_cast<Visitor&&>(v)( jv.get_array() );
  48. case kind::object: return static_cast<Visitor&&>(v)( jv.get_object() );
  49. case kind::bool_: return static_cast<Visitor&&>(v)( jv.get_bool() );
  50. case kind::int64: return static_cast<Visitor&&>(v)( jv.get_int64() );
  51. case kind::uint64: return static_cast<Visitor&&>(v)( jv.get_uint64() );
  52. case kind::double_: return static_cast<Visitor&&>(v)( jv.get_double() );
  53. case kind::null: {
  54. auto const np = nullptr;
  55. return static_cast<Visitor&&>(v)(np) ;
  56. }
  57. }
  58. }
  59. template<class Visitor>
  60. auto
  61. visit(
  62. Visitor&& v,
  63. value&& jv) -> decltype(
  64. static_cast<Visitor&&>(v)( std::declval<std::nullptr_t&&>() ) )
  65. {
  66. switch(jv.kind())
  67. {
  68. default: // unreachable()?
  69. case kind::string: return static_cast<Visitor&&>(v)( std::move( jv.get_string() ) );
  70. case kind::array: return static_cast<Visitor&&>(v)( std::move( jv.get_array() ) );
  71. case kind::object: return static_cast<Visitor&&>(v)( std::move( jv.get_object() ) );
  72. case kind::bool_: return static_cast<Visitor&&>(v)( std::move( jv.get_bool() ) );
  73. case kind::int64: return static_cast<Visitor&&>(v)( std::move( jv.get_int64() ) );
  74. case kind::uint64: return static_cast<Visitor&&>(v)( std::move( jv.get_uint64() ) );
  75. case kind::double_: return static_cast<Visitor&&>(v)( std::move( jv.get_double() ) );
  76. case kind::null: return static_cast<Visitor&&>(v)( std::nullptr_t() ) ;
  77. }
  78. }
  79. } // namespace json
  80. } // namespace boost
  81. #endif