Merge pull request #48 from miloyip/Swap
Safer implementation of Swap()
This commit is contained in:
commit
7bd3b17fbd
@ -363,11 +363,16 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//! Exchange the contents of this value with those of other.
|
||||||
|
/*!
|
||||||
|
\param other Another value.
|
||||||
|
\note Constant complexity.
|
||||||
|
*/
|
||||||
GenericValue& Swap(GenericValue& other) {
|
GenericValue& Swap(GenericValue& other) {
|
||||||
char temp[sizeof(GenericValue)];
|
GenericValue temp;
|
||||||
memcpy(&temp[0], this, sizeof(GenericValue));
|
temp.RawAssign(*this);
|
||||||
memcpy(this, &other, sizeof(GenericValue));
|
RawAssign(other);
|
||||||
memcpy(&other, temp, sizeof(GenericValue));
|
other.RawAssign(temp);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,6 +47,28 @@ TEST(Document, Parse) {
|
|||||||
EXPECT_EQ(i + 1, a[i].GetUint());
|
EXPECT_EQ(i + 1, a[i].GetUint());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(Document, Swap) {
|
||||||
|
Document d1;
|
||||||
|
Document::AllocatorType& a = d1.GetAllocator();
|
||||||
|
|
||||||
|
d1.SetArray().PushBack(1, a).PushBack(2, a);
|
||||||
|
|
||||||
|
Value o;
|
||||||
|
o.SetObject().AddMember("a", 1, a);
|
||||||
|
|
||||||
|
// Swap between Document and Value
|
||||||
|
d1.Swap(o);
|
||||||
|
EXPECT_TRUE(d1.IsObject());
|
||||||
|
EXPECT_TRUE(o.IsArray());
|
||||||
|
|
||||||
|
// Swap between Document and Document
|
||||||
|
Document d2;
|
||||||
|
d2.SetArray().PushBack(3, a);
|
||||||
|
d1.Swap(d2);
|
||||||
|
EXPECT_TRUE(d1.IsArray());
|
||||||
|
EXPECT_TRUE(d2.IsObject());
|
||||||
|
}
|
||||||
|
|
||||||
// This should be slow due to assignment in inner-loop.
|
// This should be slow due to assignment in inner-loop.
|
||||||
struct OutputStringStream : public std::ostringstream {
|
struct OutputStringStream : public std::ostringstream {
|
||||||
typedef char Ch;
|
typedef char Ch;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user