QDBusDataConverter Class Reference

Template based converter for getting complex data into or from QDBusData objects. More...

#include <qdbusdataconverter.h>

List of all members.

Public Types

enum  Result { Success, InvalidSignature, InvalidArgument }
 

Conversion result values.

More...

Static Public Member Functions

template<class T >
static Result convertFromQDBusData (const QDBusData &dbusData, T &typeData)
 Conversion from a filled QDBusData instance to a native type.
template<class T >
static Result convertToQDBusData (const T &typeData, QDBusData &dbusData)
 Conversion from a native type to a QDBusData instance.

Detailed Description

Template based converter for getting complex data into or from QDBusData objects.

Any data to transport over D-Bus, i.e. method/signal paramaters or properties, need to be converted into a QDBusData instance.

For complex types, e.g. structures or nested containers, this can be quite some code, and will likely be needed for more than one call. Therefore it is more convenient to implement the conversions once per complex type.

Example: sending and recieving a QRect over D-Bus. In D-Bus terminology a QRect is a struct of four 32-bit signed integers. The code to do this manually looks like this:

 QRect rect(0, 0, 100, 100);
 
 QValueList<QDBusData> structMembers;
 structMembers << QDBusData::fromInt32(rect.x());
 structMembers << QDBusData::fromInt32(rect.y());
 structMembers << QDBusData::fromInt32(rect.wdth());
 structMembers << QDBusData::fromInt32(rect.height());

 QDBusData rectStruct = QDBusData::fromStruct(structMembers);

and reverse (without the error checking)

 QDBusData dbusData; // assume we got this from a D-Bus call
 
 QValueList<QDBusData> structMembers = dbudData.toStruct();
 
 int x = structMembers[0].toInt32();
 int y = structMembers[1].toInt32();
 int w = structMembers[2].toInt32();
 int h = structMembers[3].toInt32();

 QRect rect(x, y, w, h);

Rather than implementing it in the method which performs the D-Bus call, basically the same code can be used as a spezialisation of the QDBusDataConverter methods and then used like this:

 QRect rect(0, 0, 100, 100);
 QDBusData rectStruct;

 QDBusDataConverter::convertToQDBusData<QRect>(rect, rectStruct);

and

 QRect rect;
 QDBusData dbusData; // assume we got this from a D-Bus call

 QDBusDataConverter::convertFromQDBusData<QRect>(dbusData, rect);
Note:
The bindings library contains the spezialisations for QRect, QPoint and QSize.

Member Enumeration Documentation

Conversion result values.

Enumerator:
Success 

Conversion successfull

InvalidSignature 

Conversion failed because the passed QDBusData instance does not contain data of the needed signature, e.g. too few to too many members for a struct or wrong types.

See also:
QDBusError::stdInvalidSignature()
InvalidArgument 

Conversion failed because the passed QDBusData contained values which are not allowed, e.g. out of range for a numerical type used a an enum or flags.

See also:
QDBusError::stdInvalidArgs()

Member Function Documentation

template<class T >
static Result QDBusDataConverter::convertFromQDBusData ( const QDBusData dbusData,
T &  typeData 
) [inline, static]

Conversion from a filled QDBusData instance to a native type.

For example the implementation for QPoint looks like this:

 template <>
 QDBusDataConverter::Result
 QDBusDataConverter::convertFromQDBusData<QPoint>(const QDBusData& dbusData, QPoint& typeData)
 {
     if (dbusData.type() != QDBusData::Struct) return InvalidSignature;
     
     QValueList<QDBusData> members = dbusData.toStruct();
     if (members.count() != 2) return InvalidSignature;

     bool ok = false;
     int x = members[0].toInt32(&ok);
     if (!ok) return InvalidSignature;

     int y = members[1].toInt32(&ok);
     if (!ok) return InvalidSignature;

     typeData = QPoint(x, y);

     return Success;
 }

And then can be used like this:

 QDBusMessage reply; // assume we got this as a D-Bus call reply
 QPoint point;

 if (QDBusDataConverter::convertFromQDBusData(reply[0], point) != QDBusDataConverter::Success)
 {
     // error handling
 }
Parameters:
dbusData the binding's data instance to get the content from
typeData the native type instance to put the content into
Returns:
the conversion result value
template<class T >
static Result QDBusDataConverter::convertToQDBusData ( const T &  typeData,
QDBusData dbusData 
) [inline, static]

Conversion from a native type to a QDBusData instance.

For example the implementation for QPoint looks like this:

 template <>
 QDBusDataConversion::Result
 QDBusDataConversion::convertToQDBusData<QPoint>(const QPoint& typeData, QDBusData& dbusData)
 {
     QValueList<QDBusData> members;
     members << QDBusData::fromInt32(typeData.x());
     members << QDBusData::fromInt32(typeData.y());

     dbusData = QDBusData::fromStruct(members);

     return Success;
 }

And then can be used like this:

 QPoint point(-10, 100);
 QDBusMessage methodCall; // assume created by QBusMessage::methodCall()

 QDBusData dbusData;
 if (QDBusDataConverter::convertToQDBusData<QPoint>(point, dbusData) != QDBusDataConverter::Success)
 {
     // error handling
 }
 else
 {
     methodCall << dbusData;
 }
Parameters:
typeData the native type instance to get the content from
dbusData the binding's data instance to put the content into
Returns:
the conversion result value

The documentation for this class was generated from the following file:

Generated by  doxygen 1.6.2