From 46e1696316e2d33c0198af2138ab481d985439f0 Mon Sep 17 00:00:00 2001 From: "Philipp A. Hartmann" Date: Mon, 13 Jul 2015 09:35:15 +0200 Subject: [PATCH] add free inline `swap` functions --- include/rapidjson/document.h | 4 ++++ test/unittest/documenttest.cpp | 12 ++++++++++++ test/unittest/valuetest.cpp | 6 ++++++ 3 files changed, 22 insertions(+) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 3496c2b..007fa94 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -660,6 +660,8 @@ public: return *this; } + friend inline void swap(GenericValue& a, GenericValue& b) RAPIDJSON_NOEXCEPT { a.Swap(b); } + //! Prepare Value for move semantics /*! \return *this */ GenericValue& Move() RAPIDJSON_NOEXCEPT { return *this; } @@ -1818,6 +1820,8 @@ public: return *this; } + friend inline void swap(GenericDocument& a, GenericDocument& b) RAPIDJSON_NOEXCEPT { a.Swap(b); } + //!@name Parse from stream //!@{ diff --git a/test/unittest/documenttest.cpp b/test/unittest/documenttest.cpp index 3db7cd8..810a99c 100644 --- a/test/unittest/documenttest.cpp +++ b/test/unittest/documenttest.cpp @@ -19,6 +19,7 @@ #include "rapidjson/encodedstream.h" #include "rapidjson/stringbuffer.h" #include +#include using namespace rapidjson; @@ -223,6 +224,17 @@ TEST(Document, Swap) { Document().Swap(d2); EXPECT_TRUE(d2.IsNull()); EXPECT_NE(&d2.GetAllocator(), &a); + + // testing std::swap compatibility + d1.SetBool(true); + using std::swap; + swap(d1, d2); + EXPECT_TRUE(d1.IsNull()); + EXPECT_TRUE(d2.IsTrue()); + + swap(o, d2); + EXPECT_TRUE(o.IsTrue()); + EXPECT_TRUE(d2.IsArray()); } diff --git a/test/unittest/valuetest.cpp b/test/unittest/valuetest.cpp index f14669a..900783c 100644 --- a/test/unittest/valuetest.cpp +++ b/test/unittest/valuetest.cpp @@ -272,6 +272,12 @@ TEST(Value, Swap) { EXPECT_TRUE(v1.IsObject()); EXPECT_TRUE(v2.IsInt()); EXPECT_EQ(1234, v2.GetInt()); + + // testing std::swap compatibility + using std::swap; + swap(v1, v2); + EXPECT_TRUE(v1.IsInt()); + EXPECT_TRUE(v2.IsObject()); } TEST(Value, Null) {