~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/corelib/kernel/qmetatype.h

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the core module of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#ifndef QMETATYPE_H
 
30
#define QMETATYPE_H
 
31
 
 
32
#include "QtCore/qdatastream.h"
 
33
 
 
34
#ifdef Bool
 
35
#error qmetatype.h must be included before any header file that define Bool
 
36
#endif
 
37
 
 
38
class Q_CORE_EXPORT QMetaType {
 
39
public:
 
40
    enum Type {
 
41
        // these are merged with QVariant
 
42
        Void = 0, Bool = 1, Int = 2, UInt = 3, Double = 6, QChar = 7,
 
43
        QString = 10, QByteArray = 12,
 
44
 
 
45
        VoidStar = 128, Long, Short, Char, ULong,
 
46
        UShort, UChar, Float, QObjectStar, QWidgetStar,
 
47
        User = 256
 
48
    };
 
49
 
 
50
    typedef void (*Destructor)(void *);
 
51
    typedef void *(*Constructor)(const void *);
 
52
    typedef void (*SaveOperator)(QDataStream &, const void *);
 
53
    typedef void (*LoadOperator)(QDataStream &, void *);
 
54
 
 
55
    static int registerType(const char *typeName, Destructor destructor,
 
56
                            Constructor constructor);
 
57
    static void registerStreamOperators(const char *typeName, SaveOperator saveOp,
 
58
                                        LoadOperator loadOp);
 
59
    static int type(const char *typeName);
 
60
    static const char *typeName(int type);
 
61
    static bool isRegistered(int type);
 
62
    static void *construct(int type, const void *copy);
 
63
    static void destroy(int type, void *data);
 
64
    static bool save(QDataStream &stream, int type, const void *data);
 
65
    static bool load(QDataStream &stream, int type, void *data);
 
66
};
 
67
 
 
68
template <typename T>
 
69
void qMetaTypeDeleteHelper(T *t)
 
70
{
 
71
    delete t;
 
72
}
 
73
 
 
74
template <typename T>
 
75
void *qMetaTypeConstructHelper(const T *t)
 
76
{
 
77
    if (!t)
 
78
        return new T;
 
79
    return new T(*static_cast<const T*>(t));
 
80
}
 
81
 
 
82
template <typename T>
 
83
void qMetaTypeSaveHelper(QDataStream &stream, const T *t)
 
84
{
 
85
    stream << *t;
 
86
}
 
87
 
 
88
template <typename T>
 
89
void qMetaTypeLoadHelper(QDataStream &stream, T *t)
 
90
{
 
91
    stream >> *t;
 
92
}
 
93
 
 
94
template <typename T>
 
95
int qRegisterMetaType(const char *typeName, T * = 0)
 
96
{
 
97
    typedef void*(*ConstructPtr)(const T*);
 
98
    ConstructPtr cptr = qMetaTypeConstructHelper<T>;
 
99
    typedef void(*DeletePtr)(T*);
 
100
    DeletePtr dptr = qMetaTypeDeleteHelper<T>;
 
101
 
 
102
    return QMetaType::registerType(typeName, reinterpret_cast<QMetaType::Destructor>(dptr),
 
103
                                   reinterpret_cast<QMetaType::Constructor>(cptr));
 
104
}
 
105
 
 
106
template <typename T>
 
107
void qRegisterMetaTypeStreamOperators(const char *typeName, T * = 0)
 
108
{
 
109
    typedef void(*SavePtr)(QDataStream &, const T *);
 
110
    typedef void(*LoadPtr)(QDataStream &, T *);
 
111
    SavePtr sptr = qMetaTypeSaveHelper<T>;
 
112
    LoadPtr lptr = qMetaTypeLoadHelper<T>;
 
113
 
 
114
    QMetaType::registerStreamOperators(typeName, reinterpret_cast<QMetaType::SaveOperator>(sptr),
 
115
                                       reinterpret_cast<QMetaType::LoadOperator>(lptr));
 
116
}
 
117
 
 
118
template <typename T>
 
119
struct QMetaTypeId
 
120
{
 
121
};
 
122
 
 
123
#define Q_DECLARE_METATYPE(TYPE) \
 
124
template <> \
 
125
struct QMetaTypeId<TYPE> \
 
126
{ \
 
127
    static int qt_metatype_id() \
 
128
    { \
 
129
       static int id = qRegisterMetaType<TYPE>(#TYPE); \
 
130
       return id; \
 
131
    } \
 
132
};
 
133
 
 
134
template<> struct QMetaTypeId<QString>
 
135
{ static inline int qt_metatype_id() { return QMetaType::QString; } };
 
136
template<> struct QMetaTypeId<int>
 
137
{ static inline int qt_metatype_id() { return QMetaType::Int; } };
 
138
template<> struct QMetaTypeId<uint>
 
139
{ static inline int qt_metatype_id() { return QMetaType::UInt; } };
 
140
template<> struct QMetaTypeId<bool>
 
141
{ static inline int qt_metatype_id() { return QMetaType::Bool; } };
 
142
template<> struct QMetaTypeId<double>
 
143
{ static inline int qt_metatype_id() { return QMetaType::Double; } };
 
144
template<> struct QMetaTypeId<QByteArray>
 
145
{ static inline int qt_metatype_id() { return QMetaType::QByteArray; } };
 
146
template<> struct QMetaTypeId<QChar>
 
147
{ static inline int qt_metatype_id() { return QMetaType::QChar; } };
 
148
template<> struct QMetaTypeId<void>
 
149
{ static inline int qt_metatype_id() { return QMetaType::VoidStar; } };
 
150
template<> struct QMetaTypeId<long>
 
151
{ static inline int qt_metatype_id() { return QMetaType::Long; } };
 
152
template<> struct QMetaTypeId<short>
 
153
{ static inline int qt_metatype_id() { return QMetaType::Short; } };
 
154
template<> struct QMetaTypeId<char>
 
155
{ static inline int qt_metatype_id() { return QMetaType::Char; } };
 
156
template<> struct QMetaTypeId<ulong>
 
157
{ static inline int qt_metatype_id() { return QMetaType::ULong; } };
 
158
template<> struct QMetaTypeId<ushort>
 
159
{ static inline int qt_metatype_id() { return QMetaType::UShort; } };
 
160
template<> struct QMetaTypeId<uchar>
 
161
{ static inline int qt_metatype_id() { return QMetaType::UChar; } };
 
162
template<> struct QMetaTypeId<float>
 
163
{ static inline int qt_metatype_id() { return QMetaType::Float; } };
 
164
class QObject;
 
165
template<> struct QMetaTypeId<QObject *>
 
166
{ static inline int qt_metatype_id() { return QMetaType::QObjectStar; } };
 
167
class QWidget;
 
168
template<> struct QMetaTypeId<QWidget *>
 
169
{ static inline int qt_metatype_id() { return QMetaType::QWidgetStar; } };
 
170
 
 
171
#endif // QMETATYPE_H