From 6288d95d1e65d6a57c480dea9edc342b4b721507 Mon Sep 17 00:00:00 2001 From: StilesCrisis Date: Sat, 4 Feb 2017 01:07:00 -0800 Subject: [PATCH] SimplePullReader C++98 support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit std::to_string can’t be used because it requires C++11. --- example/simplepullreader/simplepullreader.cpp | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/example/simplepullreader/simplepullreader.cpp b/example/simplepullreader/simplepullreader.cpp index af8d5a7..0b11b40 100644 --- a/example/simplepullreader/simplepullreader.cpp +++ b/example/simplepullreader/simplepullreader.cpp @@ -1,27 +1,33 @@ #include "rapidjson/reader.h" #include +#include using namespace rapidjson; using namespace std; +// If you can require C++11, you could use std::to_string here +template std::string stringify(T x) { + return (std::stringstream() << x).str(); +} + struct MyHandler { const char* type; std::string data; bool Null() { type = "Null"; data.clear(); return true; } bool Bool(bool b) { type = "Bool:"; data = b? "true": "false"; return true; } - bool Int(int i) { type = "Int:"; data = std::to_string(i); return true; } - bool Uint(unsigned u) { type = "Uint:"; data = std::to_string(u); return true; } - bool Int64(int64_t i) { type = "Int64:"; data = std::to_string(i); return true; } - bool Uint64(uint64_t u) { type = "Uint64:"; data = std::to_string(u); return true; } - bool Double(double d) { type = "Double:"; data = std::to_string(d); return true; } + bool Int(int i) { type = "Int:"; data = stringify(i); return true; } + bool Uint(unsigned u) { type = "Uint:"; data = stringify(u); return true; } + bool Int64(int64_t i) { type = "Int64:"; data = stringify(i); return true; } + bool Uint64(uint64_t u) { type = "Uint64:"; data = stringify(u); return true; } + bool Double(double d) { type = "Double:"; data = stringify(d); return true; } bool RawNumber(const char* str, SizeType length, bool) { type = "Number:"; data = std::string(str, length); return true; } bool String(const char* str, SizeType length, bool) { type = "String:"; data = std::string(str, length); return true; } bool StartObject() { type = "StartObject"; data.clear(); return true; } bool Key(const char* str, SizeType length, bool) { type = "Key:"; data = std::string(str, length); return true; } - bool EndObject(SizeType memberCount) { type = "EndObject:"; data = std::to_string(memberCount); return true; } + bool EndObject(SizeType memberCount) { type = "EndObject:"; data = stringify(memberCount); return true; } bool StartArray() { type = "StartArray"; data.clear(); return true; } - bool EndArray(SizeType elementCount) { type = "EndArray:"; data = std::to_string(elementCount); return true; } + bool EndArray(SizeType elementCount) { type = "EndArray:"; data = stringify(elementCount); return true; } }; int main() {