[telepathy-qt4/master] shared-ptr: Changed SharedPtr(T*) to be a templated constructor.

Andre Moreira Magalhaes (andrunko) andre.magalhaes at collabora.co.uk
Tue Apr 7 05:46:29 PDT 2009


Rationale (Jonathon Jongsma):
[explicit inline SharedPtr(T *d) : d(d) { if (d) { d->ref(); } }]
boost::shared_ptr uses a templated constructor for this case (e.g.  template
<class Y> SharedPtr(Y *d) ...) which allows it to somehow remember the actual
pointer type passed so that if it was a different type that is convertible to
T*, it will call delete with its original type and thus will not leak even if Y
does not have a virtual destructor). Granted, they probably use some black magic
to accomplish this, but it might be worth considering.
---
 TelepathyQt4/Client/account.cpp    |    1 -
 TelepathyQt4/shared-ptr.h          |    3 ++-
 tests/dbus/account-basics.cpp      |    2 +-
 tests/dbus/chan-basics.cpp         |    2 +-
 tests/dbus/chan-group.cpp          |    2 +-
 tests/dbus/cm-basics.cpp           |    2 +-
 tests/dbus/conn-basics.cpp         |    2 +-
 tests/dbus/conn-requests.cpp       |    2 +-
 tests/dbus/conn-roster.cpp         |    2 +-
 tests/dbus/contacts.cpp            |    2 +-
 tests/dbus/handles.cpp             |    2 +-
 tests/dbus/streamed-media-chan.cpp |    2 +-
 tests/dbus/text-chan.cpp           |    4 +---
 13 files changed, 13 insertions(+), 15 deletions(-)

diff --git a/TelepathyQt4/Client/account.cpp b/TelepathyQt4/Client/account.cpp
index c7dd8df..72e7757 100644
--- a/TelepathyQt4/Client/account.cpp
+++ b/TelepathyQt4/Client/account.cpp
@@ -109,7 +109,6 @@ Account::Private::Private(Account *parent)
       valid(false),
       enabled(false),
       connectsAutomatically(false),
-      cm(0),
       protocolInfo(0),
       connectionStatus(Telepathy::ConnectionStatusDisconnected),
       connectionStatusReason(Telepathy::ConnectionStatusReasonNoneSpecified)
