Skip to content

Commit b56eb28

Browse files
authored
Merge branch 'master' into pointer_less_than
2 parents eb6ee17 + 8549e3d commit b56eb28

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-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: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "rapidjson/ostreamwrapper.h"
1919
#include <sstream>
2020
#include <map>
21+
#include <algorithm>
2122

2223
using namespace rapidjson;
2324

@@ -531,6 +532,36 @@ TEST(Pointer, Assignment) {
531532
}
532533
}
533534

535+
TEST(Pointer, Swap) {
536+
Pointer p("/foo/0");
537+
Pointer q(&p.GetAllocator());
538+
539+
q.Swap(p);
540+
EXPECT_EQ(&q.GetAllocator(), &p.GetAllocator());
541+
EXPECT_TRUE(p.IsValid());
542+
EXPECT_TRUE(q.IsValid());
543+
EXPECT_EQ(0u, p.GetTokenCount());
544+
EXPECT_EQ(2u, q.GetTokenCount());
545+
EXPECT_EQ(3u, q.GetTokens()[0].length);
546+
EXPECT_STREQ("foo", q.GetTokens()[0].name);
547+
EXPECT_EQ(1u, q.GetTokens()[1].length);
548+
EXPECT_STREQ("0", q.GetTokens()[1].name);
549+
EXPECT_EQ(0u, q.GetTokens()[1].index);
550+
551+
// std::swap compatibility
552+
std::swap(p, q);
553+
EXPECT_EQ(&p.GetAllocator(), &q.GetAllocator());
554+
EXPECT_TRUE(q.IsValid());
555+
EXPECT_TRUE(p.IsValid());
556+
EXPECT_EQ(0u, q.GetTokenCount());
557+
EXPECT_EQ(2u, p.GetTokenCount());
558+
EXPECT_EQ(3u, p.GetTokens()[0].length);
559+
EXPECT_STREQ("foo", p.GetTokens()[0].name);
560+
EXPECT_EQ(1u, p.GetTokens()[1].length);
561+
EXPECT_STREQ("0", p.GetTokens()[1].name);
562+
EXPECT_EQ(0u, p.GetTokens()[1].index);
563+
}
564+
534565
TEST(Pointer, Append) {
535566
{
536567
Pointer p;
@@ -869,7 +900,7 @@ TEST(Pointer, Set_NoAllocator) {
869900
#endif
870901
}
871902

872-
TEST(Pointer, Swap) {
903+
TEST(Pointer, Swap_Value) {
873904
Document d;
874905
d.Parse(kJson);
875906
Document::AllocatorType& a = d.GetAllocator();
@@ -878,7 +909,7 @@ TEST(Pointer, Swap) {
878909
EXPECT_STREQ("bar", d["foo"][1].GetString());
879910
}
880911

881-
TEST(Pointer, Swap_NoAllocator) {
912+
TEST(Pointer, Swap_Value_NoAllocator) {
882913
Document d;
883914
d.Parse(kJson);
884915
Pointer("/foo/0").Swap(d, *Pointer("/foo/1").Get(d));

0 commit comments

Comments
 (0)