PageRenderTime 69ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/src/corelib/kernel/qmetaobject.cpp

https://bitbucket.org/gcubar/qt
C++ | 2806 lines | 1371 code | 197 blank | 1238 comment | 433 complexity | 9d77fe2017d0bf6073145577b350c1dd MD5 | raw file
Possible License(s): CC0-1.0, CC-BY-SA-4.0, LGPL-2.1, GPL-3.0, Apache-2.0, LGPL-2.0, LGPL-3.0, BSD-3-Clause
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
  4. ** Contact: http://www.qt-project.org/legal
  5. **
  6. ** This file is part of the QtCore module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and Digia. For licensing terms and
  14. ** conditions see http://qt.digia.com/licensing. For further information
  15. ** use the contact form at http://qt.digia.com/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 2.1 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 2.1 requirements
  23. ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  24. **
  25. ** In addition, as a special exception, Digia gives you certain additional
  26. ** rights. These rights are described in the Digia Qt LGPL Exception
  27. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  28. **
  29. ** GNU General Public License Usage
  30. ** Alternatively, this file may be used under the terms of the GNU
  31. ** General Public License version 3.0 as published by the Free Software
  32. ** Foundation and appearing in the file LICENSE.GPL included in the
  33. ** packaging of this file. Please review the following information to
  34. ** ensure the GNU General Public License version 3.0 requirements will be
  35. ** met: http://www.gnu.org/copyleft/gpl.html.
  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.
  1172. Tag information can be added in the following
  1173. way in the function declaration:
  1174. \code
  1175. #define THISISTESTTAG // tag text
  1176. ...
  1177. private slots:
  1178. THISISTESTTAG void testFunc();
  1179. \endcode
  1180. and the information can be accessed by using:
  1181. \code
  1182. MainWindow win;
  1183. win.show();
  1184. int functionIndex = win.metaObject()->indexOfSlot("testFunc()");
  1185. QMetaMethod mm = metaObject()->method(functionIndex);
  1186. qDebug() << mm.tag(); // prints THISISTESTTAG
  1187. \endcode
  1188. For the moment,
  1189. \c moc doesn't support any special tags.
  1190. */
  1191. const char *QMetaMethod::tag() const
  1192. {
  1193. if (!mobj)
  1194. return 0;
  1195. return mobj->d.stringdata + mobj->d.data[handle + 3];
  1196. }
  1197. /*! \internal */
  1198. int QMetaMethod::attributes() const
  1199. {
  1200. if (!mobj)
  1201. return false;
  1202. return ((mobj->d.data[handle + 4])>>4);
  1203. }
  1204. /*!
  1205. \since 4.6
  1206. Returns this method's index.
  1207. */
  1208. int QMetaMethod::methodIndex() const
  1209. {
  1210. if (!mobj)
  1211. return -1;
  1212. return ((handle - priv(mobj->d.data)->methodData) / 5) + mobj->methodOffset();
  1213. }
  1214. /*!
  1215. \internal
  1216. Returns the method revision if one was
  1217. specified by Q_REVISION, otherwise returns 0.
  1218. */
  1219. int QMetaMethod::revision() const
  1220. {
  1221. if (!mobj)
  1222. return 0;
  1223. if ((QMetaMethod::Access)(mobj->d.data[handle + 4] & MethodRevisioned)) {
  1224. int offset = priv(mobj->d.data)->methodData
  1225. + priv(mobj->d.data)->methodCount * 5
  1226. + (handle - priv(mobj->d.data)->methodData) / 5;
  1227. return mobj->d.data[offset];
  1228. }
  1229. return 0;
  1230. }
  1231. /*!
  1232. Returns the access specification of this method (private,
  1233. protected, or public).
  1234. Signals are always protected, meaning that you can only emit them
  1235. from the class or from a subclass.
  1236. \sa methodType()
  1237. */
  1238. QMetaMethod::Access QMetaMethod::access() const
  1239. {
  1240. if (!mobj)
  1241. return Private;
  1242. return (QMetaMethod::Access)(mobj->d.data[handle + 4] & AccessMask);
  1243. }
  1244. /*!
  1245. Returns the type of this method (signal, slot, or method).
  1246. \sa access()
  1247. */
  1248. QMetaMethod::MethodType QMetaMethod::methodType() const
  1249. {
  1250. if (!mobj)
  1251. return QMetaMethod::Method;
  1252. return (QMetaMethod::MethodType)((mobj->d.data[handle + 4] & MethodTypeMask)>>2);
  1253. }
  1254. /*!
  1255. Invokes this method on the object \a object. Returns true if the member could be invoked.
  1256. Returns false if there is no such member or the parameters did not match.
  1257. The invocation can be either synchronous or asynchronous, depending on the
  1258. \a connectionType:
  1259. \list
  1260. \o If \a connectionType is Qt::DirectConnection, the member will be invoked immediately.
  1261. \o If \a connectionType is Qt::QueuedConnection,
  1262. a QEvent will be posted and the member is invoked as soon as the application
  1263. enters the main event loop.
  1264. \o If \a connectionType is Qt::AutoConnection, the member is invoked
  1265. synchronously if \a object lives in the same thread as the
  1266. caller; otherwise it will invoke the member asynchronously.
  1267. \endlist
  1268. The return value of this method call is placed in \a
  1269. returnValue. If the invocation is asynchronous, the return value cannot
  1270. be evaluated. You can pass up to ten arguments (\a val0, \a val1,
  1271. \a val2, \a val3, \a val4, \a val5, \a val6, \a val7, \a val8,
  1272. and \a val9) to this method call.
  1273. QGenericArgument and QGenericReturnArgument are internal
  1274. helper classes. Because signals and slots can be dynamically
  1275. invoked, you must enclose the arguments using the Q_ARG() and
  1276. Q_RETURN_ARG() macros. Q_ARG() takes a type name and a
  1277. const reference of that type; Q_RETURN_ARG() takes a type name
  1278. and a non-const reference.
  1279. To asynchronously invoke the
  1280. \l{QPushButton::animateClick()}{animateClick()} slot on a
  1281. QPushButton:
  1282. \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 6
  1283. With asynchronous method invocations, the parameters must be of
  1284. types that are known to Qt's meta-object system, because Qt needs
  1285. to copy the arguments to store them in an event behind the
  1286. scenes. If you try to use a queued connection and get the error
  1287. message
  1288. \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 7
  1289. call qRegisterMetaType() to register the data type before you
  1290. call QMetaMethod::invoke().
  1291. To synchronously invoke the \c compute(QString, int, double) slot on
  1292. some arbitrary object \c obj retrieve its return value:
  1293. \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 8
  1294. QMetaObject::normalizedSignature() is used here to ensure that the format
  1295. of the signature is what invoke() expects. E.g. extra whitespace is
  1296. removed.
  1297. If the "compute" slot does not take exactly one QString, one int
  1298. and one double in the specified order, the call will fail.
  1299. \warning this method will not test the validity of the arguments: \a object
  1300. must be an instance of the class of the QMetaObject of which this QMetaMethod
  1301. has been constructed with. The arguments must have the same type as the ones
  1302. expected by the method, else, the behaviour is undefined.
  1303. \sa Q_ARG(), Q_RETURN_ARG(), qRegisterMetaType(), QMetaObject::invokeMethod()
  1304. */
  1305. bool QMetaMethod::invoke(QObject *object,
  1306. Qt::ConnectionType connectionType,
  1307. QGenericReturnArgument returnValue,
  1308. QGenericArgument val0,
  1309. QGenericArgument val1,
  1310. QGenericArgument val2,
  1311. QGenericArgument val3,
  1312. QGenericArgument val4,
  1313. QGenericArgument val5,
  1314. QGenericArgument val6,
  1315. QGenericArgument val7,
  1316. QGenericArgument val8,
  1317. QGenericArgument val9) const
  1318. {
  1319. if (!object || !mobj)
  1320. return false;
  1321. Q_ASSERT(mobj->cast(object));
  1322. // check return type
  1323. if (returnValue.data()) {
  1324. const char *retType = typeName();
  1325. if (qstrcmp(returnValue.name(), retType) != 0) {
  1326. // normalize the return value as well
  1327. // the trick here is to make a function signature out of the return type
  1328. // so that we can call normalizedSignature() and avoid duplicating code
  1329. QByteArray unnormalized;
  1330. int len = qstrlen(returnValue.name());
  1331. unnormalized.reserve(len + 3);
  1332. unnormalized = "_("; // the function is called "_"
  1333. unnormalized.append(returnValue.name());
  1334. unnormalized.append(')');
  1335. QByteArray normalized = QMetaObject::normalizedSignature(unnormalized.constData());
  1336. normalized.truncate(normalized.length() - 1); // drop the ending ')'
  1337. if (qstrcmp(normalized.constData() + 2, retType) != 0)
  1338. return false;
  1339. }
  1340. }
  1341. // check argument count (we don't allow invoking a method if given too few arguments)
  1342. const char *typeNames[] = {
  1343. returnValue.name(),
  1344. val0.name(),
  1345. val1.name(),
  1346. val2.name(),
  1347. val3.name(),
  1348. val4.name(),
  1349. val5.name(),
  1350. val6.name(),
  1351. val7.name(),
  1352. val8.name(),
  1353. val9.name()
  1354. };
  1355. int paramCount;
  1356. for (paramCount = 1; paramCount < MaximumParamCount; ++paramCount) {
  1357. if (qstrlen(typeNames[paramCount]) <= 0)
  1358. break;
  1359. }
  1360. int metaMethodArgumentCount = 0;
  1361. {
  1362. // based on QMetaObject::parameterNames()
  1363. const char *names = mobj->d.stringdata + mobj->d.data[handle + 1];
  1364. if (*names == 0) {
  1365. // do we have one or zero arguments?
  1366. const char *signature = mobj->d.stringdata + mobj->d.data[handle];
  1367. while (*signature && *signature != '(')
  1368. ++signature;
  1369. if (*++signature != ')')
  1370. ++metaMethodArgumentCount;
  1371. } else {
  1372. --names;
  1373. do {
  1374. ++names;
  1375. while (*names && *names != ',')
  1376. ++names;
  1377. ++metaMethodArgumentCount;
  1378. } while (*names);
  1379. }
  1380. }
  1381. if (paramCount <= metaMethodArgumentCount)
  1382. return false;
  1383. // check connection type
  1384. QThread *currentThread = QThread::currentThread();
  1385. QThread *objectThread = object->thread();
  1386. if (connectionType == Qt::AutoConnection) {
  1387. connectionType = currentThread == objectThread
  1388. ? Qt::DirectConnection
  1389. : Qt::QueuedConnection;
  1390. }
  1391. #ifdef QT_NO_THREAD
  1392. if (connectionType == Qt::BlockingQueuedConnection) {
  1393. connectionType = Qt::DirectConnection;
  1394. }
  1395. #endif
  1396. // invoke!
  1397. void *param[] = {
  1398. returnValue.data(),
  1399. val0.data(),
  1400. val1.data(),
  1401. val2.data(),
  1402. val3.data(),
  1403. val4.data(),
  1404. val5.data(),
  1405. val6.data(),
  1406. val7.data(),
  1407. val8.data(),
  1408. val9.data()
  1409. };
  1410. // recompute the methodIndex by reversing the arithmetic in QMetaObject::property()
  1411. int idx_relative = ((handle - priv(mobj->d.data)->methodData) / 5);
  1412. int idx_offset = mobj->methodOffset();
  1413. QObjectPrivate::StaticMetaCallFunction callFunction =
  1414. (QMetaObjectPrivate::get(mobj)->revision >= 6 && mobj->d.extradata)
  1415. ? reinterpret_cast<const QMetaObjectExtraData *>(mobj->d.extradata)->static_metacall : 0;
  1416. if (connectionType == Qt::DirectConnection) {
  1417. if (callFunction) {
  1418. callFunction(object, QMetaObject::InvokeMetaMethod, idx_relative, param);
  1419. return true;
  1420. } else {
  1421. return QMetaObject::metacall(object, QMetaObject::InvokeMetaMethod, idx_relative + idx_offset, param) < 0;
  1422. }
  1423. } else if (connectionType == Qt::QueuedConnection) {
  1424. if (returnValue.data()) {
  1425. qWarning("QMetaMethod::invoke: Unable to invoke methods with return values in "
  1426. "queued connections");
  1427. return false;
  1428. }
  1429. int nargs = 1; // include return type
  1430. void **args = (void **) qMalloc(paramCount * sizeof(void *));
  1431. Q_CHECK_PTR(args);
  1432. int *types = (int *) qMalloc(paramCount * sizeof(int));
  1433. Q_CHECK_PTR(types);
  1434. types[0] = 0; // return type
  1435. args[0] = 0;
  1436. for (int i = 1; i < paramCount; ++i) {
  1437. types[i] = QMetaType::type(typeNames[i]);
  1438. if (types[i]) {
  1439. args[i] = QMetaType::construct(types[i], param[i]);
  1440. ++nargs;
  1441. } else if (param[i]) {
  1442. qWarning("QMetaMethod::invoke: Unable to handle unregistered datatype '%s'",
  1443. typeNames[i]);
  1444. for (int x = 1; x < i; ++x) {
  1445. if (types[x] && args[x])
  1446. QMetaType::destroy(types[x], args[x]);
  1447. }
  1448. qFree(types);
  1449. qFree(args);
  1450. return false;
  1451. }
  1452. }
  1453. QCoreApplication::postEvent(object, new QMetaCallEvent(idx_offset, idx_relative, callFunction,
  1454. 0, -1, nargs, types, args));
  1455. } else { // blocking queued connection
  1456. #ifndef QT_NO_THREAD
  1457. if (currentThread == objectThread) {
  1458. qWarning("QMetaMethod::invoke: Dead lock detected in "
  1459. "BlockingQueuedConnection: Receiver is %s(%p)",
  1460. mobj->className(), object);
  1461. }
  1462. QSemaphore semaphore;
  1463. QCoreApplication::postEvent(object, new QMetaCallEvent(idx_offset, idx_relative, callFunction,
  1464. 0, -1, 0, 0, param, &semaphore));
  1465. semaphore.acquire();
  1466. #endif // QT_NO_THREAD
  1467. }
  1468. return true;
  1469. }
  1470. /*! \fn bool QMetaMethod::invoke(QObject *object,
  1471. QGenericReturnArgument returnValue,
  1472. QGenericArgument val0 = QGenericArgument(0),
  1473. QGenericArgument val1 = QGenericArgument(),
  1474. QGenericArgument val2 = QGenericArgument(),
  1475. QGenericArgument val3 = QGenericArgument(),
  1476. QGenericArgument val4 = QGenericArgument(),
  1477. QGenericArgument val5 = QGenericArgument(),
  1478. QGenericArgument val6 = QGenericArgument(),
  1479. QGenericArgument val7 = QGenericArgument(),
  1480. QGenericArgument val8 = QGenericArgument(),
  1481. QGenericArgument val9 = QGenericArgument()) const
  1482. \overload invoke()
  1483. This overload always invokes this method using the connection type Qt::AutoConnection.
  1484. */
  1485. /*! \fn bool QMetaMethod::invoke(QObject *object,
  1486. Qt::ConnectionType connectionType,
  1487. QGenericArgument val0 = QGenericArgument(0),
  1488. QGenericArgument val1 = QGenericArgument(),
  1489. QGenericArgument val2 = QGenericArgument(),
  1490. QGenericArgument val3 = QGenericArgument(),
  1491. QGenericArgument val4 = QGenericArgument(),
  1492. QGenericArgument val5 = QGenericArgument(),
  1493. QGenericArgument val6 = QGenericArgument(),
  1494. QGenericArgument val7 = QGenericArgument(),
  1495. QGenericArgument val8 = QGenericArgument(),
  1496. QGenericArgument val9 = QGenericArgument()) const
  1497. \overload invoke()
  1498. This overload can be used if the return value of the member is of no interest.
  1499. */
  1500. /*!
  1501. \fn bool QMetaMethod::invoke(QObject *object,
  1502. QGenericArgument val0 = QGenericArgument(0),
  1503. QGenericArgument val1 = QGenericArgument(),
  1504. QGenericArgument val2 = QGenericArgument(),
  1505. QGenericArgument val3 = QGenericArgument(),
  1506. QGenericArgument val4 = QGenericArgument(),
  1507. QGenericArgument val5 = QGenericArgument(),
  1508. QGenericArgument val6 = QGenericArgument(),
  1509. QGenericArgument val7 = QGenericArgument(),
  1510. QGenericArgument val8 = QGenericArgument(),
  1511. QGenericArgument val9 = QGenericArgument()) const
  1512. \overload invoke()
  1513. This overload invokes this method using the
  1514. connection type Qt::AutoConnection and ignores return values.
  1515. */
  1516. /*!
  1517. \class QMetaEnum
  1518. \brief The QMetaEnum class provides meta-data about an enumerator.
  1519. \ingroup objectmodel
  1520. Use name() for the enumerator's name. The enumerator's keys (names
  1521. of each enumerated item) are returned by key(); use keyCount() to find
  1522. the number of keys. isFlag() returns whether the enumerator is
  1523. meant to be used as a flag, meaning that its values can be combined
  1524. using the OR operator.
  1525. The conversion functions keyToValue(), valueToKey(), keysToValue(),
  1526. and valueToKeys() allow conversion between the integer
  1527. representation of an enumeration or set value and its literal
  1528. representation. The scope() function returns the class scope this
  1529. enumerator was declared in.
  1530. \sa QMetaObject, QMetaMethod, QMetaProperty
  1531. */
  1532. /*!
  1533. \fn bool QMetaEnum::isValid() const
  1534. Returns true if this enum is valid (has a name); otherwise returns
  1535. false.
  1536. \sa name()
  1537. */
  1538. /*!
  1539. \fn const QMetaObject *QMetaEnum::enclosingMetaObject() const
  1540. \internal
  1541. */
  1542. /*!
  1543. \fn QMetaEnum::QMetaEnum()
  1544. \internal
  1545. */
  1546. /*!
  1547. Returns the name of the enumerator (without the scope).
  1548. For example, the Qt::AlignmentFlag enumeration has \c
  1549. AlignmentFlag as the name and \l Qt as the scope.
  1550. \sa isValid(), scope()
  1551. */
  1552. const char *QMetaEnum::name() const
  1553. {
  1554. if (!mobj)
  1555. return 0;
  1556. return mobj->d.stringdata + mobj->d.data[handle];
  1557. }
  1558. /*!
  1559. Returns the number of keys.
  1560. \sa key()
  1561. */
  1562. int QMetaEnum::keyCount() const
  1563. {
  1564. if (!mobj)
  1565. return 0;
  1566. return mobj->d.data[handle + 2];
  1567. }
  1568. /*!
  1569. Returns the key with the given \a index, or 0 if no such key exists.
  1570. \sa keyCount(), value(), valueToKey()
  1571. */
  1572. const char *QMetaEnum::key(int index) const
  1573. {
  1574. if (!mobj)
  1575. return 0;
  1576. int count = mobj->d.data[handle + 2];
  1577. int data = mobj->d.data[handle + 3];
  1578. if (index >= 0 && index < count)
  1579. return mobj->d.stringdata + mobj->d.data[data + 2*index];
  1580. return 0;
  1581. }
  1582. /*!
  1583. Returns the value with the given \a index; or returns -1 if there
  1584. is no such value.
  1585. \sa keyCount(), key(), keyToValue()
  1586. */
  1587. int QMetaEnum::value(int index) const
  1588. {
  1589. if (!mobj)
  1590. return 0;
  1591. int count = mobj->d.data[handle + 2];
  1592. int data = mobj->d.data[handle + 3];
  1593. if (index >= 0 && index < count)
  1594. return mobj->d.data[data + 2*index + 1];
  1595. return -1;
  1596. }
  1597. /*!
  1598. Returns true if this enumerator is used as a flag; otherwise returns
  1599. false.
  1600. When used as flags, enumerators can be combined using the OR
  1601. operator.
  1602. \sa keysToValue(), valueToKeys()
  1603. */
  1604. bool QMetaEnum::isFlag() const
  1605. {
  1606. return mobj && mobj->d.data[handle + 1];
  1607. }
  1608. /*!
  1609. Returns the scope this enumerator was declared in.
  1610. For example, the Qt::AlignmentFlag enumeration has \c Qt as
  1611. the scope and \c AlignmentFlag as the name.
  1612. \sa name()
  1613. */
  1614. const char *QMetaEnum::scope() const
  1615. {
  1616. return mobj?mobj->d.stringdata : 0;
  1617. }
  1618. /*!
  1619. Returns the integer value of the given enumeration \a key, or -1
  1620. if \a key is not defined.
  1621. For flag types, use keysToValue().
  1622. \sa valueToKey(), isFlag(), keysToValue()
  1623. */
  1624. int QMetaEnum::keyToValue(const char *key) const
  1625. {
  1626. if (!mobj || !key)
  1627. return -1;
  1628. uint scope = 0;
  1629. const char *qualified_key = key;
  1630. const char *s = key + qstrlen(key);
  1631. while (s > key && *s != ':')
  1632. --s;
  1633. if (s > key && *(s-1)==':') {
  1634. scope = s - key - 1;
  1635. key += scope + 2;
  1636. }
  1637. int count = mobj->d.data[handle + 2];
  1638. int data = mobj->d.data[handle + 3];
  1639. for (int i = 0; i < count; ++i)
  1640. if ((!scope || (qstrlen(mobj->d.stringdata) == scope && strncmp(qualified_key, mobj->d.stringdata, scope) == 0))
  1641. && strcmp(key, mobj->d.stringdata + mobj->d.data[data + 2*i]) == 0)
  1642. return mobj->d.data[data + 2*i + 1];
  1643. return -1;
  1644. }
  1645. /*!
  1646. Returns the string that is used as the name of the given
  1647. enumeration \a value, or 0 if \a value is not defined.
  1648. For flag types, use valueToKeys().
  1649. \sa isFlag(), valueToKeys()
  1650. */
  1651. const char* QMetaEnum::valueToKey(int value) const
  1652. {
  1653. if (!mobj)
  1654. return 0;
  1655. int count = mobj->d.data[handle + 2];
  1656. int data = mobj->d.data[handle + 3];
  1657. for (int i = 0; i < count; ++i)
  1658. if (value == (int)mobj->d.data[data + 2*i + 1])
  1659. return mobj->d.stringdata + mobj->d.data[data + 2*i];
  1660. return 0;
  1661. }
  1662. /*!
  1663. Returns the value derived from combining together the values of
  1664. the \a keys using the OR operator, or -1 if \a keys is not
  1665. defined. Note that the strings in \a keys must be '|'-separated.
  1666. \sa isFlag(), valueToKey(), valueToKeys()
  1667. */
  1668. int QMetaEnum::keysToValue(const char *keys) const
  1669. {
  1670. if (!mobj)
  1671. return -1;
  1672. QStringList l = QString::fromLatin1(keys).split(QLatin1Char('|'));
  1673. //#### TODO write proper code, do not use QStringList
  1674. int value = 0;
  1675. int count = mobj->d.data[handle + 2];
  1676. int data = mobj->d.data[handle + 3];
  1677. for (int li = 0; li < l.size(); ++li) {
  1678. QString trimmed = l.at(li).trimmed();
  1679. QByteArray qualified_key = trimmed.toLatin1();
  1680. const char *key = qualified_key.constData();
  1681. uint scope = 0;
  1682. const char *s = key + qstrlen(key);
  1683. while (s > key && *s != ':')
  1684. --s;
  1685. if (s > key && *(s-1)==':') {
  1686. scope = s - key - 1;
  1687. key += scope + 2;
  1688. }
  1689. int i;
  1690. for (i = count-1; i >= 0; --i)
  1691. if ((!scope || (qstrlen(mobj->d.stringdata) == scope && strncmp(qualified_key.constData(), mobj->d.stringdata, scope) == 0))
  1692. && strcmp(key, mobj->d.stringdata + mobj->d.data[data + 2*i]) == 0) {
  1693. value |= mobj->d.data[data + 2*i + 1];
  1694. break;
  1695. }
  1696. if (i < 0)
  1697. value |= -1;
  1698. }
  1699. return value;
  1700. }
  1701. /*!
  1702. Returns a byte array of '|'-separated keys that represents the
  1703. given \a value.
  1704. \sa isFlag(), valueToKey(), keysToValue()
  1705. */
  1706. QByteArray QMetaEnum::valueToKeys(int value) const
  1707. {
  1708. QByteArray keys;
  1709. if (!mobj)
  1710. return keys;
  1711. int count = mobj->d.data[handle + 2];
  1712. int data = mobj->d.data[handle + 3];
  1713. int v = value;
  1714. for(int i = 0; i < count; i++) {
  1715. int k = mobj->d.data[data + 2*i + 1];
  1716. if ((k != 0 && (v & k) == k ) || (k == value)) {
  1717. v = v & ~k;
  1718. if (!keys.isEmpty())
  1719. keys += '|';
  1720. keys += mobj->d.stringdata + mobj->d.data[data + 2*i];
  1721. }
  1722. }
  1723. return keys;
  1724. }
  1725. static QByteArray qualifiedName(const QMetaEnum &e)
  1726. {
  1727. return QByteArray(e.scope()) + "::" + e.name();
  1728. }
  1729. /*!
  1730. \class QMetaProperty
  1731. \brief The QMetaProperty class provides meta-data about a property.
  1732. \ingroup objectmodel
  1733. Property meta-data is obtained from an object's meta-object. See
  1734. QMetaObject::property() and QMetaObject::propertyCount() for
  1735. details.
  1736. \section1 Property Meta-Data
  1737. A property has a name() and a type(), as well as various
  1738. attributes that specify its behavior: isReadable(), isWritable(),
  1739. isDesignable(), isScriptable(), and isStored().
  1740. If the property is an enumeration, isEnumType() returns true; if the
  1741. property is an enumeration that is also a flag (i.e. its values
  1742. can be combined using the OR operator), isEnumType() and
  1743. isFlagType() both return true. The enumerator for these types is
  1744. available from enumerator().
  1745. The property's values are set and retrieved with read(), write(),
  1746. and reset(); they can also be changed through QObject's set and get
  1747. functions. See QObject::setProperty() and QObject::property() for
  1748. details.
  1749. \section1 Copying and Assignment
  1750. QMetaProperty objects can be copied by value. However, each copy will
  1751. refer to the same underlying property meta-data.
  1752. \sa QMetaObject, QMetaEnum, QMetaMethod, {Qt's Property System}
  1753. */
  1754. /*!
  1755. \fn bool QMetaProperty::isValid() const
  1756. Returns true if this property is valid (readable); otherwise
  1757. returns false.
  1758. \sa isReadable()
  1759. */
  1760. /*!
  1761. \fn const QMetaObject *QMetaProperty::enclosingMetaObject() const
  1762. \internal
  1763. */
  1764. /*!
  1765. \internal
  1766. */
  1767. QMetaProperty::QMetaProperty()
  1768. : mobj(0), handle(0), idx(0)
  1769. {
  1770. }
  1771. /*!
  1772. Returns this property's name.
  1773. \sa type(), typeName()
  1774. */
  1775. const char *QMetaProperty::name() const
  1776. {
  1777. if (!mobj)
  1778. return 0;
  1779. int handle = priv(mobj->d.data)->propertyData + 3*idx;
  1780. return mobj->d.stringdata + mobj->d.data[handle];
  1781. }
  1782. /*!
  1783. Returns the name of this property's type.
  1784. \sa type(), name()
  1785. */
  1786. const char *QMetaProperty::typeName() const
  1787. {
  1788. if (!mobj)
  1789. return 0;
  1790. int handle = priv(mobj->d.data)->propertyData + 3*idx;
  1791. return mobj->d.stringdata + mobj->d.data[handle + 1];
  1792. }
  1793. /*!
  1794. Returns this property's type. The return value is one
  1795. of the values of the QVariant::Type enumeration.
  1796. \sa userType(), typeName(), name()
  1797. */
  1798. QVariant::Type QMetaProperty::type() const
  1799. {
  1800. if (!mobj)
  1801. return QVariant::Invalid;
  1802. int handle = priv(mobj->d.data)->propertyData + 3*idx;
  1803. uint flags = mobj->d.data[handle + 2];
  1804. uint type = flags >> 24;
  1805. if (type == 0xff) // special value for QVariant
  1806. type = QVariant::LastType;
  1807. if (type)
  1808. return QVariant::Type(type);
  1809. if (isEnumType()) {
  1810. int enumMetaTypeId = QMetaType::type(qualifiedName(menum));
  1811. if (enumMetaTypeId == 0)
  1812. return QVariant::Int;
  1813. }
  1814. #ifdef QT_COORD_TYPE
  1815. // qreal metatype must be resolved at runtime.
  1816. if (strcmp(typeName(), "qreal") == 0)
  1817. return QVariant::Type(qMetaTypeId<qreal>());
  1818. #endif
  1819. return QVariant::UserType;
  1820. }
  1821. /*!
  1822. \since 4.2
  1823. Returns this property's user type. The return value is one
  1824. of the values that are registered with QMetaType, or 0 if
  1825. the type is not registered.
  1826. \sa type(), QMetaType, typeName()
  1827. */
  1828. int QMetaProperty::userType() const
  1829. {
  1830. QVariant::Type tp = type();
  1831. if (tp != QVariant::UserType)
  1832. return tp;
  1833. if (isEnumType()) {
  1834. int enumMetaTypeId = QMetaType::type(qualifiedName(menum));
  1835. return enumMetaTypeId;
  1836. }
  1837. return QMetaType::type(typeName());
  1838. }
  1839. /*!
  1840. \since 4.6
  1841. Returns this property's index.
  1842. */
  1843. int QMetaProperty::propertyIndex() const
  1844. {
  1845. if (!mobj)
  1846. return -1;
  1847. return idx + mobj->propertyOffset();
  1848. }
  1849. /*!
  1850. Returns true if the property's type is an enumeration value that
  1851. is used as a flag; otherwise returns false.
  1852. Flags can be combined using the OR operator. A flag type is
  1853. implicitly also an enum type.
  1854. \sa isEnumType(), enumerator(), QMetaEnum::isFlag()
  1855. */
  1856. bool QMetaProperty::isFlagType() const
  1857. {
  1858. return isEnumType() && menum.isFlag();
  1859. }
  1860. /*!
  1861. Returns true if the property's type is an enumeration value;
  1862. otherwise returns false.
  1863. \sa enumerator(), isFlagType()
  1864. */
  1865. bool QMetaProperty::isEnumType() const
  1866. {
  1867. if (!mobj)
  1868. return false;
  1869. int handle = priv(mobj->d.data)->propertyData + 3*idx;
  1870. int flags = mobj->d.data[handle + 2];
  1871. return (flags & EnumOrFlag) && menum.name();
  1872. }
  1873. /*!
  1874. \internal
  1875. Returns true if the property has a C++ setter function that
  1876. follows Qt's standard "name" / "setName" pattern. Designer and uic
  1877. query hasStdCppSet() in order to avoid expensive
  1878. QObject::setProperty() calls. All properties in Qt [should] follow
  1879. this pattern.
  1880. */
  1881. bool QMetaProperty::hasStdCppSet() const
  1882. {
  1883. if (!mobj)
  1884. return false;
  1885. int handle = priv(mobj->d.data)->propertyData + 3*idx;
  1886. int flags = mobj->d.data[handle + 2];
  1887. return (flags & StdCppSet);
  1888. }
  1889. /*!
  1890. Returns the enumerator if this property's type is an enumerator
  1891. type; otherwise the returned value is undefined.
  1892. \sa isEnumType(), isFlagType()
  1893. */
  1894. QMetaEnum QMetaProperty::enumerator() const
  1895. {
  1896. return menum;
  1897. }
  1898. /*!
  1899. Reads the property's value from the given \a object. Returns the value
  1900. if it was able to read it; otherwise returns an invalid variant.
  1901. \sa write(), reset(), isReadable()
  1902. */
  1903. QVariant QMetaProperty::read(const QObject *object) const
  1904. {
  1905. if (!object || !mobj)
  1906. return QVariant();
  1907. uint t = QVariant::Int;
  1908. if (isEnumType()) {
  1909. /*
  1910. try to create a QVariant that can be converted to this enum
  1911. type (only works if the enum has already been registered
  1912. with QMetaType)
  1913. */
  1914. int enumMetaTypeId = QMetaType::type(qualifiedName(menum));
  1915. if (enumMetaTypeId != 0)
  1916. t = enumMetaTypeId;
  1917. } else {
  1918. int handle = priv(mobj->d.data)->propertyData + 3*idx;
  1919. uint flags = mobj->d.data[handle + 2];
  1920. const char *typeName = mobj->d.stringdata + mobj->d.data[handle + 1];
  1921. t = (flags >> 24);
  1922. if (t == 0xff) // special value for QVariant
  1923. t = QVariant::LastType;
  1924. if (t == QVariant::Invalid)
  1925. t = QMetaType::type(typeName);
  1926. if (t == QVariant::Invalid)
  1927. t = QVariant::nameToType(typeName);
  1928. if (t == QVariant::Invalid || t == QVariant::UserType) {
  1929. if (t == QVariant::Invalid)
  1930. qWarning("QMetaProperty::read: Unable to handle unregistered datatype '%s' for property '%s::%s'", typeName, mobj->className(), name());
  1931. return QVariant();
  1932. }
  1933. }
  1934. // the status variable is changed by qt_metacall to indicate what it did
  1935. // this feature is currently only used by QtDBus and should not be depended
  1936. // upon. Don't change it without looking into QDBusAbstractInterface first
  1937. // -1 (unchanged): normal qt_metacall, result stored in argv[0]
  1938. // changed: result stored directly in value
  1939. int status = -1;
  1940. QVariant value;
  1941. void *argv[] = { 0, &value, &status };
  1942. if (t == QVariant::LastType) {
  1943. argv[0] = &value;
  1944. } else {
  1945. value = QVariant(t, (void*)0);
  1946. argv[0] = value.data();
  1947. }
  1948. QMetaObject::metacall(const_cast<QObject*>(object), QMetaObject::ReadProperty,
  1949. idx + mobj->propertyOffset(), argv);
  1950. if (status != -1)
  1951. return value;
  1952. if (t != QVariant::LastType && argv[0] != value.data())
  1953. // pointer or reference
  1954. return QVariant((QVariant::Type)t, argv[0]);
  1955. return value;
  1956. }
  1957. /*!
  1958. Writes \a value as the property's value to the given \a object. Returns
  1959. true if the write succeeded; otherwise returns false.
  1960. \sa read(), reset(), isWritable()
  1961. */
  1962. bool QMetaProperty::write(QObject *object, const QVariant &value) const
  1963. {
  1964. if (!object || !isWritable())
  1965. return false;
  1966. QVariant v = value;
  1967. uint t = QVariant::Invalid;
  1968. if (isEnumType()) {
  1969. if (v.type() == QVariant::String
  1970. #ifdef QT3_SUPPORT
  1971. || v.type() == QVariant::CString
  1972. #endif
  1973. ) {
  1974. if (isFlagType())
  1975. v = QVariant(menum.keysToValue(value.toByteArray()));
  1976. else
  1977. v = QVariant(menum.keyToValue(value.toByteArray()));
  1978. } else if (v.type() != QVariant::Int && v.type() != QVariant::UInt) {
  1979. int enumMetaTypeId = QMetaType::type(qualifiedName(menum));
  1980. if ((enumMetaTypeId == 0) || (v.userType() != enumMetaTypeId) || !v.constData())
  1981. return false;
  1982. v = QVariant(*reinterpret_cast<const int *>(v.constData()));
  1983. }
  1984. v.convert(QVariant::Int);
  1985. } else {
  1986. int handle = priv(mobj->d.data)->propertyData + 3*idx;
  1987. uint flags = mobj->d.data[handle + 2];
  1988. t = flags >> 24;
  1989. if (t == 0xff) // special value for QVariant
  1990. t = QVariant::LastType;
  1991. if (t == QVariant::Invalid) {
  1992. const char *typeName = mobj->d.stringdata + mobj->d.data[handle + 1];
  1993. const char *vtypeName = value.typeName();
  1994. if (vtypeName && strcmp(typeName, vtypeName) == 0)
  1995. t = value.userType();
  1996. else
  1997. t = QVariant::nameToType(typeName);
  1998. }
  1999. if (t == QVariant::Invalid)
  2000. return false;
  2001. if (t != QVariant::LastType && t != (uint)value.userType() && (t < QMetaType::User && !v.convert((QVariant::Type)t)))
  2002. return false;
  2003. }
  2004. // the status variable is changed by qt_metacall to indicate what it did
  2005. // this feature is currently only used by QtDBus and should not be depended
  2006. // upon. Don't change it without looking into QDBusAbstractInterface first
  2007. // -1 (unchanged): normal qt_metacall, result stored in argv[0]
  2008. // changed: result stored directly in value, return the value of status
  2009. int status = -1;
  2010. // the flags variable is used by the declarative module to implement
  2011. // interception of property writes.
  2012. int flags = 0;
  2013. void *argv[] = { 0, &v, &status, &flags };
  2014. if (t == QVariant::LastType)
  2015. argv[0] = &v;
  2016. else
  2017. argv[0] = v.data();
  2018. QMetaObject::metacall(object, QMetaObject::WriteProperty, idx + mobj->propertyOffset(), argv);
  2019. return status;
  2020. }
  2021. /*!
  2022. Resets the property for the given \a object with a reset method.
  2023. Returns true if the reset worked; otherwise returns false.
  2024. Reset methods are optional; only a few properties support them.
  2025. \sa read(), write()
  2026. */
  2027. bool QMetaProperty::reset(QObject *object) const
  2028. {
  2029. if (!object || !mobj || !isResettable())
  2030. return false;
  2031. void *argv[] = { 0 };
  2032. QMetaObject::metacall(object, QMetaObject::ResetProperty, idx + mobj->propertyOffset(), argv);
  2033. return true;
  2034. }
  2035. /*!
  2036. Returns true if this property can be reset to a default value; otherwise
  2037. returns false.
  2038. \sa reset()
  2039. */
  2040. bool QMetaProperty::isResettable() const
  2041. {
  2042. if (!mobj)
  2043. return false;
  2044. int flags = mobj->d.data[handle + 2];
  2045. return flags & Resettable;
  2046. }
  2047. /*!
  2048. Returns true if this property is readable; otherwise returns false.
  2049. \sa isWritable(), read(), isValid()
  2050. */
  2051. bool QMetaProperty::isReadable() const
  2052. {
  2053. if (!mobj)
  2054. return false;
  2055. int flags = mobj->d.data[handle + 2];
  2056. return flags & Readable;
  2057. }
  2058. /*!
  2059. Returns true if this property has a corresponding change notify signal;
  2060. otherwise returns false.
  2061. \sa notifySignal()
  2062. */
  2063. bool QMetaProperty::hasNotifySignal() const
  2064. {
  2065. if (!mobj)
  2066. return false;
  2067. int flags = mobj->d.data[handle + 2];
  2068. return flags & Notify;
  2069. }
  2070. /*!
  2071. \since 4.5
  2072. Returns the QMetaMethod instance of the property change notifying signal if
  2073. one was specified, otherwise returns an invalid QMetaMethod.
  2074. \sa hasNotifySignal()
  2075. */
  2076. QMetaMethod QMetaProperty::notifySignal() const
  2077. {
  2078. int id = notifySignalIndex();
  2079. if (id != -1)
  2080. return mobj->method(id);
  2081. else
  2082. return QMetaMethod();
  2083. }
  2084. /*!
  2085. \since 4.6
  2086. Returns the index of the property change notifying signal if one was
  2087. specified, otherwise returns -1.
  2088. \sa hasNotifySignal()
  2089. */
  2090. int QMetaProperty::notifySignalIndex() const
  2091. {
  2092. if (hasNotifySignal()) {
  2093. int offset = priv(mobj->d.data)->propertyData +
  2094. priv(mobj->d.data)->propertyCount * 3 + idx;
  2095. return mobj->d.data[offset] + mobj->methodOffset();
  2096. } else {
  2097. return -1;
  2098. }
  2099. }
  2100. /*!
  2101. \internal
  2102. Returns the property revision if one was
  2103. specified by REVISION, otherwise returns 0.
  2104. */
  2105. int QMetaProperty::revision() const
  2106. {
  2107. if (!mobj)
  2108. return 0;
  2109. int flags = mobj->d.data[handle + 2];
  2110. if (flags & Revisioned) {
  2111. int offset = priv(mobj->d.data)->propertyData +
  2112. priv(mobj->d.data)->propertyCount * 3 + idx;
  2113. // Revision data is placed after NOTIFY data, if present.
  2114. // Iterate through properties to discover whether we have NOTIFY signals.
  2115. for (int i = 0; i < priv(mobj->d.data)->propertyCount; ++i) {
  2116. int handle = priv(mobj->d.data)->propertyData + 3*i;
  2117. if (mobj->d.data[handle + 2] & Notify) {
  2118. offset += priv(mobj->d.data)->propertyCount;
  2119. break;
  2120. }
  2121. }
  2122. return mobj->d.data[offset];
  2123. } else {
  2124. return 0;
  2125. }
  2126. }
  2127. /*!
  2128. Returns true if this property is writable; otherwise returns
  2129. false.
  2130. \sa isReadable(), write()
  2131. */
  2132. bool QMetaProperty::isWritable() const
  2133. {
  2134. if (!mobj)
  2135. return false;
  2136. int flags = mobj->d.data[handle + 2];
  2137. return flags & Writable;
  2138. }
  2139. /*!
  2140. Returns true if this property is designable for the given \a object;
  2141. otherwise returns false.
  2142. If no \a object is given, the function returns false if the
  2143. \c{Q_PROPERTY()}'s \c DESIGNABLE attribute is false; otherwise
  2144. returns true (if the attribute is true or is a function or expression).
  2145. \sa isScriptable(), isStored()
  2146. */
  2147. bool QMetaProperty::isDesignable(const QObject *object) const
  2148. {
  2149. if (!mobj)
  2150. return false;
  2151. int flags = mobj->d.data[handle + 2];
  2152. bool b = flags & Designable;
  2153. if (object) {
  2154. void *argv[] = { &b };
  2155. QMetaObject::metacall(const_cast<QObject*>(object), QMetaObject::QueryPropertyDesignable,
  2156. idx + mobj->propertyOffset(), argv);
  2157. }
  2158. return b;
  2159. }
  2160. /*!
  2161. Returns true if the property is scriptable for the given \a object;
  2162. otherwise returns false.
  2163. If no \a object is given, the function returns false if the
  2164. \c{Q_PROPERTY()}'s \c SCRIPTABLE attribute is false; otherwise returns
  2165. true (if the attribute is true or is a function or expression).
  2166. \sa isDesignable(), isStored()
  2167. */
  2168. bool QMetaProperty::isScriptable(const QObject *object) const
  2169. {
  2170. if (!mobj)
  2171. return false;
  2172. int flags = mobj->d.data[handle + 2];
  2173. bool b = flags & Scriptable;
  2174. if (object) {
  2175. void *argv[] = { &b };
  2176. QMetaObject::metacall(const_cast<QObject*>(object), QMetaObject::QueryPropertyScriptable,
  2177. idx + mobj->propertyOffset(), argv);
  2178. }
  2179. return b;
  2180. }
  2181. /*!
  2182. Returns true if the property is stored for \a object; otherwise returns
  2183. false.
  2184. If no \a object is given, the function returns false if the
  2185. \c{Q_PROPERTY()}'s \c STORED attribute is false; otherwise returns
  2186. true (if the attribute is true or is a function or expression).
  2187. \sa isDesignable(), isScriptable()
  2188. */
  2189. bool QMetaProperty::isStored(const QObject *object) const
  2190. {
  2191. if (!mobj)
  2192. return false;
  2193. int flags = mobj->d.data[handle + 2];
  2194. bool b = flags & Stored;
  2195. if (object) {
  2196. void *argv[] = { &b };
  2197. QMetaObject::metacall(const_cast<QObject*>(object), QMetaObject::QueryPropertyStored,
  2198. idx + mobj->propertyOffset(), argv);
  2199. }
  2200. return b;
  2201. }
  2202. /*!
  2203. Returns true if this property is designated as the \c USER
  2204. property, i.e., the one that the user can edit for \a object or
  2205. that is significant in some other way. Otherwise it returns
  2206. false. e.g., the \c text property is the \c USER editable property
  2207. of a QLineEdit.
  2208. If \a object is null, the function returns false if the \c
  2209. {Q_PROPERTY()}'s \c USER attribute is false. Otherwise it returns
  2210. true.
  2211. \sa QMetaObject::userProperty(), isDesignable(), isScriptable()
  2212. */
  2213. bool QMetaProperty::isUser(const QObject *object) const
  2214. {
  2215. if (!mobj)
  2216. return false;
  2217. int flags = mobj->d.data[handle + 2];
  2218. bool b = flags & User;
  2219. if (object) {
  2220. void *argv[] = { &b };
  2221. QMetaObject::metacall(const_cast<QObject*>(object), QMetaObject::QueryPropertyUser,
  2222. idx + mobj->propertyOffset(), argv);
  2223. }
  2224. return b;
  2225. }
  2226. /*!
  2227. \since 4.6
  2228. Returns true if the property is constant; otherwise returns false.
  2229. A property is constant if the \c{Q_PROPERTY()}'s \c CONSTANT attribute
  2230. is set.
  2231. */
  2232. bool QMetaProperty::isConstant() const
  2233. {
  2234. if (!mobj)
  2235. return false;
  2236. int flags = mobj->d.data[handle + 2];
  2237. return flags & Constant;
  2238. }
  2239. /*!
  2240. \since 4.6
  2241. Returns true if the property is final; otherwise returns false.
  2242. A property is final if the \c{Q_PROPERTY()}'s \c FINAL attribute
  2243. is set.
  2244. */
  2245. bool QMetaProperty::isFinal() const
  2246. {
  2247. if (!mobj)
  2248. return false;
  2249. int flags = mobj->d.data[handle + 2];
  2250. return flags & Final;
  2251. }
  2252. /*!
  2253. \obsolete
  2254. Returns true if the property is editable for the given \a object;
  2255. otherwise returns false.
  2256. If no \a object is given, the function returns false if the
  2257. \c{Q_PROPERTY()}'s \c EDITABLE attribute is false; otherwise returns
  2258. true (if the attribute is true or is a function or expression).
  2259. \sa isDesignable(), isScriptable(), isStored()
  2260. */
  2261. bool QMetaProperty::isEditable(const QObject *object) const
  2262. {
  2263. if (!mobj)
  2264. return false;
  2265. int flags = mobj->d.data[handle + 2];
  2266. bool b = flags & Editable;
  2267. if (object) {
  2268. void *argv[] = { &b };
  2269. QMetaObject::metacall(const_cast<QObject*>(object), QMetaObject::QueryPropertyEditable,
  2270. idx + mobj->propertyOffset(), argv);
  2271. }
  2272. return b;
  2273. }
  2274. /*!
  2275. \class QMetaClassInfo
  2276. \brief The QMetaClassInfo class provides additional information
  2277. about a class.
  2278. \ingroup objectmodel
  2279. Class information items are simple \e{name}--\e{value} pairs that
  2280. are specified using Q_CLASSINFO() in the source code. The
  2281. information can be retrieved using name() and value(). For example:
  2282. \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 5
  2283. This mechanism is free for you to use in your Qt applications. Qt
  2284. doesn't use it for any of its classes.
  2285. \sa QMetaObject
  2286. */
  2287. /*!
  2288. \fn QMetaClassInfo::QMetaClassInfo()
  2289. \internal
  2290. */
  2291. /*!
  2292. \fn const QMetaObject *QMetaClassInfo::enclosingMetaObject() const
  2293. \internal
  2294. */
  2295. /*!
  2296. Returns the name of this item.
  2297. \sa value()
  2298. */
  2299. const char *QMetaClassInfo::name() const
  2300. {
  2301. if (!mobj)
  2302. return 0;
  2303. return mobj->d.stringdata + mobj->d.data[handle];
  2304. }
  2305. /*!
  2306. Returns the value of this item.
  2307. \sa name()
  2308. */
  2309. const char* QMetaClassInfo::value() const
  2310. {
  2311. if (!mobj)
  2312. return 0;
  2313. return mobj->d.stringdata + mobj->d.data[handle + 1];
  2314. }
  2315. /*!
  2316. \macro QGenericArgument Q_ARG(Type, const Type &value)
  2317. \relates QMetaObject
  2318. This macro takes a \a Type and a \a value of that type and
  2319. returns a \l QGenericArgument object that can be passed to
  2320. QMetaObject::invokeMethod().
  2321. \sa Q_RETURN_ARG()
  2322. */
  2323. /*!
  2324. \macro QGenericReturnArgument Q_RETURN_ARG(Type, Type &value)
  2325. \relates QMetaObject
  2326. This macro takes a \a Type and a non-const reference to a \a
  2327. value of that type and returns a QGenericReturnArgument object
  2328. that can be passed to QMetaObject::invokeMethod().
  2329. \sa Q_ARG()
  2330. */
  2331. /*!
  2332. \class QGenericArgument
  2333. \brief The QGenericArgument class is an internal helper class for
  2334. marshalling arguments.
  2335. This class should never be used directly. Please use the \l Q_ARG()
  2336. macro instead.
  2337. \sa Q_ARG(), QMetaObject::invokeMethod(), QGenericReturnArgument
  2338. */
  2339. /*!
  2340. \fn QGenericArgument::QGenericArgument(const char *name, const void *data)
  2341. Constructs a QGenericArgument object with the given \a name and \a data.
  2342. */
  2343. /*!
  2344. \fn QGenericArgument::data () const
  2345. Returns the data set in the constructor.
  2346. */
  2347. /*!
  2348. \fn QGenericArgument::name () const
  2349. Returns the name set in the constructor.
  2350. */
  2351. /*!
  2352. \class QGenericReturnArgument
  2353. \brief The QGenericReturnArgument class is an internal helper class for
  2354. marshalling arguments.
  2355. This class should never be used directly. Please use the
  2356. Q_RETURN_ARG() macro instead.
  2357. \sa Q_RETURN_ARG(), QMetaObject::invokeMethod(), QGenericArgument
  2358. */
  2359. /*!
  2360. \fn QGenericReturnArgument::QGenericReturnArgument(const char *name, void *data)
  2361. Constructs a QGenericReturnArgument object with the given \a name
  2362. and \a data.
  2363. */
  2364. /*! \internal
  2365. If the local_method_index is a cloned method, return the index of the original.
  2366. Example: if the index of "destroyed()" is passed, the index of "destroyed(QObject*)" is returned
  2367. */
  2368. int QMetaObjectPrivate::originalClone(const QMetaObject *mobj, int local_method_index)
  2369. {
  2370. Q_ASSERT(local_method_index < get(mobj)->methodCount);
  2371. int handle = get(mobj)->methodData + 5 * local_method_index;
  2372. while (mobj->d.data[handle + 4] & MethodCloned) {
  2373. Q_ASSERT(local_method_index > 0);
  2374. handle -= 5;
  2375. local_method_index--;
  2376. }
  2377. return local_method_index;
  2378. }
  2379. QT_END_NAMESPACE