Fix wrong assertion in StringRef::load.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / ftw / qfastmetabuilder_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QFASTMETABUILDER_P_H
43 #define QFASTMETABUILDER_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists for the convenience
50 // of moc.  This header file may change from version to version without notice,
51 // or even be removed.
52 //
53 // We mean it.
54 //
55
56 #include <QtCore/qglobal.h>
57 #include <QtCore/qmetatype.h>
58
59 #include <private/qhashedstring_p.h>
60
61 QT_BEGIN_NAMESPACE
62
63 class QFastMetaBuilder
64 {
65 public:
66     QFastMetaBuilder();
67     ~QFastMetaBuilder();
68
69     struct StringRef {
70     public:
71         inline StringRef();
72         inline StringRef(const StringRef &);
73         inline StringRef &operator=(const StringRef &);
74
75         inline void load(const QHashedStringRef &);
76         inline void load(const QByteArray &);
77         inline void load(const char *);
78
79         inline bool isEmpty() const;
80         inline QFastMetaBuilder *builder() const;
81         inline int offset() const;
82         inline char *data();
83         inline int length() const;
84     private:
85         friend class QFastMetaBuilder;
86
87         QFastMetaBuilder *_b;
88         int _o;
89         int _l;
90     };
91     StringRef newString(int length);
92
93     // Returns class name
94     StringRef init(int classNameLength,
95                    int propertyCount, int methodCount, 
96                    int signalCount, int classInfoCount);
97
98     void setClassInfo(int index, const StringRef &key, const StringRef &value);
99
100     enum PropertyFlag {
101         None = 0x00000000,
102         Writable = 0x00000002,
103         Resettable = 0x00000004,
104         Constant = 0x00000400,
105         Final = 0x00000800
106     };
107     // void setProperty(int index, const StringRef &name, QMetaType::Type type, int notifySignal = -1);
108     void setProperty(int index, const StringRef &name, const StringRef &type, 
109                      QMetaType::Type mtype, PropertyFlag flags, int notifySignal = -1);
110     void setProperty(int index, const StringRef &name, const StringRef &type, 
111                      PropertyFlag flags, int notifySignal = -1);
112     void setMethod(int index, const StringRef &signature,
113                    const StringRef &parameterNames = StringRef(), 
114                    const StringRef &type = StringRef());
115     void setSignal(int index, const StringRef &signature, 
116                    const StringRef &parameterNames = StringRef(), 
117                    const StringRef &type = StringRef());
118
119     int metaObjectIndexForSignal(int) const;
120     int metaObjectIndexForMethod(int) const;
121
122     QByteArray toData() const { return m_data; }
123     static void fromData(QMetaObject *, const QMetaObject *parent, const QByteArray &);
124 private:
125     friend class StringRef;
126
127     QByteArray m_data;
128     int m_zeroPtr;
129
130     void allocateStringData();
131     char *m_stringData;
132     int m_stringDataLength;
133     int m_stringDataAllocated;
134 };
135
136 QFastMetaBuilder::StringRef::StringRef()
137 : _b(0), _o(0), _l(0)
138 {
139 }
140
141 QFastMetaBuilder::StringRef::StringRef(const StringRef &o)
142 : _b(o._b), _o(o._o), _l(o._l)
143 {
144 }
145
146 QFastMetaBuilder::StringRef &QFastMetaBuilder::StringRef::operator=(const StringRef &o)
147 {
148     _b = o._b;
149     _o = o._o;
150     _l = o._l;
151     return *this;
152 }
153
154 bool QFastMetaBuilder::StringRef::isEmpty() const
155 {
156     return _l == 0;
157 }
158
159 QFastMetaBuilder *QFastMetaBuilder::StringRef::builder() const
160 {
161     return _b;
162 }
163
164 int QFastMetaBuilder::StringRef::offset() const
165 {
166     return _o;
167 }
168
169 char *QFastMetaBuilder::StringRef::data()
170 {
171     Q_ASSERT(_b);
172     if (_b->m_stringDataLength != _b->m_stringDataAllocated)
173         _b->allocateStringData();
174     return _b->m_stringData + _o;
175 }
176
177 int QFastMetaBuilder::StringRef::length() const
178 {
179     return _l;
180 }
181
182 void QFastMetaBuilder::StringRef::load(const QHashedStringRef &str)
183 {
184     Q_ASSERT(str.utf8length() == _l);
185     str.writeUtf8(data());
186     *(data() + _l) = 0;
187 }
188
189 void QFastMetaBuilder::StringRef::load(const QByteArray &str)
190 {
191     Q_ASSERT(str.length() == _l);
192     strcpy(data(), str.constData());
193 }
194
195 void QFastMetaBuilder::StringRef::load(const char *str)
196 {
197     Q_ASSERT(strlen(str) == (uint)_l);
198     strcpy(data(), str);
199 }
200
201 QT_END_NAMESPACE
202
203 #endif // QFASTMETABUILDER_P_H
204