GenericValue: add copy constructor and CopyFrom
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 is contained in:
parent
a0e5e68fdb
commit
8bde3be116
@ -69,6 +69,16 @@ public:
|
||||
flags_ = defaultFlags[type];
|
||||
}
|
||||
|
||||
//! Explicit copy constructor (with allocator)
|
||||
/*! Creates a copy of a Value by using the given Allocator
|
||||
\tparam SourceAllocator allocator of \c rhs
|
||||
\param rhs Value to copy from (read-only)
|
||||
\param allocator Allocator to use for copying
|
||||
\see CopyFrom()
|
||||
*/
|
||||
template< typename SourceAllocator >
|
||||
GenericValue(const GenericValue<Encoding,SourceAllocator>& rhs, Allocator & allocator);
|
||||
|
||||
//! Constructor for boolean value.
|
||||
explicit GenericValue(bool b) : flags_(b ? kTrueFlag : kFalseFlag) {}
|
||||
|
||||
@ -183,6 +193,21 @@ public:
|
||||
new (this) GenericValue(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
//! Deep-copy assignment from Value
|
||||
/*! Assigns a \b copy of the Value to the current Value object
|
||||
\tparam SourceAllocator Allocator type of \c rhs
|
||||
\param rhs Value to copy from (read-only)
|
||||
\param allocator Allocator to use for copying
|
||||
*/
|
||||
template <typename SourceAllocator>
|
||||
GenericValue& CopyFrom(const GenericValue<Encoding,SourceAllocator>& rhs, Allocator& allocator) {
|
||||
RAPIDJSON_ASSERT((void*)this != (void*)&rhs);
|
||||
this->~GenericValue();
|
||||
new (this) GenericValue(rhs,allocator);
|
||||
return *this;
|
||||
}
|
||||
|
||||
//@}
|
||||
|
||||
//!@name Type
|
||||
@ -840,8 +865,10 @@ public:
|
||||
//! Get the capacity of stack in bytes.
|
||||
size_t GetStackCapacity() const { return stack_.GetCapacity(); }
|
||||
|
||||
//private:
|
||||
//friend class GenericReader<Encoding>; // for Reader to call the following private handler functions
|
||||
private:
|
||||
// callers of the following private Handler functions
|
||||
template <typename,typename,typename> friend class GenericReader; // for parsing
|
||||
friend class GenericValue<Encoding,Allocator>; // for deep copying
|
||||
|
||||
// Implementation of Handler
|
||||
void Null() { new (stack_.template Push<ValueType>()) ValueType(); }
|
||||
@ -893,6 +920,17 @@ private:
|
||||
|
||||
typedef GenericDocument<UTF8<> > Document;
|
||||
|
||||
// defined here due to the dependency on GenericDocument
|
||||
template <typename Encoding, typename Allocator>
|
||||
template <typename SourceAllocator>
|
||||
inline
|
||||
GenericValue<Encoding,Allocator>::GenericValue(const GenericValue<Encoding,SourceAllocator>& rhs, Allocator& allocator)
|
||||
{
|
||||
GenericDocument<Encoding,Allocator> d(&allocator);
|
||||
rhs.Accept(d);
|
||||
RawAssign(*d.stack_.template Pop<GenericValue>(1));
|
||||
}
|
||||
|
||||
} // namespace rapidjson
|
||||
|
||||
#ifdef _MSC_VER
|
||||
|
Loading…
x
Reference in New Issue
Block a user