~lidaobing/ibus/ibus-qt

« back to all changes in this revision

Viewing changes to src/qibusserializable.h

  • Committer: LI Daobing
  • Date: 2009-07-28 14:30:44 UTC
  • Revision ID: lidaobing@gmail.com-20090728143044-2swzp2fwuu7p8xae
importĀ 1.2.0.20090728

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef __Q_IBUS_SERIALIZABLE_H_
 
2
#define __Q_IBUS_SERIALIZABLE_H_
 
3
 
 
4
#include "qibusobject.h"
 
5
#include <QDBusArgument>
 
6
#include <QHash>
 
7
#include <QMap>
 
8
 
 
9
#define INIT_PRIO_HIGH __attribute__((init_priority(1000)))
 
10
// #define INIT_PRIO_LOW  __attribute__((init_priority(2000)))
 
11
#define INIT_PRIO_LOW
 
12
 
 
13
#define IBUS_SERIALIZABLE                               \
 
14
public:                                                 \
 
15
    static Serializable *newInstance (void);            \
 
16
    static MetaTypeInfo staticMetaTypeInfo;             \
 
17
    virtual const MetaTypeInfo *metaTypeInfo (void) const;
 
18
 
 
19
#define IBUS_DECLARE_SERIALIZABLE(classname, name)      \
 
20
    Serializable *                                      \
 
21
    classname::newInstance (void)                       \
 
22
    {                                                   \
 
23
        return (Serializable *) new classname ();       \
 
24
    }                                                   \
 
25
    const Serializable::MetaTypeInfo *                  \
 
26
    classname::metaTypeInfo (void) const                \
 
27
    {                                                   \
 
28
        return & (classname::staticMetaTypeInfo);       \
 
29
    }                                                   \
 
30
    Serializable::MetaTypeInfo                          \
 
31
    classname::staticMetaTypeInfo INIT_PRIO_LOW (QString(#name), classname::newInstance);
 
32
 
 
33
namespace IBus {
 
34
 
 
35
class Serializable;
 
36
typedef Pointer<Serializable> SerializablePointer;
 
37
 
 
38
QDBusVariant qDBusVariantFromSerializable (const SerializablePointer &p);
 
39
SerializablePointer qDBusVariantToSerializable (const QDBusVariant &variant);
 
40
 
 
41
class Serializable : public Object
 
42
{
 
43
    Q_OBJECT;
 
44
 
 
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);
 
47
 
 
48
    typedef Serializable * (NewInstanceFunc) (void);
 
49
 
 
50
protected:
 
51
    class MetaTypeInfo
 
52
    {
 
53
    public:
 
54
        MetaTypeInfo(const QString &name, NewInstanceFunc _new) : m_className (name) {
 
55
            Serializable::registerObject (m_className, _new);
 
56
        }
 
57
        ~MetaTypeInfo (void) {
 
58
           Serializable::unregisterObject (m_className);
 
59
        }
 
60
        const QString &className (void) const {
 
61
            return m_className;
 
62
        }
 
63
    private:
 
64
        QString m_className;
 
65
    };
 
66
 
 
67
public:
 
68
    Serializable () {}
 
69
    void setAttachment (const QString &key, const SerializablePointer &value);
 
70
    SerializablePointer getAttachment (const QString &key) const;
 
71
    SerializablePointer removeAttachment (const QString &key);
 
72
 
 
73
protected:
 
74
    virtual bool serialize (QDBusArgument &argument) const;
 
75
    virtual bool deserialize (const QDBusArgument &argument);
 
76
 
 
77
private:
 
78
   QMap <QString, SerializablePointer> m_attachments;
 
79
 
 
80
/* static */
 
81
protected:
 
82
    static void registerObject (const QString &name, NewInstanceFunc _new);
 
83
    static void unregisterObject (const QString &name);
 
84
 
 
85
private:
 
86
    static SerializablePointer createInstance (const QString &name);
 
87
    static QHash<QString, NewInstanceFunc *> type_table;
 
88
 
 
89
    IBUS_SERIALIZABLE
 
90
};
 
91
 
 
92
template<typename T>
 
93
QDBusArgument& operator<< (QDBusArgument& argument, const Pointer<T> &p)
 
94
{
 
95
    argument.beginStructure ();
 
96
    argument << p->metaTypeInfo ()->className ();
 
97
    p->serialize (argument);
 
98
    argument.endStructure ();
 
99
 
 
100
    return argument;
 
101
}
 
102
 
 
103
template<typename T>
 
104
const QDBusArgument& operator>> (const QDBusArgument& argument, Pointer<T> &p)
 
105
{
 
106
    Q_ASSERT ((argument.currentType () == QDBusArgument::VariantType) ||
 
107
              (argument.currentType () == QDBusArgument::StructureType));
 
108
 
 
109
    if (argument.currentType () == QDBusArgument::VariantType) {
 
110
        QDBusVariant v;
 
111
        argument >> v;
 
112
        p = qDBusVariantToSerializable (v);
 
113
        return argument;
 
114
    }
 
115
 
 
116
    if (argument.currentType () == QDBusArgument::StructureType) {
 
117
        QString name;
 
118
        argument.beginStructure ();
 
119
        argument >> name;
 
120
        p = Serializable::createInstance (name);
 
121
        if (!p.isNull () && !p->deserialize (argument)) {
 
122
            p = NULL;
 
123
        }
 
124
        argument.endStructure ();
 
125
        return argument;
 
126
    }
 
127
 
 
128
    return argument;
 
129
}
 
130
 
 
131
};
 
132
 
 
133
Q_DECLARE_METATYPE (IBus::SerializablePointer);
 
134
 
 
135
#endif