1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
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
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "private/qmetaobjectbuilder_p.h"
47 \class QMetaObjectBuilder
49 \brief The QMetaObjectBuilder class supports building QMetaObject objects at runtime.
54 \enum QMetaObjectBuilder::AddMember
55 This enum defines which members of QMetaObject should be copied by QMetaObjectBuilder::addMetaObject()
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.
75 // copied from moc's generator.cpp
76 uint qvariant_nameToType(const char* name)
81 if (strcmp(name, "QVariant") == 0)
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;
92 uint tp = QMetaType::type(name);
93 return tp < QMetaType::User ? tp : 0;
97 Returns true if the type is a QVariant types.
99 bool isVariantType(const char* type)
101 return qvariant_nameToType(type) != 0;
104 // copied from qmetaobject.cpp
105 // do not touch without touching the moc as well
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,
119 ResolveStored = 0x00020000,
120 Editable = 0x00040000,
121 ResolveEditable = 0x00080000,
123 ResolveUser = 0x00200000,
125 Revisioned = 0x00800000
129 AccessPrivate = 0x00,
130 AccessProtected = 0x01,
132 AccessMask = 0x03, //mask
137 MethodConstructor = 0x0c,
138 MethodTypeMask = 0x0c,
140 MethodCompatibility = 0x10,
142 MethodScriptable = 0x40,
143 MethodRevisioned = 0x80
146 struct QMetaObjectPrivate
150 int classInfoCount, classInfoData;
151 int methodCount, methodData;
152 int propertyCount, propertyData;
153 int enumeratorCount, enumeratorData;
154 int constructorCount, constructorData;
158 static inline const QMetaObjectPrivate *priv(const uint* data)
159 { return reinterpret_cast<const QMetaObjectPrivate*>(data); }
160 // end of copied lines from qmetaobject.cpp
162 class QMetaMethodBuilderPrivate
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))
176 QByteArray signature;
177 QByteArray returnType;
178 QList<QByteArray> parameterNames;
182 QMetaMethod::MethodType methodType() const
184 return (QMetaMethod::MethodType)((attributes & MethodTypeMask) >> 2);
187 QMetaMethod::Access access() const
189 return (QMetaMethod::Access)(attributes & AccessMask);
192 void setAccess(QMetaMethod::Access value)
194 attributes = ((attributes & ~AccessMask) | (int)value);
198 class QMetaPropertyBuilderPrivate
201 QMetaPropertyBuilderPrivate
202 (const QByteArray& _name, const QByteArray& _type, int notifierIdx=-1)
204 type(QMetaObject::normalizedType(_type.constData())),
205 flags(Readable | Writable | Scriptable), notifySignal(-1)
207 if (notifierIdx >= 0) {
209 notifySignal = notifierIdx;
218 bool flag(int f) const
220 return ((flags & f) != 0);
223 void setFlag(int f, bool value)
232 class QMetaEnumBuilderPrivate
235 QMetaEnumBuilderPrivate(const QByteArray& _name)
236 : name(_name), isFlag(false)
242 QList<QByteArray> keys;
246 class QMetaObjectBuilderPrivate
249 QMetaObjectBuilderPrivate()
252 superClass = &QObject::staticMetaObject;
253 staticMetacallFunction = 0;
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;
268 QList<const QMetaObject *> relatedMetaObjects;
274 Constructs a new QMetaObjectBuilder.
276 QMetaObjectBuilder::QMetaObjectBuilder()
278 d = new QMetaObjectBuilderPrivate();
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.
287 The \a members parameter indicates which members of \a prototype
288 should be added. The default is AllMembers.
292 QMetaObjectBuilder::QMetaObjectBuilder
293 (const QMetaObject *prototype, QMetaObjectBuilder::AddMembers members)
295 d = new QMetaObjectBuilderPrivate();
296 addMetaObject(prototype, members);
300 Destroys this meta object builder.
302 QMetaObjectBuilder::~QMetaObjectBuilder()
308 Returns the name of the class being constructed by this
309 meta object builder. The default value is an empty QByteArray.
311 \sa setClassName(), superClass()
313 QByteArray QMetaObjectBuilder::className() const
319 Sets the \a name of the class being constructed by this
322 \sa className(), setSuperClass()
324 void QMetaObjectBuilder::setClassName(const QByteArray& name)
330 Returns the superclass meta object of the class being constructed
331 by this meta object builder. The default value is the meta object
334 \sa setSuperClass(), className()
336 const QMetaObject *QMetaObjectBuilder::superClass() const
338 return d->superClass;
342 Sets the superclass meta object of the class being constructed
343 by this meta object builder to \a meta. The \a meta parameter
346 \sa superClass(), setClassName()
348 void QMetaObjectBuilder::setSuperClass(const QMetaObject *meta)
351 d->superClass = meta;
355 Returns the flags of the class being constructed by this meta object
360 QMetaObjectBuilder::MetaObjectFlags QMetaObjectBuilder::flags() const
362 return (QMetaObjectBuilder::MetaObjectFlags)d->flags;
366 Sets the \a flags of the class being constructed by this meta object
371 void QMetaObjectBuilder::setFlags(MetaObjectFlags flags)
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.
381 \sa addMethod(), method(), removeMethod(), indexOfMethod()
383 int QMetaObjectBuilder::methodCount() const
385 return d->methods.size();
389 Returns the number of constructors in this class.
391 \sa addConstructor(), constructor(), removeConstructor(), indexOfConstructor()
393 int QMetaObjectBuilder::constructorCount() const
395 return d->constructors.size();
399 Returns the number of properties in this class, excluding the number
400 of properties in the base class.
402 \sa addProperty(), property(), removeProperty(), indexOfProperty()
404 int QMetaObjectBuilder::propertyCount() const
406 return d->properties.size();
410 Returns the number of enumerators in this class, excluding the
411 number of enumerators in the base class.
413 \sa addEnumerator(), enumerator(), removeEnumerator()
414 \sa indexOfEnumerator()
416 int QMetaObjectBuilder::enumeratorCount() const
418 return d->enumerators.size();
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.
425 \sa addClassInfo(), classInfoName(), classInfoValue(), removeClassInfo()
426 \sa indexOfClassInfo()
428 int QMetaObjectBuilder::classInfoCount() const
430 return d->classInfoNames.size();
434 Returns the number of related meta objects that are associated
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.
441 \sa addRelatedMetaObject(), relatedMetaObject()
442 \sa removeRelatedMetaObject()
444 int QMetaObjectBuilder::relatedMetaObjectCount() const
446 return d->relatedMetaObjects.size();
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
455 \sa method(), methodCount(), removeMethod(), indexOfMethod()
457 QMetaMethodBuilder QMetaObjectBuilder::addMethod(const QByteArray& signature)
459 int index = d->methods.size();
460 d->methods.append(QMetaMethodBuilderPrivate(QMetaMethod::Method, signature));
461 return QMetaMethodBuilder(this, index);
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.
472 \sa method(), methodCount(), removeMethod(), indexOfMethod()
474 QMetaMethodBuilder QMetaObjectBuilder::addMethod
475 (const QByteArray& signature, const QByteArray& returnType)
477 int index = d->methods.size();
478 d->methods.append(QMetaMethodBuilderPrivate
479 (QMetaMethod::Method, signature, returnType));
480 return QMetaMethodBuilder(this, index);
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.
489 This function will detect if \a prototype is an ordinary method,
490 signal, slot, or constructor and act accordingly.
492 \sa method(), methodCount(), removeMethod(), indexOfMethod()
494 QMetaMethodBuilder QMetaObjectBuilder::addMethod(const QMetaMethod& prototype)
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());
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
519 \sa addMethod(), addSignal(), indexOfSlot()
521 QMetaMethodBuilder QMetaObjectBuilder::addSlot(const QByteArray& signature)
523 int index = d->methods.size();
524 d->methods.append(QMetaMethodBuilderPrivate(QMetaMethod::Slot, signature));
525 return QMetaMethodBuilder(this, index);
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
534 \sa addMethod(), addSlot(), indexOfSignal()
536 QMetaMethodBuilder QMetaObjectBuilder::addSignal(const QByteArray& signature)
538 int index = d->methods.size();
539 d->methods.append(QMetaMethodBuilderPrivate
540 (QMetaMethod::Signal, signature, QByteArray(), QMetaMethod::Protected));
541 return QMetaMethodBuilder(this, index);
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
550 \sa constructor(), constructorCount(), removeConstructor()
551 \sa indexOfConstructor()
553 QMetaMethodBuilder QMetaObjectBuilder::addConstructor(const QByteArray& signature)
555 int index = d->constructors.size();
556 d->constructors.append(QMetaMethodBuilderPrivate(QMetaMethod::Constructor, signature));
557 return QMetaMethodBuilder(this, -(index + 1));
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.
566 This function requires that \a prototype be a constructor.
568 \sa constructor(), constructorCount(), removeConstructor()
569 \sa indexOfConstructor()
571 QMetaMethodBuilder QMetaObjectBuilder::addConstructor(const QMetaMethod& prototype)
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());
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.
590 \sa property(), propertyCount(), removeProperty(), indexOfProperty()
592 QMetaPropertyBuilder QMetaObjectBuilder::addProperty
593 (const QByteArray& name, const QByteArray& type, int notifierId)
595 int index = d->properties.size();
596 d->properties.append(QMetaPropertyBuilderPrivate(name, type, notifierId));
597 return QMetaPropertyBuilder(this, index);
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.
606 \sa property(), propertyCount(), removeProperty(), indexOfProperty()
608 QMetaPropertyBuilder QMetaObjectBuilder::addProperty(const QMetaProperty& prototype)
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());
626 index = addMethod(method).index();
627 d->properties[property._index].notifySignal = index;
628 d->properties[property._index].setFlag(Notify, true);
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.
638 \sa enumerator(), enumeratorCount(), removeEnumerator(),
639 \sa indexOfEnumerator()
641 QMetaEnumBuilder QMetaObjectBuilder::addEnumerator(const QByteArray& name)
643 int index = d->enumerators.size();
644 d->enumerators.append(QMetaEnumBuilderPrivate(name));
645 return QMetaEnumBuilder(this, index);
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.
654 \sa enumerator(), enumeratorCount(), removeEnumerator(),
655 \sa indexOfEnumerator()
657 QMetaEnumBuilder QMetaObjectBuilder::addEnumerator(const QMetaEnum& prototype)
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));
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.
671 \sa classInfoCount(), classInfoName(), classInfoValue(), removeClassInfo()
672 \sa indexOfClassInfo()
674 int QMetaObjectBuilder::addClassInfo(const QByteArray& name, const QByteArray& value)
676 int index = d->classInfoNames.size();
677 d->classInfoNames += name;
678 d->classInfoValues += value;
683 Adds \a meta to this class as a related meta object. Returns
684 the index of the new related meta object entry.
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.
690 \sa relatedMetaObjectCount(), relatedMetaObject()
691 \sa removeRelatedMetaObject()
693 #ifdef Q_NO_DATA_RELOCATION
694 int QMetaObjectBuilder::addRelatedMetaObject(const QMetaObjectAccessor &meta)
696 int QMetaObjectBuilder::addRelatedMetaObject(const QMetaObject *meta)
700 int index = d->relatedMetaObjects.size();
701 d->relatedMetaObjects.append(meta);
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.
709 The \a members parameter indicates which members of \a prototype
710 should be added. The default is AllMembers.
712 void QMetaObjectBuilder::addMetaObject
713 (const QMetaObject *prototype, QMetaObjectBuilder::AddMembers members)
718 if ((members & ClassName) != 0)
719 d->className = prototype->className();
721 if ((members & SuperClass) != 0)
722 d->superClass = prototype->superClass();
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)
730 if (method.access() == QMetaMethod::Private && (members & PrivateMethods) == 0)
732 if (method.access() == QMetaMethod::Protected && (members & ProtectedMethods) == 0)
735 if (method.methodType() == QMetaMethod::Method && (members & Methods) != 0) {
737 } else if (method.methodType() == QMetaMethod::Signal &&
738 (members & Signals) != 0) {
740 } else if (method.methodType() == QMetaMethod::Slot &&
741 (members & Slots) != 0) {
747 if ((members & Constructors) != 0) {
748 for (index = 0; index < prototype->constructorCount(); ++index)
749 addConstructor(prototype->constructor(index));
752 if ((members & Properties) != 0) {
753 for (index = prototype->propertyOffset(); index < prototype->propertyCount(); ++index)
754 addProperty(prototype->property(index));
757 if ((members & Enumerators) != 0) {
758 for (index = prototype->enumeratorOffset(); index < prototype->enumeratorCount(); ++index)
759 addEnumerator(prototype->enumerator(index));
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());
769 if ((members & RelatedMetaObjects) != 0) {
770 #ifdef Q_NO_DATA_RELOCATION
771 const QMetaObjectAccessor *objects = 0;
773 const QMetaObject **objects;
774 if (priv(prototype->d.data)->revision < 2) {
775 objects = (const QMetaObject **)(prototype->d.extradata);
779 const QMetaObjectExtraData *extra = (const QMetaObjectExtraData *)(prototype->d.extradata);
781 objects = extra->objects;
786 while (*objects != 0) {
787 addRelatedMetaObject(*objects);
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);
804 Returns the method at \a index in this class.
806 \sa methodCount(), addMethod(), removeMethod(), indexOfMethod()
808 QMetaMethodBuilder QMetaObjectBuilder::method(int index) const
810 if (index >= 0 && index < d->methods.size())
811 return QMetaMethodBuilder(this, index);
813 return QMetaMethodBuilder();
817 Returns the constructor at \a index in this class.
819 \sa methodCount(), addMethod(), removeMethod(), indexOfConstructor()
821 QMetaMethodBuilder QMetaObjectBuilder::constructor(int index) const
823 if (index >= 0 && index < d->constructors.size())
824 return QMetaMethodBuilder(this, -(index + 1));
826 return QMetaMethodBuilder();
830 Returns the property at \a index in this class.
832 \sa methodCount(), addMethod(), removeMethod(), indexOfProperty()
834 QMetaPropertyBuilder QMetaObjectBuilder::property(int index) const
836 if (index >= 0 && index < d->properties.size())
837 return QMetaPropertyBuilder(this, index);
839 return QMetaPropertyBuilder();
843 Returns the enumerator at \a index in this class.
845 \sa enumeratorCount(), addEnumerator(), removeEnumerator()
846 \sa indexOfEnumerator()
848 QMetaEnumBuilder QMetaObjectBuilder::enumerator(int index) const
850 if (index >= 0 && index < d->enumerators.size())
851 return QMetaEnumBuilder(this, index);
853 return QMetaEnumBuilder();
857 Returns the related meta object at \a index in this class.
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.
863 \sa relatedMetaObjectCount(), addRelatedMetaObject()
864 \sa removeRelatedMetaObject()
866 const QMetaObject *QMetaObjectBuilder::relatedMetaObject(int index) const
868 if (index >= 0 && index < d->relatedMetaObjects.size())
869 #ifdef Q_NO_DATA_RELOCATION
870 return &((*(d->relatedMetaObjects[index]))());
872 return d->relatedMetaObjects[index];
879 Returns the name of the item of class information at \a index
882 \sa classInfoCount(), addClassInfo(), classInfoValue(), removeClassInfo()
883 \sa indexOfClassInfo()
885 QByteArray QMetaObjectBuilder::classInfoName(int index) const
887 if (index >= 0 && index < d->classInfoNames.size())
888 return d->classInfoNames[index];
894 Returns the value of the item of class information at \a index
897 \sa classInfoCount(), addClassInfo(), classInfoName(), removeClassInfo()
898 \sa indexOfClassInfo()
900 QByteArray QMetaObjectBuilder::classInfoValue(int index) const
902 if (index >= 0 && index < d->classInfoValues.size())
903 return d->classInfoValues[index];
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.
914 \sa methodCount(), addMethod(), method(), indexOfMethod()
916 void QMetaObjectBuilder::removeMethod(int index)
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)--;
932 Removes the constructor at \a index from this class. The indices of
933 all following constructors will be adjusted downwards by 1.
935 \sa constructorCount(), addConstructor(), constructor()
936 \sa indexOfConstructor()
938 void QMetaObjectBuilder::removeConstructor(int index)
940 if (index >= 0 && index < d->constructors.size())
941 d->constructors.removeAt(index);
945 Removes the property at \a index from this class. The indices of
946 all following properties will be adjusted downwards by 1.
948 \sa propertyCount(), addProperty(), property(), indexOfProperty()
950 void QMetaObjectBuilder::removeProperty(int index)
952 if (index >= 0 && index < d->properties.size())
953 d->properties.removeAt(index);
957 Removes the enumerator at \a index from this class. The indices of
958 all following enumerators will be adjusted downwards by 1.
960 \sa enumertorCount(), addEnumerator(), enumerator()
961 \sa indexOfEnumerator()
963 void QMetaObjectBuilder::removeEnumerator(int index)
965 if (index >= 0 && index < d->enumerators.size())
966 d->enumerators.removeAt(index);
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.
973 \sa classInfoCount(), addClassInfo(), classInfoName(), classInfoValue()
974 \sa indexOfClassInfo()
976 void QMetaObjectBuilder::removeClassInfo(int index)
978 if (index >= 0 && index < d->classInfoNames.size()) {
979 d->classInfoNames.removeAt(index);
980 d->classInfoValues.removeAt(index);
985 Removes the related meta object at \a index from this class.
986 The indices of all following related meta objects will be adjusted
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.
993 \sa relatedMetaObjectCount(), addRelatedMetaObject()
994 \sa relatedMetaObject()
996 void QMetaObjectBuilder::removeRelatedMetaObject(int index)
998 if (index >= 0 && index < d->relatedMetaObjects.size())
999 d->relatedMetaObjects.removeAt(index);
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.
1006 \sa method(), methodCount(), addMethod(), removeMethod()
1008 int QMetaObjectBuilder::indexOfMethod(const QByteArray& signature)
1010 QByteArray sig = QMetaObject::normalizedSignature(signature);
1011 for (int index = 0; index < d->methods.size(); ++index) {
1012 if (sig == d->methods[index].signature)
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.
1022 \sa indexOfMethod(), indexOfSlot()
1024 int QMetaObjectBuilder::indexOfSignal(const QByteArray& signature)
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)
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.
1039 \sa indexOfMethod(), indexOfSignal()
1041 int QMetaObjectBuilder::indexOfSlot(const QByteArray& signature)
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)
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.
1056 \sa constructor(), constructorCount(), addConstructor(), removeConstructor()
1058 int QMetaObjectBuilder::indexOfConstructor(const QByteArray& signature)
1060 QByteArray sig = QMetaObject::normalizedSignature(signature);
1061 for (int index = 0; index < d->constructors.size(); ++index) {
1062 if (sig == d->constructors[index].signature)
1069 Finds a property with the specified \a name and returns its index;
1070 otherwise returns -1.
1072 \sa property(), propertyCount(), addProperty(), removeProperty()
1074 int QMetaObjectBuilder::indexOfProperty(const QByteArray& name)
1076 for (int index = 0; index < d->properties.size(); ++index) {
1077 if (name == d->properties[index].name)
1084 Finds an enumerator with the specified \a name and returns its index;
1085 otherwise returns -1.
1087 \sa enumertor(), enumeratorCount(), addEnumerator(), removeEnumerator()
1089 int QMetaObjectBuilder::indexOfEnumerator(const QByteArray& name)
1091 for (int index = 0; index < d->enumerators.size(); ++index) {
1092 if (name == d->enumerators[index].name)
1099 Finds an item of class information with the specified \a name and
1100 returns its index; otherwise returns -1.
1102 \sa classInfoName(), classInfoValue(), classInfoCount(), addClassInfo()
1103 \sa removeClassInfo()
1105 int QMetaObjectBuilder::indexOfClassInfo(const QByteArray& name)
1107 for (int index = 0; index < d->classInfoNames.size(); ++index) {
1108 if (name == d->classInfoNames[index])
1114 // Align on a specific type boundary.
1115 #define ALIGN(size,type) \
1116 (size) = ((size) + sizeof(type) - 1) & ~(sizeof(type) - 1)
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)
1123 if (value.size() == 0 && empty >= 0)
1126 memcpy(str + *offset, value.constData(), value.size());
1127 str[*offset + value.size()] = '\0';
1130 *offset += value.size() + 1;
1134 // Build the parameter array string for a method.
1135 static QByteArray buildParameterNames
1136 (const QByteArray& signature, const QList<QByteArray>& parameterNames)
1138 // If the parameter name list is specified, then concatenate them.
1139 if (!parameterNames.isEmpty()) {
1142 foreach (const QByteArray &name, parameterNames) {
1152 // Count commas in the signature, excluding those inside template arguments.
1153 int index = signature.indexOf('(');
1155 return QByteArray();
1157 if (index >= signature.size())
1158 return QByteArray();
1159 if (signature[index] == ')')
1160 return QByteArray();
1163 while (index < signature.size() && signature[index] != ',') {
1164 char ch = signature[index++];
1169 else if (ch == ',' && brackets <= 0)
1172 return QByteArray(count - 1, ',');
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,
1186 bool hasNotifySignals = false;
1189 (d->relatedMetaObjects.size() > 0 || d->staticMetacallFunction))
1192 // Create the main QMetaObject structure at the start of the buffer.
1193 QMetaObject *meta = reinterpret_cast<QMetaObject *>(buf);
1194 size += sizeof(QMetaObject);
1197 if (!relocatable) meta->d.superdata = d->superClass;
1198 meta->d.extradata = 0;
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;
1213 pmeta->revision = 3;
1214 pmeta->flags = d->flags;
1215 pmeta->className = 0; // Class name is always the first string.
1217 pmeta->classInfoCount = d->classInfoNames.size();
1218 pmeta->classInfoData = dataIndex;
1219 dataIndex += 2 * d->classInfoNames.size();
1221 pmeta->methodCount = d->methods.size();
1222 pmeta->methodData = dataIndex;
1223 dataIndex += 5 * d->methods.size();
1225 pmeta->propertyCount = d->properties.size();
1226 pmeta->propertyData = dataIndex;
1227 dataIndex += 3 * d->properties.size();
1228 if (hasNotifySignals)
1229 dataIndex += d->properties.size();
1231 pmeta->enumeratorCount = d->enumerators.size();
1232 pmeta->enumeratorData = dataIndex;
1233 dataIndex += 4 * d->enumerators.size();
1235 pmeta->constructorCount = d->constructors.size();
1236 pmeta->constructorData = dataIndex;
1237 dataIndex += 5 * d->constructors.size();
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();
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();
1255 // Zero terminator at the end of the data offset table.
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);
1264 meta->d.stringdata = reinterpret_cast<const char *>((quintptr)size);
1265 meta->d.data = reinterpret_cast<uint *>((quintptr)pmetaSize);
1267 meta->d.stringdata = str;
1268 meta->d.data = reinterpret_cast<uint *>(data);
1272 // Reset the current data position to just past the QMetaObjectPrivate.
1275 // Add the class name to the string table.
1277 buildString(buf, str, &offset, d->className, -1);
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);
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);
1288 data[dataIndex] = name;
1289 data[dataIndex + 1] = value;
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);
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;
1306 data[dataIndex] = sig;
1307 data[dataIndex + 1] = params;
1308 data[dataIndex + 2] = ret;
1309 data[dataIndex + 3] = tag;
1310 data[dataIndex + 4] = attrs;
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;
1322 if (!isVariantType(prop->type)) {
1323 flags |= EnumOrFlag;
1325 flags |= qvariant_nameToType(prop->type) << 24;
1329 data[dataIndex] = name;
1330 data[dataIndex + 1] = type;
1331 data[dataIndex + 2] = flags;
1335 if (hasNotifySignals) {
1336 for (index = 0; index < d->properties.size(); ++index) {
1337 QMetaPropertyBuilderPrivate *prop = &(d->properties[index]);
1339 if (prop->notifySignal != -1)
1340 data[dataIndex] = prop->notifySignal;
1342 data[dataIndex] = 0;
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;
1356 data[dataIndex] = name;
1357 data[dataIndex + 1] = isFlag;
1358 data[dataIndex + 2] = count;
1359 data[dataIndex + 3] = enumOffset;
1361 for (int key = 0; key < count; ++key) {
1362 int keyIndex = buildString(buf, str, &offset, enumerator->keys[key], empty);
1364 data[enumOffset++] = keyIndex;
1365 data[enumOffset++] = enumerator->values[key];
1369 enumIndex += 2 * count;
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);
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;
1384 data[dataIndex] = sig;
1385 data[dataIndex + 1] = params;
1386 data[dataIndex + 2] = ret;
1387 data[dataIndex + 3] = tag;
1388 data[dataIndex + 4] = attrs;
1393 // One more empty string to act as a terminator.
1394 buildString(buf, str, &offset, QByteArray(), -1);
1397 // Output the zero terminator in the data array.
1399 data[enumIndex] = 0;
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);
1413 const QMetaObject **objects =
1414 reinterpret_cast<const QMetaObject **>(buf + size);
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];
1425 extra->static_metacall = d->staticMetacallFunction;
1426 meta->d.extradata = reinterpret_cast<void *>(extra);
1428 if (d->relatedMetaObjects.size() > 0)
1429 size += sizeof(QMetaObject *) * (d->relatedMetaObjects.size() + 1);
1432 // Align the final size and return it.
1433 ALIGN(size, void *);
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.
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
1447 QMetaObject *QMetaObjectBuilder::toMetaObject() const
1449 int size = buildMetaObject(d, 0, false);
1450 char *buf = reinterpret_cast<char *>(qMalloc(size));
1451 memset(buf, 0, size);
1452 buildMetaObject(d, buf, false);
1453 return reinterpret_cast<QMetaObject *>(buf);
1459 Converts this meta object builder into relocatable data. This data can
1460 be stored, copied and later passed to fromRelocatableData() to create a
1461 concrete QMetaObject.
1463 The data is specific to the architecture on which it was created, but is not
1464 specific to the process that created it. Not all meta object builder's can
1465 be converted to data in this way. If \a ok is provided, it will be set to
1466 true if the conversion succeeds, and false otherwise. If a
1467 staticMetacallFunction() or any relatedMetaObject()'s are specified the
1468 conversion to relocatable data will fail.
1470 QByteArray QMetaObjectBuilder::toRelocatableData(bool *ok) const
1472 int size = buildMetaObject(d, 0, true);
1474 if (ok) *ok = false;
1475 return QByteArray();
1480 char *buf = data.data();
1481 memset(buf, 0, size);
1482 buildMetaObject(d, buf, true);
1490 Sets the \a data returned from toRelocatableData() onto a concrete
1491 QMetaObject instance, \a output. As the meta object's super class is not
1492 saved in the relocatable data, it must be passed as \a superClass.
1494 void QMetaObjectBuilder::fromRelocatableData(QMetaObject *output,
1495 const QMetaObject *superclass,
1496 const QByteArray &data)
1501 const char *buf = data.constData();
1502 const QMetaObject *dataMo = reinterpret_cast<const QMetaObject *>(buf);
1504 quintptr stringdataOffset = (quintptr)dataMo->d.stringdata;
1505 quintptr dataOffset = (quintptr)dataMo->d.data;
1507 output->d.superdata = superclass;
1508 output->d.stringdata = buf + stringdataOffset;
1509 output->d.data = reinterpret_cast<const uint *>(buf + dataOffset);
1513 \typedef QMetaObjectBuilder::StaticMetacallFunction
1515 Typedef for static metacall functions. The three parameters are
1516 the call type value, the constructor index, and the
1517 array of parameters.
1521 Returns the static metacall function to use to construct objects
1522 of this class. The default value is null.
1524 \sa setStaticMetacallFunction()
1526 QMetaObjectBuilder::StaticMetacallFunction QMetaObjectBuilder::staticMetacallFunction() const
1528 return d->staticMetacallFunction;
1532 Sets the static metacall function to use to construct objects
1533 of this class to \a value. The default value is null.
1535 \sa staticMetacallFunction()
1537 void QMetaObjectBuilder::setStaticMetacallFunction
1538 (QMetaObjectBuilder::StaticMetacallFunction value)
1540 d->staticMetacallFunction = value;
1543 #ifndef QT_NO_DATASTREAM
1546 Serializes the contents of the meta object builder onto \a stream.
1550 void QMetaObjectBuilder::serialize(QDataStream& stream) const
1554 // Write the class and super class names.
1555 stream << d->className;
1557 stream << QByteArray(d->superClass->className());
1559 stream << QByteArray();
1561 // Write the counts for each type of class member.
1562 stream << d->classInfoNames.size();
1563 stream << d->methods.size();
1564 stream << d->properties.size();
1565 stream << d->enumerators.size();
1566 stream << d->constructors.size();
1567 stream << d->relatedMetaObjects.size();
1569 // Write the items of class information.
1570 for (index = 0; index < d->classInfoNames.size(); ++index) {
1571 stream << d->classInfoNames[index];
1572 stream << d->classInfoValues[index];
1575 // Write the methods.
1576 for (index = 0; index < d->methods.size(); ++index) {
1577 const QMetaMethodBuilderPrivate *method = &(d->methods[index]);
1578 stream << method->signature;
1579 stream << method->returnType;
1580 stream << method->parameterNames;
1581 stream << method->tag;
1582 stream << method->attributes;
1585 // Write the properties.
1586 for (index = 0; index < d->properties.size(); ++index) {
1587 const QMetaPropertyBuilderPrivate *property = &(d->properties[index]);
1588 stream << property->name;
1589 stream << property->type;
1590 stream << property->flags;
1591 stream << property->notifySignal;
1594 // Write the enumerators.
1595 for (index = 0; index < d->enumerators.size(); ++index) {
1596 const QMetaEnumBuilderPrivate *enumerator = &(d->enumerators[index]);
1597 stream << enumerator->name;
1598 stream << enumerator->isFlag;
1599 stream << enumerator->keys;
1600 stream << enumerator->values;
1603 // Write the constructors.
1604 for (index = 0; index < d->constructors.size(); ++index) {
1605 const QMetaMethodBuilderPrivate *method = &(d->constructors[index]);
1606 stream << method->signature;
1607 stream << method->returnType;
1608 stream << method->parameterNames;
1609 stream << method->tag;
1610 stream << method->attributes;
1613 // Write the related meta objects.
1614 #ifdef Q_NO_DATA_RELOCATION
1615 //### What do we do here?
1617 for (index = 0; index < d->relatedMetaObjects.size(); ++index) {
1618 const QMetaObject *meta = d->relatedMetaObjects[index];
1619 stream << QByteArray(meta->className());
1623 // Add an extra empty QByteArray for additional data in future versions.
1624 // This should help maintain backwards compatibility, allowing older
1625 // versions to read newer data.
1626 stream << QByteArray();
1629 // Resolve a class name using the name reference map.
1630 static const QMetaObject *resolveClassName
1631 (const QMap<QByteArray, const QMetaObject *>& references,
1632 const QByteArray& name)
1634 if (name == QByteArray("QObject"))
1635 return &QObject::staticMetaObject;
1637 return references.value(name, 0);
1641 Deserializes a meta object builder from \a stream into
1642 this meta object builder.
1644 The \a references parameter specifies a mapping from class names
1645 to QMetaObject instances for resolving the super class name and
1646 related meta objects in the object that is deserialized.
1647 The meta object for QObject is implicitly added to \a references
1648 and does not need to be supplied.
1650 The QDataStream::status() value on \a stream will be set to
1651 QDataStream::ReadCorruptData if the input data is corrupt.
1652 The status will be set to QDataStream::ReadPastEnd if the
1653 input was exhausted before the full meta object was read.
1657 void QMetaObjectBuilder::deserialize
1658 (QDataStream& stream,
1659 const QMap<QByteArray, const QMetaObject *>& references)
1662 const QMetaObject *cl;
1665 // Clear all members in the builder to their default states.
1666 d->className.clear();
1667 d->superClass = &QObject::staticMetaObject;
1668 d->classInfoNames.clear();
1669 d->classInfoValues.clear();
1671 d->properties.clear();
1672 d->enumerators.clear();
1673 d->constructors.clear();
1674 d->relatedMetaObjects.clear();
1675 d->staticMetacallFunction = 0;
1677 // Read the class and super class names.
1678 stream >> d->className;
1680 if (name.isEmpty()) {
1682 } else if ((cl = resolveClassName(references, name)) != 0) {
1685 stream.setStatus(QDataStream::ReadCorruptData);
1689 // Read the counts for each type of class member.
1690 int classInfoCount, methodCount, propertyCount;
1691 int enumeratorCount, constructorCount, relatedMetaObjectCount;
1692 stream >> classInfoCount;
1693 stream >> methodCount;
1694 stream >> propertyCount;
1695 stream >> enumeratorCount;
1696 stream >> constructorCount;
1697 stream >> relatedMetaObjectCount;
1698 if (classInfoCount < 0 || methodCount < 0 ||
1699 propertyCount < 0 || enumeratorCount < 0 ||
1700 constructorCount < 0 || relatedMetaObjectCount < 0) {
1701 stream.setStatus(QDataStream::ReadCorruptData);
1705 // Read the items of class information.
1706 for (index = 0; index < classInfoCount; ++index) {
1707 if (stream.status() != QDataStream::Ok)
1712 addClassInfo(name, value);
1715 // Read the member methods.
1716 for (index = 0; index < methodCount; ++index) {
1717 if (stream.status() != QDataStream::Ok)
1721 QMetaMethodBuilderPrivate *method = &(d->methods[index]);
1722 stream >> method->returnType;
1723 stream >> method->parameterNames;
1724 stream >> method->tag;
1725 stream >> method->attributes;
1726 if (method->methodType() == QMetaMethod::Constructor) {
1727 // Cannot add a constructor in this set of methods.
1728 stream.setStatus(QDataStream::ReadCorruptData);
1733 // Read the properties.
1734 for (index = 0; index < propertyCount; ++index) {
1735 if (stream.status() != QDataStream::Ok)
1740 addProperty(name, type);
1741 QMetaPropertyBuilderPrivate *property = &(d->properties[index]);
1742 stream >> property->flags;
1743 stream >> property->notifySignal;
1744 if (property->notifySignal < -1 ||
1745 property->notifySignal >= d->methods.size()) {
1746 // Notify signal method index is out of range.
1747 stream.setStatus(QDataStream::ReadCorruptData);
1750 if (property->notifySignal >= 0 &&
1751 d->methods[property->notifySignal].methodType() != QMetaMethod::Signal) {
1752 // Notify signal method index does not refer to a signal.
1753 stream.setStatus(QDataStream::ReadCorruptData);
1758 // Read the enumerators.
1759 for (index = 0; index < enumeratorCount; ++index) {
1760 if (stream.status() != QDataStream::Ok)
1763 addEnumerator(name);
1764 QMetaEnumBuilderPrivate *enumerator = &(d->enumerators[index]);
1765 stream >> enumerator->isFlag;
1766 stream >> enumerator->keys;
1767 stream >> enumerator->values;
1768 if (enumerator->keys.size() != enumerator->values.size()) {
1769 // Mismatch between number of keys and number of values.
1770 stream.setStatus(QDataStream::ReadCorruptData);
1775 // Read the constructor methods.
1776 for (index = 0; index < constructorCount; ++index) {
1777 if (stream.status() != QDataStream::Ok)
1780 addConstructor(name);
1781 QMetaMethodBuilderPrivate *method = &(d->constructors[index]);
1782 stream >> method->returnType;
1783 stream >> method->parameterNames;
1784 stream >> method->tag;
1785 stream >> method->attributes;
1786 if (method->methodType() != QMetaMethod::Constructor) {
1787 // The type must be Constructor.
1788 stream.setStatus(QDataStream::ReadCorruptData);
1793 // Read the related meta objects.
1794 #ifdef Q_NO_DATA_RELOCATION
1795 //### What do we do here
1797 for (index = 0; index < relatedMetaObjectCount; ++index) {
1798 if (stream.status() != QDataStream::Ok)
1801 cl = resolveClassName(references, name);
1803 stream.setStatus(QDataStream::ReadCorruptData);
1806 addRelatedMetaObject(cl);
1810 // Read the extra data block, which is reserved for future use.
1814 #endif // !QT_NO_DATASTREAM
1817 \class QMetaMethodBuilder
1819 \brief The QMetaMethodBuilder class enables modifications to a method definition on a meta object builder.
1822 QMetaMethodBuilderPrivate *QMetaMethodBuilder::d_func() const
1824 // Positive indices indicate methods, negative indices indicate constructors.
1825 if (_mobj && _index >= 0 && _index < _mobj->d->methods.size())
1826 return &(_mobj->d->methods[_index]);
1827 else if (_mobj && -_index >= 1 && -_index <= _mobj->d->constructors.size())
1828 return &(_mobj->d->constructors[(-_index) - 1]);
1834 \fn QMetaMethodBuilder::QMetaMethodBuilder()
1839 Returns the index of this method within its QMetaObjectBuilder.
1841 int QMetaMethodBuilder::index() const
1844 return _index; // Method, signal, or slot
1846 return (-_index) - 1; // Constructor
1850 Returns the type of this method (signal, slot, method, or constructor).
1852 QMetaMethod::MethodType QMetaMethodBuilder::methodType() const
1854 QMetaMethodBuilderPrivate *d = d_func();
1856 return d->methodType();
1858 return QMetaMethod::Method;
1862 Returns the signature of this method.
1864 \sa parameterNames(), returnType()
1866 QByteArray QMetaMethodBuilder::signature() const
1868 QMetaMethodBuilderPrivate *d = d_func();
1870 return d->signature;
1872 return QByteArray();
1876 Returns the return type for this method; empty if the method's
1877 return type is \c{void}.
1879 \sa setReturnType(), signature()
1881 QByteArray QMetaMethodBuilder::returnType() const
1883 QMetaMethodBuilderPrivate *d = d_func();
1885 return d->returnType;
1887 return QByteArray();
1891 Sets the return type for this method to \a value. If \a value
1892 is empty, then the method's return type is \c{void}. The \a value
1893 will be normalized before it is added to the method.
1895 \sa returnType(), signature()
1897 void QMetaMethodBuilder::setReturnType(const QByteArray& value)
1899 QMetaMethodBuilderPrivate *d = d_func();
1901 d->returnType = QMetaObject::normalizedType(value);
1905 Returns the list of parameter names for this method.
1907 \sa setParameterNames()
1909 QList<QByteArray> QMetaMethodBuilder::parameterNames() const
1911 QMetaMethodBuilderPrivate *d = d_func();
1913 return d->parameterNames;
1915 return QList<QByteArray>();
1919 Sets the list of parameter names for this method to \a value.
1921 \sa parameterNames()
1923 void QMetaMethodBuilder::setParameterNames(const QList<QByteArray>& value)
1925 QMetaMethodBuilderPrivate *d = d_func();
1927 d->parameterNames = value;
1931 Returns the tag associated with this method.
1935 QByteArray QMetaMethodBuilder::tag() const
1937 QMetaMethodBuilderPrivate *d = d_func();
1941 return QByteArray();
1945 Sets the tag associated with this method to \a value.
1949 void QMetaMethodBuilder::setTag(const QByteArray& value)
1951 QMetaMethodBuilderPrivate *d = d_func();
1957 Returns the access specification of this method (private, protected,
1958 or public). The default value is QMetaMethod::Public for methods,
1959 slots, and constructors. The default value is QMetaMethod::Protected
1964 QMetaMethod::Access QMetaMethodBuilder::access() const
1966 QMetaMethodBuilderPrivate *d = d_func();
1970 return QMetaMethod::Public;
1974 Sets the access specification of this method (private, protected,
1975 or public) to \a value. If the method is a signal, this function
1980 void QMetaMethodBuilder::setAccess(QMetaMethod::Access value)
1982 QMetaMethodBuilderPrivate *d = d_func();
1983 if (d && d->methodType() != QMetaMethod::Signal)
1984 d->setAccess(value);
1988 Returns the additional attributes for this method.
1992 int QMetaMethodBuilder::attributes() const
1994 QMetaMethodBuilderPrivate *d = d_func();
1996 return (d->attributes >> 4);
2002 Sets the additional attributes for this method to \a value.
2006 void QMetaMethodBuilder::setAttributes(int value)
2008 QMetaMethodBuilderPrivate *d = d_func();
2010 d->attributes = ((d->attributes & 0x0f) | (value << 4));
2014 \class QMetaPropertyBuilder
2016 \brief The QMetaPropertyBuilder class enables modifications to a property definition on a meta object builder.
2019 QMetaPropertyBuilderPrivate *QMetaPropertyBuilder::d_func() const
2021 if (_mobj && _index >= 0 && _index < _mobj->d->properties.size())
2022 return &(_mobj->d->properties[_index]);
2028 \fn QMetaPropertyBuilder::QMetaPropertyBuilder()
2033 \fn int QMetaPropertyBuilder::index() const
2035 Returns the index of this property within its QMetaObjectBuilder.
2039 Returns the name associated with this property.
2043 QByteArray QMetaPropertyBuilder::name() const
2045 QMetaPropertyBuilderPrivate *d = d_func();
2049 return QByteArray();
2053 Returns the type associated with this property.
2057 QByteArray QMetaPropertyBuilder::type() const
2059 QMetaPropertyBuilderPrivate *d = d_func();
2063 return QByteArray();
2067 Returns true if this property has a notify signal; false otherwise.
2069 \sa notifySignal(), setNotifySignal(), removeNotifySignal()
2071 bool QMetaPropertyBuilder::hasNotifySignal() const
2073 QMetaPropertyBuilderPrivate *d = d_func();
2075 return d->flag(Notify);
2081 Returns the notify signal associated with this property.
2083 \sa hasNotifySignal(), setNotifySignal(), removeNotifySignal()
2085 QMetaMethodBuilder QMetaPropertyBuilder::notifySignal() const
2087 QMetaPropertyBuilderPrivate *d = d_func();
2088 if (d && d->notifySignal >= 0)
2089 return QMetaMethodBuilder(_mobj, d->notifySignal);
2091 return QMetaMethodBuilder();
2095 Sets the notify signal associated with this property to \a value.
2097 \sa hasNotifySignal(), notifySignal(), removeNotifySignal()
2099 void QMetaPropertyBuilder::setNotifySignal(const QMetaMethodBuilder& value)
2101 QMetaPropertyBuilderPrivate *d = d_func();
2104 d->notifySignal = value._index;
2105 d->setFlag(Notify, true);
2107 d->notifySignal = -1;
2108 d->setFlag(Notify, false);
2114 Removes the notify signal from this property.
2116 \sa hasNotifySignal(), notifySignal(), setNotifySignal()
2118 void QMetaPropertyBuilder::removeNotifySignal()
2120 QMetaPropertyBuilderPrivate *d = d_func();
2122 d->notifySignal = -1;
2123 d->setFlag(Notify, false);
2128 Returns true if this property is readable; otherwise returns false.
2129 The default value is true.
2131 \sa setReadable(), isWritable()
2133 bool QMetaPropertyBuilder::isReadable() const
2135 QMetaPropertyBuilderPrivate *d = d_func();
2137 return d->flag(Readable);
2143 Returns true if this property is writable; otherwise returns false.
2144 The default value is true.
2146 \sa setWritable(), isReadable()
2148 bool QMetaPropertyBuilder::isWritable() const
2150 QMetaPropertyBuilderPrivate *d = d_func();
2152 return d->flag(Writable);
2158 Returns true if this property can be reset to a default value; otherwise
2159 returns false. The default value is false.
2163 bool QMetaPropertyBuilder::isResettable() const
2165 QMetaPropertyBuilderPrivate *d = d_func();
2167 return d->flag(Resettable);
2173 Returns true if this property is designable; otherwise returns false.
2174 This default value is false.
2176 \sa setDesignable(), isScriptable(), isStored()
2178 bool QMetaPropertyBuilder::isDesignable() const
2180 QMetaPropertyBuilderPrivate *d = d_func();
2182 return d->flag(Designable);
2188 Returns true if the property is scriptable; otherwise returns false.
2189 This default value is true.
2191 \sa setScriptable(), isDesignable(), isStored()
2193 bool QMetaPropertyBuilder::isScriptable() const
2195 QMetaPropertyBuilderPrivate *d = d_func();
2197 return d->flag(Scriptable);
2203 Returns true if the property is stored; otherwise returns false.
2204 This default value is false.
2206 \sa setStored(), isDesignable(), isScriptable()
2208 bool QMetaPropertyBuilder::isStored() const
2210 QMetaPropertyBuilderPrivate *d = d_func();
2212 return d->flag(Stored);
2218 Returns true if the property is editable; otherwise returns false.
2219 This default value is false.
2221 \sa setEditable(), isDesignable(), isScriptable(), isStored()
2223 bool QMetaPropertyBuilder::isEditable() const
2225 QMetaPropertyBuilderPrivate *d = d_func();
2227 return d->flag(Editable);
2233 Returns true if this property is designated as the \c USER
2234 property, i.e., the one that the user can edit or that is
2235 significant in some other way. Otherwise it returns
2236 false. This default value is false.
2238 \sa setUser(), isDesignable(), isScriptable()
2240 bool QMetaPropertyBuilder::isUser() const
2242 QMetaPropertyBuilderPrivate *d = d_func();
2244 return d->flag(User);
2250 Returns true if the property has a C++ setter function that
2251 follows Qt's standard "name" / "setName" pattern. Designer and uic
2252 query hasStdCppSet() in order to avoid expensive
2253 QObject::setProperty() calls. All properties in Qt [should] follow
2254 this pattern. The default value is false.
2258 bool QMetaPropertyBuilder::hasStdCppSet() const
2260 QMetaPropertyBuilderPrivate *d = d_func();
2262 return d->flag(StdCppSet);
2268 Returns true if the property is an enumerator or flag type;
2269 otherwise returns false. This default value is false.
2273 bool QMetaPropertyBuilder::isEnumOrFlag() const
2275 QMetaPropertyBuilderPrivate *d = d_func();
2277 return d->flag(EnumOrFlag);
2283 Sets this property to readable if \a value is true.
2285 \sa isReadable(), setWritable()
2287 void QMetaPropertyBuilder::setReadable(bool value)
2289 QMetaPropertyBuilderPrivate *d = d_func();
2291 d->setFlag(Readable, value);
2295 Sets this property to writable if \a value is true.
2297 \sa isWritable(), setReadable()
2299 void QMetaPropertyBuilder::setWritable(bool value)
2301 QMetaPropertyBuilderPrivate *d = d_func();
2303 d->setFlag(Writable, value);
2307 Sets this property to resettable if \a value is true.
2311 void QMetaPropertyBuilder::setResettable(bool value)
2313 QMetaPropertyBuilderPrivate *d = d_func();
2315 d->setFlag(Resettable, value);
2319 Sets this property to designable if \a value is true.
2321 \sa isDesignable(), setScriptable(), setStored()
2323 void QMetaPropertyBuilder::setDesignable(bool value)
2325 QMetaPropertyBuilderPrivate *d = d_func();
2327 d->setFlag(Designable, value);
2331 Sets this property to scriptable if \a value is true.
2333 \sa isScriptable(), setDesignable(), setStored()
2335 void QMetaPropertyBuilder::setScriptable(bool value)
2337 QMetaPropertyBuilderPrivate *d = d_func();
2339 d->setFlag(Scriptable, value);
2343 Sets this property to storable if \a value is true.
2345 \sa isStored(), setDesignable(), setScriptable()
2347 void QMetaPropertyBuilder::setStored(bool value)
2349 QMetaPropertyBuilderPrivate *d = d_func();
2351 d->setFlag(Stored, value);
2355 Sets this property to editable if \a value is true.
2357 \sa isEditable(), setDesignable(), setScriptable(), setStored()
2359 void QMetaPropertyBuilder::setEditable(bool value)
2361 QMetaPropertyBuilderPrivate *d = d_func();
2363 d->setFlag(Editable, value);
2367 Sets the \c USER flag on this property to \a value.
2369 \sa isUser(), setDesignable(), setScriptable()
2371 void QMetaPropertyBuilder::setUser(bool value)
2373 QMetaPropertyBuilderPrivate *d = d_func();
2375 d->setFlag(User, value);
2379 Sets the C++ setter flag on this property to \a value, which is
2380 true if the property has a C++ setter function that follows Qt's
2381 standard "name" / "setName" pattern.
2385 void QMetaPropertyBuilder::setStdCppSet(bool value)
2387 QMetaPropertyBuilderPrivate *d = d_func();
2389 d->setFlag(StdCppSet, value);
2393 Sets this property to be of an enumerator or flag type if
2398 void QMetaPropertyBuilder::setEnumOrFlag(bool value)
2400 QMetaPropertyBuilderPrivate *d = d_func();
2402 d->setFlag(EnumOrFlag, value);
2406 \class QMetaEnumBuilder
2408 \brief The QMetaEnumBuilder class enables modifications to an enumerator definition on a meta object builder.
2411 QMetaEnumBuilderPrivate *QMetaEnumBuilder::d_func() const
2413 if (_mobj && _index >= 0 && _index < _mobj->d->enumerators.size())
2414 return &(_mobj->d->enumerators[_index]);
2420 \fn QMetaEnumBuilder::QMetaEnumBuilder()
2425 \fn int QMetaEnumBuilder::index() const
2427 Returns the index of this enumerator within its QMetaObjectBuilder.
2431 Returns the name of the enumerator (without the scope).
2433 QByteArray QMetaEnumBuilder::name() const
2435 QMetaEnumBuilderPrivate *d = d_func();
2439 return QByteArray();
2443 Returns true if this enumerator is used as a flag; otherwise returns
2448 bool QMetaEnumBuilder::isFlag() const
2450 QMetaEnumBuilderPrivate *d = d_func();
2458 Sets this enumerator to be used as a flag if \a value is true.
2462 void QMetaEnumBuilder::setIsFlag(bool value)
2464 QMetaEnumBuilderPrivate *d = d_func();
2470 Returns the number of keys.
2474 int QMetaEnumBuilder::keyCount() const
2476 QMetaEnumBuilderPrivate *d = d_func();
2478 return d->keys.size();
2484 Returns the key with the given \a index, or an empty QByteArray
2485 if no such key exists.
2487 \sa keyCount(), addKey(), value()
2489 QByteArray QMetaEnumBuilder::key(int index) const
2491 QMetaEnumBuilderPrivate *d = d_func();
2492 if (d && index >= 0 && index < d->keys.size())
2493 return d->keys[index];
2495 return QByteArray();
2499 Returns the value with the given \a index; or returns -1 if there
2502 \sa keyCount(), addKey(), key()
2504 int QMetaEnumBuilder::value(int index) const
2506 QMetaEnumBuilderPrivate *d = d_func();
2507 if (d && index >= 0 && index < d->keys.size())
2508 return d->values[index];
2514 Adds a new key called \a name to this enumerator, associated
2515 with \a value. Returns the index of the new key.
2517 \sa keyCount(), key(), value(), removeKey()
2519 int QMetaEnumBuilder::addKey(const QByteArray& name, int value)
2521 QMetaEnumBuilderPrivate *d = d_func();
2523 int index = d->keys.size();
2533 Removes the key at \a index from this enumerator.
2537 void QMetaEnumBuilder::removeKey(int index)
2539 QMetaEnumBuilderPrivate *d = d_func();
2540 if (d && index >= 0 && index < d->keys.size()) {
2541 d->keys.removeAt(index);
2542 d->values.removeAt(index);