stream_parser.ipp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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_STREAM_PARSER_IPP
  10. #define BOOST_JSON_IMPL_STREAM_PARSER_IPP
  11. #include <boost/json/stream_parser.hpp>
  12. #include <boost/json/basic_parser_impl.hpp>
  13. #include <boost/json/error.hpp>
  14. #include <cstring>
  15. #include <stdexcept>
  16. #include <utility>
  17. namespace boost {
  18. namespace json {
  19. stream_parser::
  20. stream_parser(
  21. storage_ptr sp,
  22. parse_options const& opt,
  23. unsigned char* buffer,
  24. std::size_t size) noexcept
  25. : p_(
  26. opt,
  27. std::move(sp),
  28. buffer,
  29. size)
  30. {
  31. reset();
  32. }
  33. stream_parser::
  34. stream_parser(
  35. storage_ptr sp,
  36. parse_options const& opt) noexcept
  37. : p_(
  38. opt,
  39. std::move(sp),
  40. nullptr,
  41. 0)
  42. {
  43. reset();
  44. }
  45. void
  46. stream_parser::
  47. reset(storage_ptr sp) noexcept
  48. {
  49. p_.reset();
  50. p_.handler().st.reset(sp);
  51. }
  52. std::size_t
  53. stream_parser::
  54. write_some(
  55. char const* data,
  56. std::size_t size,
  57. system::error_code& ec)
  58. {
  59. return p_.write_some(
  60. true, data, size, ec);
  61. }
  62. std::size_t
  63. stream_parser::
  64. write_some(
  65. char const* data,
  66. std::size_t size,
  67. std::error_code& ec)
  68. {
  69. system::error_code jec;
  70. std::size_t const result = write_some(data, size, jec);
  71. ec = jec;
  72. return result;
  73. }
  74. std::size_t
  75. stream_parser::
  76. write_some(
  77. char const* data,
  78. std::size_t size)
  79. {
  80. system::error_code ec;
  81. auto const n = write_some(
  82. data, size, ec);
  83. if(ec)
  84. detail::throw_system_error( ec );
  85. return n;
  86. }
  87. std::size_t
  88. stream_parser::
  89. write(
  90. char const* data,
  91. std::size_t size,
  92. system::error_code& ec)
  93. {
  94. auto const n = write_some(
  95. data, size, ec);
  96. if(! ec && n < size)
  97. {
  98. BOOST_JSON_FAIL(ec, error::extra_data);
  99. p_.fail(ec);
  100. }
  101. return n;
  102. }
  103. std::size_t
  104. stream_parser::
  105. write(
  106. char const* data,
  107. std::size_t size,
  108. std::error_code& ec)
  109. {
  110. system::error_code jec;
  111. std::size_t const result = write(data, size, jec);
  112. ec = jec;
  113. return result;
  114. }
  115. std::size_t
  116. stream_parser::
  117. write(
  118. char const* data,
  119. std::size_t size)
  120. {
  121. system::error_code ec;
  122. auto const n = write(
  123. data, size, ec);
  124. if(ec)
  125. detail::throw_system_error( ec );
  126. return n;
  127. }
  128. void
  129. stream_parser::
  130. finish(system::error_code& ec)
  131. {
  132. p_.write_some(false, nullptr, 0, ec);
  133. }
  134. void
  135. stream_parser::
  136. finish()
  137. {
  138. system::error_code ec;
  139. finish(ec);
  140. if(ec)
  141. detail::throw_system_error( ec );
  142. }
  143. void
  144. stream_parser::
  145. finish(std::error_code& ec)
  146. {
  147. system::error_code jec;
  148. finish(jec);
  149. ec = jec;
  150. }
  151. value
  152. stream_parser::
  153. release()
  154. {
  155. if(! p_.done())
  156. {
  157. // prevent undefined behavior
  158. finish();
  159. }
  160. return p_.handler().st.release();
  161. }
  162. } // namespace json
  163. } // namespace boost
  164. #endif