aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/qml/ftw/qfastmetabuilder_p.h
blob: 16f7b6e7498c07375f3f6b8d6d139afa2bb5aaad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtQml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QFASTMETABUILDER_P_H
#define QFASTMETABUILDER_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists for the convenience
// of moc.  This header file may change from version to version without notice,
// or even be removed.
//
// We mean it.
//

#include <QtCore/qglobal.h>
#include <QtCore/qmetatype.h>
#include <QtCore/qmetaobject.h>

#include <private/qhashedstring_p.h>

QT_BEGIN_NAMESPACE

struct QMetaObject;
class QFastMetaBuilder
{
public:
    QFastMetaBuilder();
    ~QFastMetaBuilder();

    struct StringRef {
    public:
        inline StringRef();
        inline StringRef(const StringRef &);
        inline StringRef &operator=(const StringRef &);

        inline void load(const QHashedStringRef &);
        inline void load(const QByteArray &);
        inline void load(const char *);

        inline bool isEmpty() const;
        inline QFastMetaBuilder *builder() const;
        inline int index() const;
        inline char *data();
        inline int length() const;
        inline void loadByteArrayData();
    private:
        friend class QFastMetaBuilder;

        QFastMetaBuilder *_b;
        int _i;
        int _o;
        int _l;
    };
    StringRef newString(int length);

    // Returns class name
    StringRef init(int classNameLength,
                   int propertyCount, int methodCount, 
                   int signalCount, int classInfoCount,
                   int paramDataSize, int *paramIndex);

    void setClassInfo(int index, const StringRef &key, const StringRef &value);

    enum PropertyFlag {
        None = 0x00000000,
        Writable = 0x00000002,
        Resettable = 0x00000004,
        Constant = 0x00000400,
        Final = 0x00000800
    };
    void setProperty(int index, const StringRef &name, int type,
                     PropertyFlag flags, int notifySignal = -1);
    void setMethod(int index, const StringRef &name, int paramIndex, int argc = 0,
                   const int *types = 0, const StringRef *parameterNames = 0,
                   QMetaType::Type type = QMetaType::Void);
    void setSignal(int index, const StringRef &name, int paramIndex, int argc = 0,
                   const int *types = 0, const StringRef *parameterNames = 0,
                   QMetaType::Type type = QMetaType::Void);

    int metaObjectIndexForSignal(int) const;
    int metaObjectIndexForMethod(int) const;

    QByteArray toData() const {
        if (m_stringCountLoaded == m_stringCount - 1) {
            // zero-string is lazily loaded last
            const_cast<StringRef &>(m_zeroString).loadByteArrayData();
        }
        Q_ASSERT(m_stringCountLoaded == m_stringCount);
        return m_data;
    }
    static void fromData(QMetaObject *, const QMetaObject *parent, const QByteArray &);
private:
    friend struct StringRef;

    QByteArray m_data;
    StringRef m_zeroString;

    void allocateStringData();
    QByteArrayData *m_stringData;
    int m_stringCount;
    int m_stringDataLength;
    int m_stringCountAllocated;
    int m_stringCountLoaded;
};

QFastMetaBuilder::StringRef::StringRef()
: _b(0), _i(0), _o(0), _l(0)
{
}

QFastMetaBuilder::StringRef::StringRef(const StringRef &o)
: _b(o._b), _i(o._i), _o(o._o), _l(o._l)
{
}

QFastMetaBuilder::StringRef &QFastMetaBuilder::StringRef::operator=(const StringRef &o)
{
    _b = o._b;
    _i = o._i;
    _o = o._o;
    _l = o._l;
    return *this;
}

bool QFastMetaBuilder::StringRef::isEmpty() const
{
    return _l == 0;
}

QFastMetaBuilder *QFastMetaBuilder::StringRef::builder() const
{
    return _b;
}

int QFastMetaBuilder::StringRef::index() const
{
    return _i;
}

char *QFastMetaBuilder::StringRef::data()
{
    Q_ASSERT(_b);
    if (_b->m_stringCountAllocated < _b->m_stringCount)
         _b->allocateStringData();
    return reinterpret_cast<char *>(&_b->m_stringData[_b->m_stringCount]) + _o;
}

int QFastMetaBuilder::StringRef::length() const
{
    return _l;
}

void QFastMetaBuilder::StringRef::load(const QHashedStringRef &str)
{
    Q_ASSERT(str.utf8length() == _l);
    str.writeUtf8(data());
    *(data() + _l) = 0;
    loadByteArrayData();
}

void QFastMetaBuilder::StringRef::load(const QByteArray &str)
{
    Q_ASSERT(str.length() == _l);
    strcpy(data(), str.constData());
    loadByteArrayData();
}

void QFastMetaBuilder::StringRef::load(const char *str)
{
    Q_ASSERT(strlen(str) == (uint)_l);
    strcpy(data(), str);
    loadByteArrayData();
}

void QFastMetaBuilder::StringRef::loadByteArrayData()
{
    if (_b->m_stringCountAllocated < _b->m_stringCount)
         _b->allocateStringData();
    Q_ASSERT(_b->m_stringCountLoaded < _b->m_stringCount);

    int offsetofCstrings = _b->m_stringCount * sizeof(QByteArrayData);
    qptrdiff offset = offsetofCstrings + _o - _i * sizeof(QByteArrayData);

    const QByteArrayData bad = { Q_REFCOUNT_INITIALIZE_STATIC, _l, 0, 0, offset };
    memcpy(&_b->m_stringData[_i], &bad, sizeof(QByteArrayData));

    ++_b->m_stringCountLoaded;
}

QT_END_NAMESPACE

#endif // QFASTMETABUILDER_P_H