[Telepathy-commits] [telepathy-qt4/master] ReferencedHandles API sketch

Olli Salli olli.salli at collabora.co.uk
Wed Jan 21 04:42:12 PST 2009


---
 TelepathyQt4/Client/ReferencedHandles         |    6 +
 TelepathyQt4/Client/ReferencedHandlesIterator |    6 +
 TelepathyQt4/Makefile.am                      |    6 +-
 TelepathyQt4/cli-connection.cpp               |   24 ++--
 TelepathyQt4/cli-connection.h                 |   19 ++--
 TelepathyQt4/cli-referenced-handles.cpp       |   52 ++++++++
 TelepathyQt4/cli-referenced-handles.h         |  162 +++++++++++++++++++++++++
 7 files changed, 254 insertions(+), 21 deletions(-)
 create mode 100644 TelepathyQt4/Client/ReferencedHandles
 create mode 100644 TelepathyQt4/Client/ReferencedHandlesIterator
 create mode 100644 TelepathyQt4/cli-referenced-handles.cpp
 create mode 100644 TelepathyQt4/cli-referenced-handles.h

diff --git a/TelepathyQt4/Client/ReferencedHandles b/TelepathyQt4/Client/ReferencedHandles
new file mode 100644
index 0000000..7efa390
--- /dev/null
+++ b/TelepathyQt4/Client/ReferencedHandles
@@ -0,0 +1,6 @@
+#ifndef _TelepathyQt4_Client_ReferencedHandlesIterator_HEADER_GUARD_
+#define _TelepathyQt4_Client_ReferencedHandlesIterator_HEADER_GUARD_
+
+#include <TelepathyQt4/cli-referenced-handles.h>
+
+#endif
diff --git a/TelepathyQt4/Client/ReferencedHandlesIterator b/TelepathyQt4/Client/ReferencedHandlesIterator
new file mode 100644
index 0000000..b48d8e1
--- /dev/null
+++ b/TelepathyQt4/Client/ReferencedHandlesIterator
@@ -0,0 +1,6 @@
+#ifndef _TelepathyQt4_Client_ReferencedHandles_HEADER_GUARD_
+#define _TelepathyQt4_Client_ReferencedHandles_HEADER_GUARD_
+
+#include <TelepathyQt4/cli-referenced-handles.h>
+
+#endif
diff --git a/TelepathyQt4/Makefile.am b/TelepathyQt4/Makefile.am
index 8e8d265..a972051 100644
--- a/TelepathyQt4/Makefile.am
+++ b/TelepathyQt4/Makefile.am
@@ -48,6 +48,7 @@ libtelepathy_qt4_la_SOURCES = \
     cli-pending-channel.cpp \
     cli-pending-operation.cpp \
     cli-properties.cpp \
+    cli-referenced-handles.cpp \
     debug.cpp \
     debug-internal.hpp \
     types.cpp \
@@ -97,6 +98,7 @@ tpqt4include_HEADERS = \
     cli-pending-channel.h \
     cli-pending-operation.h \
     cli-properties.h \
+    cli-referenced-handles.h \
     cli-simple-pending-operations.h \
     constants.h \
     debug.h \
@@ -116,7 +118,9 @@ tpqt4clientinclude_HEADERS = \
     Client/PendingOperation \
     Client/PendingSuccess \
     Client/PendingVoidMethodCall \
-    Client/Properties
+    Client/Properties \
+    Client/ReferencedHandles \
+    Client/ReferencedHandlesIterator
 
 nodist_geninclude_HEADERS = \
     _gen/cli-account.h \
diff --git a/TelepathyQt4/cli-connection.cpp b/TelepathyQt4/cli-connection.cpp
index ef2ccfa..6237184 100644
--- a/TelepathyQt4/cli-connection.cpp
+++ b/TelepathyQt4/cli-connection.cpp
@@ -516,8 +516,7 @@ struct PendingHandles::Private
     QStringList namesRequested;
     UIntList handlesToReference;
     QDBusError error;
-    // Replace with the ReferencedHandles instance
-    bool replyReceived;
+    ReferencedHandles handles;
 };
 
 PendingHandles::PendingHandles(Connection* connection, uint handleType, const QStringList& names)
@@ -527,7 +526,6 @@ PendingHandles::PendingHandles(Connection* connection, uint handleType, const QS
     mPriv->handleType = handleType;
     mPriv->isRequest = true;
     mPriv->namesRequested = names;
-    mPriv->replyReceived = false;
 }
 
 PendingHandles::PendingHandles(Connection* connection, uint handleType, const UIntList& handles, bool allHeld)
@@ -537,7 +535,9 @@ PendingHandles::PendingHandles(Connection* connection, uint handleType, const UI
     mPriv->handleType = handleType;
     mPriv->isRequest = false;
     mPriv->handlesToReference = handles;
-    mPriv->replyReceived = false;
+
+    if (allHeld)
+        mPriv->handles = ReferencedHandles(connection, handleType, handles);
 }
 
 PendingHandles::~PendingHandles()
