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

GenericValue: add copy constructor and CopyFrom #2

Merged
merged 3 commits into from
Apr 2, 2014
Merged

Conversation

pah
Copy link
Owner

@pah pah commented Mar 31, 2014

To allow deep copying from an existing GenericValue, an explicit "copy constructor" (with required Allocator param) and a CopyFrom assignment function are added.

  Document d; Document::AllocatorType& a = d.GetAllocator();
  Value v1("foo");
  // Value v2(v1); // not allowed

  Value v2(v1,a);                             // make a copy
  RAPIDJSON_ASSERT(v1.IsString());            // v1 untouched
  d.SetArray().PushBack(v1,a).PushBack(v2,a);
  RAPIDJSON_ASSERT(v1.Empty() && v2.Empty());

  v2.CopyFrom(d,a);                           // copy whole document
  RAPIDJSON_ASSERT(d.IsArray() && d.Size());  // d untouched
  v1.SetObject().AddMember( "array", v2, a );
  d.PushBack(v1,a);

The strings in the source value are copied, if and only if they have been allocated as a copy during their construction (determined by kCopyFlag). This is needed to avoid double free()s or problems with differing lifetimes of the allocated string storage.

Additionally, the Handler implementation in GenericDocument is made private again, restricting access to GenericReader and GenericValue.

pah added 3 commits April 2, 2014 16:12
Instead of always just shallowly referencing the potentially allocated
strings when calling the Handler::String function, request a copy in
case the string has been allocated from an Allocator before.

This is necessary to avoid double free()s of the string memory,
especially when using the Handler to create a deep copy of a Value.
To allow deep copying from an existing GenericValue, an
explicit "copy constructor" (with required Allocator param)
and an "CopyFrom" assignment function are added.

  Document d; Document::AllocatorType& a = d.GetAllocator();
  Value v1("foo");
  // Value v2(v1); // not allowed

  Value v2(v1,a);                             // make a copy
  RAPIDJSON_ASSERT(v1.IsString());            // v1 untouched
  d.SetArray().PushBack(v1,a).PushBack(v2,a);
  RAPIDJSON_ASSERT(v1.Empty() && v2.Empty());

  v2.CopyFrom(d,a);                           // copy whole document
  RAPIDJSON_ASSERT(d.IsArray() && d.Size());  // d untouched
  v1.SetObject().AddMember( "array", v2, a );
  d.PushBack(v1,a);

Additionally, the Handler implementation in GenericDocument is made
private again, restricting access to GenericReader and GenericValue.
This commit adds some simple tests for the deep-copying
of values, either based on the explicit constructor, or
the CopyFrom function.

It uses the CrtAllocator to test for possible double-free
errors due to insufficient copying.
@pah pah added upstream and removed upstream labels Apr 2, 2014
@pah pah merged commit 2760462 into master Apr 2, 2014
@pah
Copy link
Owner Author

pah commented Apr 2, 2014

Unfortunately, GitHub doesn't allow to "re-open" a pull-request and accidentally this PR has been marked to be against master, instead of svn/trunk.

@pah
Copy link
Owner Author

pah commented Apr 7, 2014

See upstream issue 12 for a related discussion.

@pah
Copy link
Owner Author

pah commented Apr 13, 2014

Updated value-copy-from branch with a fix for #5, see svn/trunk...value-copy-from.

@pah pah added the upstream label Apr 15, 2014
@pah
Copy link
Owner Author

pah commented Jun 25, 2014

See upstream pull-request Tencent/rapidjson#20.

@pah
Copy link
Owner Author

pah commented Jun 26, 2014

Merged upstream.

pah pushed a commit that referenced this pull request Jul 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant