Skip to content

Commit 3202b0a

Browse files
authored
Merge pull request Tencent#964 from pah/fixes/962-copy-const-strings
GenericValue::CopyFrom: add option to force copying of strings
2 parents 0033268 + 4ef1ff4 commit 3202b0a

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

include/rapidjson/document.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -615,10 +615,11 @@ class GenericValue {
615615
\tparam SourceAllocator allocator of \c rhs
616616
\param rhs Value to copy from (read-only)
617617
\param allocator Allocator for allocating copied elements and buffers. Commonly use GenericDocument::GetAllocator().
618+
\param copyConstStrings Force copying of constant strings (e.g. referencing an in-situ buffer)
618619
\see CopyFrom()
619620
*/
620621
template <typename SourceAllocator>
621-
GenericValue(const GenericValue<Encoding,SourceAllocator>& rhs, Allocator& allocator) {
622+
GenericValue(const GenericValue<Encoding,SourceAllocator>& rhs, Allocator& allocator, bool copyConstStrings = false) {
622623
switch (rhs.GetType()) {
623624
case kObjectType: {
624625
SizeType count = rhs.data_.o.size;
@@ -645,7 +646,7 @@ class GenericValue {
645646
}
646647
break;
647648
case kStringType:
648-
if (rhs.data_.f.flags == kConstStringFlag) {
649+
if (rhs.data_.f.flags == kConstStringFlag && !copyConstStrings) {
649650
data_.f.flags = rhs.data_.f.flags;
650651
data_ = *reinterpret_cast<const Data*>(&rhs.data_);
651652
}
@@ -850,12 +851,13 @@ class GenericValue {
850851
\tparam SourceAllocator Allocator type of \c rhs
851852
\param rhs Value to copy from (read-only)
852853
\param allocator Allocator to use for copying
854+
\param copyConstStrings Force copying of constant strings (e.g. referencing an in-situ buffer)
853855
*/
854856
template <typename SourceAllocator>
855-
GenericValue& CopyFrom(const GenericValue<Encoding, SourceAllocator>& rhs, Allocator& allocator) {
857+
GenericValue& CopyFrom(const GenericValue<Encoding, SourceAllocator>& rhs, Allocator& allocator, bool copyConstStrings = false) {
856858
RAPIDJSON_ASSERT(static_cast<void*>(this) != static_cast<void const*>(&rhs));
857859
this->~GenericValue();
858-
new (this) GenericValue(rhs, allocator);
860+
new (this) GenericValue(rhs, allocator, copyConstStrings);
859861
return *this;
860862
}
861863

0 commit comments

Comments
 (0)