From 55d00d047c2c029594eca35e4c43b114f1cdf759 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sat, 30 Apr 2011 00:29:14 +0900 Subject: [PATCH] Added swap for UriStruct --- src/uri.cc | 22 ++++++++++++++++++++++ src/uri.h | 3 +++ test/UriTest.cc | 15 +++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/src/uri.cc b/src/uri.cc index 475b7663b..77d6028fe 100644 --- a/src/uri.cc +++ b/src/uri.cc @@ -77,6 +77,28 @@ UriStruct& UriStruct::operator=(const UriStruct& c) return *this; } +void UriStruct::swap(UriStruct& other) +{ + using std::swap; + if(this != &other) { + swap(protocol, other.protocol); + swap(host, other.host); + swap(port, other.port); + swap(dir, other.dir); + swap(file, other.file); + swap(query, other.query); + swap(username, other.username); + swap(password, other.password); + swap(hasPassword, other.hasPassword); + swap(ipv6LiteralAddress, other.ipv6LiteralAddress); + } +} + +void swap(UriStruct& lhs, UriStruct& rhs) +{ + lhs.swap(rhs); +} + bool parse(UriStruct& result, const std::string& uri) { // http://user:password@aria2.sourceforge.net:80/dir/file?query#fragment diff --git a/src/uri.h b/src/uri.h index c51be38e0..9ae0b5385 100644 --- a/src/uri.h +++ b/src/uri.h @@ -60,8 +60,11 @@ struct UriStruct { ~UriStruct(); UriStruct& operator=(const UriStruct& c); + void swap(UriStruct& other); }; +void swap(UriStruct& lhs, UriStruct& rhs); + // Splits URI uri into components and stores them into result. On // success returns true. Otherwise returns false and result is // undefined. diff --git a/test/UriTest.cc b/test/UriTest.cc index 025f41ca8..721240c67 100644 --- a/test/UriTest.cc +++ b/test/UriTest.cc @@ -34,6 +34,7 @@ class UriTest:public CppUnit::TestFixture { CPPUNIT_TEST(testSetUri_ipv6); CPPUNIT_TEST(testInnerLink); CPPUNIT_TEST(testConstruct); + CPPUNIT_TEST(testSwap); CPPUNIT_TEST_SUITE_END(); public: @@ -62,6 +63,7 @@ public: void testSetUri_ipv6(); void testInnerLink(); void testConstruct(); + void testSwap(); }; @@ -470,6 +472,19 @@ void UriTest::testConstruct() } } +void UriTest::testSwap() +{ + UriStruct us1; + CPPUNIT_ASSERT(parse(us1, "http://u1:p1@[::1]/dir1/file1?k1=v1")); + UriStruct us2; + CPPUNIT_ASSERT(parse(us2, "ftp://host2/dir2/file2?k2=v2")); + us1.swap(us2); + CPPUNIT_ASSERT_EQUAL(std::string("ftp://host2/dir2/file2?k2=v2"), + construct(us1)); + CPPUNIT_ASSERT_EQUAL(std::string("http://u1:p1@[::1]/dir1/file1?k1=v1"), + construct(us2)); +} + } // namespace uri } // namespace aria2