QtDBus: use qMetaTypeId<T>() instead of qRegisterMetaType<T>("T")

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>
This commit is contained in:
Marc Mutz 2012-07-20 10:34:27 +02:00 committed by Qt by Nokia
parent 95232af3ef
commit e9ecf1b420
5 changed files with 25 additions and 10 deletions

View 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

View 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

View 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

View 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

View 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));