x3_unpack.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // MessagePack for C++ deserializing routine
  3. //
  4. // Copyright (C) 2017 KONDO Takatoshi
  5. //
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef MSGPACK_V2_X3_UNPACK_HPP
  11. #define MSGPACK_V2_X3_UNPACK_HPP
  12. #if defined(MSGPACK_USE_X3_PARSE)
  13. #include <boost/version.hpp>
  14. #if BOOST_VERSION >= 106100
  15. #include "msgpack/versioning.hpp"
  16. #include "msgpack/create_object_visitor.hpp"
  17. #include "msgpack/x3_unpack_decl.hpp"
  18. #include "msgpack/x3_parse.hpp"
  19. namespace msgpack {
  20. /// @cond
  21. MSGPACK_API_VERSION_NAMESPACE(v2) {
  22. /// @endcond
  23. namespace detail {
  24. template <typename Iterator>
  25. inline void
  26. unpack_imp(Iterator&& begin, Iterator&& end,
  27. msgpack::zone& result_zone, msgpack::object& result, bool& referenced,
  28. unpack_reference_func f, void* user_data,
  29. unpack_limit const& limit)
  30. {
  31. create_object_visitor v(f, user_data, limit);
  32. v.set_zone(result_zone);
  33. referenced = false;
  34. v.set_referenced(referenced);
  35. if (!parse(std::forward<Iterator>(begin), std::forward<Iterator>(end), v)) {
  36. throw msgpack::parse_error("parse error");
  37. }
  38. referenced = v.referenced();
  39. result = v.data();
  40. }
  41. } // namespace detail
  42. template <typename Iterator>
  43. inline msgpack::object_handle unpack(
  44. Iterator&& begin, Iterator&& end,
  45. bool& referenced,
  46. unpack_reference_func f, void* user_data,
  47. unpack_limit const& limit)
  48. {
  49. msgpack::object obj;
  50. msgpack::unique_ptr<msgpack::zone> z(new msgpack::zone);
  51. referenced = false;
  52. detail::unpack_imp(
  53. std::forward<Iterator>(begin), std::forward<Iterator>(end), *z, obj, referenced, f, user_data, limit);
  54. return msgpack::object_handle(obj, msgpack::move(z));
  55. }
  56. template <typename Iterator>
  57. inline msgpack::object_handle unpack(
  58. Iterator&& begin, Iterator&& end,
  59. unpack_reference_func f, void* user_data,
  60. unpack_limit const& limit)
  61. {
  62. bool referenced;
  63. return unpack(std::forward<Iterator>(begin), std::forward<Iterator>(end), referenced, f, user_data, limit);
  64. }
  65. template <typename Iterator>
  66. inline msgpack::object unpack(
  67. msgpack::zone& z,
  68. Iterator&& begin, Iterator&& end,
  69. bool& referenced,
  70. unpack_reference_func f, void* user_data,
  71. unpack_limit const& limit)
  72. {
  73. msgpack::object obj;
  74. referenced = false;
  75. detail::unpack_imp(
  76. std::forward<Iterator>(begin), std::forward<Iterator>(end), z, obj, referenced, f, user_data, limit);
  77. return obj;
  78. }
  79. template <typename Iterator>
  80. inline msgpack::object unpack(
  81. msgpack::zone& z,
  82. Iterator&& begin, Iterator&& end,
  83. unpack_reference_func f, void* user_data,
  84. unpack_limit const& limit)
  85. {
  86. bool referenced;
  87. return unpack(
  88. z, std::forward<Iterator>(begin), std::forward<Iterator>(end), referenced, f, user_data, limit);
  89. }
  90. /// @cond
  91. } // MSGPACK_API_VERSION_NAMESPACE(v2)
  92. /// @endcond
  93. } // namespace msgpack
  94. #else // BOOST_VERSION >= 106100
  95. #error Boost 1.61.0 or later is required to use x3 parse
  96. #endif // BOOST_VERSION >= 106100
  97. #endif // defined(MSGPACK_USE_X3_PARSE)
  98. #endif // MSGPACK_V2_X3_UNPACK_HPP