Browse Source

Added swap for UriStruct

Tatsuhiro Tsujikawa 14 years ago
parent
commit
55d00d047c
3 changed files with 40 additions and 0 deletions
  1. 22 0
      src/uri.cc
  2. 3 0
      src/uri.h
  3. 15 0
      test/UriTest.cc

+ 22 - 0
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

+ 3 - 0
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.

+ 15 - 0
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