Commit 185a7cc2 authored by Milo Yip's avatar Milo Yip Committed by GitHub

Merge pull request #740 from CreoValis/writer-move-ctor

Move constructor support for Writer
parents 52682115 1a64cd09
...@@ -22,6 +22,11 @@ RAPIDJSON_DIAG_PUSH ...@@ -22,6 +22,11 @@ RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(effc++) RAPIDJSON_DIAG_OFF(effc++)
#endif #endif
#if defined(__clang__)
RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(c++98-compat)
#endif
RAPIDJSON_NAMESPACE_BEGIN RAPIDJSON_NAMESPACE_BEGIN
//! Combination of PrettyWriter format flags. //! Combination of PrettyWriter format flags.
...@@ -57,6 +62,11 @@ public: ...@@ -57,6 +62,11 @@ public:
explicit PrettyWriter(StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) : explicit PrettyWriter(StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) :
Base(allocator, levelDepth), indentChar_(' '), indentCharCount_(4) {} Base(allocator, levelDepth), indentChar_(' '), indentCharCount_(4) {}
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
PrettyWriter(PrettyWriter&& rhs) :
Base(std::forward<PrettyWriter>(rhs)), indentChar_(rhs.indentChar_), indentCharCount_(rhs.indentCharCount_), formatOptions_(rhs.formatOptions_) {}
#endif
//! Set custom indentation. //! Set custom indentation.
/*! \param indentChar Character for indentation. Must be whitespace character (' ', '\\t', '\\n', '\\r'). /*! \param indentChar Character for indentation. Must be whitespace character (' ', '\\t', '\\n', '\\r').
\param indentCharCount Number of indent characters for each indentation level. \param indentCharCount Number of indent characters for each indentation level.
...@@ -254,6 +264,10 @@ private: ...@@ -254,6 +264,10 @@ private:
RAPIDJSON_NAMESPACE_END RAPIDJSON_NAMESPACE_END
#if defined(__clang__)
RAPIDJSON_DIAG_POP
#endif
#ifdef __GNUC__ #ifdef __GNUC__
RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_POP
#endif #endif
......
...@@ -42,6 +42,7 @@ RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant ...@@ -42,6 +42,7 @@ RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant
RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(padded) RAPIDJSON_DIAG_OFF(padded)
RAPIDJSON_DIAG_OFF(unreachable-code) RAPIDJSON_DIAG_OFF(unreachable-code)
RAPIDJSON_DIAG_OFF(c++98-compat)
#endif #endif
RAPIDJSON_NAMESPACE_BEGIN RAPIDJSON_NAMESPACE_BEGIN
...@@ -103,6 +104,13 @@ public: ...@@ -103,6 +104,13 @@ public:
Writer(StackAllocator* allocator = 0, size_t levelDepth = kDefaultLevelDepth) : Writer(StackAllocator* allocator = 0, size_t levelDepth = kDefaultLevelDepth) :
os_(0), level_stack_(allocator, levelDepth * sizeof(Level)), maxDecimalPlaces_(kDefaultMaxDecimalPlaces), hasRoot_(false) {} os_(0), level_stack_(allocator, levelDepth * sizeof(Level)), maxDecimalPlaces_(kDefaultMaxDecimalPlaces), hasRoot_(false) {}
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
Writer(Writer&& rhs) :
os_(rhs.os_), level_stack_(std::move(rhs.level_stack_)), maxDecimalPlaces_(rhs.maxDecimalPlaces_), hasRoot_(rhs.hasRoot_) {
rhs.os_=nullptr;
}
#endif
//! Reset the writer with a new stream. //! Reset the writer with a new stream.
/*! /*!
This function reset the writer with a new stream and default settings, This function reset the writer with a new stream and default settings,
......
...@@ -18,6 +18,11 @@ ...@@ -18,6 +18,11 @@
#include "rapidjson/stringbuffer.h" #include "rapidjson/stringbuffer.h"
#include "rapidjson/filewritestream.h" #include "rapidjson/filewritestream.h"
#ifdef __clang__
RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(c++98-compat)
#endif
using namespace rapidjson; using namespace rapidjson;
static const char kJson[] = "{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3,-1],\"u64\":1234567890123456789,\"i64\":-1234567890123456789}"; static const char kJson[] = "{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3,-1],\"u64\":1234567890123456789,\"i64\":-1234567890123456789}";
...@@ -201,3 +206,29 @@ TEST(PrettyWriter, RawValue) { ...@@ -201,3 +206,29 @@ TEST(PrettyWriter, RawValue) {
"}", "}",
buffer.GetString()); buffer.GetString());
} }
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
TEST(PrettyWriter, MoveCtor) {
StringBuffer buffer;
auto writerGen=[](StringBuffer &target) -> PrettyWriter<StringBuffer> {
PrettyWriter<StringBuffer> writer(target);
writer.StartObject();
writer.Key("a");
writer.Int(1);
return std::move(writer);
};
PrettyWriter<StringBuffer> writer(writerGen(buffer));
writer.EndObject();
EXPECT_TRUE(writer.IsComplete());
EXPECT_STREQ(
"{\n"
" \"a\": 1\n"
"}",
buffer.GetString());
}
#endif
#ifdef __clang__
RAPIDJSON_DIAG_POP
#endif
...@@ -20,6 +20,11 @@ ...@@ -20,6 +20,11 @@
#include "rapidjson/stringbuffer.h" #include "rapidjson/stringbuffer.h"
#include "rapidjson/memorybuffer.h" #include "rapidjson/memorybuffer.h"
#ifdef __clang__
RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(c++98-compat)
#endif
using namespace rapidjson; using namespace rapidjson;
TEST(Writer, Compact) { TEST(Writer, Compact) {
...@@ -495,3 +500,25 @@ TEST(Writer, RawValue) { ...@@ -495,3 +500,25 @@ TEST(Writer, RawValue) {
EXPECT_TRUE(writer.IsComplete()); EXPECT_TRUE(writer.IsComplete());
EXPECT_STREQ("{\"a\":1,\"raw\":[\"Hello\\nWorld\", 123.456]}", buffer.GetString()); EXPECT_STREQ("{\"a\":1,\"raw\":[\"Hello\\nWorld\", 123.456]}", buffer.GetString());
} }
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
TEST(Writer, MoveCtor) {
StringBuffer buffer;
auto writerGen=[](StringBuffer &target) -> Writer<StringBuffer> {
Writer<StringBuffer> writer(target);
writer.StartObject();
writer.Key("a");
writer.Int(1);
return std::move(writer);
};
Writer<StringBuffer> writer(writerGen(buffer));
writer.EndObject();
EXPECT_TRUE(writer.IsComplete());
EXPECT_STREQ("{\"a\":1}", buffer.GetString());
}
#endif
#ifdef __clang__
RAPIDJSON_DIAG_POP
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment