]> git.saurik.com Git - wxWidgets.git/commitdiff
Committing modified version of jwiesemann's patch (see #11223):
authorFrancesco Montorsi <f18m_cpp217828@yahoo.it>
Tue, 30 Mar 2010 21:38:31 +0000 (21:38 +0000)
committerFrancesco Montorsi <f18m_cpp217828@yahoo.it>
Tue, 30 Mar 2010 21:38:31 +0000 (21:38 +0000)
Fix wxURL copy ctor and assignment: the default ones provided by the compiler are wrong because wxURL uses pointers internally.
Also add a test for copy & assignment in URLTestCase.

Closes #11223

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@63806 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/url.h
src/common/url.cpp
tests/uris/url.cpp

index c74abf302c537755bb8e1dce7ebd38947ca4bafe..71189f1830710a553695b79b860afac37ad50335 100644 (file)
@@ -48,11 +48,13 @@ class WXDLLIMPEXP_NET wxURL : public wxURI
 {
 public:
     wxURL(const wxString& sUrl = wxEmptyString);
-    wxURL(const wxURI& url);
+    wxURL(const wxURI& uri);
+    wxURL(const wxURL& url);
     virtual ~wxURL();
 
     wxURL& operator = (const wxString& url);
-    wxURL& operator = (const wxURI& url);
+    wxURL& operator = (const wxURI& uri);
+    wxURL& operator = (const wxURL& url);
 
     wxProtocol& GetProtocol()        { return *m_protocol; }
     wxURLError GetError() const      { return m_error; }
@@ -99,6 +101,7 @@ protected:
     void Init(const wxString&);
     bool ParseURL();
     void CleanData();
+    void Free();
     bool FetchProtocol();
 
     friend class wxProtoInfo;
index 918923287a50f2c4ebdf8bb61817dda5d590f7d6..383a5d088bbeb964ce39e1ec07a418c203e93258 100644 (file)
@@ -65,9 +65,15 @@ wxURL::wxURL(const wxString& url) : wxURI(url)
     ParseURL();
 }
 
-wxURL::wxURL(const wxURI& url) : wxURI(url)
+wxURL::wxURL(const wxURI& uri) : wxURI(uri)
 {
-    Init(url.BuildURI());
+    Init(uri.BuildURI());
+    ParseURL();
+}
+
+wxURL::wxURL(const wxURL& url) : wxURI(url)
+{
+    Init(url.GetURL());
     ParseURL();
 }
 
@@ -102,18 +108,39 @@ void wxURL::Init(const wxString& url)
 // Assignment
 // --------------------------------------------------------------
 
-wxURL& wxURL::operator = (const wxURI& url)
+wxURL& wxURL::operator = (const wxString& url)
 {
     wxURI::operator = (url);
-    Init(url.BuildURI());
+    Free();
+    Init(url);
     ParseURL();
+
     return *this;
 }
-wxURL& wxURL::operator = (const wxString& url)
+
+wxURL& wxURL::operator = (const wxURI& uri)
 {
-    wxURI::operator = (url);
-    Init(url);
-    ParseURL();
+    if (&uri != this)
+    {
+        wxURI::operator = (uri);
+        Free();
+        Init(uri.BuildURI());
+        ParseURL();
+    }
+
+    return *this;
+}
+
+wxURL& wxURL::operator = (const wxURL& url)
+{
+    if (&url != this)
+    {
+        wxURI::operator = (url);
+        Free();
+        Init(url.GetURL());
+        ParseURL();
+    }
+
     return *this;
 }
 
@@ -195,7 +222,7 @@ void wxURL::CleanData()
     }
 }
 
-wxURL::~wxURL()
+void wxURL::Free()
 {
     CleanData();
 #if wxUSE_PROTOCOL_HTTP
@@ -207,6 +234,11 @@ wxURL::~wxURL()
 #endif
 }
 
+wxURL::~wxURL()
+{
+    Free();
+}
+
 // --------------------------------------------------------------
 // FetchProtocol
 // --------------------------------------------------------------
index 8cf9d0961c9051806ae2727b29301dd9be88fcef..7f17ec46251ea080444d9b57695c88614b144226 100644 (file)
@@ -37,9 +37,11 @@ public:
 private:
     CPPUNIT_TEST_SUITE( URLTestCase );
         CPPUNIT_TEST( GetInputStream );
+        CPPUNIT_TEST( CopyAndAssignment );
     CPPUNIT_TEST_SUITE_END();
 
     void GetInputStream();
+    void CopyAndAssignment();
 
     DECLARE_NO_COPY_CLASS(URLTestCase)
 };
@@ -68,7 +70,7 @@ void URLTestCase::GetInputStream()
         wxLogWarning("No network connectivity; skipping the URLTestCase::GetInputStream test unit.");
         return;
     }
-    
+
     wxURL url("http://wxwidgets.org/logo9.jpg");
     CPPUNIT_ASSERT_EQUAL(wxURL_NOERR, url.GetError());
 
@@ -77,8 +79,54 @@ void URLTestCase::GetInputStream()
 
     wxMemoryOutputStream ostream;
     CPPUNIT_ASSERT(in_stream->Read(ostream).GetLastError() == wxSTREAM_EOF);
-    
+
     // wx logo image currently is 13219 bytes
     CPPUNIT_ASSERT(ostream.GetSize() == 13219);
+
+    // we have to delete the object created by GetInputStream()
+    delete in_stream;
 }
 
+void URLTestCase::CopyAndAssignment()
+{
+    wxURL url1("http://www.example.org/");
+    wxURL url2;
+    wxURI *puri = &url2;        // downcast
+
+    { // Copy constructor
+        wxURL url3(url1);
+        CPPUNIT_ASSERT(url1 == url3);
+    }
+    { // Constructor for string
+        wxURL url3(url1.GetURL());
+        CPPUNIT_ASSERT(url1 == url3);
+    }
+    { // 'Copy' constructor for uri
+        wxURL url3(*puri);
+        CPPUNIT_ASSERT(url2 == url3);
+    }
+
+    // assignment for uri
+    *puri = url1;
+    CPPUNIT_ASSERT(url1 == url2);
+
+    // assignment to self through base pointer
+    *puri = url2;
+
+    // Assignment of string
+    url1 = wxS("http://www.example2.org/index.html");
+    *puri = wxS("http://www.example2.org/index.html");
+    CPPUNIT_ASSERT(url1 == url2);
+
+    // Assignment
+    url1 = wxS("");
+    url2 = url1;
+    CPPUNIT_ASSERT(url1 == url2);
+
+    // assignment to self
+    url2 = url2;
+
+    // check for destructor (with base pointer!)
+    puri = new wxURL();
+    delete puri;
+}