5#if (__cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1700)) && !defined(__clang__)
11#include <condition_variable>
18template<
unsigned parseFlags = kParseDefaultFlags>
19class AsyncDocumentParser {
31 parseThread_ = std::thread(&AsyncDocumentParser::Parse,
this);
34 ~AsyncDocumentParser() {
35 if (!parseThread_.joinable())
39 std::unique_lock<std::mutex>
lock(mutex_);
42 while (!stream_.Empty() && !completed_)
46 static const char terminator[] =
"";
47 stream_.src_ = terminator;
48 stream_.end_ = terminator + 1;
49 notEmpty_.notify_one();
55 void ParsePart(
const char* buffer,
size_t length) {
56 std::unique_lock<std::mutex>
lock(mutex_);
59 while (!stream_.Empty() && !completed_)
67 stream_.src_ = buffer;
68 stream_.end_ = buffer + length;
69 notEmpty_.notify_one();
74 d_.ParseStream<parseFlags>(stream_);
77 std::unique_lock<std::mutex>
lock(mutex_);
82 struct AsyncStringStream {
85 AsyncStringStream(AsyncDocumentParser& parser) : parser_(parser), src_(), end_(), count_() {}
88 std::unique_lock<std::mutex>
lock(parser_.mutex_);
92 parser_.notEmpty_.wait(lock);
98 std::unique_lock<std::mutex>
lock(parser_.mutex_);
102 parser_.notEmpty_.wait(lock);
109 parser_.finish_.notify_one();
114 size_t Tell()
const {
return count_; }
117 char* PutBegin() {
return 0; }
120 size_t PutEnd(
char*) {
return 0; }
122 bool Empty()
const {
return src_ == end_; }
124 AsyncDocumentParser& parser_;
130 AsyncStringStream stream_;
132 std::thread parseThread_;
134 std::condition_variable notEmpty_;
135 std::condition_variable finish_;
143 AsyncDocumentParser<> parser(d);
145 const char json1[] =
" { \"hello\" : \"world\", \"t\" : tr";
147 const char json2[] =
"ue, \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.14";
148 const char json3[] =
"16, \"a\":[1, 2, 3, 4] } ";
150 parser.ParsePart(json1,
sizeof(json1) - 1);
151 parser.ParsePart(json2,
sizeof(json2) - 1);
152 parser.ParsePart(json3,
sizeof(json3) - 1);
155 if (
d.HasParseError()) {
156 std::cout <<
"Error at offset " <<
d.GetErrorOffset() <<
": " <<
GetParseError_En(
d.GetParseError()) << std::endl;
164 std::cout << std::endl;
173 std::cout <<
"This example requires C++11 compiler" << std::endl;
mie::Vuint Put(const uint64_t *x, size_t n)
RAPIDJSON_NAMESPACE_BEGIN const RAPIDJSON_ERROR_CHARTYPE * GetParseError_En(ParseErrorCode parseErrorCode)
Maps error code of parsing into error message.