@@ -577,8 +577,7 @@ const UIntList& PendingHandles::handlesToReference() const
 
 bool PendingHandles::isFinished() const
 {
-    // Replace with check for the ReferencedHandles instance
-    return mPriv->replyReceived || !mPriv->error.message().isEmpty();
+    return mPriv->handles.connection() == connection() || !mPriv->error.message().isEmpty();
 }
 
 bool PendingHandles::isError() const
@@ -601,6 +600,11 @@ bool PendingHandles::isValid() const
     return isFinished() && !isError();
 }
 
+ReferencedHandles PendingHandles::handles() const
+{
+    return mPriv->handles;
+}
+
 void PendingHandles::onCallFinished(QDBusPendingCallWatcher* watcher)
 {
     // Thanks QDBus for this the need for this error-handling code duplication
@@ -613,9 +617,7 @@ void PendingHandles::onCallFinished(QDBusPendingCallWatcher* watcher)
             debug().nospace() << " Failure: error " << reply.error().name() << ": " << reply.error().message();
             mPriv->error = reply.error();
         } else {
-            // Replace with constructing the ReferencedHandles from
-            // reply.value()
-            mPriv->replyReceived = true;
+            mPriv->handles = ReferencedHandles(connection(), handleType(), reply.value());
         }
     } else {
         QDBusPendingReply<void> reply;
@@ -626,9 +628,7 @@ void PendingHandles::onCallFinished(QDBusPendingCallWatcher* watcher)
             debug().nospace() << " Failure: error " << reply.error().name() << ": " << reply.error().message();
             mPriv->error = reply.error();
         } else {
-            // Replace with constructing the ReferencedHandles from
-            // handlesToReference()
-            mPriv->replyReceived = true;
+            mPriv->handles = ReferencedHandles(connection(), handleType(), handlesToReference());
         }
     }
 
diff --git a/TelepathyQt4/cli-connection.h b/TelepathyQt4/cli-connection.h
index 701c5eb..668f7b1 100644
--- a/TelepathyQt4/cli-connection.h
+++ b/TelepathyQt4/cli-connection.h
@@ -58,6 +58,7 @@ class PendingHandles;
 #include <TelepathyQt4/Client/DBus>
 #include <TelepathyQt4/Client/OptionalInterfaceFactory>
 #include <TelepathyQt4/Client/PendingOperation>
+#include <TelepathyQt4/Client/ReferencedHandles>
 
 #include "cli-pending-channel.h"
 
@@ -618,17 +619,19 @@ public:
      */
     bool isValid() const;
 
-    /*
-     * TODO: Add ReferencedHandles and way to get it from here
-     *
-     * Probably like this:
+    /**
+     * Returns the now-referenced handles resulting from the operation. If the
+     * operation has not (yet) finished successfully (isFinished() returns
+     * <code>false</code>), the return value is undefined.
      *
-     * const ReferencedHandles& handles() const;
+     * For requests of new handles, <code>handles()[i]</code> will be the handle
+     * corresponding to the entity name <code>namesToRequest()[i]</code>. For
+     * references of existing handles, <code>handles()[i] ==
+     * handlesToReference()[i]</code> will be true for any <code>i</code>.
      *
-     * So that the first user getting a copy of the handles doesn't have to do
-     * an extra ref. An internal ReferencedHandles can be stored anyway for
-     * unrefing purposes.
+     * \return ReferencedHandles instance containing the handles.
      */
