1
#ifndef __Q_IBUS_SERIALIZABLE_H_
2
#define __Q_IBUS_SERIALIZABLE_H_
4
#include "qibusobject.h"
5
#include <QDBusArgument>
9
#define INIT_PRIO_HIGH __attribute__((init_priority(1000)))
10
// #define INIT_PRIO_LOW __attribute__((init_priority(2000)))
13
#define IBUS_SERIALIZABLE \
15
static Serializable *newInstance (void); \
16
static MetaTypeInfo staticMetaTypeInfo; \
17
virtual const MetaTypeInfo *metaTypeInfo (void) const;
19
#define IBUS_DECLARE_SERIALIZABLE(classname, name) \
21
classname::newInstance (void) \
23
return (Serializable *) new classname (); \
25
const Serializable::MetaTypeInfo * \
26
classname::metaTypeInfo (void) const \
28
return & (classname::staticMetaTypeInfo); \
30
Serializable::MetaTypeInfo \
31
classname::staticMetaTypeInfo INIT_PRIO_LOW (QString(#name), classname::newInstance);
36
typedef Pointer<Serializable> SerializablePointer;
38
QDBusVariant qDBusVariantFromSerializable (const SerializablePointer &p);
39
SerializablePointer qDBusVariantToSerializable (const QDBusVariant &variant);
41
class Serializable : public Object
45
template<typename T> friend QDBusArgument& operator<< (QDBusArgument& argument, const Pointer<T> &p);
46
template<typename T> friend const QDBusArgument& operator>> (const QDBusArgument& argument, Pointer<T> &p);
48
typedef Serializable * (NewInstanceFunc) (void);
54
MetaTypeInfo(const QString &name, NewInstanceFunc _new) : m_className (name) {
55
Serializable::registerObject (m_className, _new);
57
~MetaTypeInfo (void) {
58
Serializable::unregisterObject (m_className);
60
const QString &className (void) const {
69
void setAttachment (const QString &key, const SerializablePointer &value);
70
SerializablePointer getAttachment (const QString &key) const;
71
SerializablePointer removeAttachment (const QString &key);
74
virtual bool serialize (QDBusArgument &argument) const;
75
virtual bool deserialize (const QDBusArgument &argument);
78
QMap <QString, SerializablePointer> m_attachments;
82
static void registerObject (const QString &name, NewInstanceFunc _new);
83
static void unregisterObject (const QString &name);
86
static SerializablePointer createInstance (const QString &name);
87
static QHash<QString, NewInstanceFunc *> type_table;
93
QDBusArgument& operator<< (QDBusArgument& argument, const Pointer<T> &p)
95
argument.beginStructure ();
96
argument << p->metaTypeInfo ()->className ();
97
p->serialize (argument);
98
argument.endStructure ();
104
const QDBusArgument& operator>> (const QDBusArgument& argument, Pointer<T> &p)
106
Q_ASSERT ((argument.currentType () == QDBusArgument::VariantType) ||
107
(argument.currentType () == QDBusArgument::StructureType));
109
if (argument.currentType () == QDBusArgument::VariantType) {
112
p = qDBusVariantToSerializable (v);
116
if (argument.currentType () == QDBusArgument::StructureType) {
118
argument.beginStructure ();
120
p = Serializable::createInstance (name);
121
if (!p.isNull () && !p->deserialize (argument)) {
124
argument.endStructure ();
133
Q_DECLARE_METATYPE (IBus::SerializablePointer);