[Telepathy-commits] [telepathy-qt4/master] OptionalInterfaceFactory: take the proxy instance as a ctor parameter

Simon McVittie simon.mcvittie at collabora.co.uk
Fri Jan 23 03:03:21 PST 2009


Also, in qt4-client-gen, alter the ctor from DBusProxy * to take a
mutable DBusProxy *.

This lets us use it as the QObject parent, meaning that OIF's cleanup
code is no longer needed.
---
 TelepathyQt4/Client/account-manager.cpp            |    2 +
 TelepathyQt4/Client/account-manager.h              |    2 +-
 TelepathyQt4/Client/account.cpp                    |    1 +
 TelepathyQt4/Client/account.h                      |    4 +--
 TelepathyQt4/Client/channel.cpp                    |    5 ++-
 TelepathyQt4/Client/channel.h                      |    4 +-
 TelepathyQt4/Client/connection-manager.cpp         |    2 +
 TelepathyQt4/Client/connection-manager.h           |    2 +-
 TelepathyQt4/Client/connection.cpp                 |    2 +
 TelepathyQt4/Client/connection.h                   |    2 +-
 TelepathyQt4/Client/optional-interface-factory.cpp |   27 +++++++++++---------
 TelepathyQt4/Client/optional-interface-factory.h   |   20 +++++++++++---
 tools/qt4-client-gen.py                            |   10 +++---
 13 files changed, 51 insertions(+), 32 deletions(-)

diff --git a/TelepathyQt4/Client/account-manager.cpp b/TelepathyQt4/Client/account-manager.cpp
index 753b29a..36d4953 100644
--- a/TelepathyQt4/Client/account-manager.cpp
+++ b/TelepathyQt4/Client/account-manager.cpp
@@ -151,6 +151,7 @@ AccountManager::AccountManager(QObject* parent)
     : StatelessDBusProxy(QDBusConnection::sessionBus(),
             QLatin1String(TELEPATHY_ACCOUNT_MANAGER_BUS_NAME),
             QLatin1String(TELEPATHY_ACCOUNT_MANAGER_OBJECT_PATH), parent),