diff --git a/TelepathyQt4/shared-ptr.h b/TelepathyQt4/shared-ptr.h
index 95431ca..8dc48bc 100644
--- a/TelepathyQt4/shared-ptr.h
+++ b/TelepathyQt4/shared-ptr.h
@@ -67,7 +67,8 @@ class SharedPtr
 {
 public:
     inline SharedPtr() : d(0) { }
-    explicit inline SharedPtr(T *d) : d(d) { if (d) { d->ref(); } }
+    template <class Y>
+    explicit inline SharedPtr(Y *d) : d(dynamic_cast<T*>(d)) { if (d) { d->ref(); } }
     inline SharedPtr(const SharedPtr<T> &o) : d(o.d) { if (d) { d->ref(); } }
     template<class X>
     inline SharedPtr(const SharedPtr<X> &o) : d(static_cast<T *>(o.data())) { if (d) { d->ref(); } }
diff --git a/tests/dbus/account-basics.cpp b/tests/dbus/account-basics.cpp
index 5af89cc..1a716ea 100644
--- a/tests/dbus/account-basics.cpp
+++ b/tests/dbus/account-basics.cpp
@@ -20,7 +20,7 @@ class TestAccountBasics : public Test
 
 public:
     TestAccountBasics(QObject *parent = 0)
-        : Test(parent), mAM(0)
+        : Test(parent)
     { }
 
 protected Q_SLOTS:
diff --git a/tests/dbus/chan-basics.cpp b/tests/dbus/chan-basics.cpp
index 7a2aa4a..a3fd601 100644
--- a/tests/dbus/chan-basics.cpp
+++ b/tests/dbus/chan-basics.cpp
@@ -26,7 +26,7 @@ class TestChanBasics : public Test
 
 public:
     TestChanBasics(QObject *parent = 0)
-        : Test(parent), mConnService(0), mConn(0), mChan(0), mHandle(0)
+        : Test(parent), mConnService(0), mHandle(0)
     { }
 
 protected Q_SLOTS:
diff --git a/tests/dbus/chan-group.cpp b/tests/dbus/chan-group.cpp
index 54cefe4..48d9ce7 100644
--- a/tests/dbus/chan-group.cpp
+++ b/tests/dbus/chan-group.cpp
@@ -28,7 +28,7 @@ class TestChanGroup : public Test
 
 public:
     TestChanGroup(QObject *parent = 0)
-        : Test(parent), mConnService(0), mConn(0), mChan(0),
+        : Test(parent), mConnService(0),
           mRoomNumber(0), mRoomCount(4), mRequested(false)
     { }
 
diff --git a/tests/dbus/cm-basics.cpp b/tests/dbus/cm-basics.cpp
index db079aa..5d3a27f 100644
--- a/tests/dbus/cm-basics.cpp
+++ b/tests/dbus/cm-basics.cpp
@@ -24,7 +24,7 @@ class TestCmBasics : public Test
 
 public:
     TestCmBasics(QObject *parent = 0)
-        : Test(parent), mCMService(0), mCM(0)
+        : Test(parent), mCMService(0)
     { }
 
 private Q_SLOTS:
diff --git a/tests/dbus/conn-basics.cpp b/tests/dbus/conn-basics.cpp
index a6c6b1a..4b2cc2e 100644
--- a/tests/dbus/conn-basics.cpp
+++ b/tests/dbus/conn-basics.cpp
@@ -23,7 +23,7 @@ class TestConnBasics : public Test
 
 public:
     TestConnBasics(QObject *parent = 0)
-        : Test(parent), mConnService(0), mConn(0)
+        : Test(parent), mConnService(0)
     { }
 
 protected Q_SLOTS:
diff --git a/tests/dbus/conn-requests.cpp b/tests/dbus/conn-requests.cpp
index 6643c27..a695ce8 100644
--- a/tests/dbus/conn-requests.cpp
+++ b/tests/dbus/conn-requests.cpp
@@ -26,7 +26,7 @@ class TestConnRequests : public Test
 
 public:
     TestConnRequests(QObject *parent = 0)
-        : Test(parent), mConnService(0), mConn(0), mHandle(0)
+        : Test(parent), mConnService(0), mHandle(0)
     { }
 
 protected Q_SLOTS:
diff --git a/tests/dbus/conn-roster.cpp b/tests/dbus/conn-roster.cpp
index 86e9afa..c1808ad 100644
--- a/tests/dbus/conn-roster.cpp
+++ b/tests/dbus/conn-roster.cpp
@@ -26,7 +26,7 @@ class TestConnRoster : public Test
 
 public:
     TestConnRoster(QObject *parent = 0)
-        : Test(parent), mConnService(0), mConn(0)
+        : Test(parent), mConnService(0)
     { }
 
 protected Q_SLOTS:
diff --git a/tests/dbus/contacts.cpp b/tests/dbus/contacts.cpp
index deb4287..b68c0df 100644
--- a/tests/dbus/contacts.cpp
+++ b/tests/dbus/contacts.cpp
@@ -29,7 +29,7 @@ class TestContacts : public Test
 
 public:
     TestContacts(QObject *parent = 0)
-        : Test(parent), mConnService(0), mConn(0)
+        : Test(parent), mConnService(0)
     {
     }
 
diff --git a/tests/dbus/handles.cpp b/tests/dbus/handles.cpp
index 5df7fa8..6cd03f4 100644
--- a/tests/dbus/handles.cpp
+++ b/tests/dbus/handles.cpp
@@ -25,7 +25,7 @@ class TestHandles : public Test
 
 public:
     TestHandles(QObject *parent = 0)
-        : Test(parent), mConnService(0), mConn(0)
+        : Test(parent), mConnService(0)
     { }
 
 protected Q_SLOTS:
diff --git a/tests/dbus/streamed-media-chan.cpp b/tests/dbus/streamed-media-chan.cpp
index fc7b277..074f1e0 100644
--- a/tests/dbus/streamed-media-chan.cpp
+++ b/tests/dbus/streamed-media-chan.cpp
@@ -22,7 +22,7 @@ class TestStreamedMediaChan : public Test
 
 public:
     TestStreamedMediaChan(QObject *parent = 0)
-        : Test(parent), mConnService(0), mConn(0)
+        : Test(parent), mConnService(0)
     { }
 
 protected Q_SLOTS:
diff --git a/tests/dbus/text-chan.cpp b/tests/dbus/text-chan.cpp
index e529a36..a29a9ad 100644
--- a/tests/dbus/text-chan.cpp
+++ b/tests/dbus/text-chan.cpp
@@ -42,9 +42,7 @@ public:
         : Test(parent),
           // service side (telepathy-glib)
           mConnService(0), mBaseConnService(0), mContactRepo(0),
-            mTextChanService(0), mMessagesChanService(0),
-          // client side (Telepathy-Qt4)
-          mConn(0), mChan(0)
+            mTextChanService(0), mMessagesChanService(0)
     { }
 
 protected Q_SLOTS:
-- 
1.5.6.5




More information about the telepathy-commits mailing list