Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add move constructor for GenericSchemaDocument #554

Merged
merged 2 commits into from
Feb 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion include/rapidjson/schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -1322,7 +1322,7 @@ class GenericSchemaDocument {
\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_(),
Expand Down Expand Up @@ -1357,6 +1357,22 @@ class GenericSchemaDocument {
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())
Expand All @@ -1369,6 +1385,11 @@ class GenericSchemaDocument {
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;
Expand Down
18 changes: 18 additions & 0 deletions test/unittest/schematest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, "\"Life, the universe, and everything\"", true);
INVALIDATE(s, "[\"Life\", \"the universe\", \"and everything\"]", "", "type", "");
}

#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS

#ifdef __clang__
RAPIDJSON_DIAG_POP
#endif