QtDBus: use qMetaTypeId<T>() instead of qRegisterMetaType<T>("T")
authorMarc Mutz <marc.mutz@kdab.com>
Fri, 20 Jul 2012 08:34:27 +0000 (10:34 +0200)
committerQt by Nokia <qt-info@nokia.com>
Tue, 24 Jul 2012 18:11:05 +0000 (20:11 +0200)
Using qMetaTypeId<T> has the advantage that multiple calls during
a program run are much more efficient, since an inlined atomic
is used to store the result. It also ensures that
Q_DECLARE_METATYPE(T) has been used, whereas qRegisterMetaType<T>
(the unary version) will happily register anything.

Had to add a proper default constructor to QDBusError, as the
one doubling as the default constructor wasn't available under
QT_BOOTSTRAP, but Q_DECLARE_METATYPE requires a default ctor.

Also changed a nullary qRegisterMetaType() to qMetaTypeId() in
qDBusRegisterMetaType(). They're equivalent, since the former
just calls the latter, but apart from the miniscule optimisation
that the compiler has to instantiate one function less, the result
is also used, so using qMetaTypeId() better expresses what 'id'
is.

Change-Id: Ib9dde17923ab9ee55f9464138a625ab8cd55c482
Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>
src/dbus/qdbuserror.cpp
src/dbus/qdbuserror.h
src/dbus/qdbusmessage.h
src/dbus/qdbusmetatype.cpp
src/dbus/qdbusmetatype.h

index b81d8a6..85d7d01 100644 (file)
@@ -250,6 +250,16 @@ static inline QDBusError::ErrorType get(const char *name)
     \value InvalidInterface     The interface is invalid.
 */
 
+/*!
+    \internal
+    Constructs a QDBusError that represents no error.
+*/
+QDBusError::QDBusError()
+    : code(NoError)
+{
+
+}
+
 #ifndef QT_BOOTSTRAPPED
 /*!
     \internal
index a6b3c9a..52e65d6 100644 (file)
@@ -95,8 +95,9 @@ public:
 #endif
     };
 
+    QDBusError();
 #ifndef QT_BOOTSTRAPPED
-    explicit QDBusError(const DBusError *error = 0);
+    explicit QDBusError(const DBusError *error);
     /*implicit*/ QDBusError(const QDBusMessage& msg);
 #endif
     QDBusError(ErrorType error, const QString &message);
@@ -126,6 +127,8 @@ Q_DBUS_EXPORT QDebug operator<<(QDebug, const QDBusError &);
 
 QT_END_NAMESPACE
 
+Q_DECLARE_METATYPE(QDBusError)
+
 QT_END_HEADER
 
 #endif // QT_NO_DBUS
index 20859eb..461a276 100644 (file)
@@ -124,6 +124,8 @@ Q_DBUS_EXPORT QDebug operator<<(QDebug, const QDBusMessage &);
 
 QT_END_NAMESPACE
 
+Q_DECLARE_METATYPE(QDBusMessage)
+
 QT_END_HEADER
 
 #endif // QT_NO_DBUS
index 5c8a7bc..835fa4f 100644 (file)
@@ -105,15 +105,15 @@ void QDBusMetaTypeId::init()
     // set the guard variable at the end
     if (!initialized) {
 #ifndef QT_BOOTSTRAPPED
-        // register our types with QtCore
-        message = qRegisterMetaType<QDBusMessage>("QDBusMessage");
-        error = qRegisterMetaType<QDBusError>("QDBusError");
+        // register our types with QtCore (calling qMetaTypeId<T>() does this implicitly)
+        message = qMetaTypeId<QDBusMessage>();
+        error = qMetaTypeId<QDBusError>();
 #endif
-        argument = qRegisterMetaType<QDBusArgument>("QDBusArgument");
-        variant = qRegisterMetaType<QDBusVariant>("QDBusVariant");
-        objectpath = qRegisterMetaType<QDBusObjectPath>("QDBusObjectPath");
-        signature = qRegisterMetaType<QDBusSignature>("QDBusSignature");
-        unixfd = qRegisterMetaType<QDBusUnixFileDescriptor>("QDBusUnixFileDescriptor");
+        argument = qMetaTypeId<QDBusArgument>();
+        variant = qMetaTypeId<QDBusVariant>();
+        objectpath = qMetaTypeId<QDBusObjectPath>();
+        signature = qMetaTypeId<QDBusSignature>();
+        unixfd = qMetaTypeId<QDBusUnixFileDescriptor>();
 
 #ifndef QDBUS_NO_SPECIALTYPES
         // and register QtCore's with us
index bb791c9..060e8ad 100644 (file)
@@ -84,7 +84,7 @@ int qDBusRegisterMetaType(
     void (*mf)(QDBusArgument &, const T *) = qDBusMarshallHelper<T>;
     void (*df)(const QDBusArgument &, T *) = qDBusDemarshallHelper<T>;
 
-    int id = qRegisterMetaType<T>(); // make sure it's registered
+    int id = qMetaTypeId<T>(); // make sure it's registered
     QDBusMetaType::registerMarshallOperators(id,
         reinterpret_cast<QDBusMetaType::MarshallFunction>(mf),
         reinterpret_cast<QDBusMetaType::DemarshallFunction>(df));