+    ReferencedHandles handles() const;
 
 Q_SIGNALS:
     /**
diff --git a/TelepathyQt4/cli-referenced-handles.cpp b/TelepathyQt4/cli-referenced-handles.cpp
new file mode 100644
index 0000000..11299fc
--- /dev/null
+++ b/TelepathyQt4/cli-referenced-handles.cpp
@@ -0,0 +1,52 @@
+/*
+ * This file is part of TelepathyQt4
+ *
+ * Copyright (C) 2008 Collabora Ltd. <http://www.collabora.co.uk/>
+ * Copyright (C) 2008 Nokia Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include "cli-referenced-handles.h"
+
+#include <QPointer>
+#include <QSharedData>
+
+#include "debug-internal.hpp"
+
+namespace Telepathy
+{
+namespace Client
+{
+
+struct ReferencedHandles::Private : public QSharedData
+{
+    QPointer<Connection> connection;
+    uint handleType;
+    UIntList handles;
+};
+
+Connection* ReferencedHandles::connection() const
+{
+    return mPriv->connection;
+}
+
+uint ReferencedHandles::handleType() const
+{
+    return mPriv->handleType;
+}
+
+}
+}
diff --git a/TelepathyQt4/cli-referenced-handles.h b/TelepathyQt4/cli-referenced-handles.h
new file mode 100644
index 0000000..c9c80e6
--- /dev/null
+++ b/TelepathyQt4/cli-referenced-handles.h
@@ -0,0 +1,162 @@
+/*
+ * This file is part of TelepathyQt4
+ *
+ * Copyright (C) 2008 Collabora Ltd. <http://www.collabora.co.uk/>
+ * Copyright (C) 2008 Nokia Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef _TelepathyQt4_cli_referenced_handles_h_HEADER_GUARD_
+#define _TelepathyQt4_cli_referenced_handles_h_HEADER_GUARD_
+
+/**
+ * \addtogroup clientsideproxies Client-side proxies
+ *
+ * Proxy objects representing remote service objects accessed via D-Bus.
+ *
+ * In addition to providing direct access to methods, signals and properties
+ * exported by the remote objects, some of these proxies offer features like
+ * automatic inspection of remote object capabilities, property tracking,
+ * backwards compatibility helpers for older services and other utilities.
+ */
+
+/**
+ * \defgroup clientconn Connection proxies
+ * \ingroup clientsideproxies
+ *
+ * Proxy objects representing remote Telepathy Connections and their optional
+ * interfaces.
+ */
+
+namespace Telepathy
+{
+namespace Client
+{
+class ReferencedHandles;
+}
+}
+
+#include <TelepathyQt4/Constants>
+#include <TelepathyQt4/Types>
+#include <TelepathyQt4/Client/Connection>
+
+#include <QSharedDataPointer>
+#include <QList>
+
+namespace Telepathy
+{
+namespace Client
+{
+
+/**
+ * \class ReferencedHandles
+ * \ingroup clientconn
+ * \headerfile <TelepathyQt4/cli-referenced-handles.h> <TelepathyQt4/Client/ReferencedHandles>
+ *
+ * Helper container for safe management of handle lifetimes. Every handle in a
+ * ReferencedHandles container is guaranteed to be valid (and stay valid, as
+ * long it's in at least one ReferencedHandles container).
+ *
+ * ReferencedHandles is a implicitly shared class.
+ */
+class ReferencedHandles
+{
+    public:
+        typedef UIntList::const_iterator const_iterator;
+        typedef UIntList::ConstIterator ConstIterator;
+        typedef UIntList::const_pointer const_pointer;
+        typedef UIntList::const_reference const_reference;
+        typedef UIntList::difference_type difference_type;
+        typedef UIntList::pointer pointer;
+        typedef UIntList::reference reference;
+        typedef UIntList::size_type size_type;
+        typedef UIntList::value_type value_type;
+
+        ReferencedHandles();
+        ReferencedHandles(const ReferencedHandles& other);
+        ~ReferencedHandles();
+
+        Connection* connection() const;
+        uint handleType() const;
+
+        uint at(int i) const;
+        uint back() const;
+        uint first() const;
+        uint front() const;
+        uint last() const;
+        uint value(int i) const;
+        uint value(int i, uint defaultValue) const;
+
+        const_iterator begin() const;
+        const_iterator constBegin() const;
+        const_iterator constEnd() const;
+        const_iterator end() const;
+
+        bool contains(uint handle) const;
+        int count(uint value) const;
+        int count() const;
+        bool empty() const;
+        bool endsWith(uint handle) const;
+        int indexOf(uint handle, int from = 0) const;
+        bool isEmpty() const;
+        int lastIndexOf(uint handle, int from = -1) const;
+        int length() const;
+        ReferencedHandles mid(int pos, int from = -1) const;
+        int size() const;
+        bool startsWith(uint handle) const;
+
+        void append(const ReferencedHandles& another);
+        void clear();
+        void move(int from, int to);
+        void pop_back();
+        void pop_front();
+        int removeAll(uint handle);
+        void removeAt(int i);
+        void removeFirst();
+        void removeLast();
+        bool removeOne(uint handle);
+        void replace(int i, uint handle);
+        void swap(int i, int j);
+        uint takeAt(int i);
+        uint takeFirst();
+        uint takeLast();
+
+        bool operator!=(const ReferencedHandles& another) const;
+        bool operator!=(const UIntList& another) const;
+        ReferencedHandles operator+(const ReferencedHandles& another);
+        ReferencedHandles& operator+=(const ReferencedHandles& another);
+        ReferencedHandles& operator<<(const ReferencedHandles& another);
+        ReferencedHandles& operator=(const ReferencedHandles& another);
+        bool operator==(const ReferencedHandles& another) const;
+        bool operator==(const UIntList& another) const;
+        uint operator[](int i) const;
+
+    private:
+        // For access to the "prime" constructor
+        friend class PendingHandles;
+
+        ReferencedHandles(Connection* connection, uint handleType, const UIntList& handles);
+
+        struct Private;
+        QSharedDataPointer<Private> mPriv;
+};
+
+typedef QListIterator<uint> ReferencedHandlesIterator;
+
+}
+}
+
+#endif
-- 
1.5.6.5




More information about the Telepathy-commits mailing list