visit.hpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. #ifndef BOOST_JSON_IMPL_VISIT_HPP
  10. #define BOOST_JSON_IMPL_VISIT_HPP
  11. BOOST_JSON_NS_BEGIN
  12. template<class Visitor>
  13. auto
  14. visit(
  15. Visitor&& v,
  16. value& jv) -> decltype(
  17. std::declval<Visitor>()(nullptr))
  18. {
  19. switch(jv.kind())
  20. {
  21. default: // unreachable()?
  22. case kind::null: return std::forward<Visitor>(v)(nullptr);
  23. case kind::bool_: return std::forward<Visitor>(v)(jv.get_bool());
  24. case kind::int64: return std::forward<Visitor>(v)(jv.get_int64());
  25. case kind::uint64: return std::forward<Visitor>(v)(jv.get_uint64());
  26. case kind::double_: return std::forward<Visitor>(v)(jv.get_double());
  27. case kind::string: return std::forward<Visitor>(v)(jv.get_string());
  28. case kind::array: return std::forward<Visitor>(v)(jv.get_array());
  29. case kind::object: return std::forward<Visitor>(v)(jv.get_object());
  30. }
  31. }
  32. template<class Visitor>
  33. auto
  34. visit(
  35. Visitor&& v,
  36. value const& jv) -> decltype(
  37. std::declval<Visitor>()(nullptr))
  38. {
  39. switch (jv.kind())
  40. {
  41. default: // unreachable()?
  42. case kind::null: return std::forward<Visitor>(v)(nullptr);
  43. case kind::bool_: return std::forward<Visitor>(v)(jv.get_bool());
  44. case kind::int64: return std::forward<Visitor>(v)(jv.get_int64());
  45. case kind::uint64: return std::forward<Visitor>(v)(jv.get_uint64());
  46. case kind::double_: return std::forward<Visitor>(v)(jv.get_double());
  47. case kind::string: return std::forward<Visitor>(v)(jv.get_string());
  48. case kind::array: return std::forward<Visitor>(v)(jv.get_array());
  49. case kind::object: return std::forward<Visitor>(v)(jv.get_object());
  50. }
  51. }
  52. BOOST_JSON_NS_END
  53. #endif