+      OptionalInterfaceFactory<AccountManager>(this),
       mPriv(new Private(this))
 {
     init();
@@ -167,6 +168,7 @@ AccountManager::AccountManager(const QDBusConnection& bus,
     : StatelessDBusProxy(bus,
             QLatin1String(TELEPATHY_ACCOUNT_MANAGER_BUS_NAME),
             QLatin1String(TELEPATHY_ACCOUNT_MANAGER_OBJECT_PATH), parent),
+      OptionalInterfaceFactory<AccountManager>(this),
       mPriv(new Private(this))
 {
     init();
diff --git a/TelepathyQt4/Client/account-manager.h b/TelepathyQt4/Client/account-manager.h
index 1ab0c58..89b3fda 100644
--- a/TelepathyQt4/Client/account-manager.h
+++ b/TelepathyQt4/Client/account-manager.h
@@ -66,7 +66,7 @@ public:
 
     inline DBus::PropertiesInterface *propertiesInterface() const
     {
-        return OptionalInterfaceFactory<AccountManager>::interface<DBus::PropertiesInterface>(this);
+        return OptionalInterfaceFactory<AccountManager>::interface<DBus::PropertiesInterface>();
     }
 
     QStringList validAccountPaths() const;
diff --git a/TelepathyQt4/Client/account.cpp b/TelepathyQt4/Client/account.cpp
index 3139917..4bdaae8 100644
--- a/TelepathyQt4/Client/account.cpp
+++ b/TelepathyQt4/Client/account.cpp
@@ -168,6 +168,7 @@ Account::Account(AccountManager *am, const QString &objectPath,
         QObject *parent)
     : StatelessDBusProxy(am->dbusConnection(),
             am->busName(), objectPath, parent),
+      OptionalInterfaceFactory<Account>(this),
       mPriv(new Private(this))
 {
     connect(mPriv->baseInterface,
diff --git a/TelepathyQt4/Client/account.h b/TelepathyQt4/Client/account.h
index 962a00e..2c098cd 100644
--- a/TelepathyQt4/Client/account.h
+++ b/TelepathyQt4/Client/account.h
@@ -132,8 +132,6 @@ public:
 
     QStringList interfaces() const;
 
-    // TODO this is a copy/paste from Connection, move it somewhere else that
-    //      could be shared between classes
     template <class Interface>
     inline Interface *optionalInterface(
             InterfaceSupportedChecking check = CheckInterfaceSupported) const
@@ -145,7 +143,7 @@ public:
         }
 
         // If present or forced, delegate to OptionalInterfaceFactory
-        return OptionalInterfaceFactory<Account>::interface<Interface>(this);
+        return OptionalInterfaceFactory<Account>::interface<Interface>();
     }
 
     inline DBus::PropertiesInterface *propertiesInterface() const
diff --git a/TelepathyQt4/Client/channel.cpp b/TelepathyQt4/Client/channel.cpp
index 6e6158c..ec68400 100644
--- a/TelepathyQt4/Client/channel.cpp
+++ b/TelepathyQt4/Client/channel.cpp
@@ -401,9 +401,10 @@ struct Channel::Private
 Channel::Channel(Connection* connection,
                  const QString& objectPath,
                  QObject* parent)
-  : StatefulDBusProxy(connection->dbusConnection(), connection->busName(),
+    : StatefulDBusProxy(connection->dbusConnection(), connection->busName(),
             objectPath, parent),
-    mPriv(new Private(*this, connection))
+      OptionalInterfaceFactory<Channel>(this),
+      mPriv(new Private(*this, connection))
 {
     mPriv->baseInterface = new ChannelInterface(this->dbusConnection(),
             this->busName(), this->objectPath(), this);
diff --git a/TelepathyQt4/Client/channel.h b/TelepathyQt4/Client/channel.h
index bca870f..95b2f53 100644
--- a/TelepathyQt4/Client/channel.h
+++ b/TelepathyQt4/Client/channel.h
@@ -586,7 +586,7 @@ public:
             return 0;
 
         // If present or forced, delegate to OptionalInterfaceFactory
-        return OptionalInterfaceFactory<Channel>::interface<Interface>(this);
+        return OptionalInterfaceFactory<Channel>::interface<Interface>();
     }
 
     /**
@@ -712,7 +712,7 @@ public:
             return 0;
 
         // If correct type or check bypassed, delegate to OIF
-        return OptionalInterfaceFactory<Channel>::interface<Interface>(this);
+        return OptionalInterfaceFactory<Channel>::interface<Interface>();
     }
 
     /**
diff --git a/TelepathyQt4/Client/connection-manager.cpp b/TelepathyQt4/Client/connection-manager.cpp
index e0bf291..b41af07 100644
--- a/TelepathyQt4/Client/connection-manager.cpp
+++ b/TelepathyQt4/Client/connection-manager.cpp
@@ -349,6 +349,7 @@ ConnectionManager::ConnectionManager(const QString &name, QObject *parent)
     : StatelessDBusProxy(QDBusConnection::sessionBus(),
             Private::makeBusName(name), Private::makeObjectPath(name),
             parent),
+      OptionalInterfaceFactory<ConnectionManager>(this),
       mPriv(new Private(name, this))
 {
     mPriv->introspectQueue.enqueue(&ConnectionManager::callReadConfig);
@@ -366,6 +367,7 @@ ConnectionManager::ConnectionManager(const QDBusConnection &bus,
         const QString &name, QObject *parent)
     : StatelessDBusProxy(bus, Private::makeBusName(name),
             Private::makeObjectPath(name), parent),
+      OptionalInterfaceFactory<ConnectionManager>(this),
       mPriv(new Private(name, this))
 {
     mPriv->introspectQueue.enqueue(&ConnectionManager::callReadConfig);
diff --git a/TelepathyQt4/Client/connection-manager.h b/TelepathyQt4/Client/connection-manager.h
index 52df936..cb68b3e 100644
--- a/TelepathyQt4/Client/connection-manager.h
+++ b/TelepathyQt4/Client/connection-manager.h
@@ -136,7 +136,7 @@ public:
 
     inline DBus::PropertiesInterface *propertiesInterface() const
     {
-        return OptionalInterfaceFactory<ConnectionManager>::interface<DBus::PropertiesInterface>(this);
+        return OptionalInterfaceFactory<ConnectionManager>::interface<DBus::PropertiesInterface>();
     }
 
     bool isReady() const;
diff --git a/TelepathyQt4/Client/connection.cpp b/TelepathyQt4/Client/connection.cpp
index ccf0599..cb135c5 100644
--- a/TelepathyQt4/Client/connection.cpp
+++ b/TelepathyQt4/Client/connection.cpp
@@ -310,6 +310,7 @@ Connection::Connection(const QString& serviceName,
                        QObject* parent)
     : StatefulDBusProxy(QDBusConnection::sessionBus(), serviceName,
           objectPath, parent),
+      OptionalInterfaceFactory<Connection>(this),
       mPriv(new Private(*this))
 {
     mPriv->startIntrospection();
@@ -320,6 +321,7 @@ Connection::Connection(const QDBusConnection& connection,
                        const QString& objectPath,
                        QObject* parent)
     : StatefulDBusProxy(connection, serviceName, objectPath, parent),
+      OptionalInterfaceFactory<Connection>(this),
       mPriv(new Private(*this))
 {
     mPriv->startIntrospection();
diff --git a/TelepathyQt4/Client/connection.h b/TelepathyQt4/Client/connection.h
index 3538f3c..bad98ba 100644
--- a/TelepathyQt4/Client/connection.h
+++ b/TelepathyQt4/Client/connection.h
@@ -305,7 +305,7 @@ public:
             return 0;
 
         // If present or forced, delegate to OptionalInterfaceFactory
-        return OptionalInterfaceFactory<Connection>::interface<Interface>(this);
+        return OptionalInterfaceFactory<Connection>::interface<Interface>();
     }
 
     /**
diff --git a/TelepathyQt4/Client/optional-interface-factory.cpp b/TelepathyQt4/Client/optional-interface-factory.cpp
index 5ff2400..abb1ccc 100644
--- a/TelepathyQt4/Client/optional-interface-factory.cpp
+++ b/TelepathyQt4/Client/optional-interface-factory.cpp
@@ -33,29 +33,32 @@ namespace Client
 
 struct OptionalInterfaceCache::Private
 {
+    QObject *proxy;
     QMap<QString, AbstractInterface*> interfaces;
+
+    Private(QObject *proxy);
 };
 
-OptionalInterfaceCache::OptionalInterfaceCache()
-    : mPriv(new Private())
+OptionalInterfaceCache::Private::Private(QObject *proxy)
+    : proxy(proxy)
 {
-    debug() << "Constructing OptionalInterfaceCache";
 }
 
-OptionalInterfaceCache::~OptionalInterfaceCache()
+OptionalInterfaceCache::OptionalInterfaceCache(QObject *proxy)
+    : mPriv(new Private(proxy))
 {
-    debug() << "Destroying OptionalInterfaceCache";
-
-    for (QMap<QString, AbstractInterface *>::iterator i = mPriv->interfaces.begin();
-            i != mPriv->interfaces.end();
-            ++i) {
-        debug().nospace() << " ~" << i.key();
-        delete i.value();
-    }
+}
 
+OptionalInterfaceCache::~OptionalInterfaceCache()
+{
     delete mPriv;
 }
 
+QObject *OptionalInterfaceCache::proxy() const
+{
+    return mPriv->proxy;
+}
+
 AbstractInterface *OptionalInterfaceCache::getCached(const QString &name) const
 {
     if (mPriv->interfaces.contains(name)) {
diff --git a/TelepathyQt4/Client/optional-interface-factory.h b/TelepathyQt4/Client/optional-interface-factory.h
index c150fd7..7924591 100644
--- a/TelepathyQt4/Client/optional-interface-factory.h
+++ b/TelepathyQt4/Client/optional-interface-factory.h
@@ -52,7 +52,7 @@ class OptionalInterfaceCache
         /**
          * Class constructor.
          */
-        OptionalInterfaceCache();
+        explicit OptionalInterfaceCache(QObject *proxy);
 
         /**
          * Class destructor.
@@ -64,6 +64,7 @@ class OptionalInterfaceCache
     protected:
         AbstractInterface *getCached(const QString &name) const;
         void cache(AbstractInterface *interface) const;
+        QObject *proxy() const;
 
     private:
         struct Private;
@@ -81,6 +82,11 @@ class OptionalInterfaceCache
  * This class is included in the public API for the benefit of high-level
  * proxies in extensions.
  *
+ * To use this helper in a subclass of DBusProxy (say, ExampleObject),
+ * ExampleObject should inherit privately from
+ * OptionalInterfaceFactory<ExampleObject>, and call
+ * OptionalInterfaceFactory(this) in its constructor's initialization list.
+ *
  * \tparam DBusProxySubclass A subclass of DBusProxy
  */
 template <typename DBusProxySubclass> class OptionalInterfaceFactory
@@ -89,8 +95,12 @@ template <typename DBusProxySubclass> class OptionalInterfaceFactory
     public:
         /**
          * Class constructor.
+         *
+         * \param this_ The class to which this OptionalInterfaceFactory is
+         *              attached
          */
-        inline OptionalInterfaceFactory() : OptionalInterfaceCache()
+        inline OptionalInterfaceFactory(DBusProxySubclass *this_)
+            : OptionalInterfaceCache(this_)
         {
         }
 
@@ -119,11 +129,10 @@ template <typename DBusProxySubclass> class OptionalInterfaceFactory
          * instance is shared, it should not be freed directly.
          *
          * \tparam Interface Class of the interface instance to get.
-         * \param proxy An instance of the appropriate DBusProxy subclass.
          * \return A pointer to an optional interface instance.
          */
         template <typename Interface>
-        inline Interface *interface(const DBusProxySubclass *proxy) const
+        inline Interface *interface() const
         {
             AbstractInterface* interfaceMustBeASubclassOfAbstractInterface = static_cast<Interface *>(NULL);
             Q_UNUSED(interfaceMustBeASubclassOfAbstractInterface);
@@ -135,7 +144,8 @@ template <typename DBusProxySubclass> class OptionalInterfaceFactory
                 return static_cast<Interface *>(cached);
 
             // Otherwise, cache and return a newly constructed proxy
-            Interface *interface = new Interface(proxy, 0);
+            Interface *interface = new Interface(
+                    static_cast<DBusProxySubclass *>(proxy()));
             cache(interface);
             return interface;
         }
diff --git a/tools/qt4-client-gen.py b/tools/qt4-client-gen.py
index ad01656..0bacbc6 100644
--- a/tools/qt4-client-gen.py
+++ b/tools/qt4-client-gen.py
@@ -213,16 +213,16 @@ public:
     /**
      * Creates a %(name)s associated with the same object as the given proxy.
      *
-     * \\param proxy The proxy to use.
-     * \\param parent Passed to the parent class constructor.
+     * \\param proxy The proxy to use. It will also be the QObject::parent()
+     *               for this object.
      */
-    %(name)s(const %(dbus_proxy)s *proxy, QObject *parent = 0);
+    %(name)s(%(dbus_proxy)s *proxy);
 """ % {'name' : name,
        'dbus_proxy' : self.dbus_proxy})
 
         self.b("""
-%(name)s::%(name)s(const %(dbus_proxy)s *proxy, QObject *parent)
-    : Telepathy::Client::AbstractInterface(proxy->busName(), proxy->objectPath(), staticInterfaceName(), proxy->dbusConnection(), parent)
+%(name)s::%(name)s(%(dbus_proxy)s *proxy)
+    : Telepathy::Client::AbstractInterface(proxy, staticInterfaceName())
 {
 }
 """ % {'name' : name,
-- 
1.5.6.5




More information about the Telepathy-commits mailing list