Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qmetaobjectbuilder.cpp
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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "private/qmetaobjectbuilder_p.h"
43
44 QT_BEGIN_NAMESPACE
45
46 /*!
47     \class QMetaObjectBuilder
48     \internal
49     \brief The QMetaObjectBuilder class supports building QMetaObject objects at runtime.
50
51 */
52
53 /*!
54     \enum QMetaObjectBuilder::AddMember
55     This enum defines which members of QMetaObject should be copied by QMetaObjectBuilder::addMetaObject()
56
57     \value ClassName Add the class name.
58     \value SuperClass Add the super class.
59     \value Methods Add methods that aren't signals or slots.
60     \value Signals Add signals.
61     \value Slots Add slots.
62     \value Constructors Add constructors.
63     \value Properties Add properties.
64     \value Enumerators Add enumerators.
65     \value ClassInfos Add items of class information.
66     \value RelatedMetaObjects Add related meta objects.
67     \value StaticMetacall Add the static metacall function.
68     \value PublicMethods Add public methods (ignored for signals).
69     \value ProtectedMethods Add protected methods (ignored for signals).
70     \value PrivateMethods All private methods (ignored for signals).
71     \value AllMembers Add all members.
72     \value AllPrimaryMembers Add everything except the class name, super class, and static metacall function.
73 */
74
75 // copied from moc's generator.cpp
76 uint qvariant_nameToType(const char* name)
77 {
78     if (!name)
79         return 0;
80
81     if (strcmp(name, "QVariant") == 0)
82         return 0xffffffff;
83     if (strcmp(name, "QCString") == 0)
84         return QMetaType::QByteArray;
85     if (strcmp(name, "Q_LLONG") == 0)
86         return QMetaType::LongLong;
87     if (strcmp(name, "Q_ULLONG") == 0)
88         return QMetaType::ULongLong;
89     if (strcmp(name, "QIconSet") == 0)
90         return QMetaType::QIcon;
91
92     uint tp = QMetaType::type(name);
93     return tp < QMetaType::User ? tp : 0;
94 }
95
96 /*
97   Returns true if the type is a QVariant types.
98 */
99 bool isVariantType(const char* type)
100 {
101     return qvariant_nameToType(type) != 0;
102 }
103
104 // copied from qmetaobject.cpp
105 // do not touch without touching the moc as well
106 enum PropertyFlags  {
107     Invalid = 0x00000000,
108     Readable = 0x00000001,
109     Writable = 0x00000002,
110     Resettable = 0x00000004,
111     EnumOrFlag = 0x00000008,
112     StdCppSet = 0x00000100,
113 //    Override = 0x00000200,
114     Designable = 0x00001000,
115     ResolveDesignable = 0x00002000,
116     Scriptable = 0x00004000,
117     ResolveScriptable = 0x00008000,
118     Stored = 0x00010000,
119     ResolveStored = 0x00020000,
120     Editable = 0x00040000,
121     ResolveEditable = 0x00080000,
122     User = 0x00100000,
123     ResolveUser = 0x00200000,
124     Notify = 0x00400000,
125     Revisioned = 0x00800000
126 };
127
128 enum MethodFlags  {
129     AccessPrivate = 0x00,
130     AccessProtected = 0x01,
131     AccessPublic = 0x02,
132     AccessMask = 0x03, //mask
133
134     MethodMethod = 0x00,
135     MethodSignal = 0x04,
136     MethodSlot = 0x08,
137     MethodConstructor = 0x0c,
138     MethodTypeMask = 0x0c,
139
140     MethodCompatibility = 0x10,
141     MethodCloned = 0x20,
142     MethodScriptable = 0x40,
143     MethodRevisioned = 0x80
144 };
145
146 struct QMetaObjectPrivate
147 {
148     int revision;
149     int className;
150     int classInfoCount, classInfoData;
151     int methodCount, methodData;
152     int propertyCount, propertyData;
153     int enumeratorCount, enumeratorData;
154     int constructorCount, constructorData;
155     int flags;
156 };
157
158 static inline const QMetaObjectPrivate *priv(const uint* data)
159 { return reinterpret_cast<const QMetaObjectPrivate*>(data); }
160 // end of copied lines from qmetaobject.cpp
161
162 class QMetaMethodBuilderPrivate
163 {
164 public:
165     QMetaMethodBuilderPrivate
166             (QMetaMethod::MethodType _methodType,
167              const QByteArray& _signature,
168              const QByteArray& _returnType = QByteArray(),
169              QMetaMethod::Access _access = QMetaMethod::Public)
170         : signature(QMetaObject::normalizedSignature(_signature.constData())),
171           returnType(QMetaObject::normalizedType(_returnType)),
172           attributes(((int)_access) | (((int)_methodType) << 2))
173     {
174     }
175
176     QByteArray signature;
177     QByteArray returnType;
178     QList<QByteArray> parameterNames;
179     QByteArray tag;
180     int attributes;
181
182     QMetaMethod::MethodType methodType() const
183     {
184         return (QMetaMethod::MethodType)((attributes & MethodTypeMask) >> 2);
185     }
186
187     QMetaMethod::Access access() const
188     {
189         return (QMetaMethod::Access)(attributes & AccessMask);
190     }
191
192     void setAccess(QMetaMethod::Access value)
193     {
194         attributes = ((attributes & ~AccessMask) | (int)value);
195     }
196 };
197
198 class QMetaPropertyBuilderPrivate
199 {
200 public:
201     QMetaPropertyBuilderPrivate
202             (const QByteArray& _name, const QByteArray& _type, int notifierIdx=-1)
203         : name(_name),
204           type(QMetaObject::normalizedType(_type.constData())),
205           flags(Readable | Writable | Scriptable), notifySignal(-1)
206     {
207         if (notifierIdx >= 0) {
208             flags |= Notify;
209             notifySignal = notifierIdx;
210         }
211     }
212
213     QByteArray name;
214     QByteArray type;
215     int flags;
216     int notifySignal;
217
218     bool flag(int f) const
219     {
220         return ((flags & f) != 0);
221     }
222
223     void setFlag(int f, bool value)
224     {
225         if (value)
226             flags |= f;
227         else
228             flags &= ~f;
229     }
230 };
231
232 class QMetaEnumBuilderPrivate
233 {
234 public:
235     QMetaEnumBuilderPrivate(const QByteArray& _name)
236         : name(_name), isFlag(false)
237     {
238     }
239
240     QByteArray name;
241     bool isFlag;
242     QList<QByteArray> keys;
243     QList<int> values;
244 };
245
246 class QMetaObjectBuilderPrivate
247 {
248 public:
249     QMetaObjectBuilderPrivate()
250         : flags(0)
251     {
252         superClass = &QObject::staticMetaObject;
253         staticMetacallFunction = 0;
254     }
255
256     QByteArray className;
257     const QMetaObject *superClass;
258     QMetaObjectBuilder::StaticMetacallFunction staticMetacallFunction;
259     QList<QMetaMethodBuilderPrivate> methods;
260     QList<QMetaMethodBuilderPrivate> constructors;
261     QList<QMetaPropertyBuilderPrivate> properties;
262     QList<QByteArray> classInfoNames;
263     QList<QByteArray> classInfoValues;
264     QList<QMetaEnumBuilderPrivate> enumerators;
265 #ifdef Q_NO_DATA_RELOCATION
266     QList<QMetaObjectAccessor> relatedMetaObjects;
267 #else
268     QList<const QMetaObject *> relatedMetaObjects;
269 #endif
270     int flags;
271 };
272
273 /*!
274     Constructs a new QMetaObjectBuilder.
275 */
276 QMetaObjectBuilder::QMetaObjectBuilder()
277 {
278     d = new QMetaObjectBuilderPrivate();
279 }
280
281 /*!
282     Constructs a new QMetaObjectBuilder which is a copy of the
283     meta object information in \a prototype.  Note: the super class
284     contents for \a prototype are not copied, only the immediate
285     class that is defined by \a prototype.
286
287     The \a members parameter indicates which members of \a prototype
288     should be added.  The default is AllMembers.
289
290     \sa addMetaObject()
291 */
292 QMetaObjectBuilder::QMetaObjectBuilder
293     (const QMetaObject *prototype, QMetaObjectBuilder::AddMembers members)
294 {
295     d = new QMetaObjectBuilderPrivate();
296     addMetaObject(prototype, members);
297 }
298
299 /*!
300     Destroys this meta object builder.
301 */
302 QMetaObjectBuilder::~QMetaObjectBuilder()
303 {
304     delete d;
305 }
306
307 /*!
308     Returns the name of the class being constructed by this
309     meta object builder.  The default value is an empty QByteArray.
310
311     \sa setClassName(), superClass()
312 */
313 QByteArray QMetaObjectBuilder::className() const
314 {
315     return d->className;
316 }
317
318 /*!
319     Sets the \a name of the class being constructed by this
320     meta object builder.
321
322     \sa className(), setSuperClass()
323 */
324 void QMetaObjectBuilder::setClassName(const QByteArray& name)
325 {
326     d->className = name;
327 }
328
329 /*!
330     Returns the superclass meta object of the class being constructed
331     by this meta object builder.  The default value is the meta object
332     for QObject.
333
334     \sa setSuperClass(), className()
335 */
336 const QMetaObject *QMetaObjectBuilder::superClass() const
337 {
338     return d->superClass;
339 }
340
341 /*!
342     Sets the superclass meta object of the class being constructed
343     by this meta object builder to \a meta.  The \a meta parameter
344     must not be null.
345
346     \sa superClass(), setClassName()
347 */
348 void QMetaObjectBuilder::setSuperClass(const QMetaObject *meta)
349 {
350     Q_ASSERT(meta);
351     d->superClass = meta;
352 }
353
354 /*!
355     Returns the flags of the class being constructed by this meta object
356     builder.
357
358     \sa setFlags()
359 */
360 QMetaObjectBuilder::MetaObjectFlags QMetaObjectBuilder::flags() const
361 {
362     return (QMetaObjectBuilder::MetaObjectFlags)d->flags;
363 }
364
365 /*!
366     Sets the \a flags of the class being constructed by this meta object
367     builder.
368
369     \sa flags()
370 */
371 void QMetaObjectBuilder::setFlags(MetaObjectFlags flags)
372 {
373     d->flags = flags;
374 }
375
376 /*!
377     Returns the number of methods in this class, excluding the number
378     of methods in the base class.  These include signals and slots
379     as well as normal member functions.
380
381     \sa addMethod(), method(), removeMethod(), indexOfMethod()
382 */
383 int QMetaObjectBuilder::methodCount() const
384 {
385     return d->methods.size();
386 }
387
388 /*!
389     Returns the number of constructors in this class.
390
391     \sa addConstructor(), constructor(), removeConstructor(), indexOfConstructor()
392 */
393 int QMetaObjectBuilder::constructorCount() const
394 {
395     return d->constructors.size();
396 }
397
398 /*!
399     Returns the number of properties in this class, excluding the number
400     of properties in the base class.
401
402     \sa addProperty(), property(), removeProperty(), indexOfProperty()
403 */
404 int QMetaObjectBuilder::propertyCount() const
405 {
406     return d->properties.size();
407 }
408
409 /*!
410     Returns the number of enumerators in this class, excluding the
411     number of enumerators in the base class.
412
413     \sa addEnumerator(), enumerator(), removeEnumerator()
414     \sa indexOfEnumerator()
415 */
416 int QMetaObjectBuilder::enumeratorCount() const
417 {
418     return d->enumerators.size();
419 }
420
421 /*!
422     Returns the number of items of class information in this class,
423     exclusing the number of items of class information in the base class.
424
425     \sa addClassInfo(), classInfoName(), classInfoValue(), removeClassInfo()
426     \sa indexOfClassInfo()
427 */
428 int QMetaObjectBuilder::classInfoCount() const
429 {
430     return d->classInfoNames.size();
431 }
432
433 /*!
434     Returns the number of related meta objects that are associated
435     with this class.
436
437     Related meta objects are used when resolving the enumerated type
438     associated with a property, where the enumerated type is in a
439     different class from the property.
440
441     \sa addRelatedMetaObject(), relatedMetaObject()
442     \sa removeRelatedMetaObject()
443 */
444 int QMetaObjectBuilder::relatedMetaObjectCount() const
445 {
446     return d->relatedMetaObjects.size();
447 }
448
449 /*!
450     Adds a new public method to this class with the specified \a signature.
451     Returns an object that can be used to adjust the other attributes
452     of the method.  The \a signature will be normalized before it is
453     added to the class.
454
455     \sa method(), methodCount(), removeMethod(), indexOfMethod()
456 */
457 QMetaMethodBuilder QMetaObjectBuilder::addMethod(const QByteArray& signature)
458 {
459     int index = d->methods.size();
460     d->methods.append(QMetaMethodBuilderPrivate(QMetaMethod::Method, signature));
461     return QMetaMethodBuilder(this, index);
462 }
463
464 /*!
465     Adds a new public method to this class with the specified
466     \a signature and \a returnType.  Returns an object that can be
467     used to adjust the other attributes of the method.  The \a signature
468     and \a returnType will be normalized before they are added to
469     the class.  If \a returnType is empty, then it indicates that
470     the method has \c{void} as its return type.
471
472     \sa method(), methodCount(), removeMethod(), indexOfMethod()
473 */
474 QMetaMethodBuilder QMetaObjectBuilder::addMethod
475     (const QByteArray& signature, const QByteArray& returnType)
476 {
477     int index = d->methods.size();
478     d->methods.append(QMetaMethodBuilderPrivate
479         (QMetaMethod::Method, signature, returnType));
480     return QMetaMethodBuilder(this, index);
481 }
482
483 /*!
484     Adds a new public method to this class that has the same information as
485     \a prototype.  This is used to clone the methods of an existing
486     QMetaObject.  Returns an object that can be used to adjust the
487     attributes of the method.
488
489     This function will detect if \a prototype is an ordinary method,
490     signal, slot, or constructor and act accordingly.
491
492     \sa method(), methodCount(), removeMethod(), indexOfMethod()
493 */
494 QMetaMethodBuilder QMetaObjectBuilder::addMethod(const QMetaMethod& prototype)
495 {
496     QMetaMethodBuilder method;
497     if (prototype.methodType() == QMetaMethod::Method)
498         method = addMethod(prototype.signature());
499     else if (prototype.methodType() == QMetaMethod::Signal)
500         method = addSignal(prototype.signature());
501     else if (prototype.methodType() == QMetaMethod::Slot)
502         method = addSlot(prototype.signature());
503     else if (prototype.methodType() == QMetaMethod::Constructor)
504         method = addConstructor(prototype.signature());
505     method.setReturnType(prototype.typeName());
506     method.setParameterNames(prototype.parameterNames());
507     method.setTag(prototype.tag());
508     method.setAccess(prototype.access());
509     method.setAttributes(prototype.attributes());
510     return method;
511 }
512
513 /*!
514     Adds a new public slot to this class with the specified \a signature.
515     Returns an object that can be used to adjust the other attributes
516     of the slot.  The \a signature will be normalized before it is
517     added to the class.
518
519     \sa addMethod(), addSignal(), indexOfSlot()
520 */
521 QMetaMethodBuilder QMetaObjectBuilder::addSlot(const QByteArray& signature)
522 {
523     int index = d->methods.size();
524     d->methods.append(QMetaMethodBuilderPrivate(QMetaMethod::Slot, signature));
525     return QMetaMethodBuilder(this, index);
526 }
527
528 /*!
529     Adds a new signal to this class with the specified \a signature.
530     Returns an object that can be used to adjust the other attributes
531     of the signal.  The \a signature will be normalized before it is
532     added to the class.
533
534     \sa addMethod(), addSlot(), indexOfSignal()
535 */
536 QMetaMethodBuilder QMetaObjectBuilder::addSignal(const QByteArray& signature)
537 {
538     int index = d->methods.size();
539     d->methods.append(QMetaMethodBuilderPrivate
540         (QMetaMethod::Signal, signature, QByteArray(), QMetaMethod::Protected));
541     return QMetaMethodBuilder(this, index);
542 }
543
544 /*!
545     Adds a new constructor to this class with the specified \a signature.
546     Returns an object that can be used to adjust the other attributes
547     of the constructor.  The \a signature will be normalized before it is
548     added to the class.
549
550     \sa constructor(), constructorCount(), removeConstructor()
551     \sa indexOfConstructor()
552 */
553 QMetaMethodBuilder QMetaObjectBuilder::addConstructor(const QByteArray& signature)
554 {
555     int index = d->constructors.size();
556     d->constructors.append(QMetaMethodBuilderPrivate(QMetaMethod::Constructor, signature));
557     return QMetaMethodBuilder(this, -(index + 1));
558 }
559
560 /*!
561     Adds a new constructor to this class that has the same information as
562     \a prototype.  This is used to clone the constructors of an existing
563     QMetaObject.  Returns an object that can be used to adjust the
564     attributes of the constructor.
565
566     This function requires that \a prototype be a constructor.
567
568     \sa constructor(), constructorCount(), removeConstructor()
569     \sa indexOfConstructor()
570 */
571 QMetaMethodBuilder QMetaObjectBuilder::addConstructor(const QMetaMethod& prototype)
572 {
573     Q_ASSERT(prototype.methodType() == QMetaMethod::Constructor);
574     QMetaMethodBuilder ctor = addConstructor(prototype.signature());
575     ctor.setReturnType(prototype.typeName());
576     ctor.setParameterNames(prototype.parameterNames());
577     ctor.setTag(prototype.tag());
578     ctor.setAccess(prototype.access());
579     ctor.setAttributes(prototype.attributes());
580     return ctor;
581 }
582
583 /*!
584     Adds a new readable/writable property to this class with the
585     specified \a name and \a type.  Returns an object that can be used
586     to adjust the other attributes of the property.  The \a type will
587     be normalized before it is added to the class. \a notifierId will
588     be registered as the property's \e notify signal.
589
590     \sa property(), propertyCount(), removeProperty(), indexOfProperty()
591 */
592 QMetaPropertyBuilder QMetaObjectBuilder::addProperty
593     (const QByteArray& name, const QByteArray& type, int notifierId)
594 {
595     int index = d->properties.size();
596     d->properties.append(QMetaPropertyBuilderPrivate(name, type, notifierId));
597     return QMetaPropertyBuilder(this, index);
598 }
599
600 /*!
601     Adds a new property to this class that has the same information as
602     \a prototype.  This is used to clone the properties of an existing
603     QMetaObject.  Returns an object that can be used to adjust the
604     attributes of the property.
605
606     \sa property(), propertyCount(), removeProperty(), indexOfProperty()
607 */
608 QMetaPropertyBuilder QMetaObjectBuilder::addProperty(const QMetaProperty& prototype)
609 {
610     QMetaPropertyBuilder property = addProperty(prototype.name(), prototype.typeName());
611     property.setReadable(prototype.isReadable());
612     property.setWritable(prototype.isWritable());
613     property.setResettable(prototype.isResettable());
614     property.setDesignable(prototype.isDesignable());
615     property.setScriptable(prototype.isScriptable());
616     property.setStored(prototype.isStored());
617     property.setEditable(prototype.isEditable());
618     property.setUser(prototype.isUser());
619     property.setStdCppSet(prototype.hasStdCppSet());
620     property.setEnumOrFlag(prototype.isEnumType());
621     if (prototype.hasNotifySignal()) {
622         // Find an existing method for the notify signal, or add a new one.
623         QMetaMethod method = prototype.notifySignal();
624         int index = indexOfMethod(method.signature());
625         if (index == -1)
626             index = addMethod(method).index();
627         d->properties[property._index].notifySignal = index;
628         d->properties[property._index].setFlag(Notify, true);
629     }
630     return property;
631 }
632
633 /*!
634     Adds a new enumerator to this class with the specified
635     \a name.  Returns an object that can be used to adjust
636     the other attributes of the enumerator.
637
638     \sa enumerator(), enumeratorCount(), removeEnumerator(),
639     \sa indexOfEnumerator()
640 */
641 QMetaEnumBuilder QMetaObjectBuilder::addEnumerator(const QByteArray& name)
642 {
643     int index = d->enumerators.size();
644     d->enumerators.append(QMetaEnumBuilderPrivate(name));
645     return QMetaEnumBuilder(this, index);
646 }
647
648 /*!
649     Adds a new enumerator to this class that has the same information as
650     \a prototype.  This is used to clone the enumerators of an existing
651     QMetaObject.  Returns an object that can be used to adjust the
652     attributes of the enumerator.
653
654     \sa enumerator(), enumeratorCount(), removeEnumerator(),
655     \sa indexOfEnumerator()
656 */
657 QMetaEnumBuilder QMetaObjectBuilder::addEnumerator(const QMetaEnum& prototype)
658 {
659     QMetaEnumBuilder en = addEnumerator(prototype.name());
660     en.setIsFlag(prototype.isFlag());
661     int count = prototype.keyCount();
662     for (int index = 0; index < count; ++index)
663         en.addKey(prototype.key(index), prototype.value(index));
664     return en;
665 }
666
667 /*!
668     Adds \a name and \a value as an item of class information to this class.
669     Returns the index of the new item of class information.
670
671     \sa classInfoCount(), classInfoName(), classInfoValue(), removeClassInfo()
672     \sa indexOfClassInfo()
673 */
674 int QMetaObjectBuilder::addClassInfo(const QByteArray& name, const QByteArray& value)
675 {
676     int index = d->classInfoNames.size();
677     d->classInfoNames += name;
678     d->classInfoValues += value;
679     return index;
680 }
681
682 /*!
683     Adds \a meta to this class as a related meta object.  Returns
684     the index of the new related meta object entry.
685
686     Related meta objects are used when resolving the enumerated type
687     associated with a property, where the enumerated type is in a
688     different class from the property.
689
690     \sa relatedMetaObjectCount(), relatedMetaObject()
691     \sa removeRelatedMetaObject()
692 */
693 #ifdef Q_NO_DATA_RELOCATION
694 int QMetaObjectBuilder::addRelatedMetaObject(const QMetaObjectAccessor &meta)
695 #else
696 int QMetaObjectBuilder::addRelatedMetaObject(const QMetaObject *meta)
697 #endif
698 {
699     Q_ASSERT(meta);
700     int index = d->relatedMetaObjects.size();
701     d->relatedMetaObjects.append(meta);
702     return index;
703 }
704
705 /*!
706     Adds the contents of \a prototype to this meta object builder.
707     This function is useful for cloning the contents of an existing QMetaObject.
708
709     The \a members parameter indicates which members of \a prototype
710     should be added.  The default is AllMembers.
711 */
712 void QMetaObjectBuilder::addMetaObject
713         (const QMetaObject *prototype, QMetaObjectBuilder::AddMembers members)
714 {
715     Q_ASSERT(prototype);
716     int index;
717
718     if ((members & ClassName) != 0)
719         d->className = prototype->className();
720
721     if ((members & SuperClass) != 0)
722         d->superClass = prototype->superClass();
723
724     if ((members & (Methods | Signals | Slots)) != 0) {
725         for (index = prototype->methodOffset(); index < prototype->methodCount(); ++index) {
726             QMetaMethod method = prototype->method(index);
727             if (method.methodType() != QMetaMethod::Signal) {
728                 if (method.access() == QMetaMethod::Public && (members & PublicMethods) == 0)
729                     continue;
730                 if (method.access() == QMetaMethod::Private && (members & PrivateMethods) == 0)
731                     continue;
732                 if (method.access() == QMetaMethod::Protected && (members & ProtectedMethods) == 0)
733                     continue;
734             }
735             if (method.methodType() == QMetaMethod::Method && (members & Methods) != 0) {
736                 addMethod(method);
737             } else if (method.methodType() == QMetaMethod::Signal &&
738                        (members & Signals) != 0) {
739                 addMethod(method);
740             } else if (method.methodType() == QMetaMethod::Slot &&
741                        (members & Slots) != 0) {
742                 addMethod(method);
743             }
744         }
745     }
746
747     if ((members & Constructors) != 0) {
748         for (index = 0; index < prototype->constructorCount(); ++index)
749             addConstructor(prototype->constructor(index));
750     }
751
752     if ((members & Properties) != 0) {
753         for (index = prototype->propertyOffset(); index < prototype->propertyCount(); ++index)
754             addProperty(prototype->property(index));
755     }
756
757     if ((members & Enumerators) != 0) {
758         for (index = prototype->enumeratorOffset(); index < prototype->enumeratorCount(); ++index)
759             addEnumerator(prototype->enumerator(index));
760     }
761
762     if ((members & ClassInfos) != 0) {
763         for (index = prototype->classInfoOffset(); index < prototype->classInfoCount(); ++index) {
764             QMetaClassInfo ci = prototype->classInfo(index);
765             addClassInfo(ci.name(), ci.value());
766         }
767     }
768
769     if ((members & RelatedMetaObjects) != 0) {
770 #ifdef Q_NO_DATA_RELOCATION
771         const QMetaObjectAccessor *objects = 0;
772 #else
773         const QMetaObject **objects;
774         if (priv(prototype->d.data)->revision < 2) {
775             objects = (const QMetaObject **)(prototype->d.extradata);
776         } else
777 #endif
778         {
779             const QMetaObjectExtraData *extra = (const QMetaObjectExtraData *)(prototype->d.extradata);
780             if (extra)
781                 objects = extra->objects;
782             else
783                 objects = 0;
784         }
785         if (objects) {
786             while (*objects != 0) {
787                 addRelatedMetaObject(*objects);
788                 ++objects;
789             }
790         }
791     }
792
793     if ((members & StaticMetacall) != 0) {
794         if (priv(prototype->d.data)->revision >= 6) {
795             const QMetaObjectExtraData *extra =
796                 (const QMetaObjectExtraData *)(prototype->d.extradata);
797             if (extra && extra->static_metacall)
798                 setStaticMetacallFunction(extra->static_metacall);
799         }
800     }
801 }
802
803 /*!
804     Returns the method at \a index in this class.
805
806     \sa methodCount(), addMethod(), removeMethod(), indexOfMethod()
807 */
808 QMetaMethodBuilder QMetaObjectBuilder::method(int index) const
809 {
810     if (index >= 0 && index < d->methods.size())
811         return QMetaMethodBuilder(this, index);
812     else
813         return QMetaMethodBuilder();
814 }
815
816 /*!
817     Returns the constructor at \a index in this class.
818
819     \sa methodCount(), addMethod(), removeMethod(), indexOfConstructor()
820 */
821 QMetaMethodBuilder QMetaObjectBuilder::constructor(int index) const
822 {
823     if (index >= 0 && index < d->constructors.size())
824         return QMetaMethodBuilder(this, -(index + 1));
825     else
826         return QMetaMethodBuilder();
827 }
828
829 /*!
830     Returns the property at \a index in this class.
831
832     \sa methodCount(), addMethod(), removeMethod(), indexOfProperty()
833 */
834 QMetaPropertyBuilder QMetaObjectBuilder::property(int index) const
835 {
836     if (index >= 0 && index < d->properties.size())
837         return QMetaPropertyBuilder(this, index);
838     else
839         return QMetaPropertyBuilder();
840 }
841
842 /*!
843     Returns the enumerator at \a index in this class.
844
845     \sa enumeratorCount(), addEnumerator(), removeEnumerator()
846     \sa indexOfEnumerator()
847 */
848 QMetaEnumBuilder QMetaObjectBuilder::enumerator(int index) const
849 {
850     if (index >= 0 && index < d->enumerators.size())
851         return QMetaEnumBuilder(this, index);
852     else
853         return QMetaEnumBuilder();
854 }
855
856 /*!
857     Returns the related meta object at \a index in this class.
858
859     Related meta objects are used when resolving the enumerated type
860     associated with a property, where the enumerated type is in a
861     different class from the property.
862
863     \sa relatedMetaObjectCount(), addRelatedMetaObject()
864     \sa removeRelatedMetaObject()
865 */
866 const QMetaObject *QMetaObjectBuilder::relatedMetaObject(int index) const
867 {
868     if (index >= 0 && index < d->relatedMetaObjects.size())
869 #ifdef Q_NO_DATA_RELOCATION
870         return &((*(d->relatedMetaObjects[index]))());
871 #else
872         return d->relatedMetaObjects[index];
873 #endif
874     else
875         return 0;
876 }
877
878 /*!
879     Returns the name of the item of class information at \a index
880     in this class.
881
882     \sa classInfoCount(), addClassInfo(), classInfoValue(), removeClassInfo()
883     \sa indexOfClassInfo()
884 */
885 QByteArray QMetaObjectBuilder::classInfoName(int index) const
886 {
887     if (index >= 0 && index < d->classInfoNames.size())
888         return d->classInfoNames[index]; 
889     else
890         return QByteArray();
891 }
892
893 /*!
894     Returns the value of the item of class information at \a index
895     in this class.
896
897     \sa classInfoCount(), addClassInfo(), classInfoName(), removeClassInfo()
898     \sa indexOfClassInfo()
899 */
900 QByteArray QMetaObjectBuilder::classInfoValue(int index) const
901 {
902     if (index >= 0 && index < d->classInfoValues.size())
903         return d->classInfoValues[index]; 
904     else
905         return QByteArray();
906 }
907
908 /*!
909     Removes the method at \a index from this class.  The indices of
910     all following methods will be adjusted downwards by 1.  If the
911     method is registered as a notify signal on a property, then the
912     notify signal will be removed from the property.
913
914     \sa methodCount(), addMethod(), method(), indexOfMethod()
915 */
916 void QMetaObjectBuilder::removeMethod(int index)
917 {
918     if (index >= 0 && index < d->methods.size()) {
919         d->methods.removeAt(index);
920         for (int prop = 0; prop < d->properties.size(); ++prop) {
921             // Adjust the indices of property notify signal references.
922             if (d->properties[prop].notifySignal == index) {
923                 d->properties[prop].notifySignal = -1;
924                 d->properties[prop].setFlag(Notify, false);
925             } else if (d->properties[prop].notifySignal > index)
926                 (d->properties[prop].notifySignal)--;
927         }
928     }
929 }
930
931 /*!
932     Removes the constructor at \a index from this class.  The indices of
933     all following constructors will be adjusted downwards by 1.
934
935     \sa constructorCount(), addConstructor(), constructor()
936     \sa indexOfConstructor()
937 */
938 void QMetaObjectBuilder::removeConstructor(int index)
939 {
940     if (index >= 0 && index < d->constructors.size())
941         d->constructors.removeAt(index);
942 }
943
944 /*!
945     Removes the property at \a index from this class.  The indices of
946     all following properties will be adjusted downwards by 1.
947
948     \sa propertyCount(), addProperty(), property(), indexOfProperty()
949 */
950 void QMetaObjectBuilder::removeProperty(int index)
951 {
952     if (index >= 0 && index < d->properties.size())
953         d->properties.removeAt(index);
954 }
955
956 /*!
957     Removes the enumerator at \a index from this class.  The indices of
958     all following enumerators will be adjusted downwards by 1.
959
960     \sa enumertorCount(), addEnumerator(), enumerator()
961     \sa indexOfEnumerator()
962 */
963 void QMetaObjectBuilder::removeEnumerator(int index)
964 {
965     if (index >= 0 && index < d->enumerators.size())
966         d->enumerators.removeAt(index);
967 }
968
969 /*!
970     Removes the item of class information at \a index from this class.
971     The indices of all following items will be adjusted downwards by 1.
972
973     \sa classInfoCount(), addClassInfo(), classInfoName(), classInfoValue()
974     \sa indexOfClassInfo()
975 */
976 void QMetaObjectBuilder::removeClassInfo(int index)
977 {
978     if (index >= 0 && index < d->classInfoNames.size()) {
979         d->classInfoNames.removeAt(index);
980         d->classInfoValues.removeAt(index);
981     }
982 }
983
984 /*!
985     Removes the related meta object at \a index from this class.
986     The indices of all following related meta objects will be adjusted
987     downwards by 1.
988
989     Related meta objects are used when resolving the enumerated type
990     associated with a property, where the enumerated type is in a
991     different class from the property.
992
993     \sa relatedMetaObjectCount(), addRelatedMetaObject()
994     \sa relatedMetaObject()
995 */
996 void QMetaObjectBuilder::removeRelatedMetaObject(int index)
997 {
998     if (index >= 0 && index < d->relatedMetaObjects.size())
999         d->relatedMetaObjects.removeAt(index);
1000 }
1001
1002 /*!
1003     Finds a method with the specified \a signature and returns its index;
1004     otherwise returns -1.  The \a signature will be normalized by this method.
1005
1006     \sa method(), methodCount(), addMethod(), removeMethod()
1007 */
1008 int QMetaObjectBuilder::indexOfMethod(const QByteArray& signature)
1009 {
1010     QByteArray sig = QMetaObject::normalizedSignature(signature);
1011     for (int index = 0; index < d->methods.size(); ++index) {
1012         if (sig == d->methods[index].signature)
1013             return index;
1014     }
1015     return -1;
1016 }
1017
1018 /*!
1019     Finds a signal with the specified \a signature and returns its index;
1020     otherwise returns -1.  The \a signature will be normalized by this method.
1021
1022     \sa indexOfMethod(), indexOfSlot()
1023 */
1024 int QMetaObjectBuilder::indexOfSignal(const QByteArray& signature)
1025 {
1026     QByteArray sig = QMetaObject::normalizedSignature(signature);
1027     for (int index = 0; index < d->methods.size(); ++index) {
1028         if (sig == d->methods[index].signature &&
1029                 d->methods[index].methodType() == QMetaMethod::Signal)
1030             return index;
1031     }
1032     return -1;
1033 }
1034
1035 /*!
1036     Finds a slot with the specified \a signature and returns its index;
1037     otherwise returns -1.  The \a signature will be normalized by this method.
1038
1039     \sa indexOfMethod(), indexOfSignal()
1040 */
1041 int QMetaObjectBuilder::indexOfSlot(const QByteArray& signature)
1042 {
1043     QByteArray sig = QMetaObject::normalizedSignature(signature);
1044     for (int index = 0; index < d->methods.size(); ++index) {
1045         if (sig == d->methods[index].signature &&
1046                 d->methods[index].methodType() == QMetaMethod::Slot)
1047             return index;
1048     }
1049     return -1;
1050 }
1051
1052 /*!
1053     Finds a constructor with the specified \a signature and returns its index;
1054     otherwise returns -1.  The \a signature will be normalized by this method.
1055
1056     \sa constructor(), constructorCount(), addConstructor(), removeConstructor()
1057 */
1058 int QMetaObjectBuilder::indexOfConstructor(const QByteArray& signature)
1059 {
1060     QByteArray sig = QMetaObject::normalizedSignature(signature);
1061     for (int index = 0; index < d->constructors.size(); ++index) {
1062         if (sig == d->constructors[index].signature)
1063             return index;
1064     }
1065     return -1;
1066 }
1067
1068 /*!
1069     Finds a property with the specified \a name and returns its index;
1070     otherwise returns -1.
1071
1072     \sa property(), propertyCount(), addProperty(), removeProperty()
1073 */
1074 int QMetaObjectBuilder::indexOfProperty(const QByteArray& name)
1075 {
1076     for (int index = 0; index < d->properties.size(); ++index) {
1077         if (name == d->properties[index].name)
1078             return index;
1079     }
1080     return -1;
1081 }
1082
1083 /*!
1084     Finds an enumerator with the specified \a name and returns its index;
1085     otherwise returns -1.
1086
1087     \sa enumertor(), enumeratorCount(), addEnumerator(), removeEnumerator()
1088 */
1089 int QMetaObjectBuilder::indexOfEnumerator(const QByteArray& name)
1090 {
1091     for (int index = 0; index < d->enumerators.size(); ++index) {
1092         if (name == d->enumerators[index].name)
1093             return index;
1094     }
1095     return -1;
1096 }
1097
1098 /*!
1099     Finds an item of class information with the specified \a name and
1100     returns its index; otherwise returns -1.
1101
1102     \sa classInfoName(), classInfoValue(), classInfoCount(), addClassInfo()
1103     \sa removeClassInfo()
1104 */
1105 int QMetaObjectBuilder::indexOfClassInfo(const QByteArray& name)
1106 {
1107     for (int index = 0; index < d->classInfoNames.size(); ++index) {
1108         if (name == d->classInfoNames[index])
1109             return index;
1110     }
1111     return -1;
1112 }
1113
1114 // Align on a specific type boundary.
1115 #define ALIGN(size,type)    \
1116     (size) = ((size) + sizeof(type) - 1) & ~(sizeof(type) - 1)
1117
1118 // Build a string into a QMetaObject representation.  Returns the
1119 // position in the string table where the string was placed.
1120 static int buildString
1121     (char *buf, char *str, int *offset, const QByteArray& value, int empty)
1122 {
1123     if (value.size() == 0 && empty >= 0)
1124         return empty;
1125     if (buf) {
1126         memcpy(str + *offset, value.constData(), value.size());
1127         str[*offset + value.size()] = '\0';
1128     }
1129     int posn = *offset;
1130     *offset += value.size() + 1;
1131     return posn;
1132 }
1133
1134 // Build the parameter array string for a method.
1135 static QByteArray buildParameterNames
1136         (const QByteArray& signature, const QList<QByteArray>& parameterNames)
1137 {
1138     // If the parameter name list is specified, then concatenate them.
1139     if (!parameterNames.isEmpty()) {
1140         QByteArray names;
1141         bool first = true;
1142         foreach (const QByteArray &name, parameterNames) {
1143             if (first)
1144                 first = false;
1145             else
1146                 names += (char)',';
1147             names += name;
1148         }
1149         return names;
1150     }
1151
1152     // Count commas in the signature, excluding those inside template arguments.
1153     int index = signature.indexOf('(');
1154     if (index < 0)
1155         return QByteArray();
1156     ++index;
1157     if (index >= signature.size())
1158         return QByteArray();
1159     if (signature[index] == ')')
1160         return QByteArray();
1161     int count = 1;
1162     int brackets = 0;
1163     while (index < signature.size() && signature[index] != ',') {
1164         char ch = signature[index++];
1165         if (ch == '<')
1166             ++brackets;
1167         else if (ch == '>')
1168             --brackets;
1169         else if (ch == ',' && brackets <= 0)
1170             ++count;
1171     }
1172     return QByteArray(count - 1, ',');
1173 }
1174
1175 // Build a QMetaObject in "buf" based on the information in "d".
1176 // If "buf" is null, then return the number of bytes needed to
1177 // build the QMetaObject.  Returns -1 if the metaobject if 
1178 // relocatable is set, but the metaobject contains extradata.
1179 static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf, 
1180                            bool relocatable)
1181 {
1182     int size = 0;
1183     int dataIndex;
1184     int enumIndex;
1185     int index;
1186     bool hasNotifySignals = false;
1187
1188     if (relocatable && 
1189         (d->relatedMetaObjects.size() > 0 || d->staticMetacallFunction))
1190         return -1;
1191
1192     // Create the main QMetaObject structure at the start of the buffer.
1193     QMetaObject *meta = reinterpret_cast<QMetaObject *>(buf);
1194     size += sizeof(QMetaObject);
1195     ALIGN(size, int);
1196     if (buf) {
1197         if (!relocatable) meta->d.superdata = d->superClass;
1198         meta->d.extradata = 0;
1199     }
1200
1201     // Populate the QMetaObjectPrivate structure.
1202     QMetaObjectPrivate *pmeta
1203         = reinterpret_cast<QMetaObjectPrivate *>(buf + size);
1204     int pmetaSize = size;
1205     dataIndex = 13;     // Number of fields in the QMetaObjectPrivate.
1206     for (index = 0; index < d->properties.size(); ++index) {
1207         if (d->properties[index].notifySignal != -1) {
1208             hasNotifySignals = true;
1209             break;
1210         }
1211     }
1212     if (buf) {
1213         pmeta->revision = 3;
1214         pmeta->flags = d->flags;
1215         pmeta->className = 0;   // Class name is always the first string.
1216
1217         pmeta->classInfoCount = d->classInfoNames.size();
1218         pmeta->classInfoData = dataIndex;
1219         dataIndex += 2 * d->classInfoNames.size();
1220
1221         pmeta->methodCount = d->methods.size();
1222         pmeta->methodData = dataIndex;
1223         dataIndex += 5 * d->methods.size();
1224
1225         pmeta->propertyCount = d->properties.size();
1226         pmeta->propertyData = dataIndex;
1227         dataIndex += 3 * d->properties.size();
1228         if (hasNotifySignals)
1229             dataIndex += d->properties.size();
1230
1231         pmeta->enumeratorCount = d->enumerators.size();
1232         pmeta->enumeratorData = dataIndex;
1233         dataIndex += 4 * d->enumerators.size();
1234
1235         pmeta->constructorCount = d->constructors.size();
1236         pmeta->constructorData = dataIndex;
1237         dataIndex += 5 * d->constructors.size();
1238     } else {
1239         dataIndex += 2 * d->classInfoNames.size();
1240         dataIndex += 5 * d->methods.size();
1241         dataIndex += 3 * d->properties.size();
1242         if (hasNotifySignals)
1243             dataIndex += d->properties.size();
1244         dataIndex += 4 * d->enumerators.size();
1245         dataIndex += 5 * d->constructors.size();
1246     }
1247
1248     // Allocate space for the enumerator key names and values.
1249     enumIndex = dataIndex;
1250     for (index = 0; index < d->enumerators.size(); ++index) {
1251         QMetaEnumBuilderPrivate *enumerator = &(d->enumerators[index]);
1252         dataIndex += 2 * enumerator->keys.size();
1253     }
1254
1255     // Zero terminator at the end of the data offset table.
1256     ++dataIndex;
1257
1258     // Find the start of the data and string tables.
1259     int *data = reinterpret_cast<int *>(pmeta);
1260     size += dataIndex * sizeof(int);
1261     char *str = reinterpret_cast<char *>(buf + size);
1262     if (buf) {
1263         if (relocatable) {
1264             meta->d.stringdata = reinterpret_cast<const char *>((quintptr)size);
1265             meta->d.data = reinterpret_cast<uint *>((quintptr)pmetaSize);
1266         } else {
1267             meta->d.stringdata = str;
1268             meta->d.data = reinterpret_cast<uint *>(data);
1269         }
1270     }
1271
1272     // Reset the current data position to just past the QMetaObjectPrivate.
1273     dataIndex = 13;
1274
1275     // Add the class name to the string table.
1276     int offset = 0;
1277     buildString(buf, str, &offset, d->className, -1);
1278
1279     // Add a common empty string, which is used to indicate "void"
1280     // method returns, empty tag strings, etc.
1281     int empty = buildString(buf, str, &offset, QByteArray(), -1);
1282
1283     // Output the class infos,
1284     for (index = 0; index < d->classInfoNames.size(); ++index) {
1285         int name = buildString(buf, str, &offset, d->classInfoNames[index], empty);
1286         int value = buildString(buf, str, &offset, d->classInfoValues[index], empty);
1287         if (buf) {
1288             data[dataIndex] = name;
1289             data[dataIndex + 1] = value;
1290         }
1291         dataIndex += 2;
1292     }
1293
1294     // Output the methods in the class.
1295     for (index = 0; index < d->methods.size(); ++index) {
1296         QMetaMethodBuilderPrivate *method = &(d->methods[index]);
1297         int sig = buildString(buf, str, &offset, method->signature, empty);
1298         int params;
1299         QByteArray names = buildParameterNames
1300             (method->signature, method->parameterNames);
1301         params = buildString(buf, str, &offset, names, empty);
1302         int ret = buildString(buf, str, &offset, method->returnType, empty);
1303         int tag = buildString(buf, str, &offset, method->tag, empty);
1304         int attrs = method->attributes;
1305         if (buf) {
1306             data[dataIndex]     = sig;
1307             data[dataIndex + 1] = params;
1308             data[dataIndex + 2] = ret;
1309             data[dataIndex + 3] = tag;
1310             data[dataIndex + 4] = attrs;
1311         }
1312         dataIndex += 5;
1313     }
1314
1315     // Output the properties in the class.
1316     for (index = 0; index < d->properties.size(); ++index) {
1317         QMetaPropertyBuilderPrivate *prop = &(d->properties[index]);
1318         int name = buildString(buf, str, &offset, prop->name, empty);
1319         int type = buildString(buf, str, &offset, prop->type, empty);
1320         int flags = prop->flags;
1321
1322         if (!isVariantType(prop->type)) {
1323             flags |= EnumOrFlag;
1324         } else {
1325             flags |= qvariant_nameToType(prop->type) << 24;
1326         }
1327
1328         if (buf) {
1329             data[dataIndex]     = name;
1330             data[dataIndex + 1] = type;
1331             data[dataIndex + 2] = flags;
1332         }
1333         dataIndex += 3;
1334     }
1335     if (hasNotifySignals) {
1336         for (index = 0; index < d->properties.size(); ++index) {
1337             QMetaPropertyBuilderPrivate *prop = &(d->properties[index]);
1338             if (buf) {
1339                 if (prop->notifySignal != -1)
1340                     data[dataIndex] = prop->notifySignal;
1341                 else
1342                     data[dataIndex] = 0;
1343             }
1344             ++dataIndex;
1345         }
1346     }
1347
1348     // Output the enumerators in the class.
1349     for (index = 0; index < d->enumerators.size(); ++index) {
1350         QMetaEnumBuilderPrivate *enumerator = &(d->enumerators[index]);
1351         int name = buildString(buf, str, &offset, enumerator->name, empty);
1352         int isFlag = (int)(enumerator->isFlag);
1353         int count = enumerator->keys.size();
1354         int enumOffset = enumIndex;
1355         if (buf) {
1356             data[dataIndex]     = name;
1357             data[dataIndex + 1] = isFlag;
1358             data[dataIndex + 2] = count;
1359             data[dataIndex + 3] = enumOffset;
1360         }
1361         for (int key = 0; key < count; ++key) {
1362             int keyIndex = buildString(buf, str, &offset, enumerator->keys[key], empty);
1363             if (buf) {
1364                 data[enumOffset++] = keyIndex;
1365                 data[enumOffset++] = enumerator->values[key];
1366             }
1367         }
1368         dataIndex += 4;
1369         enumIndex += 2 * count;
1370     }
1371
1372     // Output the constructors in the class.
1373     for (index = 0; index < d->constructors.size(); ++index) {
1374         QMetaMethodBuilderPrivate *method = &(d->constructors[index]);
1375         int sig = buildString(buf, str, &offset, method->signature, empty);
1376         int params;
1377         QByteArray names = buildParameterNames
1378             (method->signature, method->parameterNames);
1379         params = buildString(buf, str, &offset, names, empty);
1380         int ret = buildString(buf, str, &offset, method->returnType, empty);
1381         int tag = buildString(buf, str, &offset, method->tag, empty);
1382         int attrs = method->attributes;
1383         if (buf) {
1384             data[dataIndex]     = sig;
1385             data[dataIndex + 1] = params;
1386             data[dataIndex + 2] = ret;
1387             data[dataIndex + 3] = tag;
1388             data[dataIndex + 4] = attrs;
1389         }
1390         dataIndex += 5;
1391     }
1392
1393     // One more empty string to act as a terminator.
1394     buildString(buf, str, &offset, QByteArray(), -1);
1395     size += offset;
1396
1397     // Output the zero terminator in the data array.
1398     if (buf)
1399         data[enumIndex] = 0;
1400
1401     // Create the extradata block if we need one.
1402     if (d->relatedMetaObjects.size() > 0 || d->staticMetacallFunction) {
1403         ALIGN(size, QMetaObject **);
1404         ALIGN(size, QMetaObjectBuilder::StaticMetacallFunction);
1405         QMetaObjectExtraData *extra =
1406             reinterpret_cast<QMetaObjectExtraData *>(buf + size);
1407         size += sizeof(QMetaObjectExtraData);
1408         ALIGN(size, QMetaObject *);
1409 #ifdef Q_NO_DATA_RELOCATION
1410         QMetaObjectAccessor *objects =
1411             reinterpret_cast<QMetaObjectAccessor *>(buf + size);
1412 #else
1413         const QMetaObject **objects =
1414             reinterpret_cast<const QMetaObject **>(buf + size);
1415 #endif
1416         if (buf) {
1417             if (d->relatedMetaObjects.size() > 0) {
1418                 extra->objects = objects;
1419                 for (index = 0; index < d->relatedMetaObjects.size(); ++index)
1420                     objects[index] = d->relatedMetaObjects[index];
1421                 objects[index] = 0;
1422             } else {
1423                 extra->objects = 0;
1424             }
1425             extra->static_metacall = d->staticMetacallFunction;
1426             meta->d.extradata = reinterpret_cast<void *>(extra);
1427         }
1428         if (d->relatedMetaObjects.size() > 0)
1429             size += sizeof(QMetaObject *) * (d->relatedMetaObjects.size() + 1);
1430     }
1431
1432     // Align the final size and return it.
1433     ALIGN(size, void *);
1434     return size;
1435 }
1436
1437 /*!
1438     Converts this meta object builder into a concrete QMetaObject.
1439     The return value should be deallocated using qFree() once it
1440     is no longer needed.
1441
1442     The returned meta object is a snapshot of the state of the
1443     QMetaObjectBuilder.  Any further modifications to the QMetaObjectBuilder
1444     will not be reflected in previous meta objects returned by
1445     this method.
1446 */
1447 QMetaObject *QMetaObjectBuilder::toMetaObject() const
1448 {
1449     int size = buildMetaObject(d, 0, false);
1450     char *buf = reinterpret_cast<char *>(qMalloc(size));
1451     buildMetaObject(d, buf, false);
1452     return reinterpret_cast<QMetaObject *>(buf);
1453 }
1454
1455 /*
1456     \internal
1457
1458     Converts this meta object builder into relocatable data.  This data can
1459     be stored, copied and later passed to fromRelocatableData() to create a
1460     concrete QMetaObject.
1461
1462     The data is specific to the architecture on which it was created, but is not
1463     specific to the process that created it.  Not all meta object builder's can
1464     be converted to data in this way.  If \a ok is provided, it will be set to
1465     true if the conversion succeeds, and false otherwise.  If a 
1466     staticMetacallFunction() or any relatedMetaObject()'s are specified the
1467     conversion to relocatable data will fail.
1468 */
1469 QByteArray QMetaObjectBuilder::toRelocatableData(bool *ok) const
1470 {
1471     int size = buildMetaObject(d, 0, true);
1472     if (size == -1) {
1473         if (ok) *ok = false;
1474         return QByteArray();
1475     }
1476
1477     QByteArray data;
1478     data.resize(size);
1479     char *buf = data.data();
1480     buildMetaObject(d, buf, true);
1481     if (ok) *ok = true;
1482     return data;
1483 }
1484
1485 /*
1486     \internal
1487
1488     Sets the \a data returned from toRelocatableData() onto a concrete 
1489     QMetaObject instance, \a output.  As the meta object's super class is not
1490     saved in the relocatable data, it must be passed as \a superClass.
1491 */
1492 void QMetaObjectBuilder::fromRelocatableData(QMetaObject *output, 
1493                                              const QMetaObject *superclass, 
1494                                              const QByteArray &data)
1495 {
1496     if (!output)
1497         return;
1498
1499     const char *buf = data.constData();
1500     const QMetaObject *dataMo = reinterpret_cast<const QMetaObject *>(buf);
1501
1502     quintptr stringdataOffset = (quintptr)dataMo->d.stringdata;
1503     quintptr dataOffset = (quintptr)dataMo->d.data;
1504
1505     output->d.superdata = superclass;
1506     output->d.stringdata = buf + stringdataOffset;
1507     output->d.data = reinterpret_cast<const uint *>(buf + dataOffset);
1508 }
1509
1510 /*!
1511     \typedef QMetaObjectBuilder::StaticMetacallFunction
1512
1513     Typedef for static metacall functions.  The three parameters are
1514     the call type value, the constructor index, and the
1515     array of parameters.
1516 */
1517
1518 /*!
1519     Returns the static metacall function to use to construct objects
1520     of this class.  The default value is null.
1521
1522     \sa setStaticMetacallFunction()
1523 */
1524 QMetaObjectBuilder::StaticMetacallFunction QMetaObjectBuilder::staticMetacallFunction() const
1525 {
1526     return d->staticMetacallFunction;
1527 }
1528
1529 /*!
1530     Sets the static metacall function to use to construct objects
1531     of this class to \a value.  The default value is null.
1532
1533     \sa staticMetacallFunction()
1534 */
1535 void QMetaObjectBuilder::setStaticMetacallFunction
1536         (QMetaObjectBuilder::StaticMetacallFunction value)
1537 {
1538     d->staticMetacallFunction = value;
1539 }
1540
1541 #ifndef QT_NO_DATASTREAM
1542
1543 /*!
1544     Serializes the contents of the meta object builder onto \a stream.
1545
1546     \sa deserialize()
1547 */
1548 void QMetaObjectBuilder::serialize(QDataStream& stream) const
1549 {
1550     int index;
1551
1552     // Write the class and super class names.
1553     stream << d->className;
1554     if (d->superClass)
1555         stream << QByteArray(d->superClass->className());
1556     else
1557         stream << QByteArray();
1558
1559     // Write the counts for each type of class member.
1560     stream << d->classInfoNames.size();
1561     stream << d->methods.size();
1562     stream << d->properties.size();
1563     stream << d->enumerators.size();
1564     stream << d->constructors.size();
1565     stream << d->relatedMetaObjects.size();
1566
1567     // Write the items of class information.
1568     for (index = 0; index < d->classInfoNames.size(); ++index) {
1569         stream << d->classInfoNames[index];
1570         stream << d->classInfoValues[index];
1571     }
1572
1573     // Write the methods.
1574     for (index = 0; index < d->methods.size(); ++index) {
1575         const QMetaMethodBuilderPrivate *method = &(d->methods[index]);
1576         stream << method->signature;
1577         stream << method->returnType;
1578         stream << method->parameterNames;
1579         stream << method->tag;
1580         stream << method->attributes;
1581     }
1582
1583     // Write the properties.
1584     for (index = 0; index < d->properties.size(); ++index) {
1585         const QMetaPropertyBuilderPrivate *property = &(d->properties[index]);
1586         stream << property->name;
1587         stream << property->type;
1588         stream << property->flags;
1589         stream << property->notifySignal;
1590     }
1591
1592     // Write the enumerators.
1593     for (index = 0; index < d->enumerators.size(); ++index) {
1594         const QMetaEnumBuilderPrivate *enumerator = &(d->enumerators[index]);
1595         stream << enumerator->name;
1596         stream << enumerator->isFlag;
1597         stream << enumerator->keys;
1598         stream << enumerator->values;
1599     }
1600
1601     // Write the constructors.
1602     for (index = 0; index < d->constructors.size(); ++index) {
1603         const QMetaMethodBuilderPrivate *method = &(d->constructors[index]);
1604         stream << method->signature;
1605         stream << method->returnType;
1606         stream << method->parameterNames;
1607         stream << method->tag;
1608         stream << method->attributes;
1609     }
1610
1611     // Write the related meta objects.
1612 #ifdef Q_NO_DATA_RELOCATION
1613     //### What do we do here?
1614 #else
1615     for (index = 0; index < d->relatedMetaObjects.size(); ++index) {
1616         const QMetaObject *meta = d->relatedMetaObjects[index];
1617         stream << QByteArray(meta->className());
1618     }
1619 #endif
1620
1621     // Add an extra empty QByteArray for additional data in future versions.
1622     // This should help maintain backwards compatibility, allowing older
1623     // versions to read newer data.
1624     stream << QByteArray();
1625 }
1626
1627 // Resolve a class name using the name reference map.
1628 static const QMetaObject *resolveClassName
1629         (const QMap<QByteArray, const QMetaObject *>& references,
1630          const QByteArray& name)
1631 {
1632     if (name == QByteArray("QObject"))
1633         return &QObject::staticMetaObject;
1634     else
1635         return references.value(name, 0);
1636 }
1637
1638 /*!
1639     Deserializes a meta object builder from \a stream into
1640     this meta object builder.
1641
1642     The \a references parameter specifies a mapping from class names
1643     to QMetaObject instances for resolving the super class name and
1644     related meta objects in the object that is deserialized.
1645     The meta object for QObject is implicitly added to \a references
1646     and does not need to be supplied.
1647
1648     The QDataStream::status() value on \a stream will be set to
1649     QDataStream::ReadCorruptData if the input data is corrupt.
1650     The status will be set to QDataStream::ReadPastEnd if the
1651     input was exhausted before the full meta object was read.
1652
1653     \sa serialize()
1654 */
1655 void QMetaObjectBuilder::deserialize
1656         (QDataStream& stream,
1657          const QMap<QByteArray, const QMetaObject *>& references)
1658 {
1659     QByteArray name;
1660     const QMetaObject *cl;
1661     int index;
1662
1663     // Clear all members in the builder to their default states.
1664     d->className.clear();
1665     d->superClass = &QObject::staticMetaObject;
1666     d->classInfoNames.clear();
1667     d->classInfoValues.clear();
1668     d->methods.clear();
1669     d->properties.clear();
1670     d->enumerators.clear();
1671     d->constructors.clear();
1672     d->relatedMetaObjects.clear();
1673     d->staticMetacallFunction = 0;
1674
1675     // Read the class and super class names.
1676     stream >> d->className;
1677     stream >> name;
1678     if (name.isEmpty()) {
1679         d->superClass = 0;
1680     } else if ((cl = resolveClassName(references, name)) != 0) {
1681         d->superClass = cl;
1682     } else {
1683         stream.setStatus(QDataStream::ReadCorruptData);
1684         return;
1685     }
1686
1687     // Read the counts for each type of class member.
1688     int classInfoCount, methodCount, propertyCount;
1689     int enumeratorCount, constructorCount, relatedMetaObjectCount;
1690     stream >> classInfoCount;
1691     stream >> methodCount;
1692     stream >> propertyCount;
1693     stream >> enumeratorCount;
1694     stream >> constructorCount;
1695     stream >> relatedMetaObjectCount;
1696     if (classInfoCount < 0 || methodCount < 0 ||
1697         propertyCount < 0 || enumeratorCount < 0 ||
1698         constructorCount < 0 || relatedMetaObjectCount < 0) {
1699         stream.setStatus(QDataStream::ReadCorruptData);
1700         return;
1701     }
1702
1703     // Read the items of class information.
1704     for (index = 0; index < classInfoCount; ++index) {
1705         if (stream.status() != QDataStream::Ok)
1706             return;
1707         QByteArray value;
1708         stream >> name;
1709         stream >> value;
1710         addClassInfo(name, value);
1711     }
1712
1713     // Read the member methods.
1714     for (index = 0; index < methodCount; ++index) {
1715         if (stream.status() != QDataStream::Ok)
1716             return;
1717         stream >> name;
1718         addMethod(name);
1719         QMetaMethodBuilderPrivate *method = &(d->methods[index]);
1720         stream >> method->returnType;
1721         stream >> method->parameterNames;
1722         stream >> method->tag;
1723         stream >> method->attributes;
1724         if (method->methodType() == QMetaMethod::Constructor) {
1725             // Cannot add a constructor in this set of methods.
1726             stream.setStatus(QDataStream::ReadCorruptData);
1727             return;
1728         }
1729     }
1730
1731     // Read the properties.
1732     for (index = 0; index < propertyCount; ++index) {
1733         if (stream.status() != QDataStream::Ok)
1734             return;
1735         QByteArray type;
1736         stream >> name;
1737         stream >> type;
1738         addProperty(name, type);
1739         QMetaPropertyBuilderPrivate *property = &(d->properties[index]);
1740         stream >> property->flags;
1741         stream >> property->notifySignal;
1742         if (property->notifySignal < -1 ||
1743             property->notifySignal >= d->methods.size()) {
1744             // Notify signal method index is out of range.
1745             stream.setStatus(QDataStream::ReadCorruptData);
1746             return;
1747         }
1748         if (property->notifySignal >= 0 &&
1749             d->methods[property->notifySignal].methodType() != QMetaMethod::Signal) {
1750             // Notify signal method index does not refer to a signal.
1751             stream.setStatus(QDataStream::ReadCorruptData);
1752             return;
1753         }
1754     }
1755
1756     // Read the enumerators.
1757     for (index = 0; index < enumeratorCount; ++index) {
1758         if (stream.status() != QDataStream::Ok)
1759             return;
1760         stream >> name;
1761         addEnumerator(name);
1762         QMetaEnumBuilderPrivate *enumerator = &(d->enumerators[index]);
1763         stream >> enumerator->isFlag;
1764         stream >> enumerator->keys;
1765         stream >> enumerator->values;
1766         if (enumerator->keys.size() != enumerator->values.size()) {
1767             // Mismatch between number of keys and number of values.
1768             stream.setStatus(QDataStream::ReadCorruptData);
1769             return;
1770         }
1771     }
1772
1773     // Read the constructor methods.
1774     for (index = 0; index < constructorCount; ++index) {
1775         if (stream.status() != QDataStream::Ok)
1776             return;
1777         stream >> name;
1778         addConstructor(name);
1779         QMetaMethodBuilderPrivate *method = &(d->constructors[index]);
1780         stream >> method->returnType;
1781         stream >> method->parameterNames;
1782         stream >> method->tag;
1783         stream >> method->attributes;
1784         if (method->methodType() != QMetaMethod::Constructor) {
1785             // The type must be Constructor.
1786             stream.setStatus(QDataStream::ReadCorruptData);
1787             return;
1788         }
1789     }
1790
1791     // Read the related meta objects.
1792 #ifdef Q_NO_DATA_RELOCATION
1793     //### What do we do here
1794 #else
1795     for (index = 0; index < relatedMetaObjectCount; ++index) {
1796         if (stream.status() != QDataStream::Ok)
1797             return;
1798         stream >> name;
1799         cl = resolveClassName(references, name);
1800         if (!cl) {
1801             stream.setStatus(QDataStream::ReadCorruptData);
1802             return;
1803         }
1804         addRelatedMetaObject(cl);
1805     }
1806 #endif
1807
1808     // Read the extra data block, which is reserved for future use.
1809     stream >> name;
1810 }
1811
1812 #endif // !QT_NO_DATASTREAM
1813
1814 /*!
1815     \class QMetaMethodBuilder
1816     \internal
1817     \brief The QMetaMethodBuilder class enables modifications to a method definition on a meta object builder.
1818 */
1819
1820 QMetaMethodBuilderPrivate *QMetaMethodBuilder::d_func() const
1821 {
1822     // Positive indices indicate methods, negative indices indicate constructors.
1823     if (_mobj && _index >= 0 && _index < _mobj->d->methods.size())
1824         return &(_mobj->d->methods[_index]);
1825     else if (_mobj && -_index >= 1 && -_index <= _mobj->d->constructors.size())
1826         return &(_mobj->d->constructors[(-_index) - 1]);
1827     else
1828         return 0;
1829 }
1830
1831 /*!
1832     \fn QMetaMethodBuilder::QMetaMethodBuilder()
1833     \internal
1834 */
1835
1836 /*!
1837     Returns the index of this method within its QMetaObjectBuilder.
1838 */
1839 int QMetaMethodBuilder::index() const
1840 {
1841     if (_index >= 0)
1842         return _index;          // Method, signal, or slot
1843     else
1844         return (-_index) - 1;   // Constructor
1845 }
1846
1847 /*!
1848     Returns the type of this method (signal, slot, method, or constructor).
1849 */
1850 QMetaMethod::MethodType QMetaMethodBuilder::methodType() const
1851 {
1852     QMetaMethodBuilderPrivate *d = d_func();
1853     if (d)
1854         return d->methodType();
1855     else
1856         return QMetaMethod::Method;
1857 }
1858
1859 /*!
1860     Returns the signature of this method.
1861
1862     \sa parameterNames(), returnType()
1863 */
1864 QByteArray QMetaMethodBuilder::signature() const
1865 {
1866     QMetaMethodBuilderPrivate *d = d_func();
1867     if (d)
1868         return d->signature;
1869     else
1870         return QByteArray();
1871 }
1872
1873 /*!
1874     Returns the return type for this method; empty if the method's
1875     return type is \c{void}.
1876
1877     \sa setReturnType(), signature()
1878 */
1879 QByteArray QMetaMethodBuilder::returnType() const
1880 {
1881     QMetaMethodBuilderPrivate *d = d_func();
1882     if (d)
1883         return d->returnType;
1884     else
1885         return QByteArray();
1886 }
1887
1888 /*!
1889     Sets the return type for this method to \a value.  If \a value
1890     is empty, then the method's return type is \c{void}.  The \a value
1891     will be normalized before it is added to the method.
1892
1893     \sa returnType(), signature()
1894 */
1895 void QMetaMethodBuilder::setReturnType(const QByteArray& value)
1896 {
1897     QMetaMethodBuilderPrivate *d = d_func();
1898     if (d)
1899         d->returnType = QMetaObject::normalizedType(value);
1900 }
1901
1902 /*!
1903     Returns the list of parameter names for this method.
1904
1905     \sa setParameterNames()
1906 */
1907 QList<QByteArray> QMetaMethodBuilder::parameterNames() const
1908 {
1909     QMetaMethodBuilderPrivate *d = d_func();
1910     if (d)
1911         return d->parameterNames;
1912     else
1913         return QList<QByteArray>();
1914 }
1915
1916 /*!
1917     Sets the list of parameter names for this method to \a value.
1918
1919     \sa parameterNames()
1920 */
1921 void QMetaMethodBuilder::setParameterNames(const QList<QByteArray>& value)
1922 {
1923     QMetaMethodBuilderPrivate *d = d_func();
1924     if (d)
1925         d->parameterNames = value;
1926 }
1927
1928 /*!
1929     Returns the tag associated with this method.
1930
1931     \sa setTag()
1932 */
1933 QByteArray QMetaMethodBuilder::tag() const
1934 {
1935     QMetaMethodBuilderPrivate *d = d_func();
1936     if (d)
1937         return d->tag;
1938     else
1939         return QByteArray();
1940 }
1941
1942 /*!
1943     Sets the tag associated with this method to \a value.
1944
1945     \sa setTag()
1946 */
1947 void QMetaMethodBuilder::setTag(const QByteArray& value)
1948 {
1949     QMetaMethodBuilderPrivate *d = d_func();
1950     if (d)
1951         d->tag = value;
1952 }
1953
1954 /*!
1955     Returns the access specification of this method (private, protected,
1956     or public).  The default value is QMetaMethod::Public for methods,
1957     slots, and constructors.  The default value is QMetaMethod::Protected
1958     for signals.
1959
1960     \sa setAccess()
1961 */
1962 QMetaMethod::Access QMetaMethodBuilder::access() const
1963 {
1964     QMetaMethodBuilderPrivate *d = d_func();
1965     if (d)
1966         return d->access();
1967     else
1968         return QMetaMethod::Public;
1969 }
1970
1971 /*!
1972     Sets the access specification of this method (private, protected,
1973     or public) to \a value.  If the method is a signal, this function
1974     will be ignored.
1975
1976     \sa access()
1977 */
1978 void QMetaMethodBuilder::setAccess(QMetaMethod::Access value)
1979 {
1980     QMetaMethodBuilderPrivate *d = d_func();
1981     if (d && d->methodType() != QMetaMethod::Signal)
1982         d->setAccess(value);
1983 }
1984
1985 /*!
1986     Returns the additional attributes for this method.
1987
1988     \sa setAttributes()
1989 */
1990 int QMetaMethodBuilder::attributes() const
1991 {
1992     QMetaMethodBuilderPrivate *d = d_func();
1993     if (d)
1994         return (d->attributes >> 4);
1995     else
1996         return 0;
1997 }
1998
1999 /*!
2000     Sets the additional attributes for this method to \a value.
2001
2002     \sa attributes()
2003 */
2004 void QMetaMethodBuilder::setAttributes(int value)
2005 {
2006     QMetaMethodBuilderPrivate *d = d_func();
2007     if (d)
2008         d->attributes = ((d->attributes & 0x0f) | (value << 4));
2009 }
2010
2011 /*!
2012     \class QMetaPropertyBuilder
2013     \internal
2014     \brief The QMetaPropertyBuilder class enables modifications to a property definition on a meta object builder.
2015 */
2016
2017 QMetaPropertyBuilderPrivate *QMetaPropertyBuilder::d_func() const
2018 {
2019     if (_mobj && _index >= 0 && _index < _mobj->d->properties.size())
2020         return &(_mobj->d->properties[_index]);
2021     else
2022         return 0;
2023 }
2024
2025 /*!
2026     \fn QMetaPropertyBuilder::QMetaPropertyBuilder()
2027     \internal
2028 */
2029
2030 /*!
2031     \fn int QMetaPropertyBuilder::index() const
2032
2033     Returns the index of this property within its QMetaObjectBuilder.
2034 */
2035
2036 /*!
2037     Returns the name associated with this property.
2038
2039     \sa type()
2040 */
2041 QByteArray QMetaPropertyBuilder::name() const
2042 {
2043     QMetaPropertyBuilderPrivate *d = d_func();
2044     if (d)
2045         return d->name;
2046     else
2047         return QByteArray();
2048 }
2049
2050 /*!
2051     Returns the type associated with this property.
2052
2053     \sa name()
2054 */
2055 QByteArray QMetaPropertyBuilder::type() const
2056 {
2057     QMetaPropertyBuilderPrivate *d = d_func();
2058     if (d)
2059         return d->type;
2060     else
2061         return QByteArray();
2062 }
2063
2064 /*!
2065     Returns true if this property has a notify signal; false otherwise.
2066
2067     \sa notifySignal(), setNotifySignal(), removeNotifySignal()
2068 */
2069 bool QMetaPropertyBuilder::hasNotifySignal() const
2070 {
2071     QMetaPropertyBuilderPrivate *d = d_func();
2072     if (d)
2073         return d->flag(Notify);
2074     else
2075         return false;
2076 }
2077
2078 /*!
2079     Returns the notify signal associated with this property.
2080
2081     \sa hasNotifySignal(), setNotifySignal(), removeNotifySignal()
2082 */
2083 QMetaMethodBuilder QMetaPropertyBuilder::notifySignal() const
2084 {
2085     QMetaPropertyBuilderPrivate *d = d_func();
2086     if (d && d->notifySignal >= 0)
2087         return QMetaMethodBuilder(_mobj, d->notifySignal);
2088     else
2089         return QMetaMethodBuilder();
2090 }
2091
2092 /*!
2093     Sets the notify signal associated with this property to \a value.
2094
2095     \sa hasNotifySignal(), notifySignal(), removeNotifySignal()
2096 */
2097 void QMetaPropertyBuilder::setNotifySignal(const QMetaMethodBuilder& value)
2098 {
2099     QMetaPropertyBuilderPrivate *d = d_func();
2100     if (d) {
2101         if (value._mobj) {
2102             d->notifySignal = value._index;
2103             d->setFlag(Notify, true);
2104         } else {
2105             d->notifySignal = -1;
2106             d->setFlag(Notify, false);
2107         }
2108     }
2109 }
2110
2111 /*!
2112     Removes the notify signal from this property.
2113
2114     \sa hasNotifySignal(), notifySignal(), setNotifySignal()
2115 */
2116 void QMetaPropertyBuilder::removeNotifySignal()
2117 {
2118     QMetaPropertyBuilderPrivate *d = d_func();
2119     if (d) {
2120         d->notifySignal = -1;
2121         d->setFlag(Notify, false);
2122     }
2123 }
2124
2125 /*!
2126     Returns true if this property is readable; otherwise returns false.
2127     The default value is true.
2128
2129     \sa setReadable(), isWritable()
2130 */
2131 bool QMetaPropertyBuilder::isReadable() const
2132 {
2133     QMetaPropertyBuilderPrivate *d = d_func();
2134     if (d)
2135         return d->flag(Readable);
2136     else
2137         return false;
2138 }
2139
2140 /*!
2141     Returns true if this property is writable; otherwise returns false.
2142     The default value is true.
2143
2144     \sa setWritable(), isReadable()
2145 */
2146 bool QMetaPropertyBuilder::isWritable() const
2147 {
2148     QMetaPropertyBuilderPrivate *d = d_func();
2149     if (d)
2150         return d->flag(Writable);
2151     else
2152         return false;
2153 }
2154
2155 /*!
2156     Returns true if this property can be reset to a default value; otherwise
2157     returns false.  The default value is false.
2158
2159     \sa setResettable()
2160 */
2161 bool QMetaPropertyBuilder::isResettable() const
2162 {
2163     QMetaPropertyBuilderPrivate *d = d_func();
2164     if (d)
2165         return d->flag(Resettable);
2166     else
2167         return false;
2168 }
2169
2170 /*!
2171     Returns true if this property is designable; otherwise returns false.
2172     This default value is false.
2173
2174     \sa setDesignable(), isScriptable(), isStored()
2175 */
2176 bool QMetaPropertyBuilder::isDesignable() const
2177 {
2178     QMetaPropertyBuilderPrivate *d = d_func();
2179     if (d)
2180         return d->flag(Designable);
2181     else
2182         return false;
2183 }
2184
2185 /*!
2186     Returns true if the property is scriptable; otherwise returns false.
2187     This default value is true.
2188
2189     \sa setScriptable(), isDesignable(), isStored()
2190 */
2191 bool QMetaPropertyBuilder::isScriptable() const
2192 {
2193     QMetaPropertyBuilderPrivate *d = d_func();
2194     if (d)
2195         return d->flag(Scriptable);
2196     else
2197         return false;
2198 }
2199
2200 /*!
2201     Returns true if the property is stored; otherwise returns false.
2202     This default value is false.
2203
2204     \sa setStored(), isDesignable(), isScriptable()
2205 */
2206 bool QMetaPropertyBuilder::isStored() const
2207 {
2208     QMetaPropertyBuilderPrivate *d = d_func();
2209     if (d)
2210         return d->flag(Stored);
2211     else
2212         return false;
2213 }
2214
2215 /*!
2216     Returns true if the property is editable; otherwise returns false.
2217     This default value is false.
2218
2219     \sa setEditable(), isDesignable(), isScriptable(), isStored()
2220 */
2221 bool QMetaPropertyBuilder::isEditable() const
2222 {
2223     QMetaPropertyBuilderPrivate *d = d_func();
2224     if (d)
2225         return d->flag(Editable);
2226     else
2227         return false;
2228 }
2229
2230 /*!
2231     Returns true if this property is designated as the \c USER
2232     property, i.e., the one that the user can edit or that is
2233     significant in some other way.  Otherwise it returns
2234     false.  This default value is false.
2235
2236     \sa setUser(), isDesignable(), isScriptable()
2237 */
2238 bool QMetaPropertyBuilder::isUser() const
2239 {
2240     QMetaPropertyBuilderPrivate *d = d_func();
2241     if (d)
2242         return d->flag(User);
2243     else
2244         return false;
2245 }
2246
2247 /*!
2248     Returns true if the property has a C++ setter function that
2249     follows Qt's standard "name" / "setName" pattern. Designer and uic
2250     query hasStdCppSet() in order to avoid expensive
2251     QObject::setProperty() calls. All properties in Qt [should] follow
2252     this pattern.  The default value is false.
2253
2254     \sa setStdCppSet()
2255 */
2256 bool QMetaPropertyBuilder::hasStdCppSet() const
2257 {
2258     QMetaPropertyBuilderPrivate *d = d_func();
2259     if (d)
2260         return d->flag(StdCppSet);
2261     else
2262         return false;
2263 }
2264
2265 /*!
2266     Returns true if the property is an enumerator or flag type;
2267     otherwise returns false.  This default value is false.
2268
2269     \sa setEnumOrFlag()
2270 */
2271 bool QMetaPropertyBuilder::isEnumOrFlag() const
2272 {
2273     QMetaPropertyBuilderPrivate *d = d_func();
2274     if (d)
2275         return d->flag(EnumOrFlag);
2276     else
2277         return false;
2278 }
2279
2280 /*!
2281     Sets this property to readable if \a value is true.
2282
2283     \sa isReadable(), setWritable()
2284 */
2285 void QMetaPropertyBuilder::setReadable(bool value)
2286 {
2287     QMetaPropertyBuilderPrivate *d = d_func();
2288     if (d)
2289         d->setFlag(Readable, value);
2290 }
2291
2292 /*!
2293     Sets this property to writable if \a value is true.
2294
2295     \sa isWritable(), setReadable()
2296 */
2297 void QMetaPropertyBuilder::setWritable(bool value)
2298 {
2299     QMetaPropertyBuilderPrivate *d = d_func();
2300     if (d)
2301         d->setFlag(Writable, value);
2302 }
2303
2304 /*!
2305     Sets this property to resettable if \a value is true.
2306
2307     \sa isResettable()
2308 */
2309 void QMetaPropertyBuilder::setResettable(bool value)
2310 {
2311     QMetaPropertyBuilderPrivate *d = d_func();
2312     if (d)
2313         d->setFlag(Resettable, value);
2314 }
2315
2316 /*!
2317     Sets this property to designable if \a value is true.
2318
2319     \sa isDesignable(), setScriptable(), setStored()
2320 */
2321 void QMetaPropertyBuilder::setDesignable(bool value)
2322 {
2323     QMetaPropertyBuilderPrivate *d = d_func();
2324     if (d)
2325         d->setFlag(Designable, value);
2326 }
2327
2328 /*!
2329     Sets this property to scriptable if \a value is true.
2330
2331     \sa isScriptable(), setDesignable(), setStored()
2332 */
2333 void QMetaPropertyBuilder::setScriptable(bool value)
2334 {
2335     QMetaPropertyBuilderPrivate *d = d_func();
2336     if (d)
2337         d->setFlag(Scriptable, value);
2338 }
2339
2340 /*!
2341     Sets this property to storable if \a value is true.
2342
2343     \sa isStored(), setDesignable(), setScriptable()
2344 */
2345 void QMetaPropertyBuilder::setStored(bool value)
2346 {
2347     QMetaPropertyBuilderPrivate *d = d_func();
2348     if (d)
2349         d->setFlag(Stored, value);
2350 }
2351
2352 /*!
2353     Sets this property to editable if \a value is true.
2354
2355     \sa isEditable(), setDesignable(), setScriptable(), setStored()
2356 */
2357 void QMetaPropertyBuilder::setEditable(bool value)
2358 {
2359     QMetaPropertyBuilderPrivate *d = d_func();
2360     if (d)
2361         d->setFlag(Editable, value);
2362 }
2363
2364 /*!
2365     Sets the \c USER flag on this property to \a value.
2366
2367     \sa isUser(), setDesignable(), setScriptable()
2368 */
2369 void QMetaPropertyBuilder::setUser(bool value)
2370 {
2371     QMetaPropertyBuilderPrivate *d = d_func();
2372     if (d)
2373         d->setFlag(User, value);
2374 }
2375
2376 /*!
2377     Sets the C++ setter flag on this property to \a value, which is
2378     true if the property has a C++ setter function that follows Qt's
2379     standard "name" / "setName" pattern.
2380
2381     \sa hasStdCppSet()
2382 */
2383 void QMetaPropertyBuilder::setStdCppSet(bool value)
2384 {
2385     QMetaPropertyBuilderPrivate *d = d_func();
2386     if (d)
2387         d->setFlag(StdCppSet, value);
2388 }
2389
2390 /*!
2391     Sets this property to be of an enumerator or flag type if
2392     \a value is true.
2393
2394     \sa isEnumOrFlag()
2395 */
2396 void QMetaPropertyBuilder::setEnumOrFlag(bool value)
2397 {
2398     QMetaPropertyBuilderPrivate *d = d_func();
2399     if (d)
2400         d->setFlag(EnumOrFlag, value);
2401 }
2402
2403 /*!
2404     \class QMetaEnumBuilder
2405     \internal
2406     \brief The QMetaEnumBuilder class enables modifications to an enumerator definition on a meta object builder.
2407 */
2408
2409 QMetaEnumBuilderPrivate *QMetaEnumBuilder::d_func() const
2410 {
2411     if (_mobj && _index >= 0 && _index < _mobj->d->enumerators.size())
2412         return &(_mobj->d->enumerators[_index]);
2413     else
2414         return 0;
2415 }
2416
2417 /*!
2418     \fn QMetaEnumBuilder::QMetaEnumBuilder()
2419     \internal
2420 */
2421
2422 /*!
2423     \fn int QMetaEnumBuilder::index() const
2424
2425     Returns the index of this enumerator within its QMetaObjectBuilder.
2426 */
2427
2428 /*!
2429     Returns the name of the enumerator (without the scope).
2430 */
2431 QByteArray QMetaEnumBuilder::name() const
2432 {
2433     QMetaEnumBuilderPrivate *d = d_func();
2434     if (d)
2435         return d->name;
2436     else
2437         return QByteArray();
2438 }
2439
2440 /*!
2441     Returns true if this enumerator is used as a flag; otherwise returns
2442     false.
2443
2444     \sa setIsFlag()
2445 */
2446 bool QMetaEnumBuilder::isFlag() const
2447 {
2448     QMetaEnumBuilderPrivate *d = d_func();
2449     if (d)
2450         return d->isFlag;
2451     else
2452         return false;
2453 }
2454
2455 /*!
2456     Sets this enumerator to be used as a flag if \a value is true.
2457
2458     \sa isFlag()
2459 */
2460 void QMetaEnumBuilder::setIsFlag(bool value)
2461 {
2462     QMetaEnumBuilderPrivate *d = d_func();
2463     if (d)
2464         d->isFlag = value;
2465 }
2466
2467 /*!
2468     Returns the number of keys.
2469
2470     \sa key(), addKey()
2471 */
2472 int QMetaEnumBuilder::keyCount() const
2473 {
2474     QMetaEnumBuilderPrivate *d = d_func();
2475     if (d)
2476         return d->keys.size();
2477     else
2478         return 0;
2479 }
2480
2481 /*!
2482     Returns the key with the given \a index, or an empty QByteArray
2483     if no such key exists.
2484
2485     \sa keyCount(), addKey(), value()
2486 */
2487 QByteArray QMetaEnumBuilder::key(int index) const
2488 {
2489     QMetaEnumBuilderPrivate *d = d_func();
2490     if (d && index >= 0 && index < d->keys.size())
2491         return d->keys[index];
2492     else
2493         return QByteArray();
2494 }
2495
2496 /*!
2497     Returns the value with the given \a index; or returns -1 if there
2498     is no such value.
2499
2500     \sa keyCount(), addKey(), key()
2501 */
2502 int QMetaEnumBuilder::value(int index) const
2503 {
2504     QMetaEnumBuilderPrivate *d = d_func();
2505     if (d && index >= 0 && index < d->keys.size())
2506         return d->values[index];
2507     else
2508         return -1;
2509 }
2510
2511 /*!
2512     Adds a new key called \a name to this enumerator, associated
2513     with \a value.  Returns the index of the new key.
2514
2515     \sa keyCount(), key(), value(), removeKey()
2516 */
2517 int QMetaEnumBuilder::addKey(const QByteArray& name, int value)
2518 {
2519     QMetaEnumBuilderPrivate *d = d_func();
2520     if (d) {
2521         int index = d->keys.size();
2522         d->keys += name;
2523         d->values += value;
2524         return index;
2525     } else {
2526         return -1;
2527     }
2528 }
2529
2530 /*!
2531     Removes the key at \a index from this enumerator.
2532
2533     \sa addKey()
2534 */
2535 void QMetaEnumBuilder::removeKey(int index)
2536 {
2537     QMetaEnumBuilderPrivate *d = d_func();
2538     if (d && index >= 0 && index < d->keys.size()) {
2539         d->keys.removeAt(index);
2540         d->values.removeAt(index);
2541     }
2542 }
2543
2544 QT_END_NAMESPACE