Skip to content

Commit a66cf79

Browse files
committed
Allow to (std::)Swap two pointers.
1 parent 66eb606 commit a66cf79

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

include/rapidjson/pointer.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,36 @@ class GenericPointer {
200200
return *this;
201201
}
202202

203+
//! Swap the content of this pointer with an other.
204+
/*!
205+
\param other The pointer to swap with.
206+
\note Constant complexity.
207+
*/
208+
GenericPointer& Swap(GenericPointer& other) RAPIDJSON_NOEXCEPT {
209+
internal::Swap(allocator_, other.allocator_);
210+
internal::Swap(ownAllocator_, other.ownAllocator_);
211+
internal::Swap(nameBuffer_, other.nameBuffer_);
212+
internal::Swap(tokens_, other.tokens_);
213+
internal::Swap(tokenCount_, other.tokenCount_);
214+
internal::Swap(parseErrorOffset_, other.parseErrorOffset_);
215+
internal::Swap(parseErrorCode_, other.parseErrorCode_);
216+
return *this;
217+
}
218+
219+
//! free-standing swap function helper
220+
/*!
221+
Helper function to enable support for common swap implementation pattern based on \c std::swap:
222+
\code
223+
void swap(MyClass& a, MyClass& b) {
224+
using std::swap;
225+
swap(a.pointer, b.pointer);
226+
// ...
227+
}
228+
\endcode
229+
\see Swap()
230+
*/
231+
friend inline void swap(GenericPointer& a, GenericPointer& b) RAPIDJSON_NOEXCEPT { a.Swap(b); }
232+
203233
//@}
204234

205235
//!@name Append token

test/unittest/pointertest.cpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,36 @@ TEST(Pointer, Assignment) {
529529
}
530530
}
531531

532+
TEST(Pointer, Swap) {
533+
Pointer p("/foo/0");
534+
Pointer q(&p.GetAllocator());
535+
536+
q.Swap(p);
537+
EXPECT_EQ(&q.GetAllocator(), &p.GetAllocator());
538+
EXPECT_TRUE(p.IsValid());
539+
EXPECT_TRUE(q.IsValid());
540+
EXPECT_EQ(0u, p.GetTokenCount());
541+
EXPECT_EQ(2u, q.GetTokenCount());
542+
EXPECT_EQ(3u, q.GetTokens()[0].length);
543+
EXPECT_STREQ("foo", q.GetTokens()[0].name);
544+
EXPECT_EQ(1u, q.GetTokens()[1].length);
545+
EXPECT_STREQ("0", q.GetTokens()[1].name);
546+
EXPECT_EQ(0u, q.GetTokens()[1].index);
547+
548+
// std::swap compatibility
549+
std::swap(p, q);
550+
EXPECT_EQ(&p.GetAllocator(), &q.GetAllocator());
551+
EXPECT_TRUE(q.IsValid());
552+
EXPECT_TRUE(p.IsValid());
553+
EXPECT_EQ(0u, q.GetTokenCount());
554+
EXPECT_EQ(2u, p.GetTokenCount());
555+
EXPECT_EQ(3u, p.GetTokens()[0].length);
556+
EXPECT_STREQ("foo", p.GetTokens()[0].name);
557+
EXPECT_EQ(1u, p.GetTokens()[1].length);
558+
EXPECT_STREQ("0", p.GetTokens()[1].name);
559+
EXPECT_EQ(0u, p.GetTokens()[1].index);
560+
}
561+
532562
TEST(Pointer, Append) {
533563
{
534564
Pointer p;
@@ -867,7 +897,7 @@ TEST(Pointer, Set_NoAllocator) {
867897
#endif
868898
}
869899

870-
TEST(Pointer, Swap) {
900+
TEST(Pointer, Swap_Value) {
871901
Document d;
872902
d.Parse(kJson);
873903
Document::AllocatorType& a = d.GetAllocator();
@@ -876,7 +906,7 @@ TEST(Pointer, Swap) {
876906
EXPECT_STREQ("bar", d["foo"][1].GetString());
877907
}
878908

879-
TEST(Pointer, Swap_NoAllocator) {
909+
TEST(Pointer, Swap_Value_NoAllocator) {
880910
Document d;
881911
d.Parse(kJson);
882912
Pointer("/foo/0").Swap(d, *Pointer("/foo/1").Get(d));

0 commit comments

Comments
 (0)