From db4bc75cd9e350aaa000bd76dab28f134bdf9ed6 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Sat, 20 Feb 2016 22:18:23 +0800 Subject: [PATCH] Add move constructor for GenericSchemaDocument --- include/rapidjson/schema.h | 23 ++++++++++++++++++++++- test/unittest/schematest.cpp | 18 ++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index e47f89a..9b033af 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -1322,7 +1322,7 @@ public: \param remoteProvider An optional remote schema document provider for resolving remote reference. Can be null. \param allocator An optional allocator instance for allocating memory. Can be null. */ - GenericSchemaDocument(const ValueType& document, IRemoteSchemaDocumentProviderType* remoteProvider = 0, Allocator* allocator = 0) : + GenericSchemaDocument(const ValueType& document, IRemoteSchemaDocumentProviderType* remoteProvider = 0, Allocator* allocator = 0) RAPIDJSON_NOEXCEPT : remoteProvider_(remoteProvider), allocator_(allocator), ownAllocator_(), @@ -1357,6 +1357,22 @@ public: schemaRef_.ShrinkToFit(); // Deallocate all memory for ref } +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Move constructor in C++11 + GenericSchemaDocument(GenericSchemaDocument&& rhs) RAPIDJSON_NOEXCEPT : + remoteProvider_(rhs.remoteProvider_), + allocator_(rhs.allocator_), + ownAllocator_(rhs.ownAllocator_), + root_(rhs.root_), + schemaMap_(std::move(rhs.schemaMap_)), + schemaRef_(std::move(rhs.schemaRef_)) + { + rhs.remoteProvider_ = 0; + rhs.allocator_ = 0; + rhs.ownAllocator_ = 0; + } +#endif + //! Destructor ~GenericSchemaDocument() { while (!schemaMap_.Empty()) @@ -1369,6 +1385,11 @@ public: const SchemaType& GetRoot() const { return *root_; } private: + //! Prohibit copying + GenericSchemaDocument(const GenericSchemaDocument&); + //! Prohibit assignment + GenericSchemaDocument& operator=(const GenericSchemaDocument&); + struct SchemaRefEntry { SchemaRefEntry(const PointerType& s, const PointerType& t, const SchemaType** outSchema, Allocator *allocator) : source(s, allocator), target(t, allocator), schema(outSchema) {} PointerType source; diff --git a/test/unittest/schematest.cpp b/test/unittest/schematest.cpp index 623c65a..dd67920 100644 --- a/test/unittest/schematest.cpp +++ b/test/unittest/schematest.cpp @@ -1152,6 +1152,24 @@ TEST(SchemaValidatingWriter, Simple) { EXPECT_TRUE(validator.GetInvalidDocumentPointer() == SchemaDocument::PointerType("")); } +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + +static SchemaDocument ReturnSchemaDocument() { + Document sd; + sd.Parse("{ \"type\": [\"number\", \"string\"] }"); + SchemaDocument s(sd); + return s; +} + +TEST(Schema, Issue552) { + SchemaDocument s = ReturnSchemaDocument(); + VALIDATE(s, "42", true); + VALIDATE(s, "\"I'm a string\"", true); + VALIDATE(s, "{ \"an\": [ \"arbitrarily\", \"nested\" ], \"data\": \"structure\" }", true); +} + +#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS + #ifdef __clang__ RAPIDJSON_DIAG_POP #endif