Cleanup DBus type registration code

Modernize the code base, use QMetaType and avoid
reinterpret_casts.

Change-Id: I0bad2ee393a0f850cf40b248cb9439b06ed9f663
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Lars Knoll 2020-09-09 12:30:11 +02:00
parent abd1b7f306
commit d39bd9e258
11 changed files with 47 additions and 71 deletions

View File

@ -212,7 +212,7 @@ bool QDBusAbstractInterfacePrivate::property(const QMetaProperty &mp, void *retu
foundSignature = arg.currentSignature().toLatin1();
if (foundSignature == expectedSignature) {
// signatures match, we can demarshall
return QDBusMetaType::demarshall(arg, type, returnValuePtr);
return QDBusMetaType::demarshall(arg, QMetaType(type), returnValuePtr);
}
} else {
foundType = value.typeName();

View File

@ -76,7 +76,7 @@ QByteArray QDBusArgumentPrivate::createSignature(int id)
// run it
QVariant v{QMetaType(id)};
QDBusArgument arg(marshaller);
QDBusMetaType::marshall(arg, v.userType(), v.constData());
QDBusMetaType::marshall(arg, v.metaType(), v.constData());
arg.d = nullptr;
// delete it

View File

@ -952,7 +952,7 @@ void QDBusConnectionPrivate::deliverCall(QObject *object, int /*flags*/, const Q
*reinterpret_cast<const QDBusArgument *>(arg.constData());
QVariant &out = auxParameters[auxParameters.count() - 1];
if (Q_UNLIKELY(!QDBusMetaType::demarshall(in, out.userType(), out.data())))
if (Q_UNLIKELY(!QDBusMetaType::demarshall(in, out.metaType(), out.data())))
qFatal("Internal error: demarshalling function for type '%s' (%d) failed!",
out.typeName(), out.userType());

View File

@ -144,7 +144,7 @@ static void copyArgument(void *to, int id, const QVariant &arg)
}
// we can demarshall
QDBusMetaType::demarshall(dbarg, id, to);
QDBusMetaType::demarshall(dbarg, QMetaType(id), to);
}
QDBusInterfacePrivate::QDBusInterfacePrivate(const QString &serv, const QString &p,

View File

@ -359,7 +359,7 @@ static int writeProperty(QObject *obj, const QByteArray &property_name, QVariant
if (id != QMetaType::QVariant && value.userType() == QDBusMetaTypeId::argument()) {
// we have to demarshall before writing
QVariant other{QMetaType(id)};
if (!QDBusMetaType::demarshall(qvariant_cast<QDBusArgument>(value), id, other.data())) {
if (!QDBusMetaType::demarshall(qvariant_cast<QDBusArgument>(value), other.metaType(), other.data())) {
qWarning("QDBusConnection: type `%s' (%d) is not registered with QtDBus. "
"Use qDBusRegisterMetaType to register it",
mp.typeName(), id);

View File

@ -528,7 +528,7 @@ bool QDBusMarshaller::appendRegisteredType(const QVariant &arg)
{
ref.ref(); // reference up
QDBusArgument self(QDBusArgumentPrivate::create(this));
return QDBusMetaType::marshall(self, arg.userType(), arg.constData());
return QDBusMetaType::marshall(self, arg.metaType(), arg.constData());
}
bool QDBusMarshaller::appendCrossMarshalling(QDBusDemarshaller *demarshaller)

View File

@ -47,6 +47,11 @@
#include <qglobal.h>
#include <qlist.h>
#include <qreadwritelock.h>
#include <qdatetime.h>
#include <qrect.h>
#include <qsize.h>
#include <qpoint.h>
#include <qline.h>
#include "qdbusargument_p.h"
#include "qdbusutil_p.h"
@ -77,16 +82,6 @@ public:
QDBusMetaType::DemarshallFunction demarshall;
};
template<typename T>
inline static void registerHelper(T * = nullptr)
{
void (*mf)(QDBusArgument &, const T *) = qDBusMarshallHelper<T>;
void (*df)(const QDBusArgument &, T *) = qDBusDemarshallHelper<T>;
QDBusMetaType::registerMarshallOperators(qMetaTypeId<T>(),
reinterpret_cast<QDBusMetaType::MarshallFunction>(mf),
reinterpret_cast<QDBusMetaType::DemarshallFunction>(df));
}
void QDBusMetaTypeId::init()
{
static QBasicAtomicInt initialized = Q_BASIC_ATOMIC_INITIALIZER(false);
@ -105,20 +100,20 @@ void QDBusMetaTypeId::init()
#ifndef QDBUS_NO_SPECIALTYPES
// and register Qt Core's with us
registerHelper<QDate>();
registerHelper<QTime>();
registerHelper<QDateTime>();
registerHelper<QRect>();
registerHelper<QRectF>();
registerHelper<QSize>();
registerHelper<QSizeF>();
registerHelper<QPoint>();
registerHelper<QPointF>();
registerHelper<QLine>();
registerHelper<QLineF>();
registerHelper<QVariantList>();
registerHelper<QVariantMap>();
registerHelper<QVariantHash>();
qDBusRegisterMetaType<QDate>();
qDBusRegisterMetaType<QTime>();
qDBusRegisterMetaType<QDateTime>();
qDBusRegisterMetaType<QRect>();
qDBusRegisterMetaType<QRectF>();
qDBusRegisterMetaType<QSize>();
qDBusRegisterMetaType<QSizeF>();
qDBusRegisterMetaType<QPoint>();
qDBusRegisterMetaType<QPointF>();
qDBusRegisterMetaType<QLine>();
qDBusRegisterMetaType<QLineF>();
qDBusRegisterMetaType<QVariantList>();
qDBusRegisterMetaType<QVariantMap>();
qDBusRegisterMetaType<QVariantHash>();
qDBusRegisterMetaType<QList<bool> >();
qDBusRegisterMetaType<QList<short> >();
@ -213,11 +208,12 @@ Q_GLOBAL_STATIC(QReadWriteLock, customTypesLock)
/*!
\internal
Registers the marshalling and demarshalling functions for meta
type \a id.
type \a metaType.
*/
void QDBusMetaType::registerMarshallOperators(int id, MarshallFunction mf,
void QDBusMetaType::registerMarshallOperators(QMetaType metaType, MarshallFunction mf,
DemarshallFunction df)
{
int id = metaType.id();
QList<QDBusCustomTypeInfo> *ct = customTypes();
if (id < 0 || !mf || !df || !ct)
return; // error!
@ -232,12 +228,13 @@ void QDBusMetaType::registerMarshallOperators(int id, MarshallFunction mf,
/*!
\internal
Executes the marshalling of type \a id (whose data is contained in
Executes the marshalling of type \a metaType (whose data is contained in
\a data) to the D-Bus marshalling argument \a arg. Returns \c true if
the marshalling succeeded, or false if an error occurred.
*/
bool QDBusMetaType::marshall(QDBusArgument &arg, int id, const void *data)
bool QDBusMetaType::marshall(QDBusArgument &arg, QMetaType metaType, const void *data)
{
int id = metaType.id();
QDBusMetaTypeId::init();
MarshallFunction mf;
@ -261,12 +258,13 @@ bool QDBusMetaType::marshall(QDBusArgument &arg, int id, const void *data)
/*!
\internal
Executes the demarshalling of type \a id (whose data will be placed in
Executes the demarshalling of type \a metaType (whose data will be placed in
\a data) from the D-Bus marshalling argument \a arg. Returns \c true if
the demarshalling succeeded, or false if an error occurred.
*/
bool QDBusMetaType::demarshall(const QDBusArgument &arg, int id, void *data)
bool QDBusMetaType::demarshall(const QDBusArgument &arg, QMetaType metaType, void *data)
{
int id = metaType.id();
QDBusMetaTypeId::init();
DemarshallFunction df;

View File

@ -55,37 +55,23 @@ public:
typedef void (*MarshallFunction)(QDBusArgument &, const void *);
typedef void (*DemarshallFunction)(const QDBusArgument &, void *);
static void registerMarshallOperators(int typeId, MarshallFunction, DemarshallFunction);
static bool marshall(QDBusArgument &, int id, const void *data);
static bool demarshall(const QDBusArgument &, int id, void *data);
static void registerMarshallOperators(QMetaType typeId, MarshallFunction, DemarshallFunction);
static bool marshall(QDBusArgument &, QMetaType id, const void *data);
static bool demarshall(const QDBusArgument &, QMetaType id, void *data);
static int signatureToType(const char *signature);
static const char *typeToSignature(int type);
};
template<typename T>
void qDBusMarshallHelper(QDBusArgument &arg, const T *t)
{ arg << *t; }
template<typename T>
void qDBusDemarshallHelper(const QDBusArgument &arg, T *t)
{ arg >> *t; }
template<typename T>
int qDBusRegisterMetaType(
#ifndef Q_QDOC
T * /* dummy */ = nullptr
#endif
)
QMetaType qDBusRegisterMetaType()
{
void (*mf)(QDBusArgument &, const T *) = qDBusMarshallHelper<T>;
void (*df)(const QDBusArgument &, T *) = qDBusDemarshallHelper<T>;
auto mf = [](QDBusArgument &arg, const void *t) { arg << *static_cast<const T *>(t); };
auto df = [](const QDBusArgument &arg, void *t) { arg >> *static_cast<T *>(t); };
int id = qMetaTypeId<T>(); // make sure it's registered
QDBusMetaType::registerMarshallOperators(id,
reinterpret_cast<QDBusMetaType::MarshallFunction>(mf),
reinterpret_cast<QDBusMetaType::DemarshallFunction>(df));
return id;
QMetaType metaType = QMetaType::fromType<T>();
QDBusMetaType::registerMarshallOperators(metaType, mf, df);
return metaType;
}
QT_END_NAMESPACE

View File

@ -78,11 +78,7 @@ struct QDBusMetaTypeId
};
inline int QDBusMetaTypeId::message()
#ifdef QT_BOOTSTRAPPED
{ return qDBusRegisterMetaType<QList<QDBusUnixFileDescriptor> >() + 1; }
#else
{ return qMetaTypeId<QDBusMessage>(); }
#endif
inline int QDBusMetaTypeId::argument()
{ return qMetaTypeId<QDBusArgument>(); }
@ -97,11 +93,7 @@ inline int QDBusMetaTypeId::signature()
{ return qMetaTypeId<QDBusSignature>(); }
inline int QDBusMetaTypeId::error()
#ifdef QT_BOOTSTRAPPED
{ return qDBusRegisterMetaType<QList<QDBusUnixFileDescriptor> >() + 2; }
#else
{ return qMetaTypeId<QDBusError>(); }
#endif
inline int QDBusMetaTypeId::unixfd()
{ return qMetaTypeId<QDBusUnixFileDescriptor>(); }

View File

@ -219,7 +219,7 @@ void qDBusReplyFill(const QDBusMessage &reply, QDBusError &error, QVariant &data
receivedSignature = arg.currentSignature().toLatin1();
if (receivedSignature == expectedSignature) {
// matched. Demarshall it
QDBusMetaType::demarshall(arg, data.userType(), data.data());
QDBusMetaType::demarshall(arg, data.metaType(), data.data());
return;
}
} else {

View File

@ -282,9 +282,9 @@ void tst_QDBusMetaType::initTestCase()
qDBusRegisterMetaType<QList<Invalid0> >();
intStringMap = qDBusRegisterMetaType<QMap<int, QString> >();
stringStringMap = qDBusRegisterMetaType<QMap<QString, QString> >();
stringStruct1Map = qDBusRegisterMetaType<QMap<QString, Struct1> >();
intStringMap = qDBusRegisterMetaType<QMap<int, QString> >().id();
stringStringMap = qDBusRegisterMetaType<QMap<QString, QString> >().id();
stringStruct1Map = qDBusRegisterMetaType<QMap<QString, Struct1> >().id();
}
void tst_QDBusMetaType::staticTypes_data()