PageRenderTime 66ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/src/corelib/kernel/qmetaobject.cpp

https://bitbucket.org/ultra_iter/qt-vtl
C++ | 2783 lines | 1371 code | 197 blank | 1215 comment | 433 complexity | d3716d0f8550989b9c5bbdaa8f8e8cf1 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-3.0, BSD-3-Clause, CC0-1.0, CC-BY-SA-4.0, LGPL-2.1, GPL-3.0, Apache-2.0
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 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 QtCore module of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** GNU Lesser General Public License Usage
  11. ** This file may be used under the terms of the GNU Lesser General Public
  12. ** License version 2.1 as published by the Free Software Foundation and
  13. ** appearing in the file LICENSE.LGPL included in the packaging of this
  14. ** file. Please review the following information to ensure the GNU Lesser
  15. ** General Public License version 2.1 requirements will be met:
  16. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  17. **
  18. ** In addition, as a special exception, Nokia gives you certain additional
  19. ** rights. These rights are described in the Nokia Qt LGPL Exception
  20. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  21. **
  22. ** GNU General Public License Usage
  23. ** Alternatively, this file may be used under the terms of the GNU General
  24. ** Public License version 3.0 as published by the Free Software Foundation
  25. ** and appearing in the file LICENSE.GPL included in the packaging of this
  26. ** file. Please review the following information to ensure the GNU General
  27. ** Public License version 3.0 requirements will be met:
  28. ** http://www.gnu.org/copyleft/gpl.html.
  29. **
  30. ** Other Usage
  31. ** Alternatively, this file may be used in accordance with the terms and
  32. ** conditions contained in a signed written agreement between you and Nokia.
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include "qmetaobject.h"
  42. #include "qmetatype.h"
  43. #include "qobject.h"
  44. #include <qcoreapplication.h>
  45. #include <qcoreevent.h>
  46. #include <qdatastream.h>
  47. #include <qstringlist.h>
  48. #include <qthread.h>
  49. #include <qvarlengtharray.h>
  50. #include <qvariant.h>
  51. #include <qhash.h>
  52. #include <qdebug.h>
  53. #include <qsemaphore.h>
  54. #include "private/qobject_p.h"
  55. #include "private/qmetaobject_p.h"
  56. #include <ctype.h>
  57. QT_BEGIN_NAMESPACE
  58. /*!
  59. \class QMetaObject
  60. \brief The QMetaObject class contains meta-information about Qt
  61. objects.
  62. \ingroup objectmodel
  63. The Qt \l{Meta-Object System} in Qt is responsible for the
  64. signals and slots inter-object communication mechanism, runtime
  65. type information, and the Qt property system. A single
  66. QMetaObject instance is created for each QObject subclass that is
  67. used in an application, and this instance stores all the
  68. meta-information for the QObject subclass. This object is
  69. available as QObject::metaObject().
  70. This class is not normally required for application programming,
  71. but it is useful if you write meta-applications, such as scripting
  72. engines or GUI builders.
  73. The functions you are most likely to find useful are these:
  74. \list
  75. \o className() returns the name of a class.
  76. \o superClass() returns the superclass's meta-object.
  77. \o method() and methodCount() provide information
  78. about a class's meta-methods (signals, slots and other
  79. \l{Q_INVOKABLE}{invokable} member functions).
  80. \o enumerator() and enumeratorCount() and provide information about
  81. a class's enumerators.
  82. \o propertyCount() and property() provide information about a
  83. class's properties.
  84. \o constructor() and constructorCount() provide information
  85. about a class's meta-constructors.
  86. \endlist
  87. The index functions indexOfConstructor(), indexOfMethod(),
  88. indexOfEnumerator(), and indexOfProperty() map names of constructors,
  89. member functions, enumerators, or properties to indexes in the
  90. meta-object. For example, Qt uses indexOfMethod() internally when you
  91. connect a signal to a slot.
  92. Classes can also have a list of \e{name}--\e{value} pairs of
  93. additional class information, stored in QMetaClassInfo objects.
  94. The number of pairs is returned by classInfoCount(), single pairs
  95. are returned by classInfo(), and you can search for pairs with
  96. indexOfClassInfo().
  97. \sa QMetaClassInfo, QMetaEnum, QMetaMethod, QMetaProperty, QMetaType,
  98. {Meta-Object System}
  99. */
  100. /*!
  101. \enum QMetaObject::Call
  102. \internal
  103. \value InvokeSlot
  104. \value EmitSignal
  105. \value ReadProperty
  106. \value WriteProperty
  107. \value ResetProperty
  108. \value QueryPropertyDesignable
  109. \value QueryPropertyScriptable
  110. \value QueryPropertyStored
  111. \value QueryPropertyEditable
  112. \value QueryPropertyUser
  113. \value CreateInstance
  114. */
  115. /*!
  116. \enum QMetaMethod::Access
  117. This enum describes the access level of a method, following the conventions used in C++.
  118. \value Private
  119. \value Protected
  120. \value Public
  121. */
  122. static inline const QMetaObjectPrivate *priv(const uint* data)
  123. { return reinterpret_cast<const QMetaObjectPrivate*>(data); }
  124. /*!
  125. \since 4.5
  126. Constructs a new instance of this class. You can pass up to ten arguments
  127. (\a val0, \a val1, \a val2, \a val3, \a val4, \a val5, \a val6, \a val7,
  128. \a val8, and \a val9) to the constructor. Returns the new object, or 0 if
  129. no suitable constructor is available.
  130. Note that only constructors that are declared with the Q_INVOKABLE
  131. modifier are made available through the meta-object system.
  132. \sa Q_ARG(), constructor()
  133. */
  134. QObject *QMetaObject::newInstance(QGenericArgument val0,
  135. QGenericArgument val1,
  136. QGenericArgument val2,
  137. QGenericArgument val3,
  138. QGenericArgument val4,
  139. QGenericArgument val5,
  140. QGenericArgument val6,
  141. QGenericArgument val7,
  142. QGenericArgument val8,
  143. QGenericArgument val9) const
  144. {
  145. QByteArray constructorName = className();
  146. {
  147. int idx = constructorName.lastIndexOf(':');
  148. if (idx != -1)
  149. constructorName.remove(0, idx+1); // remove qualified part
  150. }
  151. QVarLengthArray<char, 512> sig;
  152. sig.append(constructorName.constData(), constructorName.length());
  153. sig.append('(');
  154. enum { MaximumParamCount = 10 };
  155. const char *typeNames[] = {val0.name(), val1.name(), val2.name(), val3.name(), val4.name(),
  156. val5.name(), val6.name(), val7.name(), val8.name(), val9.name()};
  157. int paramCount;
  158. for (paramCount = 0; paramCount < MaximumParamCount; ++paramCount) {
  159. int len = qstrlen(typeNames[paramCount]);
  160. if (len <= 0)
  161. break;
  162. sig.append(typeNames[paramCount], len);
  163. sig.append(',');
  164. }
  165. if (paramCount == 0)
  166. sig.append(')'); // no parameters
  167. else
  168. sig[sig.size() - 1] = ')';
  169. sig.append('\0');
  170. int idx = indexOfConstructor(sig.constData());
  171. if (idx < 0) {
  172. QByteArray norm = QMetaObject::normalizedSignature(sig.constData());
  173. idx = indexOfConstructor(norm.constData());
  174. }
  175. if (idx < 0)
  176. return 0;
  177. QVariant ret(QMetaType::QObjectStar, (void*)0);
  178. void *param[] = {ret.data(), val0.data(), val1.data(), val2.data(), val3.data(), val4.data(),
  179. val5.data(), val6.data(), val7.data(), val8.data(), val9.data()};
  180. if (static_metacall(CreateInstance, idx, param) >= 0)
  181. return 0;
  182. return *reinterpret_cast<QObject**>(param[0]);
  183. }
  184. /*!
  185. \internal
  186. */
  187. int QMetaObject::static_metacall(Call cl, int idx, void **argv) const
  188. {
  189. const QMetaObjectExtraData *extra = reinterpret_cast<const QMetaObjectExtraData *>(d.extradata);
  190. if (priv(d.data)->revision >= 6) {
  191. if (!extra || !extra->static_metacall)
  192. return 0;
  193. extra->static_metacall(0, cl, idx, argv);
  194. return -1;
  195. } else if (priv(d.data)->revision >= 2) {
  196. if (!extra || !extra->static_metacall)
  197. return 0;
  198. typedef int (*OldMetacall)(QMetaObject::Call, int, void **);
  199. OldMetacall o = reinterpret_cast<OldMetacall>(extra->static_metacall);
  200. return o(cl, idx, argv);
  201. }
  202. return 0;
  203. }
  204. /*!
  205. \internal
  206. */
  207. int QMetaObject::metacall(QObject *object, Call cl, int idx, void **argv)
  208. {
  209. if (QMetaObject *mo = object->d_ptr->metaObject)
  210. return static_cast<QAbstractDynamicMetaObject*>(mo)->metaCall(cl, idx, argv);
  211. else
  212. return object->qt_metacall(cl, idx, argv);
  213. }
  214. /*!
  215. \fn const char *QMetaObject::className() const
  216. Returns the class name.
  217. \sa superClass()
  218. */
  219. /*!
  220. \fn QMetaObject *QMetaObject::superClass() const
  221. Returns the meta-object of the superclass, or 0 if there is no
  222. such object.
  223. \sa className()
  224. */
  225. /*!
  226. \internal
  227. Returns \a obj if object \a obj inherits from this
  228. meta-object; otherwise returns 0.
  229. */
  230. QObject *QMetaObject::cast(QObject *obj) const
  231. {
  232. if (obj) {
  233. const QMetaObject *m = obj->metaObject();
  234. do {
  235. if (m == this)
  236. return obj;
  237. } while ((m = m->d.superdata));
  238. }
  239. return 0;
  240. }
  241. /*!
  242. \internal
  243. Returns \a obj if object \a obj inherits from this
  244. meta-object; otherwise returns 0.
  245. */
  246. const QObject *QMetaObject::cast(const QObject *obj) const
  247. {
  248. if (obj) {
  249. const QMetaObject *m = obj->metaObject();
  250. do {
  251. if (m == this)
  252. return obj;
  253. } while ((m = m->d.superdata));
  254. }
  255. return 0;
  256. }
  257. #ifndef QT_NO_TRANSLATION
  258. /*!
  259. \internal
  260. */
  261. QString QMetaObject::tr(const char *s, const char *c) const
  262. {
  263. return QCoreApplication::translate(d.stringdata, s, c, QCoreApplication::CodecForTr);
  264. }
  265. /*!
  266. \internal
  267. */
  268. QString QMetaObject::tr(const char *s, const char *c, int n) const
  269. {
  270. return QCoreApplication::translate(d.stringdata, s, c, QCoreApplication::CodecForTr, n);
  271. }
  272. /*!
  273. \internal
  274. */
  275. QString QMetaObject::trUtf8(const char *s, const char *c) const
  276. {
  277. return QCoreApplication::translate(d.stringdata, s, c, QCoreApplication::UnicodeUTF8);
  278. }
  279. /*!
  280. \internal
  281. */
  282. QString QMetaObject::trUtf8(const char *s, const char *c, int n) const
  283. {
  284. return QCoreApplication::translate(d.stringdata, s, c, QCoreApplication::UnicodeUTF8, n);
  285. }
  286. #endif // QT_NO_TRANSLATION
  287. /*!
  288. Returns the method offset for this class; i.e. the index position
  289. of this class's first member function.
  290. The offset is the sum of all the methods in the class's
  291. superclasses (which is always positive since QObject has the
  292. deleteLater() slot and a destroyed() signal).
  293. \sa method(), methodCount(), indexOfMethod()
  294. */
  295. int QMetaObject::methodOffset() const
  296. {
  297. int offset = 0;
  298. const QMetaObject *m = d.superdata;
  299. while (m) {
  300. offset += priv(m->d.data)->methodCount;
  301. m = m->d.superdata;
  302. }
  303. return offset;
  304. }
  305. /*!
  306. Returns the enumerator offset for this class; i.e. the index
  307. position of this class's first enumerator.
  308. If the class has no superclasses with enumerators, the offset is
  309. 0; otherwise the offset is the sum of all the enumerators in the
  310. class's superclasses.
  311. \sa enumerator(), enumeratorCount(), indexOfEnumerator()
  312. */
  313. int QMetaObject::enumeratorOffset() const
  314. {
  315. int offset = 0;
  316. const QMetaObject *m = d.superdata;
  317. while (m) {
  318. offset += priv(m->d.data)->enumeratorCount;
  319. m = m->d.superdata;
  320. }
  321. return offset;
  322. }
  323. /*!
  324. Returns the property offset for this class; i.e. the index
  325. position of this class's first property.
  326. The offset is the sum of all the properties in the class's
  327. superclasses (which is always positive since QObject has the
  328. name() property).
  329. \sa property(), propertyCount(), indexOfProperty()
  330. */
  331. int QMetaObject::propertyOffset() const
  332. {
  333. int offset = 0;
  334. const QMetaObject *m = d.superdata;
  335. while (m) {
  336. offset += priv(m->d.data)->propertyCount;
  337. m = m->d.superdata;
  338. }
  339. return offset;
  340. }
  341. /*!
  342. Returns the class information offset for this class; i.e. the
  343. index position of this class's first class information item.
  344. If the class has no superclasses with class information, the
  345. offset is 0; otherwise the offset is the sum of all the class
  346. information items in the class's superclasses.
  347. \sa classInfo(), classInfoCount(), indexOfClassInfo()
  348. */
  349. int QMetaObject::classInfoOffset() const
  350. {
  351. int offset = 0;
  352. const QMetaObject *m = d.superdata;
  353. while (m) {
  354. offset += priv(m->d.data)->classInfoCount;
  355. m = m->d.superdata;
  356. }
  357. return offset;
  358. }
  359. /*!
  360. \since 4.5
  361. Returns the number of constructors in this class.
  362. \sa constructor(), indexOfConstructor()
  363. */
  364. int QMetaObject::constructorCount() const
  365. {
  366. if (priv(d.data)->revision < 2)
  367. return 0;
  368. return priv(d.data)->constructorCount;
  369. }
  370. /*!
  371. Returns the number of methods known to the meta-object system in this class,
  372. including the number of properties provided by each base class. These
  373. include signals and slots as well as member functions declared with the
  374. Q_INVOKABLE macro.
  375. Use code like the following to obtain a QStringList containing the methods
  376. specific to a given class:
  377. \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp methodCount
  378. \sa method(), methodOffset(), indexOfMethod()
  379. */
  380. int QMetaObject::methodCount() const
  381. {
  382. int n = priv(d.data)->methodCount;
  383. const QMetaObject *m = d.superdata;
  384. while (m) {
  385. n += priv(m->d.data)->methodCount;
  386. m = m->d.superdata;
  387. }
  388. return n;
  389. }
  390. /*!
  391. Returns the number of enumerators in this class.
  392. \sa enumerator(), enumeratorOffset(), indexOfEnumerator()
  393. */
  394. int QMetaObject::enumeratorCount() const
  395. {
  396. int n = priv(d.data)->enumeratorCount;
  397. const QMetaObject *m = d.superdata;
  398. while (m) {
  399. n += priv(m->d.data)->enumeratorCount;
  400. m = m->d.superdata;
  401. }
  402. return n;
  403. }
  404. /*!
  405. Returns the number of properties in this class, including the number of
  406. properties provided by each base class.
  407. Use code like the following to obtain a QStringList containing the properties
  408. specific to a given class:
  409. \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp propertyCount
  410. \sa property(), propertyOffset(), indexOfProperty()
  411. */
  412. int QMetaObject::propertyCount() const
  413. {
  414. int n = priv(d.data)->propertyCount;
  415. const QMetaObject *m = d.superdata;
  416. while (m) {
  417. n += priv(m->d.data)->propertyCount;
  418. m = m->d.superdata;
  419. }
  420. return n;
  421. }
  422. /*!
  423. Returns the number of items of class information in this class.
  424. \sa classInfo(), classInfoOffset(), indexOfClassInfo()
  425. */
  426. int QMetaObject::classInfoCount() const
  427. {
  428. int n = priv(d.data)->classInfoCount;
  429. const QMetaObject *m = d.superdata;
  430. while (m) {
  431. n += priv(m->d.data)->classInfoCount;
  432. m = m->d.superdata;
  433. }
  434. return n;
  435. }
  436. /** \internal
  437. * helper function for indexOf{Method,Slot,Signal}, returns the relative index of the method within
  438. * the baseObject
  439. * \a MethodType might be MethodSignal or MethodSlot, or 0 to match everything.
  440. * \a normalizeStringData set to true if we should do a second pass for old moc generated files normalizing all the symbols.
  441. */
  442. template<int MethodType>
  443. static inline int indexOfMethodRelative(const QMetaObject **baseObject,
  444. const char *method,
  445. bool normalizeStringData)
  446. {
  447. for (const QMetaObject *m = *baseObject; m; m = m->d.superdata) {
  448. int i = (MethodType == MethodSignal && priv(m->d.data)->revision >= 4)
  449. ? (priv(m->d.data)->signalCount - 1) : (priv(m->d.data)->methodCount - 1);
  450. const int end = (MethodType == MethodSlot && priv(m->d.data)->revision >= 4)
  451. ? (priv(m->d.data)->signalCount) : 0;
  452. if (!normalizeStringData) {
  453. for (; i >= end; --i) {
  454. const char *stringdata = m->d.stringdata + m->d.data[priv(m->d.data)->methodData + 5*i];
  455. if (method[0] == stringdata[0] && strcmp(method + 1, stringdata + 1) == 0) {
  456. *baseObject = m;
  457. return i;
  458. }
  459. }
  460. } else if (priv(m->d.data)->revision < 5) {
  461. for (; i >= end; --i) {
  462. const char *stringdata = (m->d.stringdata + m->d.data[priv(m->d.data)->methodData + 5 * i]);
  463. const QByteArray normalizedSignature = QMetaObject::normalizedSignature(stringdata);
  464. if (normalizedSignature == method) {
  465. *baseObject = m;
  466. return i;
  467. }
  468. }
  469. }
  470. }
  471. return -1;
  472. }
  473. /*!
  474. \since 4.5
  475. Finds \a constructor and returns its index; otherwise returns -1.
  476. Note that the \a constructor has to be in normalized form, as returned
  477. by normalizedSignature().
  478. \sa constructor(), constructorCount(), normalizedSignature()
  479. */
  480. int QMetaObject::indexOfConstructor(const char *constructor) const
  481. {
  482. if (priv(d.data)->revision < 2)
  483. return -1;
  484. for (int i = priv(d.data)->constructorCount-1; i >= 0; --i) {
  485. const char *data = d.stringdata + d.data[priv(d.data)->constructorData + 5*i];
  486. if (data[0] == constructor[0] && strcmp(constructor + 1, data + 1) == 0) {
  487. return i;
  488. }
  489. }
  490. return -1;
  491. }
  492. /*!
  493. Finds \a method and returns its index; otherwise returns -1.
  494. Note that the \a method has to be in normalized form, as returned
  495. by normalizedSignature().
  496. \sa method(), methodCount(), methodOffset(), normalizedSignature()
  497. */
  498. int QMetaObject::indexOfMethod(const char *method) const
  499. {
  500. const QMetaObject *m = this;
  501. int i = indexOfMethodRelative<0>(&m, method, false);
  502. if (i < 0) {
  503. m = this;
  504. i = indexOfMethodRelative<0>(&m, method, true);
  505. }
  506. if (i >= 0)
  507. i += m->methodOffset();
  508. return i;
  509. }
  510. /*!
  511. Finds \a signal and returns its index; otherwise returns -1.
  512. This is the same as indexOfMethod(), except that it will return
  513. -1 if the method exists but isn't a signal.
  514. Note that the \a signal has to be in normalized form, as returned
  515. by normalizedSignature().
  516. \sa indexOfMethod(), normalizedSignature(), method(), methodCount(), methodOffset()
  517. */
  518. int QMetaObject::indexOfSignal(const char *signal) const
  519. {
  520. const QMetaObject *m = this;
  521. int i = QMetaObjectPrivate::indexOfSignalRelative(&m, signal, false);
  522. if (i < 0) {
  523. m = this;
  524. i = QMetaObjectPrivate::indexOfSignalRelative(&m, signal, true);
  525. }
  526. if (i >= 0)
  527. i += m->methodOffset();
  528. return i;
  529. }
  530. /*! \internal
  531. Same as QMetaObject::indexOfSignal, but the result is the local offset to the base object.
  532. \a baseObject will be adjusted to the enclosing QMetaObject, or 0 if the signal is not found
  533. */
  534. int QMetaObjectPrivate::indexOfSignalRelative(const QMetaObject **baseObject,
  535. const char *signal,
  536. bool normalizeStringData)
  537. {
  538. int i = indexOfMethodRelative<MethodSignal>(baseObject, signal, normalizeStringData);
  539. #ifndef QT_NO_DEBUG
  540. const QMetaObject *m = *baseObject;
  541. if (i >= 0 && m && m->d.superdata) {
  542. int conflict = m->d.superdata->indexOfMethod(signal);
  543. if (conflict >= 0)
  544. qWarning("QMetaObject::indexOfSignal: signal %s from %s redefined in %s",
  545. signal, m->d.superdata->d.stringdata, m->d.stringdata);
  546. }
  547. #endif
  548. return i;
  549. }
  550. /*!
  551. Finds \a slot and returns its index; otherwise returns -1.
  552. This is the same as indexOfMethod(), except that it will return
  553. -1 if the method exists but isn't a slot.
  554. \sa indexOfMethod(), method(), methodCount(), methodOffset()
  555. */
  556. int QMetaObject::indexOfSlot(const char *slot) const
  557. {
  558. const QMetaObject *m = this;
  559. int i = QMetaObjectPrivate::indexOfSlotRelative(&m, slot, false);
  560. if (i < 0)
  561. i = QMetaObjectPrivate::indexOfSlotRelative(&m, slot, true);
  562. if (i >= 0)
  563. i += m->methodOffset();
  564. return i;
  565. }
  566. // same as indexOfSignalRelative but for slots.
  567. int QMetaObjectPrivate::indexOfSlotRelative(const QMetaObject **m,
  568. const char *slot,
  569. bool normalizeStringData)
  570. {
  571. return indexOfMethodRelative<MethodSlot>(m, slot, normalizeStringData);
  572. }
  573. static const QMetaObject *QMetaObject_findMetaObject(const QMetaObject *self, const char *name)
  574. {
  575. while (self) {
  576. if (strcmp(self->d.stringdata, name) == 0)
  577. return self;
  578. if (self->d.extradata) {
  579. #ifdef Q_NO_DATA_RELOCATION
  580. const QMetaObjectAccessor *e;
  581. Q_ASSERT(priv(self->d.data)->revision >= 2);
  582. #else
  583. const QMetaObject **e;
  584. if (priv(self->d.data)->revision < 2) {
  585. e = (const QMetaObject**)(self->d.extradata);
  586. } else
  587. #endif
  588. {
  589. const QMetaObjectExtraData *extra = (const QMetaObjectExtraData*)(self->d.extradata);
  590. e = extra->objects;
  591. }
  592. if (e) {
  593. while (*e) {
  594. #ifdef Q_NO_DATA_RELOCATION
  595. if (const QMetaObject *m =QMetaObject_findMetaObject(&((*e)()), name))
  596. #else
  597. if (const QMetaObject *m =QMetaObject_findMetaObject((*e), name))
  598. #endif
  599. return m;
  600. ++e;
  601. }
  602. }
  603. }
  604. self = self->d.superdata;
  605. }
  606. return self;
  607. }
  608. /*!
  609. Finds enumerator \a name and returns its index; otherwise returns
  610. -1.
  611. \sa enumerator(), enumeratorCount(), enumeratorOffset()
  612. */
  613. int QMetaObject::indexOfEnumerator(const char *name) const
  614. {
  615. const QMetaObject *m = this;
  616. while (m) {
  617. const QMetaObjectPrivate *d = priv(m->d.data);
  618. for (int i = d->enumeratorCount - 1; i >= 0; --i) {
  619. const char *prop = m->d.stringdata + m->d.data[d->enumeratorData + 4*i];
  620. if (name[0] == prop[0] && strcmp(name + 1, prop + 1) == 0) {
  621. i += m->enumeratorOffset();
  622. return i;
  623. }
  624. }
  625. m = m->d.superdata;
  626. }
  627. return -1;
  628. }
  629. /*!
  630. Finds property \a name and returns its index; otherwise returns
  631. -1.
  632. \sa property(), propertyCount(), propertyOffset()
  633. */
  634. int QMetaObject::indexOfProperty(const char *name) const
  635. {
  636. const QMetaObject *m = this;
  637. while (m) {
  638. const QMetaObjectPrivate *d = priv(m->d.data);
  639. for (int i = d->propertyCount-1; i >= 0; --i) {
  640. const char *prop = m->d.stringdata + m->d.data[d->propertyData + 3*i];
  641. if (name[0] == prop[0] && strcmp(name + 1, prop + 1) == 0) {
  642. i += m->propertyOffset();
  643. return i;
  644. }
  645. }
  646. m = m->d.superdata;
  647. }
  648. if (priv(this->d.data)->revision >= 3 && (priv(this->d.data)->flags & DynamicMetaObject)) {
  649. QAbstractDynamicMetaObject *me =
  650. const_cast<QAbstractDynamicMetaObject *>(static_cast<const QAbstractDynamicMetaObject *>(this));
  651. return me->createProperty(name, 0);
  652. }
  653. return -1;
  654. }
  655. /*!
  656. Finds class information item \a name and returns its index;
  657. otherwise returns -1.
  658. \sa classInfo(), classInfoCount(), classInfoOffset()
  659. */
  660. int QMetaObject::indexOfClassInfo(const char *name) const
  661. {
  662. int i = -1;
  663. const QMetaObject *m = this;
  664. while (m && i < 0) {
  665. for (i = priv(m->d.data)->classInfoCount-1; i >= 0; --i)
  666. if (strcmp(name, m->d.stringdata
  667. + m->d.data[priv(m->d.data)->classInfoData + 2*i]) == 0) {
  668. i += m->classInfoOffset();
  669. break;
  670. }
  671. m = m->d.superdata;
  672. }
  673. return i;
  674. }
  675. /*!
  676. \since 4.5
  677. Returns the meta-data for the constructor with the given \a index.
  678. \sa constructorCount(), newInstance()
  679. */
  680. QMetaMethod QMetaObject::constructor(int index) const
  681. {
  682. int i = index;
  683. QMetaMethod result;
  684. if (priv(d.data)->revision >= 2 && i >= 0 && i < priv(d.data)->constructorCount) {
  685. result.mobj = this;
  686. result.handle = priv(d.data)->constructorData + 5*i;
  687. }
  688. return result;
  689. }
  690. /*!
  691. Returns the meta-data for the method with the given \a index.
  692. \sa methodCount(), methodOffset(), indexOfMethod()
  693. */
  694. QMetaMethod QMetaObject::method(int index) const
  695. {
  696. int i = index;
  697. i -= methodOffset();
  698. if (i < 0 && d.superdata)
  699. return d.superdata->method(index);
  700. QMetaMethod result;
  701. if (i >= 0 && i < priv(d.data)->methodCount) {
  702. result.mobj = this;
  703. result.handle = priv(d.data)->methodData + 5*i;
  704. }
  705. return result;
  706. }
  707. /*!
  708. Returns the meta-data for the enumerator with the given \a index.
  709. \sa enumeratorCount(), enumeratorOffset(), indexOfEnumerator()
  710. */
  711. QMetaEnum QMetaObject::enumerator(int index) const
  712. {
  713. int i = index;
  714. i -= enumeratorOffset();
  715. if (i < 0 && d.superdata)
  716. return d.superdata->enumerator(index);
  717. QMetaEnum result;
  718. if (i >= 0 && i < priv(d.data)->enumeratorCount) {
  719. result.mobj = this;
  720. result.handle = priv(d.data)->enumeratorData + 4*i;
  721. }
  722. return result;
  723. }
  724. /*!
  725. Returns the meta-data for the property with the given \a index.
  726. If no such property exists, a null QMetaProperty is returned.
  727. \sa propertyCount(), propertyOffset(), indexOfProperty()
  728. */
  729. QMetaProperty QMetaObject::property(int index) const
  730. {
  731. int i = index;
  732. i -= propertyOffset();
  733. if (i < 0 && d.superdata)
  734. return d.superdata->property(index);
  735. QMetaProperty result;
  736. if (i >= 0 && i < priv(d.data)->propertyCount) {
  737. int handle = priv(d.data)->propertyData + 3*i;
  738. int flags = d.data[handle + 2];
  739. const char *type = d.stringdata + d.data[handle + 1];
  740. result.mobj = this;
  741. result.handle = handle;
  742. result.idx = i;
  743. if (flags & EnumOrFlag) {
  744. result.menum = enumerator(indexOfEnumerator(type));
  745. if (!result.menum.isValid()) {
  746. QByteArray enum_name = type;
  747. QByteArray scope_name = d.stringdata;
  748. int s = enum_name.lastIndexOf("::");
  749. if (s > 0) {
  750. scope_name = enum_name.left(s);
  751. enum_name = enum_name.mid(s + 2);
  752. }
  753. const QMetaObject *scope = 0;
  754. if (scope_name == "Qt")
  755. scope = &QObject::staticQtMetaObject;
  756. else
  757. scope = QMetaObject_findMetaObject(this, scope_name);
  758. if (scope)
  759. result.menum = scope->enumerator(scope->indexOfEnumerator(enum_name));
  760. }
  761. }
  762. }
  763. return result;
  764. }
  765. /*!
  766. \since 4.2
  767. Returns the property that has the \c USER flag set to true.
  768. \sa QMetaProperty::isUser()
  769. */
  770. QMetaProperty QMetaObject::userProperty() const
  771. {
  772. const int propCount = propertyCount();
  773. for (int i = propCount - 1; i >= 0; --i) {
  774. const QMetaProperty prop = property(i);
  775. if (prop.isUser())
  776. return prop;
  777. }
  778. return QMetaProperty();
  779. }
  780. /*!
  781. Returns the meta-data for the item of class information with the
  782. given \a index.
  783. Example:
  784. \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 0
  785. \sa classInfoCount(), classInfoOffset(), indexOfClassInfo()
  786. */
  787. QMetaClassInfo QMetaObject::classInfo(int index) const
  788. {
  789. int i = index;
  790. i -= classInfoOffset();
  791. if (i < 0 && d.superdata)
  792. return d.superdata->classInfo(index);
  793. QMetaClassInfo result;
  794. if (i >= 0 && i < priv(d.data)->classInfoCount) {
  795. result.mobj = this;
  796. result.handle = priv(d.data)->classInfoData + 2*i;
  797. }
  798. return result;
  799. }
  800. /*!
  801. Returns true if the \a signal and \a method arguments are
  802. compatible; otherwise returns false.
  803. Both \a signal and \a method are expected to be normalized.
  804. \sa normalizedSignature()
  805. */
  806. bool QMetaObject::checkConnectArgs(const char *signal, const char *method)
  807. {
  808. const char *s1 = signal;
  809. const char *s2 = method;
  810. while (*s1++ != '(') { } // scan to first '('
  811. while (*s2++ != '(') { }
  812. if (*s2 == ')' || qstrcmp(s1,s2) == 0) // method has no args or
  813. return true; // exact match
  814. int s1len = qstrlen(s1);
  815. int s2len = qstrlen(s2);
  816. if (s2len < s1len && strncmp(s1,s2,s2len-1)==0 && s1[s2len-1]==',')
  817. return true; // method has less args
  818. return false;
  819. }
  820. static void qRemoveWhitespace(const char *s, char *d)
  821. {
  822. char last = 0;
  823. while (*s && is_space(*s))
  824. s++;
  825. while (*s) {
  826. while (*s && !is_space(*s))
  827. last = *d++ = *s++;
  828. while (*s && is_space(*s))
  829. s++;
  830. if (*s && ((is_ident_char(*s) && is_ident_char(last))
  831. || ((*s == ':') && (last == '<')))) {
  832. last = *d++ = ' ';
  833. }
  834. }
  835. *d = '\0';
  836. }
  837. static char *qNormalizeType(char *d, int &templdepth, QByteArray &result)
  838. {
  839. const char *t = d;
  840. while (*d && (templdepth
  841. || (*d != ',' && *d != ')'))) {
  842. if (*d == '<')
  843. ++templdepth;
  844. if (*d == '>')
  845. --templdepth;
  846. ++d;
  847. }
  848. if (strncmp("void", t, d - t) != 0)
  849. result += normalizeTypeInternal(t, d);
  850. return d;
  851. }
  852. /*!
  853. \since 4.2
  854. Normalizes a \a type.
  855. See QMetaObject::normalizedSignature() for a description on how
  856. Qt normalizes.
  857. Example:
  858. \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 1
  859. \sa normalizedSignature()
  860. */
  861. QByteArray QMetaObject::normalizedType(const char *type)
  862. {
  863. QByteArray result;
  864. if (!type || !*type)
  865. return result;
  866. QVarLengthArray<char> stackbuf(qstrlen(type) + 1);
  867. qRemoveWhitespace(type, stackbuf.data());
  868. int templdepth = 0;
  869. qNormalizeType(stackbuf.data(), templdepth, result);
  870. return result;
  871. }
  872. /*!
  873. Normalizes the signature of the given \a method.
  874. Qt uses normalized signatures to decide whether two given signals
  875. and slots are compatible. Normalization reduces whitespace to a
  876. minimum, moves 'const' to the front where appropriate, removes
  877. 'const' from value types and replaces const references with
  878. values.
  879. \sa checkConnectArgs(), normalizedType()
  880. */
  881. QByteArray QMetaObject::normalizedSignature(const char *method)
  882. {
  883. QByteArray result;
  884. if (!method || !*method)
  885. return result;
  886. int len = int(strlen(method));
  887. QVarLengthArray<char> stackbuf(len + 1);
  888. char *d = stackbuf.data();
  889. qRemoveWhitespace(method, d);
  890. result.reserve(len);
  891. int argdepth = 0;
  892. int templdepth = 0;
  893. while (*d) {
  894. if (argdepth == 1) {
  895. d = qNormalizeType(d, templdepth, result);
  896. if (!*d) //most likely an invalid signature.
  897. break;
  898. }
  899. if (*d == '(')
  900. ++argdepth;
  901. if (*d == ')')
  902. --argdepth;
  903. result += *d++;
  904. }
  905. return result;
  906. }
  907. enum { MaximumParamCount = 11 }; // up to 10 arguments + 1 return value
  908. /*!
  909. Invokes the \a member (a signal or a slot name) on the object \a
  910. obj. Returns true if the member could be invoked. Returns false
  911. if there is no such member or the parameters did not match.
  912. The invocation can be either synchronous or asynchronous,
  913. depending on \a type:
  914. \list
  915. \o If \a type is Qt::DirectConnection, the member will be invoked immediately.
  916. \o If \a type is Qt::QueuedConnection,
  917. a QEvent will be sent and the member is invoked as soon as the application
  918. enters the main event loop.
  919. \o If \a type is Qt::BlockingQueuedConnection, the method will be invoked in
  920. the same way as for Qt::QueuedConnection, except that the current thread
  921. will block until the event is delivered. Using this connection type to
  922. communicate between objects in the same thread will lead to deadlocks.
  923. \o If \a type is Qt::AutoConnection, the member is invoked
  924. synchronously if \a obj lives in the same thread as the
  925. caller; otherwise it will invoke the member asynchronously.
  926. \endlist
  927. The return value of the \a member function call is placed in \a
  928. ret. If the invocation is asynchronous, the return value cannot
  929. be evaluated. You can pass up to ten arguments (\a val0, \a val1,
  930. \a val2, \a val3, \a val4, \a val5, \a val6, \a val7, \a val8,
  931. and \a val9) to the \a member function.
  932. QGenericArgument and QGenericReturnArgument are internal
  933. helper classes. Because signals and slots can be dynamically
  934. invoked, you must enclose the arguments using the Q_ARG() and
  935. Q_RETURN_ARG() macros. Q_ARG() takes a type name and a
  936. const reference of that type; Q_RETURN_ARG() takes a type name
  937. and a non-const reference.
  938. You only need to pass the name of the signal or slot to this function,
  939. not the entire signature. For example, to asynchronously invoke
  940. the \l{QPushButton::animateClick()}{animateClick()} slot on a
  941. QPushButton, use the following code:
  942. \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 2
  943. With asynchronous method invocations, the parameters must be of
  944. types that are known to Qt's meta-object system, because Qt needs
  945. to copy the arguments to store them in an event behind the
  946. scenes. If you try to use a queued connection and get the error
  947. message
  948. \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 3
  949. call qRegisterMetaType() to register the data type before you
  950. call invokeMethod().
  951. To synchronously invoke the \c compute(QString, int, double) slot on
  952. some arbitrary object \c obj retrieve its return value:
  953. \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 4
  954. If the "compute" slot does not take exactly one QString, one int
  955. and one double in the specified order, the call will fail.
  956. \sa Q_ARG(), Q_RETURN_ARG(), qRegisterMetaType(), QMetaMethod::invoke()
  957. */
  958. bool QMetaObject::invokeMethod(QObject *obj,
  959. const char *member,
  960. Qt::ConnectionType type,
  961. QGenericReturnArgument ret,
  962. QGenericArgument val0,
  963. QGenericArgument val1,
  964. QGenericArgument val2,
  965. QGenericArgument val3,
  966. QGenericArgument val4,
  967. QGenericArgument val5,
  968. QGenericArgument val6,
  969. QGenericArgument val7,
  970. QGenericArgument val8,
  971. QGenericArgument val9)
  972. {
  973. if (!obj)
  974. return false;
  975. QVarLengthArray<char, 512> sig;
  976. int len = qstrlen(member);
  977. if (len <= 0)
  978. return false;
  979. sig.append(member, len);
  980. sig.append('(');
  981. const char *typeNames[] = {ret.name(), val0.name(), val1.name(), val2.name(), val3.name(),
  982. val4.name(), val5.name(), val6.name(), val7.name(), val8.name(),
  983. val9.name()};
  984. int paramCount;
  985. for (paramCount = 1; paramCount < MaximumParamCount; ++paramCount) {
  986. len = qstrlen(typeNames[paramCount]);
  987. if (len <= 0)
  988. break;
  989. sig.append(typeNames[paramCount], len);
  990. sig.append(',');
  991. }
  992. if (paramCount == 1)
  993. sig.append(')'); // no parameters
  994. else
  995. sig[sig.size() - 1] = ')';
  996. sig.append('\0');
  997. int idx = obj->metaObject()->indexOfMethod(sig.constData());
  998. if (idx < 0) {
  999. QByteArray norm = QMetaObject::normalizedSignature(sig.constData());
  1000. idx = obj->metaObject()->indexOfMethod(norm.constData());
  1001. }
  1002. if (idx < 0 || idx >= obj->metaObject()->methodCount()) {
  1003. qWarning("QMetaObject::invokeMethod: No such method %s::%s",
  1004. obj->metaObject()->className(), sig.constData());
  1005. return false;
  1006. }
  1007. QMetaMethod method = obj->metaObject()->method(idx);
  1008. return method.invoke(obj, type, ret,
  1009. val0, val1, val2, val3, val4, val5, val6, val7, val8, val9);
  1010. }
  1011. /*! \fn bool QMetaObject::invokeMethod(QObject *obj, const char *member,
  1012. QGenericReturnArgument ret,
  1013. QGenericArgument val0 = QGenericArgument(0),
  1014. QGenericArgument val1 = QGenericArgument(),
  1015. QGenericArgument val2 = QGenericArgument(),
  1016. QGenericArgument val3 = QGenericArgument(),
  1017. QGenericArgument val4 = QGenericArgument(),
  1018. QGenericArgument val5 = QGenericArgument(),
  1019. QGenericArgument val6 = QGenericArgument(),
  1020. QGenericArgument val7 = QGenericArgument(),
  1021. QGenericArgument val8 = QGenericArgument(),
  1022. QGenericArgument val9 = QGenericArgument());
  1023. \overload invokeMethod()
  1024. This overload always invokes the member using the connection type Qt::AutoConnection.
  1025. */
  1026. /*! \fn bool QMetaObject::invokeMethod(QObject *obj, const char *member,
  1027. Qt::ConnectionType type,
  1028. QGenericArgument val0 = QGenericArgument(0),
  1029. QGenericArgument val1 = QGenericArgument(),
  1030. QGenericArgument val2 = QGenericArgument(),
  1031. QGenericArgument val3 = QGenericArgument(),
  1032. QGenericArgument val4 = QGenericArgument(),
  1033. QGenericArgument val5 = QGenericArgument(),
  1034. QGenericArgument val6 = QGenericArgument(),
  1035. QGenericArgument val7 = QGenericArgument(),
  1036. QGenericArgument val8 = QGenericArgument(),
  1037. QGenericArgument val9 = QGenericArgument())
  1038. \overload invokeMethod()
  1039. This overload can be used if the return value of the member is of no interest.
  1040. */
  1041. /*!
  1042. \fn bool QMetaObject::invokeMethod(QObject *obj, const char *member,
  1043. QGenericArgument val0 = QGenericArgument(0),
  1044. QGenericArgument val1 = QGenericArgument(),
  1045. QGenericArgument val2 = QGenericArgument(),
  1046. QGenericArgument val3 = QGenericArgument(),
  1047. QGenericArgument val4 = QGenericArgument(),
  1048. QGenericArgument val5 = QGenericArgument(),
  1049. QGenericArgument val6 = QGenericArgument(),
  1050. QGenericArgument val7 = QGenericArgument(),
  1051. QGenericArgument val8 = QGenericArgument(),
  1052. QGenericArgument val9 = QGenericArgument())
  1053. \overload invokeMethod()
  1054. This overload invokes the member using the connection type Qt::AutoConnection and
  1055. ignores return values.
  1056. */
  1057. /*!
  1058. \class QMetaMethod
  1059. \brief The QMetaMethod class provides meta-data about a member
  1060. function.
  1061. \ingroup objectmodel
  1062. A QMetaMethod has a methodType(), a signature(), a list of
  1063. parameterTypes() and parameterNames(), a return typeName(), a
  1064. tag(), and an access() specifier. You can use invoke() to invoke
  1065. the method on an arbitrary QObject.
  1066. A method will only be registered with the meta-object system if it
  1067. is a slot, a signal, or declared with the Q_INVOKABLE macro.
  1068. Constructors can also be registered with Q_INVOKABLE.
  1069. \sa QMetaObject, QMetaEnum, QMetaProperty, {Qt's Property System}
  1070. */
  1071. /*!
  1072. \enum QMetaMethod::Attributes
  1073. \internal
  1074. \value Compatibility
  1075. \value Cloned
  1076. \value Scriptable
  1077. */
  1078. /*!
  1079. \fn const QMetaObject *QMetaMethod::enclosingMetaObject() const
  1080. \internal
  1081. */
  1082. /*!
  1083. \enum QMetaMethod::MethodType
  1084. \value Method The function is a plain member function.
  1085. \value Signal The function is a signal.
  1086. \value Slot The function is a slot.
  1087. \value Constructor The function is a constructor.
  1088. */
  1089. /*!
  1090. \fn QMetaMethod::QMetaMethod()
  1091. \internal
  1092. */
  1093. /*!
  1094. Returns the signature of this method (e.g.,
  1095. \c{setValue(double)}).
  1096. \sa parameterTypes(), parameterNames()
  1097. */
  1098. const char *QMetaMethod::signature() const
  1099. {
  1100. if (!mobj)
  1101. return 0;
  1102. return mobj->d.stringdata + mobj->d.data[handle];
  1103. }
  1104. /*!
  1105. Returns a list of parameter types.
  1106. \sa parameterNames(), signature()
  1107. */
  1108. QList<QByteArray> QMetaMethod::parameterTypes() const
  1109. {
  1110. QList<QByteArray> list;
  1111. if (!mobj)
  1112. return list;
  1113. const char *signature = mobj->d.stringdata + mobj->d.data[handle];
  1114. while (*signature && *signature != '(')
  1115. ++signature;
  1116. while (*signature && *signature != ')' && *++signature != ')') {
  1117. const char *begin = signature;
  1118. int level = 0;
  1119. while (*signature && (level > 0 || *signature != ',') && *signature != ')') {
  1120. if (*signature == '<')
  1121. ++level;
  1122. else if (*signature == '>')
  1123. --level;
  1124. ++signature;
  1125. }
  1126. list += QByteArray(begin, signature - begin);
  1127. }
  1128. return list;
  1129. }
  1130. /*!
  1131. Returns a list of parameter names.
  1132. \sa parameterTypes(), signature()
  1133. */
  1134. QList<QByteArray> QMetaMethod::parameterNames() const
  1135. {
  1136. QList<QByteArray> list;
  1137. if (!mobj)
  1138. return list;
  1139. const char *names = mobj->d.stringdata + mobj->d.data[handle + 1];
  1140. if (*names == 0) {
  1141. // do we have one or zero arguments?
  1142. const char *signature = mobj->d.stringdata + mobj->d.data[handle];
  1143. while (*signature && *signature != '(')
  1144. ++signature;
  1145. if (*++signature != ')')
  1146. list += QByteArray();
  1147. } else {
  1148. --names;
  1149. do {
  1150. const char *begin = ++names;
  1151. while (*names && *names != ',')
  1152. ++names;
  1153. list += QByteArray(begin, names - begin);
  1154. } while (*names);
  1155. }
  1156. return list;
  1157. }
  1158. /*!
  1159. Returns the return type of this method, or an empty string if the
  1160. return type is \e void.
  1161. */
  1162. const char *QMetaMethod::typeName() const
  1163. {
  1164. if (!mobj)
  1165. return 0;
  1166. return mobj->d.stringdata + mobj->d.data[handle + 2];
  1167. }
  1168. /*!
  1169. Returns the tag associated with this method.
  1170. Tags are special macros recognized by \c moc that make it
  1171. possible to add extra information about a method. For the moment,
  1172. \c moc doesn't support any special tags.
  1173. */
  1174. const char *QMetaMethod::tag() const
  1175. {
  1176. if (!mobj)
  1177. return 0;
  1178. return mobj->d.stringdata + mobj->d.data[handle + 3];
  1179. }
  1180. /*! \internal */
  1181. int QMetaMethod::attributes() const
  1182. {
  1183. if (!mobj)
  1184. return false;
  1185. return ((mobj->d.data[handle + 4])>>4);
  1186. }
  1187. /*!
  1188. \since 4.6
  1189. Returns this method's index.
  1190. */
  1191. int QMetaMethod::methodIndex() const
  1192. {
  1193. if (!mobj)
  1194. return -1;
  1195. return ((handle - priv(mobj->d.data)->methodData) / 5) + mobj->methodOffset();
  1196. }
  1197. /*!
  1198. \internal
  1199. Returns the method revision if one was
  1200. specified by Q_REVISION, otherwise returns 0.
  1201. */
  1202. int QMetaMethod::revision() const
  1203. {
  1204. if (!mobj)
  1205. return 0;
  1206. if ((QMetaMethod::Access)(mobj->d.data[handle + 4] & MethodRevisioned)) {
  1207. int offset = priv(mobj->d.data)->methodData
  1208. + priv(mobj->d.data)->methodCount * 5
  1209. + (handle - priv(mobj->d.data)->methodData) / 5;
  1210. return mobj->d.data[offset];
  1211. }
  1212. return 0;
  1213. }
  1214. /*!
  1215. Returns the access specification of this method (private,
  1216. protected, or public).
  1217. Signals are always protected, meaning that you can only emit them
  1218. from the class or from a subclass.
  1219. \sa methodType()
  1220. */
  1221. QMetaMethod::Access QMetaMethod::access() const
  1222. {
  1223. if (!mobj)
  1224. return Private;
  1225. return (QMetaMethod::Access)(mobj->d.data[handle + 4] & AccessMask);
  1226. }
  1227. /*!
  1228. Returns the type of this method (signal, slot, or method).
  1229. \sa access()
  1230. */
  1231. QMetaMethod::MethodType QMetaMethod::methodType() const
  1232. {
  1233. if (!mobj)
  1234. return QMetaMethod::Method;
  1235. return (QMetaMethod::MethodType)((mobj->d.data[handle + 4] & MethodTypeMask)>>2);
  1236. }
  1237. /*!
  1238. Invokes this method on the object \a object. Returns true if the member could be invoked.
  1239. Returns false if there is no such member or the parameters did not match.
  1240. The invocation can be either synchronous or asynchronous, depending on the
  1241. \a connectionType:
  1242. \list
  1243. \o If \a connectionType is Qt::DirectConnection, the member will be invoked immediately.
  1244. \o If \a connectionType is Qt::QueuedConnection,
  1245. a QEvent will be posted and the member is invoked as soon as the application
  1246. enters the main event loop.
  1247. \o If \a connectionType is Qt::AutoConnection, the member is invoked
  1248. synchronously if \a object lives in the same thread as the
  1249. caller; otherwise it will invoke the member asynchronously.
  1250. \endlist
  1251. The return value of this method call is placed in \a
  1252. returnValue. If the invocation is asynchronous, the return value cannot
  1253. be evaluated. You can pass up to ten arguments (\a val0, \a val1,
  1254. \a val2, \a val3, \a val4, \a val5, \a val6, \a val7, \a val8,
  1255. and \a val9) to this method call.
  1256. QGenericArgument and QGenericReturnArgument are internal
  1257. helper classes. Because signals and slots can be dynamically
  1258. invoked, you must enclose the arguments using the Q_ARG() and
  1259. Q_RETURN_ARG() macros. Q_ARG() takes a type name and a
  1260. const reference of that type; Q_RETURN_ARG() takes a type name
  1261. and a non-const reference.
  1262. To asynchronously invoke the
  1263. \l{QPushButton::animateClick()}{animateClick()} slot on a
  1264. QPushButton:
  1265. \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 6
  1266. With asynchronous method invocations, the parameters must be of
  1267. types that are known to Qt's meta-object system, because Qt needs
  1268. to copy the arguments to store them in an event behind the
  1269. scenes. If you try to use a queued connection and get the error
  1270. message
  1271. \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 7
  1272. call qRegisterMetaType() to register the data type before you
  1273. call QMetaMethod::invoke().
  1274. To synchronously invoke the \c compute(QString, int, double) slot on
  1275. some arbitrary object \c obj retrieve its return value:
  1276. \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 8
  1277. QMetaObject::normalizedSignature() is used here to ensure that the format
  1278. of the signature is what invoke() expects. E.g. extra whitespace is
  1279. removed.
  1280. If the "compute" slot does not take exactly one QString, one int
  1281. and one double in the specified order, the call will fail.
  1282. \warning this method will not test the validity of the arguments: \a object
  1283. must be an instance of the class of the QMetaObject of which this QMetaMethod
  1284. has been constructed with. The arguments must have the same type as the ones
  1285. expected by the method, else, the behaviour is undefined.
  1286. \sa Q_ARG(), Q_RETURN_ARG(), qRegisterMetaType(), QMetaObject::invokeMethod()
  1287. */
  1288. bool QMetaMethod::invoke(QObject *object,
  1289. Qt::ConnectionType connectionType,
  1290. QGenericReturnArgument returnValue,
  1291. QGenericArgument val0,
  1292. QGenericArgument val1,
  1293. QGenericArgument val2,
  1294. QGenericArgument val3,
  1295. QGenericArgument val4,
  1296. QGenericArgument val5,
  1297. QGenericArgument val6,
  1298. QGenericArgument val7,
  1299. QGenericArgument val8,
  1300. QGenericArgument val9) const
  1301. {
  1302. if (!object || !mobj)
  1303. return false;
  1304. Q_ASSERT(mobj->cast(object));
  1305. // check return type
  1306. if (returnValue.data()) {
  1307. const char *retType = typeName();
  1308. if (qstrcmp(returnValue.name(), retType) != 0) {
  1309. // normalize the return value as well
  1310. // the trick here is to make a function signature out of the return type
  1311. // so that we can call normalizedSignature() and avoid duplicating code
  1312. QByteArray unnormalized;
  1313. int len = qstrlen(returnValue.name());
  1314. unnormalized.reserve(len + 3);
  1315. unnormalized = "_("; // the function is called "_"
  1316. unnormalized.append(returnValue.name());
  1317. unnormalized.append(')');
  1318. QByteArray normalized = QMetaObject::normalizedSignature(unnormalized.constData());
  1319. normalized.truncate(normalized.length() - 1); // drop the ending ')'
  1320. if (qstrcmp(normalized.constData() + 2, retType) != 0)
  1321. return false;
  1322. }
  1323. }
  1324. // check argument count (we don't allow invoking a method if given too few arguments)
  1325. const char *typeNames[] = {
  1326. returnValue.name(),
  1327. val0.name(),
  1328. val1.name(),
  1329. val2.name(),
  1330. val3.name(),
  1331. val4.name(),
  1332. val5.name(),
  1333. val6.name(),
  1334. val7.name(),
  1335. val8.name(),
  1336. val9.name()
  1337. };
  1338. int paramCount;
  1339. for (paramCount = 1; paramCount < MaximumParamCount; ++paramCount) {
  1340. if (qstrlen(typeNames[paramCount]) <= 0)
  1341. break;
  1342. }
  1343. int metaMethodArgumentCount = 0;
  1344. {
  1345. // based on QMetaObject::parameterNames()
  1346. const char *names = mobj->d.stringdata + mobj->d.data[handle + 1];
  1347. if (*names == 0) {
  1348. // do we have one or zero arguments?
  1349. const char *signature = mobj->d.stringdata + mobj->d.data[handle];
  1350. while (*signature && *signature != '(')
  1351. ++signature;
  1352. if (*++signature != ')')
  1353. ++metaMethodArgumentCount;
  1354. } else {
  1355. --names;
  1356. do {
  1357. ++names;
  1358. while (*names && *names != ',')
  1359. ++names;
  1360. ++metaMethodArgumentCount;
  1361. } while (*names);
  1362. }
  1363. }
  1364. if (paramCount <= metaMethodArgumentCount)
  1365. return false;
  1366. // check connection type
  1367. QThread *currentThread = QThread::currentThread();
  1368. QThread *objectThread = object->thread();
  1369. if (connectionType == Qt::AutoConnection) {
  1370. connectionType = currentThread == objectThread
  1371. ? Qt::DirectConnection
  1372. : Qt::QueuedConnection;
  1373. }
  1374. #ifdef QT_NO_THREAD
  1375. if (connectionType == Qt::BlockingQueuedConnection) {
  1376. connectionType = Qt::DirectConnection;
  1377. }
  1378. #endif
  1379. // invoke!
  1380. void *param[] = {
  1381. returnValue.data(),
  1382. val0.data(),
  1383. val1.data(),
  1384. val2.data(),
  1385. val3.data(),
  1386. val4.data(),
  1387. val5.data(),
  1388. val6.data(),
  1389. val7.data(),
  1390. val8.data(),
  1391. val9.data()
  1392. };
  1393. // recompute the methodIndex by reversing the arithmetic in QMetaObject::property()
  1394. int idx_relative = ((handle - priv(mobj->d.data)->methodData) / 5);
  1395. int idx_offset = mobj->methodOffset();
  1396. QObjectPrivate::StaticMetaCallFunction callFunction =
  1397. (QMetaObjectPrivate::get(mobj)->revision >= 6 && mobj->d.extradata)
  1398. ? reinterpret_cast<const QMetaObjectExtraData *>(mobj->d.extradata)->static_metacall : 0;
  1399. if (connectionType == Qt::DirectConnection) {
  1400. if (callFunction) {
  1401. callFunction(object, QMetaObject::InvokeMetaMethod, idx_relative, param);
  1402. return true;
  1403. } else {
  1404. return QMetaObject::metacall(object, QMetaObject::InvokeMetaMethod, idx_relative + idx_offset, param) < 0;
  1405. }
  1406. } else if (connectionType == Qt::QueuedConnection) {
  1407. if (returnValue.data()) {
  1408. qWarning("QMetaMethod::invoke: Unable to invoke methods with return values in "
  1409. "queued connections");
  1410. return false;
  1411. }
  1412. int nargs = 1; // include return type
  1413. void **args = (void **) qMalloc(paramCount * sizeof(void *));
  1414. Q_CHECK_PTR(args);
  1415. int *types = (int *) qMalloc(paramCount * sizeof(int));
  1416. Q_CHECK_PTR(types);
  1417. types[0] = 0; // return type
  1418. args[0] = 0;
  1419. for (int i = 1; i < paramCount; ++i) {
  1420. types[i] = QMetaType::type(typeNames[i]);
  1421. if (types[i]) {
  1422. args[i] = QMetaType::construct(types[i], param[i]);
  1423. ++nargs;
  1424. } else if (param[i]) {
  1425. qWarning("QMetaMethod::invoke: Unable to handle unregistered datatype '%s'",
  1426. typeNames[i]);
  1427. for (int x = 1; x < i; ++x) {
  1428. if (types[x] && args[x])
  1429. QMetaType::destroy(types[x], args[x]);
  1430. }
  1431. qFree(types);
  1432. qFree(args);
  1433. return false;
  1434. }
  1435. }
  1436. QCoreApplication::postEvent(object, new QMetaCallEvent(idx_offset, idx_relative, callFunction,
  1437. 0, -1, nargs, types, args));
  1438. } else { // blocking queued connection
  1439. #ifndef QT_NO_THREAD
  1440. if (currentThread == objectThread) {
  1441. qWarning("QMetaMethod::invoke: Dead lock detected in "
  1442. "BlockingQueuedConnection: Receiver is %s(%p)",
  1443. mobj->className(), object);
  1444. }
  1445. QSemaphore semaphore;
  1446. QCoreApplication::postEvent(object, new QMetaCallEvent(idx_offset, idx_relative, callFunction,
  1447. 0, -1, 0, 0, param, &semaphore));
  1448. semaphore.acquire();
  1449. #endif // QT_NO_THREAD
  1450. }
  1451. return true;
  1452. }
  1453. /*! \fn bool QMetaMethod::invoke(QObject *object,
  1454. QGenericReturnArgument returnValue,
  1455. QGenericArgument val0 = QGenericArgument(0),
  1456. QGenericArgument val1 = QGenericArgument(),
  1457. QGenericArgument val2 = QGenericArgument(),
  1458. QGenericArgument val3 = QGenericArgument(),
  1459. QGenericArgument val4 = QGenericArgument(),
  1460. QGenericArgument val5 = QGenericArgument(),
  1461. QGenericArgument val6 = QGenericArgument(),
  1462. QGenericArgument val7 = QGenericArgument(),
  1463. QGenericArgument val8 = QGenericArgument(),
  1464. QGenericArgument val9 = QGenericArgument()) const
  1465. \overload invoke()
  1466. This overload always invokes this method using the connection type Qt::AutoConnection.
  1467. */
  1468. /*! \fn bool QMetaMethod::invoke(QObject *object,
  1469. Qt::ConnectionType connectionType,
  1470. QGenericArgument val0 = QGenericArgument(0),
  1471. QGenericArgument val1 = QGenericArgument(),
  1472. QGenericArgument val2 = QGenericArgument(),
  1473. QGenericArgument val3 = QGenericArgument(),
  1474. QGenericArgument val4 = QGenericArgument(),
  1475. QGenericArgument val5 = QGenericArgument(),
  1476. QGenericArgument val6 = QGenericArgument(),
  1477. QGenericArgument val7 = QGenericArgument(),
  1478. QGenericArgument val8 = QGenericArgument(),
  1479. QGenericArgument val9 = QGenericArgument()) const
  1480. \overload invoke()
  1481. This overload can be used if the return value of the member is of no interest.
  1482. */
  1483. /*!
  1484. \fn bool QMetaMethod::invoke(QObject *object,
  1485. QGenericArgument val0 = QGenericArgument(0),
  1486. QGenericArgument val1 = QGenericArgument(),
  1487. QGenericArgument val2 = QGenericArgument(),
  1488. QGenericArgument val3 = QGenericArgument(),
  1489. QGenericArgument val4 = QGenericArgument(),
  1490. QGenericArgument val5 = QGenericArgument(),
  1491. QGenericArgument val6 = QGenericArgument(),
  1492. QGenericArgument val7 = QGenericArgument(),
  1493. QGenericArgument val8 = QGenericArgument(),
  1494. QGenericArgument val9 = QGenericArgument()) const
  1495. \overload invoke()
  1496. This overload invokes this method using the
  1497. connection type Qt::AutoConnection and ignores return values.
  1498. */
  1499. /*!
  1500. \class QMetaEnum
  1501. \brief The QMetaEnum class provides meta-data about an enumerator.
  1502. \ingroup objectmodel
  1503. Use name() for the enumerator's name. The enumerator's keys (names
  1504. of each enumerated item) are returned by key(); use keyCount() to find
  1505. the number of keys. isFlag() returns whether the enumerator is
  1506. meant to be used as a flag, meaning that its values can be combined
  1507. using the OR operator.
  1508. The conversion functions keyToValue(), valueToKey(), keysToValue(),
  1509. and valueToKeys() allow conversion between the integer
  1510. representation of an enumeration or set value and its literal
  1511. representation. The scope() function returns the class scope this
  1512. enumerator was declared in.
  1513. \sa QMetaObject, QMetaMethod, QMetaProperty
  1514. */
  1515. /*!
  1516. \fn bool QMetaEnum::isValid() const
  1517. Returns true if this enum is valid (has a name); otherwise returns
  1518. false.
  1519. \sa name()
  1520. */
  1521. /*!
  1522. \fn const QMetaObject *QMetaEnum::enclosingMetaObject() const
  1523. \internal
  1524. */
  1525. /*!
  1526. \fn QMetaEnum::QMetaEnum()
  1527. \internal
  1528. */
  1529. /*!
  1530. Returns the name of the enumerator (without the scope).
  1531. For example, the Qt::AlignmentFlag enumeration has \c
  1532. AlignmentFlag as the name and \l Qt as the scope.
  1533. \sa isValid(), scope()
  1534. */
  1535. const char *QMetaEnum::name() const
  1536. {
  1537. if (!mobj)
  1538. return 0;
  1539. return mobj->d.stringdata + mobj->d.data[handle];
  1540. }
  1541. /*!
  1542. Returns the number of keys.
  1543. \sa key()
  1544. */
  1545. int QMetaEnum::keyCount() const
  1546. {
  1547. if (!mobj)
  1548. return 0;
  1549. return mobj->d.data[handle + 2];
  1550. }
  1551. /*!
  1552. Returns the key with the given \a index, or 0 if no such key exists.
  1553. \sa keyCount(), value(), valueToKey()
  1554. */
  1555. const char *QMetaEnum::key(int index) const
  1556. {
  1557. if (!mobj)
  1558. return 0;
  1559. int count = mobj->d.data[handle + 2];
  1560. int data = mobj->d.data[handle + 3];
  1561. if (index >= 0 && index < count)
  1562. return mobj->d.stringdata + mobj->d.data[data + 2*index];
  1563. return 0;
  1564. }
  1565. /*!
  1566. Returns the value with the given \a index; or returns -1 if there
  1567. is no such value.
  1568. \sa keyCount(), key(), keyToValue()
  1569. */
  1570. int QMetaEnum::value(int index) const
  1571. {
  1572. if (!mobj)
  1573. return 0;
  1574. int count = mobj->d.data[handle + 2];
  1575. int data = mobj->d.data[handle + 3];
  1576. if (index >= 0 && index < count)
  1577. return mobj->d.data[data + 2*index + 1];
  1578. return -1;
  1579. }
  1580. /*!
  1581. Returns true if this enumerator is used as a flag; otherwise returns
  1582. false.
  1583. When used as flags, enumerators can be combined using the OR
  1584. operator.
  1585. \sa keysToValue(), valueToKeys()
  1586. */
  1587. bool QMetaEnum::isFlag() const
  1588. {
  1589. return mobj && mobj->d.data[handle + 1];
  1590. }
  1591. /*!
  1592. Returns the scope this enumerator was declared in.
  1593. For example, the Qt::AlignmentFlag enumeration has \c Qt as
  1594. the scope and \c AlignmentFlag as the name.
  1595. \sa name()
  1596. */
  1597. const char *QMetaEnum::scope() const
  1598. {
  1599. return mobj?mobj->d.stringdata : 0;
  1600. }
  1601. /*!
  1602. Returns the integer value of the given enumeration \a key, or -1
  1603. if \a key is not defined.
  1604. For flag types, use keysToValue().
  1605. \sa valueToKey(), isFlag(), keysToValue()
  1606. */
  1607. int QMetaEnum::keyToValue(const char *key) const
  1608. {
  1609. if (!mobj || !key)
  1610. return -1;
  1611. uint scope = 0;
  1612. const char *qualified_key = key;
  1613. const char *s = key + qstrlen(key);
  1614. while (s > key && *s != ':')
  1615. --s;
  1616. if (s > key && *(s-1)==':') {
  1617. scope = s - key - 1;
  1618. key += scope + 2;
  1619. }
  1620. int count = mobj->d.data[handle + 2];
  1621. int data = mobj->d.data[handle + 3];
  1622. for (int i = 0; i < count; ++i)
  1623. if ((!scope || (qstrlen(mobj->d.stringdata) == scope && strncmp(qualified_key, mobj->d.stringdata, scope) == 0))
  1624. && strcmp(key, mobj->d.stringdata + mobj->d.data[data + 2*i]) == 0)
  1625. return mobj->d.data[data + 2*i + 1];
  1626. return -1;
  1627. }
  1628. /*!
  1629. Returns the string that is used as the name of the given
  1630. enumeration \a value, or 0 if \a value is not defined.
  1631. For flag types, use valueToKeys().
  1632. \sa isFlag(), valueToKeys()
  1633. */
  1634. const char* QMetaEnum::valueToKey(int value) const
  1635. {
  1636. if (!mobj)
  1637. return 0;
  1638. int count = mobj->d.data[handle + 2];
  1639. int data = mobj->d.data[handle + 3];
  1640. for (int i = 0; i < count; ++i)
  1641. if (value == (int)mobj->d.data[data + 2*i + 1])
  1642. return mobj->d.stringdata + mobj->d.data[data + 2*i];
  1643. return 0;
  1644. }
  1645. /*!
  1646. Returns the value derived from combining together the values of
  1647. the \a keys using the OR operator, or -1 if \a keys is not
  1648. defined. Note that the strings in \a keys must be '|'-separated.
  1649. \sa isFlag(), valueToKey(), valueToKeys()
  1650. */
  1651. int QMetaEnum::keysToValue(const char *keys) const
  1652. {
  1653. if (!mobj)
  1654. return -1;
  1655. QStringList l = QString::fromLatin1(keys).split(QLatin1Char('|'));
  1656. //#### TODO write proper code, do not use QStringList
  1657. int value = 0;
  1658. int count = mobj->d.data[handle + 2];
  1659. int data = mobj->d.data[handle + 3];
  1660. for (int li = 0; li < l.size(); ++li) {
  1661. QString trimmed = l.at(li).trimmed();
  1662. QByteArray qualified_key = trimmed.toLatin1();
  1663. const char *key = qualified_key.constData();
  1664. uint scope = 0;
  1665. const char *s = key + qstrlen(key);
  1666. while (s > key && *s != ':')
  1667. --s;
  1668. if (s > key && *(s-1)==':') {
  1669. scope = s - key - 1;
  1670. key += scope + 2;
  1671. }
  1672. int i;
  1673. for (i = count-1; i >= 0; --i)
  1674. if ((!scope || (qstrlen(mobj->d.stringdata) == scope && strncmp(qualified_key.constData(), mobj->d.stringdata, scope) == 0))
  1675. && strcmp(key, mobj->d.stringdata + mobj->d.data[data + 2*i]) == 0) {
  1676. value |= mobj->d.data[data + 2*i + 1];
  1677. break;
  1678. }
  1679. if (i < 0)
  1680. value |= -1;
  1681. }
  1682. return value;
  1683. }
  1684. /*!
  1685. Returns a byte array of '|'-separated keys that represents the
  1686. given \a value.
  1687. \sa isFlag(), valueToKey(), keysToValue()
  1688. */
  1689. QByteArray QMetaEnum::valueToKeys(int value) const
  1690. {
  1691. QByteArray keys;
  1692. if (!mobj)
  1693. return keys;
  1694. int count = mobj->d.data[handle + 2];
  1695. int data = mobj->d.data[handle + 3];
  1696. int v = value;
  1697. for(int i = 0; i < count; i++) {
  1698. int k = mobj->d.data[data + 2*i + 1];
  1699. if ((k != 0 && (v & k) == k ) || (k == value)) {
  1700. v = v & ~k;
  1701. if (!keys.isEmpty())
  1702. keys += '|';
  1703. keys += mobj->d.stringdata + mobj->d.data[data + 2*i];
  1704. }
  1705. }
  1706. return keys;
  1707. }
  1708. static QByteArray qualifiedName(const QMetaEnum &e)
  1709. {
  1710. return QByteArray(e.scope()) + "::" + e.name();
  1711. }
  1712. /*!
  1713. \class QMetaProperty
  1714. \brief The QMetaProperty class provides meta-data about a property.
  1715. \ingroup objectmodel
  1716. Property meta-data is obtained from an object's meta-object. See
  1717. QMetaObject::property() and QMetaObject::propertyCount() for
  1718. details.
  1719. \section1 Property Meta-Data
  1720. A property has a name() and a type(), as well as various
  1721. attributes that specify its behavior: isReadable(), isWritable(),
  1722. isDesignable(), isScriptable(), and isStored().
  1723. If the property is an enumeration, isEnumType() returns true; if the
  1724. property is an enumeration that is also a flag (i.e. its values
  1725. can be combined using the OR operator), isEnumType() and
  1726. isFlagType() both return true. The enumerator for these types is
  1727. available from enumerator().
  1728. The property's values are set and retrieved with read(), write(),
  1729. and reset(); they can also be changed through QObject's set and get
  1730. functions. See QObject::setProperty() and QObject::property() for
  1731. details.
  1732. \section1 Copying and Assignment
  1733. QMetaProperty objects can be copied by value. However, each copy will
  1734. refer to the same underlying property meta-data.
  1735. \sa QMetaObject, QMetaEnum, QMetaMethod, {Qt's Property System}
  1736. */
  1737. /*!
  1738. \fn bool QMetaProperty::isValid() const
  1739. Returns true if this property is valid (readable); otherwise
  1740. returns false.
  1741. \sa isReadable()
  1742. */
  1743. /*!
  1744. \fn const QMetaObject *QMetaProperty::enclosingMetaObject() const
  1745. \internal
  1746. */
  1747. /*!
  1748. \internal
  1749. */
  1750. QMetaProperty::QMetaProperty()
  1751. : mobj(0), handle(0), idx(0)
  1752. {
  1753. }
  1754. /*!
  1755. Returns this property's name.
  1756. \sa type(), typeName()
  1757. */
  1758. const char *QMetaProperty::name() const
  1759. {
  1760. if (!mobj)
  1761. return 0;
  1762. int handle = priv(mobj->d.data)->propertyData + 3*idx;
  1763. return mobj->d.stringdata + mobj->d.data[handle];
  1764. }
  1765. /*!
  1766. Returns the name of this property's type.
  1767. \sa type(), name()
  1768. */
  1769. const char *QMetaProperty::typeName() const
  1770. {
  1771. if (!mobj)
  1772. return 0;
  1773. int handle = priv(mobj->d.data)->propertyData + 3*idx;
  1774. return mobj->d.stringdata + mobj->d.data[handle + 1];
  1775. }
  1776. /*!
  1777. Returns this property's type. The return value is one
  1778. of the values of the QVariant::Type enumeration.
  1779. \sa userType(), typeName(), name()
  1780. */
  1781. QVariant::Type QMetaProperty::type() const
  1782. {
  1783. if (!mobj)
  1784. return QVariant::Invalid;
  1785. int handle = priv(mobj->d.data)->propertyData + 3*idx;
  1786. uint flags = mobj->d.data[handle + 2];
  1787. uint type = flags >> 24;
  1788. if (type == 0xff) // special value for QVariant
  1789. type = QVariant::LastType;
  1790. if (type)
  1791. return QVariant::Type(type);
  1792. if (isEnumType()) {
  1793. int enumMetaTypeId = QMetaType::type(qualifiedName(menum));
  1794. if (enumMetaTypeId == 0)
  1795. return QVariant::Int;
  1796. }
  1797. #ifdef QT_COORD_TYPE
  1798. // qreal metatype must be resolved at runtime.
  1799. if (strcmp(typeName(), "qreal") == 0)
  1800. return QVariant::Type(qMetaTypeId<qreal>());
  1801. #endif
  1802. return QVariant::UserType;
  1803. }
  1804. /*!
  1805. \since 4.2
  1806. Returns this property's user type. The return value is one
  1807. of the values that are registered with QMetaType, or 0 if
  1808. the type is not registered.
  1809. \sa type(), QMetaType, typeName()
  1810. */
  1811. int QMetaProperty::userType() const
  1812. {
  1813. QVariant::Type tp = type();
  1814. if (tp != QVariant::UserType)
  1815. return tp;
  1816. if (isEnumType()) {
  1817. int enumMetaTypeId = QMetaType::type(qualifiedName(menum));
  1818. return enumMetaTypeId;
  1819. }
  1820. return QMetaType::type(typeName());
  1821. }
  1822. /*!
  1823. \since 4.6
  1824. Returns this property's index.
  1825. */
  1826. int QMetaProperty::propertyIndex() const
  1827. {
  1828. if (!mobj)
  1829. return -1;
  1830. return idx + mobj->propertyOffset();
  1831. }
  1832. /*!
  1833. Returns true if the property's type is an enumeration value that
  1834. is used as a flag; otherwise returns false.
  1835. Flags can be combined using the OR operator. A flag type is
  1836. implicitly also an enum type.
  1837. \sa isEnumType(), enumerator(), QMetaEnum::isFlag()
  1838. */
  1839. bool QMetaProperty::isFlagType() const
  1840. {
  1841. return isEnumType() && menum.isFlag();
  1842. }
  1843. /*!
  1844. Returns true if the property's type is an enumeration value;
  1845. otherwise returns false.
  1846. \sa enumerator(), isFlagType()
  1847. */
  1848. bool QMetaProperty::isEnumType() const
  1849. {
  1850. if (!mobj)
  1851. return false;
  1852. int handle = priv(mobj->d.data)->propertyData + 3*idx;
  1853. int flags = mobj->d.data[handle + 2];
  1854. return (flags & EnumOrFlag) && menum.name();
  1855. }
  1856. /*!
  1857. \internal
  1858. Returns true if the property has a C++ setter function that
  1859. follows Qt's standard "name" / "setName" pattern. Designer and uic
  1860. query hasStdCppSet() in order to avoid expensive
  1861. QObject::setProperty() calls. All properties in Qt [should] follow
  1862. this pattern.
  1863. */
  1864. bool QMetaProperty::hasStdCppSet() const
  1865. {
  1866. if (!mobj)
  1867. return false;
  1868. int handle = priv(mobj->d.data)->propertyData + 3*idx;
  1869. int flags = mobj->d.data[handle + 2];
  1870. return (flags & StdCppSet);
  1871. }
  1872. /*!
  1873. Returns the enumerator if this property's type is an enumerator
  1874. type; otherwise the returned value is undefined.
  1875. \sa isEnumType(), isFlagType()
  1876. */
  1877. QMetaEnum QMetaProperty::enumerator() const
  1878. {
  1879. return menum;
  1880. }
  1881. /*!
  1882. Reads the property's value from the given \a object. Returns the value
  1883. if it was able to read it; otherwise returns an invalid variant.
  1884. \sa write(), reset(), isReadable()
  1885. */
  1886. QVariant QMetaProperty::read(const QObject *object) const
  1887. {
  1888. if (!object || !mobj)
  1889. return QVariant();
  1890. uint t = QVariant::Int;
  1891. if (isEnumType()) {
  1892. /*
  1893. try to create a QVariant that can be converted to this enum
  1894. type (only works if the enum has already been registered
  1895. with QMetaType)
  1896. */
  1897. int enumMetaTypeId = QMetaType::type(qualifiedName(menum));
  1898. if (enumMetaTypeId != 0)
  1899. t = enumMetaTypeId;
  1900. } else {
  1901. int handle = priv(mobj->d.data)->propertyData + 3*idx;
  1902. uint flags = mobj->d.data[handle + 2];
  1903. const char *typeName = mobj->d.stringdata + mobj->d.data[handle + 1];
  1904. t = (flags >> 24);
  1905. if (t == 0xff) // special value for QVariant
  1906. t = QVariant::LastType;
  1907. if (t == QVariant::Invalid)
  1908. t = QMetaType::type(typeName);
  1909. if (t == QVariant::Invalid)
  1910. t = QVariant::nameToType(typeName);
  1911. if (t == QVariant::Invalid || t == QVariant::UserType) {
  1912. if (t == QVariant::Invalid)
  1913. qWarning("QMetaProperty::read: Unable to handle unregistered datatype '%s' for property '%s::%s'", typeName, mobj->className(), name());
  1914. return QVariant();
  1915. }
  1916. }
  1917. // the status variable is changed by qt_metacall to indicate what it did
  1918. // this feature is currently only used by QtDBus and should not be depended
  1919. // upon. Don't change it without looking into QDBusAbstractInterface first
  1920. // -1 (unchanged): normal qt_metacall, result stored in argv[0]
  1921. // changed: result stored directly in value
  1922. int status = -1;
  1923. QVariant value;
  1924. void *argv[] = { 0, &value, &status };
  1925. if (t == QVariant::LastType) {
  1926. argv[0] = &value;
  1927. } else {
  1928. value = QVariant(t, (void*)0);
  1929. argv[0] = value.data();
  1930. }
  1931. QMetaObject::metacall(const_cast<QObject*>(object), QMetaObject::ReadProperty,
  1932. idx + mobj->propertyOffset(), argv);
  1933. if (status != -1)
  1934. return value;
  1935. if (t != QVariant::LastType && argv[0] != value.data())
  1936. // pointer or reference
  1937. return QVariant((QVariant::Type)t, argv[0]);
  1938. return value;
  1939. }
  1940. /*!
  1941. Writes \a value as the property's value to the given \a object. Returns
  1942. true if the write succeeded; otherwise returns false.
  1943. \sa read(), reset(), isWritable()
  1944. */
  1945. bool QMetaProperty::write(QObject *object, const QVariant &value) const
  1946. {
  1947. if (!object || !isWritable())
  1948. return false;
  1949. QVariant v = value;
  1950. uint t = QVariant::Invalid;
  1951. if (isEnumType()) {
  1952. if (v.type() == QVariant::String
  1953. #ifdef QT3_SUPPORT
  1954. || v.type() == QVariant::CString
  1955. #endif
  1956. ) {
  1957. if (isFlagType())
  1958. v = QVariant(menum.keysToValue(value.toByteArray()));
  1959. else
  1960. v = QVariant(menum.keyToValue(value.toByteArray()));
  1961. } else if (v.type() != QVariant::Int && v.type() != QVariant::UInt) {
  1962. int enumMetaTypeId = QMetaType::type(qualifiedName(menum));
  1963. if ((enumMetaTypeId == 0) || (v.userType() != enumMetaTypeId) || !v.constData())
  1964. return false;
  1965. v = QVariant(*reinterpret_cast<const int *>(v.constData()));
  1966. }
  1967. v.convert(QVariant::Int);
  1968. } else {
  1969. int handle = priv(mobj->d.data)->propertyData + 3*idx;
  1970. uint flags = mobj->d.data[handle + 2];
  1971. t = flags >> 24;
  1972. if (t == 0xff) // special value for QVariant
  1973. t = QVariant::LastType;
  1974. if (t == QVariant::Invalid) {
  1975. const char *typeName = mobj->d.stringdata + mobj->d.data[handle + 1];
  1976. const char *vtypeName = value.typeName();
  1977. if (vtypeName && strcmp(typeName, vtypeName) == 0)
  1978. t = value.userType();
  1979. else
  1980. t = QVariant::nameToType(typeName);
  1981. }
  1982. if (t == QVariant::Invalid)
  1983. return false;
  1984. if (t != QVariant::LastType && t != (uint)value.userType() && (t < QMetaType::User && !v.convert((QVariant::Type)t)))
  1985. return false;
  1986. }
  1987. // the status variable is changed by qt_metacall to indicate what it did
  1988. // this feature is currently only used by QtDBus and should not be depended
  1989. // upon. Don't change it without looking into QDBusAbstractInterface first
  1990. // -1 (unchanged): normal qt_metacall, result stored in argv[0]
  1991. // changed: result stored directly in value, return the value of status
  1992. int status = -1;
  1993. // the flags variable is used by the declarative module to implement
  1994. // interception of property writes.
  1995. int flags = 0;
  1996. void *argv[] = { 0, &v, &status, &flags };
  1997. if (t == QVariant::LastType)
  1998. argv[0] = &v;
  1999. else
  2000. argv[0] = v.data();
  2001. QMetaObject::metacall(object, QMetaObject::WriteProperty, idx + mobj->propertyOffset(), argv);
  2002. return status;
  2003. }
  2004. /*!
  2005. Resets the property for the given \a object with a reset method.
  2006. Returns true if the reset worked; otherwise returns false.
  2007. Reset methods are optional; only a few properties support them.
  2008. \sa read(), write()
  2009. */
  2010. bool QMetaProperty::reset(QObject *object) const
  2011. {
  2012. if (!object || !mobj || !isResettable())
  2013. return false;
  2014. void *argv[] = { 0 };
  2015. QMetaObject::metacall(object, QMetaObject::ResetProperty, idx + mobj->propertyOffset(), argv);
  2016. return true;
  2017. }
  2018. /*!
  2019. Returns true if this property can be reset to a default value; otherwise
  2020. returns false.
  2021. \sa reset()
  2022. */
  2023. bool QMetaProperty::isResettable() const
  2024. {
  2025. if (!mobj)
  2026. return false;
  2027. int flags = mobj->d.data[handle + 2];
  2028. return flags & Resettable;
  2029. }
  2030. /*!
  2031. Returns true if this property is readable; otherwise returns false.
  2032. \sa isWritable(), read(), isValid()
  2033. */
  2034. bool QMetaProperty::isReadable() const
  2035. {
  2036. if (!mobj)
  2037. return false;
  2038. int flags = mobj->d.data[handle + 2];
  2039. return flags & Readable;
  2040. }
  2041. /*!
  2042. Returns true if this property has a corresponding change notify signal;
  2043. otherwise returns false.
  2044. \sa notifySignal()
  2045. */
  2046. bool QMetaProperty::hasNotifySignal() const
  2047. {
  2048. if (!mobj)
  2049. return false;
  2050. int flags = mobj->d.data[handle + 2];
  2051. return flags & Notify;
  2052. }
  2053. /*!
  2054. \since 4.5
  2055. Returns the QMetaMethod instance of the property change notifying signal if
  2056. one was specified, otherwise returns an invalid QMetaMethod.
  2057. \sa hasNotifySignal()
  2058. */
  2059. QMetaMethod QMetaProperty::notifySignal() const
  2060. {
  2061. int id = notifySignalIndex();
  2062. if (id != -1)
  2063. return mobj->method(id);
  2064. else
  2065. return QMetaMethod();
  2066. }
  2067. /*!
  2068. \since 4.6
  2069. Returns the index of the property change notifying signal if one was
  2070. specified, otherwise returns -1.
  2071. \sa hasNotifySignal()
  2072. */
  2073. int QMetaProperty::notifySignalIndex() const
  2074. {
  2075. if (hasNotifySignal()) {
  2076. int offset = priv(mobj->d.data)->propertyData +
  2077. priv(mobj->d.data)->propertyCount * 3 + idx;
  2078. return mobj->d.data[offset] + mobj->methodOffset();
  2079. } else {
  2080. return -1;
  2081. }
  2082. }
  2083. /*!
  2084. \internal
  2085. Returns the property revision if one was
  2086. specified by REVISION, otherwise returns 0.
  2087. */
  2088. int QMetaProperty::revision() const
  2089. {
  2090. if (!mobj)
  2091. return 0;
  2092. int flags = mobj->d.data[handle + 2];
  2093. if (flags & Revisioned) {
  2094. int offset = priv(mobj->d.data)->propertyData +
  2095. priv(mobj->d.data)->propertyCount * 3 + idx;
  2096. // Revision data is placed after NOTIFY data, if present.
  2097. // Iterate through properties to discover whether we have NOTIFY signals.
  2098. for (int i = 0; i < priv(mobj->d.data)->propertyCount; ++i) {
  2099. int handle = priv(mobj->d.data)->propertyData + 3*i;
  2100. if (mobj->d.data[handle + 2] & Notify) {
  2101. offset += priv(mobj->d.data)->propertyCount;
  2102. break;
  2103. }
  2104. }
  2105. return mobj->d.data[offset];
  2106. } else {
  2107. return 0;
  2108. }
  2109. }
  2110. /*!
  2111. Returns true if this property is writable; otherwise returns
  2112. false.
  2113. \sa isReadable(), write()
  2114. */
  2115. bool QMetaProperty::isWritable() const
  2116. {
  2117. if (!mobj)
  2118. return false;
  2119. int flags = mobj->d.data[handle + 2];
  2120. return flags & Writable;
  2121. }
  2122. /*!
  2123. Returns true if this property is designable for the given \a object;
  2124. otherwise returns false.
  2125. If no \a object is given, the function returns false if the
  2126. \c{Q_PROPERTY()}'s \c DESIGNABLE attribute is false; otherwise
  2127. returns true (if the attribute is true or is a function or expression).
  2128. \sa isScriptable(), isStored()
  2129. */
  2130. bool QMetaProperty::isDesignable(const QObject *object) const
  2131. {
  2132. if (!mobj)
  2133. return false;
  2134. int flags = mobj->d.data[handle + 2];
  2135. bool b = flags & Designable;
  2136. if (object) {
  2137. void *argv[] = { &b };
  2138. QMetaObject::metacall(const_cast<QObject*>(object), QMetaObject::QueryPropertyDesignable,
  2139. idx + mobj->propertyOffset(), argv);
  2140. }
  2141. return b;
  2142. }
  2143. /*!
  2144. Returns true if the property is scriptable for the given \a object;
  2145. otherwise returns false.
  2146. If no \a object is given, the function returns false if the
  2147. \c{Q_PROPERTY()}'s \c SCRIPTABLE attribute is false; otherwise returns
  2148. true (if the attribute is true or is a function or expression).
  2149. \sa isDesignable(), isStored()
  2150. */
  2151. bool QMetaProperty::isScriptable(const QObject *object) const
  2152. {
  2153. if (!mobj)
  2154. return false;
  2155. int flags = mobj->d.data[handle + 2];
  2156. bool b = flags & Scriptable;
  2157. if (object) {
  2158. void *argv[] = { &b };
  2159. QMetaObject::metacall(const_cast<QObject*>(object), QMetaObject::QueryPropertyScriptable,
  2160. idx + mobj->propertyOffset(), argv);
  2161. }
  2162. return b;
  2163. }
  2164. /*!
  2165. Returns true if the property is stored for \a object; otherwise returns
  2166. false.
  2167. If no \a object is given, the function returns false if the
  2168. \c{Q_PROPERTY()}'s \c STORED attribute is false; otherwise returns
  2169. true (if the attribute is true or is a function or expression).
  2170. \sa isDesignable(), isScriptable()
  2171. */
  2172. bool QMetaProperty::isStored(const QObject *object) const
  2173. {
  2174. if (!mobj)
  2175. return false;
  2176. int flags = mobj->d.data[handle + 2];
  2177. bool b = flags & Stored;
  2178. if (object) {
  2179. void *argv[] = { &b };
  2180. QMetaObject::metacall(const_cast<QObject*>(object), QMetaObject::QueryPropertyStored,
  2181. idx + mobj->propertyOffset(), argv);
  2182. }
  2183. return b;
  2184. }
  2185. /*!
  2186. Returns true if this property is designated as the \c USER
  2187. property, i.e., the one that the user can edit for \a object or
  2188. that is significant in some other way. Otherwise it returns
  2189. false. e.g., the \c text property is the \c USER editable property
  2190. of a QLineEdit.
  2191. If \a object is null, the function returns false if the \c
  2192. {Q_PROPERTY()}'s \c USER attribute is false. Otherwise it returns
  2193. true.
  2194. \sa QMetaObject::userProperty(), isDesignable(), isScriptable()
  2195. */
  2196. bool QMetaProperty::isUser(const QObject *object) const
  2197. {
  2198. if (!mobj)
  2199. return false;
  2200. int flags = mobj->d.data[handle + 2];
  2201. bool b = flags & User;
  2202. if (object) {
  2203. void *argv[] = { &b };
  2204. QMetaObject::metacall(const_cast<QObject*>(object), QMetaObject::QueryPropertyUser,
  2205. idx + mobj->propertyOffset(), argv);
  2206. }
  2207. return b;
  2208. }
  2209. /*!
  2210. \since 4.6
  2211. Returns true if the property is constant; otherwise returns false.
  2212. A property is constant if the \c{Q_PROPERTY()}'s \c CONSTANT attribute
  2213. is set.
  2214. */
  2215. bool QMetaProperty::isConstant() const
  2216. {
  2217. if (!mobj)
  2218. return false;
  2219. int flags = mobj->d.data[handle + 2];
  2220. return flags & Constant;
  2221. }
  2222. /*!
  2223. \since 4.6
  2224. Returns true if the property is final; otherwise returns false.
  2225. A property is final if the \c{Q_PROPERTY()}'s \c FINAL attribute
  2226. is set.
  2227. */
  2228. bool QMetaProperty::isFinal() const
  2229. {
  2230. if (!mobj)
  2231. return false;
  2232. int flags = mobj->d.data[handle + 2];
  2233. return flags & Final;
  2234. }
  2235. /*!
  2236. \obsolete
  2237. Returns true if the property is editable for the given \a object;
  2238. otherwise returns false.
  2239. If no \a object is given, the function returns false if the
  2240. \c{Q_PROPERTY()}'s \c EDITABLE attribute is false; otherwise returns
  2241. true (if the attribute is true or is a function or expression).
  2242. \sa isDesignable(), isScriptable(), isStored()
  2243. */
  2244. bool QMetaProperty::isEditable(const QObject *object) const
  2245. {
  2246. if (!mobj)
  2247. return false;
  2248. int flags = mobj->d.data[handle + 2];
  2249. bool b = flags & Editable;
  2250. if (object) {
  2251. void *argv[] = { &b };
  2252. QMetaObject::metacall(const_cast<QObject*>(object), QMetaObject::QueryPropertyEditable,
  2253. idx + mobj->propertyOffset(), argv);
  2254. }
  2255. return b;
  2256. }
  2257. /*!
  2258. \class QMetaClassInfo
  2259. \brief The QMetaClassInfo class provides additional information
  2260. about a class.
  2261. \ingroup objectmodel
  2262. Class information items are simple \e{name}--\e{value} pairs that
  2263. are specified using Q_CLASSINFO() in the source code. The
  2264. information can be retrieved using name() and value(). For example:
  2265. \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 5
  2266. This mechanism is free for you to use in your Qt applications. Qt
  2267. doesn't use it for any of its classes.
  2268. \sa QMetaObject
  2269. */
  2270. /*!
  2271. \fn QMetaClassInfo::QMetaClassInfo()
  2272. \internal
  2273. */
  2274. /*!
  2275. \fn const QMetaObject *QMetaClassInfo::enclosingMetaObject() const
  2276. \internal
  2277. */
  2278. /*!
  2279. Returns the name of this item.
  2280. \sa value()
  2281. */
  2282. const char *QMetaClassInfo::name() const
  2283. {
  2284. if (!mobj)
  2285. return 0;
  2286. return mobj->d.stringdata + mobj->d.data[handle];
  2287. }
  2288. /*!
  2289. Returns the value of this item.
  2290. \sa name()
  2291. */
  2292. const char* QMetaClassInfo::value() const
  2293. {
  2294. if (!mobj)
  2295. return 0;
  2296. return mobj->d.stringdata + mobj->d.data[handle + 1];
  2297. }
  2298. /*!
  2299. \macro QGenericArgument Q_ARG(Type, const Type &value)
  2300. \relates QMetaObject
  2301. This macro takes a \a Type and a \a value of that type and
  2302. returns a \l QGenericArgument object that can be passed to
  2303. QMetaObject::invokeMethod().
  2304. \sa Q_RETURN_ARG()
  2305. */
  2306. /*!
  2307. \macro QGenericReturnArgument Q_RETURN_ARG(Type, Type &value)
  2308. \relates QMetaObject
  2309. This macro takes a \a Type and a non-const reference to a \a
  2310. value of that type and returns a QGenericReturnArgument object
  2311. that can be passed to QMetaObject::invokeMethod().
  2312. \sa Q_ARG()
  2313. */
  2314. /*!
  2315. \class QGenericArgument
  2316. \brief The QGenericArgument class is an internal helper class for
  2317. marshalling arguments.
  2318. This class should never be used directly. Please use the \l Q_ARG()
  2319. macro instead.
  2320. \sa Q_ARG(), QMetaObject::invokeMethod(), QGenericReturnArgument
  2321. */
  2322. /*!
  2323. \fn QGenericArgument::QGenericArgument(const char *name, const void *data)
  2324. Constructs a QGenericArgument object with the given \a name and \a data.
  2325. */
  2326. /*!
  2327. \fn QGenericArgument::data () const
  2328. Returns the data set in the constructor.
  2329. */
  2330. /*!
  2331. \fn QGenericArgument::name () const
  2332. Returns the name set in the constructor.
  2333. */
  2334. /*!
  2335. \class QGenericReturnArgument
  2336. \brief The QGenericReturnArgument class is an internal helper class for
  2337. marshalling arguments.
  2338. This class should never be used directly. Please use the
  2339. Q_RETURN_ARG() macro instead.
  2340. \sa Q_RETURN_ARG(), QMetaObject::invokeMethod(), QGenericArgument
  2341. */
  2342. /*!
  2343. \fn QGenericReturnArgument::QGenericReturnArgument(const char *name, void *data)
  2344. Constructs a QGenericReturnArgument object with the given \a name
  2345. and \a data.
  2346. */
  2347. /*! \internal
  2348. If the local_method_index is a cloned method, return the index of the original.
  2349. Example: if the index of "destroyed()" is passed, the index of "destroyed(QObject*)" is returned
  2350. */
  2351. int QMetaObjectPrivate::originalClone(const QMetaObject *mobj, int local_method_index)
  2352. {
  2353. Q_ASSERT(local_method_index < get(mobj)->methodCount);
  2354. int handle = get(mobj)->methodData + 5 * local_method_index;
  2355. while (mobj->d.data[handle + 4] & MethodCloned) {
  2356. Q_ASSERT(local_method_index > 0);
  2357. handle -= 5;
  2358. local_method_index--;
  2359. }
  2360. return local_method_index;
  2361. }
  2362. QT_END_NAMESPACE