PageRenderTime 63ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/qt-everywhere-opensource-src-4.7.1/src/corelib/kernel/qmetaobject.cpp

https://bitbucket.org/boroondas/3p-qt-open-83
C++ | 2706 lines | 1324 code | 192 blank | 1190 comment | 413 complexity | 86266d5fd1761318cb369ce96bc31dd1 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, BSD-3-Clause, GPL-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, GPL-3.0, CC-BY-SA-4.0, Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2010 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. ** Commercial Usage
  11. ** Licensees holding valid Qt Commercial licenses may use this file in
  12. ** accordance with the Qt Commercial License Agreement provided with the
  13. ** Software or, alternatively, in accordance with the terms contained in
  14. ** a written agreement between you and Nokia.
  15. **
  16. ** GNU Lesser General Public License Usage
  17. ** Alternatively, this file may be used under the terms of the GNU Lesser
  18. ** General Public License version 2.1 as published by the Free Software
  19. ** Foundation and appearing in the file LICENSE.LGPL included in the
  20. ** packaging of this file. Please review the following information to
  21. ** ensure the GNU Lesser General Public License version 2.1 requirements
  22. ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  23. **
  24. ** In addition, as a special exception, Nokia gives you certain additional
  25. ** rights. These rights are described in the Nokia Qt LGPL Exception
  26. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  27. **
  28. ** GNU General Public License Usage
  29. ** Alternatively, this file may be used under the terms of the GNU
  30. ** General Public License version 3.0 as published by the Free Software
  31. ** Foundation and appearing in the file LICENSE.GPL included in the
  32. ** packaging of this file. Please review the following information to
  33. ** ensure the GNU General Public License version 3.0 requirements will be
  34. ** met: http://www.gnu.org/copyleft/gpl.html.
  35. **
  36. ** If you have questions regarding the use of this file, please contact
  37. ** Nokia at qt-info@nokia.com.
  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. if (priv(d.data)->revision < 2)
  190. return 0;
  191. const QMetaObjectExtraData *extra = (const QMetaObjectExtraData*)(d.extradata);
  192. if (!extra || !extra->static_metacall)
  193. return 0;
  194. return extra->static_metacall(cl, idx, argv);
  195. }
  196. /*!
  197. \internal
  198. */
  199. int QMetaObject::metacall(QObject *object, Call cl, int idx, void **argv)
  200. {
  201. if (QMetaObject *mo = object->d_ptr->metaObject)
  202. return static_cast<QAbstractDynamicMetaObject*>(mo)->metaCall(cl, idx, argv);
  203. else
  204. return object->qt_metacall(cl, idx, argv);
  205. }
  206. /*!
  207. \fn const char *QMetaObject::className() const
  208. Returns the class name.
  209. \sa superClass()
  210. */
  211. /*!
  212. \fn QMetaObject *QMetaObject::superClass() const
  213. Returns the meta-object of the superclass, or 0 if there is no
  214. such object.
  215. \sa className()
  216. */
  217. /*!
  218. \internal
  219. Returns \a obj if object \a obj inherits from this
  220. meta-object; otherwise returns 0.
  221. */
  222. QObject *QMetaObject::cast(QObject *obj) const
  223. {
  224. if (obj) {
  225. const QMetaObject *m = obj->metaObject();
  226. do {
  227. if (m == this)
  228. return const_cast<QObject*>(obj);
  229. } while ((m = m->d.superdata));
  230. }
  231. return 0;
  232. }
  233. #ifndef QT_NO_TRANSLATION
  234. /*!
  235. \internal
  236. */
  237. QString QMetaObject::tr(const char *s, const char *c) const
  238. {
  239. return QCoreApplication::translate(d.stringdata, s, c, QCoreApplication::CodecForTr);
  240. }
  241. /*!
  242. \internal
  243. */
  244. QString QMetaObject::tr(const char *s, const char *c, int n) const
  245. {
  246. return QCoreApplication::translate(d.stringdata, s, c, QCoreApplication::CodecForTr, n);
  247. }
  248. /*!
  249. \internal
  250. */
  251. QString QMetaObject::trUtf8(const char *s, const char *c) const
  252. {
  253. return QCoreApplication::translate(d.stringdata, s, c, QCoreApplication::UnicodeUTF8);
  254. }
  255. /*!
  256. \internal
  257. */
  258. QString QMetaObject::trUtf8(const char *s, const char *c, int n) const
  259. {
  260. return QCoreApplication::translate(d.stringdata, s, c, QCoreApplication::UnicodeUTF8, n);
  261. }
  262. #endif // QT_NO_TRANSLATION
  263. /*!
  264. Returns the method offset for this class; i.e. the index position
  265. of this class's first member function.
  266. The offset is the sum of all the methods in the class's
  267. superclasses (which is always positive since QObject has the
  268. deleteLater() slot and a destroyed() signal).
  269. \sa method(), methodCount(), indexOfMethod()
  270. */
  271. int QMetaObject::methodOffset() const
  272. {
  273. int offset = 0;
  274. const QMetaObject *m = d.superdata;
  275. while (m) {
  276. offset += priv(m->d.data)->methodCount;
  277. m = m->d.superdata;
  278. }
  279. return offset;
  280. }
  281. /*!
  282. Returns the enumerator offset for this class; i.e. the index
  283. position of this class's first enumerator.
  284. If the class has no superclasses with enumerators, the offset is
  285. 0; otherwise the offset is the sum of all the enumerators in the
  286. class's superclasses.
  287. \sa enumerator(), enumeratorCount(), indexOfEnumerator()
  288. */
  289. int QMetaObject::enumeratorOffset() const
  290. {
  291. int offset = 0;
  292. const QMetaObject *m = d.superdata;
  293. while (m) {
  294. offset += priv(m->d.data)->enumeratorCount;
  295. m = m->d.superdata;
  296. }
  297. return offset;
  298. }
  299. /*!
  300. Returns the property offset for this class; i.e. the index
  301. position of this class's first property.
  302. The offset is the sum of all the properties in the class's
  303. superclasses (which is always positive since QObject has the
  304. name() property).
  305. \sa property(), propertyCount(), indexOfProperty()
  306. */
  307. int QMetaObject::propertyOffset() const
  308. {
  309. int offset = 0;
  310. const QMetaObject *m = d.superdata;
  311. while (m) {
  312. offset += priv(m->d.data)->propertyCount;
  313. m = m->d.superdata;
  314. }
  315. return offset;
  316. }
  317. /*!
  318. Returns the class information offset for this class; i.e. the
  319. index position of this class's first class information item.
  320. If the class has no superclasses with class information, the
  321. offset is 0; otherwise the offset is the sum of all the class
  322. information items in the class's superclasses.
  323. \sa classInfo(), classInfoCount(), indexOfClassInfo()
  324. */
  325. int QMetaObject::classInfoOffset() const
  326. {
  327. int offset = 0;
  328. const QMetaObject *m = d.superdata;
  329. while (m) {
  330. offset += priv(m->d.data)->classInfoCount;
  331. m = m->d.superdata;
  332. }
  333. return offset;
  334. }
  335. /*!
  336. \since 4.5
  337. Returns the number of constructors in this class.
  338. \sa constructor(), indexOfConstructor()
  339. */
  340. int QMetaObject::constructorCount() const
  341. {
  342. if (priv(d.data)->revision < 2)
  343. return 0;
  344. return priv(d.data)->constructorCount;
  345. }
  346. /*!
  347. Returns the number of methods in this class, including the number of
  348. properties provided by each base class. These include signals and slots
  349. as well as normal member functions.
  350. Use code like the following to obtain a QStringList containing the methods
  351. specific to a given class:
  352. \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp methodCount
  353. \sa method(), methodOffset(), indexOfMethod()
  354. */
  355. int QMetaObject::methodCount() const
  356. {
  357. int n = priv(d.data)->methodCount;
  358. const QMetaObject *m = d.superdata;
  359. while (m) {
  360. n += priv(m->d.data)->methodCount;
  361. m = m->d.superdata;
  362. }
  363. return n;
  364. }
  365. /*!
  366. Returns the number of enumerators in this class.
  367. \sa enumerator(), enumeratorOffset(), indexOfEnumerator()
  368. */
  369. int QMetaObject::enumeratorCount() const
  370. {
  371. int n = priv(d.data)->enumeratorCount;
  372. const QMetaObject *m = d.superdata;
  373. while (m) {
  374. n += priv(m->d.data)->enumeratorCount;
  375. m = m->d.superdata;
  376. }
  377. return n;
  378. }
  379. /*!
  380. Returns the number of properties in this class, including the number of
  381. properties provided by each base class.
  382. Use code like the following to obtain a QStringList containing the properties
  383. specific to a given class:
  384. \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp propertyCount
  385. \sa property(), propertyOffset(), indexOfProperty()
  386. */
  387. int QMetaObject::propertyCount() const
  388. {
  389. int n = priv(d.data)->propertyCount;
  390. const QMetaObject *m = d.superdata;
  391. while (m) {
  392. n += priv(m->d.data)->propertyCount;
  393. m = m->d.superdata;
  394. }
  395. return n;
  396. }
  397. /*!
  398. Returns the number of items of class information in this class.
  399. \sa classInfo(), classInfoOffset(), indexOfClassInfo()
  400. */
  401. int QMetaObject::classInfoCount() const
  402. {
  403. int n = priv(d.data)->classInfoCount;
  404. const QMetaObject *m = d.superdata;
  405. while (m) {
  406. n += priv(m->d.data)->classInfoCount;
  407. m = m->d.superdata;
  408. }
  409. return n;
  410. }
  411. /** \internal
  412. * helper function for indexOf{Method,Slot,Signal}, returns the relative index of the method within
  413. * the baseObject
  414. * \a MethodType might be MethodSignal or MethodSlot, or 0 to match everything.
  415. * \a normalizeStringData set to true if we should do a second pass for old moc generated files normalizing all the symbols.
  416. */
  417. template<int MethodType>
  418. static inline int indexOfMethodRelative(const QMetaObject **baseObject,
  419. const char *method,
  420. bool normalizeStringData)
  421. {
  422. for (const QMetaObject *m = *baseObject; m; m = m->d.superdata) {
  423. int i = (MethodType == MethodSignal && priv(m->d.data)->revision >= 4)
  424. ? (priv(m->d.data)->signalCount - 1) : (priv(m->d.data)->methodCount - 1);
  425. const int end = (MethodType == MethodSlot && priv(m->d.data)->revision >= 4)
  426. ? (priv(m->d.data)->signalCount) : 0;
  427. if (!normalizeStringData) {
  428. for (; i >= end; --i) {
  429. const char *stringdata = m->d.stringdata + m->d.data[priv(m->d.data)->methodData + 5*i];
  430. if (method[0] == stringdata[0] && strcmp(method + 1, stringdata + 1) == 0) {
  431. *baseObject = m;
  432. return i;
  433. }
  434. }
  435. } else if (priv(m->d.data)->revision < 5) {
  436. for (; i >= end; --i) {
  437. const char *stringdata = (m->d.stringdata + m->d.data[priv(m->d.data)->methodData + 5 * i]);
  438. const QByteArray normalizedSignature = QMetaObject::normalizedSignature(stringdata);
  439. if (normalizedSignature == method) {
  440. *baseObject = m;
  441. return i;
  442. }
  443. }
  444. }
  445. }
  446. return -1;
  447. }
  448. /*!
  449. \since 4.5
  450. Finds \a constructor and returns its index; otherwise returns -1.
  451. Note that the \a constructor has to be in normalized form, as returned
  452. by normalizedSignature().
  453. \sa constructor(), constructorCount(), normalizedSignature()
  454. */
  455. int QMetaObject::indexOfConstructor(const char *constructor) const
  456. {
  457. if (priv(d.data)->revision < 2)
  458. return -1;
  459. for (int i = priv(d.data)->constructorCount-1; i >= 0; --i) {
  460. const char *data = d.stringdata + d.data[priv(d.data)->constructorData + 5*i];
  461. if (data[0] == constructor[0] && strcmp(constructor + 1, data + 1) == 0) {
  462. return i;
  463. }
  464. }
  465. return -1;
  466. }
  467. /*!
  468. Finds \a method and returns its index; otherwise returns -1.
  469. Note that the \a method has to be in normalized form, as returned
  470. by normalizedSignature().
  471. \sa method(), methodCount(), methodOffset(), normalizedSignature()
  472. */
  473. int QMetaObject::indexOfMethod(const char *method) const
  474. {
  475. const QMetaObject *m = this;
  476. int i = indexOfMethodRelative<0>(&m, method, false);
  477. if (i < 0) {
  478. m = this;
  479. i = indexOfMethodRelative<0>(&m, method, true);
  480. }
  481. if (i >= 0)
  482. i += m->methodOffset();
  483. return i;
  484. }
  485. /*!
  486. Finds \a signal and returns its index; otherwise returns -1.
  487. This is the same as indexOfMethod(), except that it will return
  488. -1 if the method exists but isn't a signal.
  489. Note that the \a signal has to be in normalized form, as returned
  490. by normalizedSignature().
  491. \sa indexOfMethod(), normalizedSignature(), method(), methodCount(), methodOffset()
  492. */
  493. int QMetaObject::indexOfSignal(const char *signal) const
  494. {
  495. const QMetaObject *m = this;
  496. int i = QMetaObjectPrivate::indexOfSignalRelative(&m, signal, false);
  497. if (i < 0) {
  498. m = this;
  499. i = QMetaObjectPrivate::indexOfSignalRelative(&m, signal, true);
  500. }
  501. if (i >= 0)
  502. i += m->methodOffset();
  503. return i;
  504. }
  505. /*! \internal
  506. Same as QMetaObject::indexOfSignal, but the result is the local offset to the base object.
  507. \a baseObject will be adjusted to the enclosing QMetaObject, or 0 if the signal is not found
  508. */
  509. int QMetaObjectPrivate::indexOfSignalRelative(const QMetaObject **baseObject,
  510. const char *signal,
  511. bool normalizeStringData)
  512. {
  513. int i = indexOfMethodRelative<MethodSignal>(baseObject, signal, normalizeStringData);
  514. #ifndef QT_NO_DEBUG
  515. const QMetaObject *m = *baseObject;
  516. if (i >= 0 && m && m->d.superdata) {
  517. int conflict = m->d.superdata->indexOfMethod(signal);
  518. if (conflict >= 0)
  519. qWarning("QMetaObject::indexOfSignal: signal %s from %s redefined in %s",
  520. signal, m->d.superdata->d.stringdata, m->d.stringdata);
  521. }
  522. #endif
  523. return i;
  524. }
  525. /*!
  526. Finds \a slot and returns its index; otherwise returns -1.
  527. This is the same as indexOfMethod(), except that it will return
  528. -1 if the method exists but isn't a slot.
  529. \sa indexOfMethod(), method(), methodCount(), methodOffset()
  530. */
  531. int QMetaObject::indexOfSlot(const char *slot) const
  532. {
  533. int i = QMetaObjectPrivate::indexOfSlot(this, slot, false);
  534. if (i < 0)
  535. i = QMetaObjectPrivate::indexOfSlot(this, slot, true);
  536. return i;
  537. }
  538. int QMetaObjectPrivate::indexOfSlot(const QMetaObject *m,
  539. const char *slot,
  540. bool normalizeStringData)
  541. {
  542. int i = indexOfMethodRelative<MethodSlot>(&m, slot, normalizeStringData);
  543. if (i >= 0)
  544. i += m->methodOffset();
  545. return i;
  546. }
  547. static const QMetaObject *QMetaObject_findMetaObject(const QMetaObject *self, const char *name)
  548. {
  549. while (self) {
  550. if (strcmp(self->d.stringdata, name) == 0)
  551. return self;
  552. if (self->d.extradata) {
  553. #ifdef Q_NO_DATA_RELOCATION
  554. const QMetaObjectAccessor *e;
  555. Q_ASSERT(priv(self->d.data)->revision >= 2);
  556. #else
  557. const QMetaObject **e;
  558. if (priv(self->d.data)->revision < 2) {
  559. e = (const QMetaObject**)(self->d.extradata);
  560. } else
  561. #endif
  562. {
  563. const QMetaObjectExtraData *extra = (const QMetaObjectExtraData*)(self->d.extradata);
  564. e = extra->objects;
  565. }
  566. if (e) {
  567. while (*e) {
  568. #ifdef Q_NO_DATA_RELOCATION
  569. if (const QMetaObject *m =QMetaObject_findMetaObject(&((*e)()), name))
  570. #else
  571. if (const QMetaObject *m =QMetaObject_findMetaObject((*e), name))
  572. #endif
  573. return m;
  574. ++e;
  575. }
  576. }
  577. }
  578. self = self->d.superdata;
  579. }
  580. return self;
  581. }
  582. /*!
  583. Finds enumerator \a name and returns its index; otherwise returns
  584. -1.
  585. \sa enumerator(), enumeratorCount(), enumeratorOffset()
  586. */
  587. int QMetaObject::indexOfEnumerator(const char *name) const
  588. {
  589. const QMetaObject *m = this;
  590. while (m) {
  591. const QMetaObjectPrivate *d = priv(m->d.data);
  592. for (int i = d->enumeratorCount - 1; i >= 0; --i) {
  593. const char *prop = m->d.stringdata + m->d.data[d->enumeratorData + 4*i];
  594. if (name[0] == prop[0] && strcmp(name + 1, prop + 1) == 0) {
  595. i += m->enumeratorOffset();
  596. return i;
  597. }
  598. }
  599. m = m->d.superdata;
  600. }
  601. return -1;
  602. }
  603. /*!
  604. Finds property \a name and returns its index; otherwise returns
  605. -1.
  606. \sa property(), propertyCount(), propertyOffset()
  607. */
  608. int QMetaObject::indexOfProperty(const char *name) const
  609. {
  610. const QMetaObject *m = this;
  611. while (m) {
  612. const QMetaObjectPrivate *d = priv(m->d.data);
  613. for (int i = d->propertyCount-1; i >= 0; --i) {
  614. const char *prop = m->d.stringdata + m->d.data[d->propertyData + 3*i];
  615. if (name[0] == prop[0] && strcmp(name + 1, prop + 1) == 0) {
  616. i += m->propertyOffset();
  617. return i;
  618. }
  619. }
  620. m = m->d.superdata;
  621. }
  622. if (priv(this->d.data)->revision >= 3 && (priv(this->d.data)->flags & DynamicMetaObject)) {
  623. QAbstractDynamicMetaObject *me =
  624. const_cast<QAbstractDynamicMetaObject *>(static_cast<const QAbstractDynamicMetaObject *>(this));
  625. return me->createProperty(name, 0);
  626. }
  627. return -1;
  628. }
  629. /*!
  630. Finds class information item \a name and returns its index;
  631. otherwise returns -1.
  632. \sa classInfo(), classInfoCount(), classInfoOffset()
  633. */
  634. int QMetaObject::indexOfClassInfo(const char *name) const
  635. {
  636. int i = -1;
  637. const QMetaObject *m = this;
  638. while (m && i < 0) {
  639. for (i = priv(m->d.data)->classInfoCount-1; i >= 0; --i)
  640. if (strcmp(name, m->d.stringdata
  641. + m->d.data[priv(m->d.data)->classInfoData + 2*i]) == 0) {
  642. i += m->classInfoOffset();
  643. break;
  644. }
  645. m = m->d.superdata;
  646. }
  647. return i;
  648. }
  649. /*!
  650. \since 4.5
  651. Returns the meta-data for the constructor with the given \a index.
  652. \sa constructorCount(), newInstance()
  653. */
  654. QMetaMethod QMetaObject::constructor(int index) const
  655. {
  656. int i = index;
  657. QMetaMethod result;
  658. if (priv(d.data)->revision >= 2 && i >= 0 && i < priv(d.data)->constructorCount) {
  659. result.mobj = this;
  660. result.handle = priv(d.data)->constructorData + 5*i;
  661. }
  662. return result;
  663. }
  664. /*!
  665. Returns the meta-data for the method with the given \a index.
  666. \sa methodCount(), methodOffset(), indexOfMethod()
  667. */
  668. QMetaMethod QMetaObject::method(int index) const
  669. {
  670. int i = index;
  671. i -= methodOffset();
  672. if (i < 0 && d.superdata)
  673. return d.superdata->method(index);
  674. QMetaMethod result;
  675. if (i >= 0 && i < priv(d.data)->methodCount) {
  676. result.mobj = this;
  677. result.handle = priv(d.data)->methodData + 5*i;
  678. }
  679. return result;
  680. }
  681. /*!
  682. Returns the meta-data for the enumerator with the given \a index.
  683. \sa enumeratorCount(), enumeratorOffset(), indexOfEnumerator()
  684. */
  685. QMetaEnum QMetaObject::enumerator(int index) const
  686. {
  687. int i = index;
  688. i -= enumeratorOffset();
  689. if (i < 0 && d.superdata)
  690. return d.superdata->enumerator(index);
  691. QMetaEnum result;
  692. if (i >= 0 && i < priv(d.data)->enumeratorCount) {
  693. result.mobj = this;
  694. result.handle = priv(d.data)->enumeratorData + 4*i;
  695. }
  696. return result;
  697. }
  698. /*!
  699. Returns the meta-data for the property with the given \a index.
  700. If no such property exists, a null QMetaProperty is returned.
  701. \sa propertyCount(), propertyOffset(), indexOfProperty()
  702. */
  703. QMetaProperty QMetaObject::property(int index) const
  704. {
  705. int i = index;
  706. i -= propertyOffset();
  707. if (i < 0 && d.superdata)
  708. return d.superdata->property(index);
  709. QMetaProperty result;
  710. if (i >= 0 && i < priv(d.data)->propertyCount) {
  711. int handle = priv(d.data)->propertyData + 3*i;
  712. int flags = d.data[handle + 2];
  713. const char *type = d.stringdata + d.data[handle + 1];
  714. result.mobj = this;
  715. result.handle = handle;
  716. result.idx = i;
  717. if (flags & EnumOrFlag) {
  718. result.menum = enumerator(indexOfEnumerator(type));
  719. if (!result.menum.isValid()) {
  720. QByteArray enum_name = type;
  721. QByteArray scope_name = d.stringdata;
  722. int s = enum_name.lastIndexOf("::");
  723. if (s > 0) {
  724. scope_name = enum_name.left(s);
  725. enum_name = enum_name.mid(s + 2);
  726. }
  727. const QMetaObject *scope = 0;
  728. if (scope_name == "Qt")
  729. scope = &QObject::staticQtMetaObject;
  730. else
  731. scope = QMetaObject_findMetaObject(this, scope_name);
  732. if (scope)
  733. result.menum = scope->enumerator(scope->indexOfEnumerator(enum_name));
  734. }
  735. }
  736. }
  737. return result;
  738. }
  739. /*!
  740. \since 4.2
  741. Returns the property that has the \c USER flag set to true.
  742. \sa QMetaProperty::isUser()
  743. */
  744. QMetaProperty QMetaObject::userProperty() const
  745. {
  746. const int propCount = propertyCount();
  747. for (int i = propCount - 1; i >= 0; --i) {
  748. const QMetaProperty prop = property(i);
  749. if (prop.isUser())
  750. return prop;
  751. }
  752. return QMetaProperty();
  753. }
  754. /*!
  755. Returns the meta-data for the item of class information with the
  756. given \a index.
  757. Example:
  758. \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 0
  759. \sa classInfoCount(), classInfoOffset(), indexOfClassInfo()
  760. */
  761. QMetaClassInfo QMetaObject::classInfo(int index) const
  762. {
  763. int i = index;
  764. i -= classInfoOffset();
  765. if (i < 0 && d.superdata)
  766. return d.superdata->classInfo(index);
  767. QMetaClassInfo result;
  768. if (i >= 0 && i < priv(d.data)->classInfoCount) {
  769. result.mobj = this;
  770. result.handle = priv(d.data)->classInfoData + 2*i;
  771. }
  772. return result;
  773. }
  774. /*!
  775. Returns true if the \a signal and \a method arguments are
  776. compatible; otherwise returns false.
  777. Both \a signal and \a method are expected to be normalized.
  778. \sa normalizedSignature()
  779. */
  780. bool QMetaObject::checkConnectArgs(const char *signal, const char *method)
  781. {
  782. const char *s1 = signal;
  783. const char *s2 = method;
  784. while (*s1++ != '(') { } // scan to first '('
  785. while (*s2++ != '(') { }
  786. if (*s2 == ')' || qstrcmp(s1,s2) == 0) // method has no args or
  787. return true; // exact match
  788. int s1len = qstrlen(s1);
  789. int s2len = qstrlen(s2);
  790. if (s2len < s1len && strncmp(s1,s2,s2len-1)==0 && s1[s2len-1]==',')
  791. return true; // method has less args
  792. return false;
  793. }
  794. static void qRemoveWhitespace(const char *s, char *d)
  795. {
  796. char last = 0;
  797. while (*s && is_space(*s))
  798. s++;
  799. while (*s) {
  800. while (*s && !is_space(*s))
  801. last = *d++ = *s++;
  802. while (*s && is_space(*s))
  803. s++;
  804. if (*s && ((is_ident_char(*s) && is_ident_char(last))
  805. || ((*s == ':') && (last == '<')))) {
  806. last = *d++ = ' ';
  807. }
  808. }
  809. *d = '\0';
  810. }
  811. static char *qNormalizeType(char *d, int &templdepth, QByteArray &result)
  812. {
  813. const char *t = d;
  814. while (*d && (templdepth
  815. || (*d != ',' && *d != ')'))) {
  816. if (*d == '<')
  817. ++templdepth;
  818. if (*d == '>')
  819. --templdepth;
  820. ++d;
  821. }
  822. if (strncmp("void", t, d - t) != 0)
  823. result += normalizeTypeInternal(t, d);
  824. return d;
  825. }
  826. /*!
  827. \since 4.2
  828. Normalizes a \a type.
  829. See QMetaObject::normalizedSignature() for a description on how
  830. Qt normalizes.
  831. Example:
  832. \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 1
  833. \sa normalizedSignature()
  834. */
  835. QByteArray QMetaObject::normalizedType(const char *type)
  836. {
  837. QByteArray result;
  838. if (!type || !*type)
  839. return result;
  840. QVarLengthArray<char> stackbuf(qstrlen(type) + 1);
  841. qRemoveWhitespace(type, stackbuf.data());
  842. int templdepth = 0;
  843. qNormalizeType(stackbuf.data(), templdepth, result);
  844. return result;
  845. }
  846. /*!
  847. Normalizes the signature of the given \a method.
  848. Qt uses normalized signatures to decide whether two given signals
  849. and slots are compatible. Normalization reduces whitespace to a
  850. minimum, moves 'const' to the front where appropriate, removes
  851. 'const' from value types and replaces const references with
  852. values.
  853. \sa checkConnectArgs(), normalizedType()
  854. */
  855. QByteArray QMetaObject::normalizedSignature(const char *method)
  856. {
  857. QByteArray result;
  858. if (!method || !*method)
  859. return result;
  860. int len = int(strlen(method));
  861. QVarLengthArray<char> stackbuf(len + 1);
  862. char *d = stackbuf.data();
  863. qRemoveWhitespace(method, d);
  864. result.reserve(len);
  865. int argdepth = 0;
  866. int templdepth = 0;
  867. while (*d) {
  868. if (argdepth == 1) {
  869. d = qNormalizeType(d, templdepth, result);
  870. if (!*d) //most likely an invalid signature.
  871. break;
  872. }
  873. if (*d == '(')
  874. ++argdepth;
  875. if (*d == ')')
  876. --argdepth;
  877. result += *d++;
  878. }
  879. return result;
  880. }
  881. enum { MaximumParamCount = 11 }; // up to 10 arguments + 1 return value
  882. /*!
  883. Invokes the \a member (a signal or a slot name) on the object \a
  884. obj. Returns true if the member could be invoked. Returns false
  885. if there is no such member or the parameters did not match.
  886. The invocation can be either synchronous or asynchronous,
  887. depending on \a type:
  888. \list
  889. \o If \a type is Qt::DirectConnection, the member will be invoked immediately.
  890. \o If \a type is Qt::QueuedConnection,
  891. a QEvent will be sent and the member is invoked as soon as the application
  892. enters the main event loop.
  893. \o If \a type is Qt::BlockingQueuedConnection, the method will be invoked in
  894. the same way as for Qt::QueuedConnection, except that the current thread
  895. will block until the event is delivered. Using this connection type to
  896. communicate between objects in the same thread will lead to deadlocks.
  897. \o If \a type is Qt::AutoConnection, the member is invoked
  898. synchronously if \a obj lives in the same thread as the
  899. caller; otherwise it will invoke the member asynchronously.
  900. \endlist
  901. The return value of the \a member function call is placed in \a
  902. ret. If the invocation is asynchronous, the return value cannot
  903. be evaluated. You can pass up to ten arguments (\a val0, \a val1,
  904. \a val2, \a val3, \a val4, \a val5, \a val6, \a val7, \a val8,
  905. and \a val9) to the \a member function.
  906. QGenericArgument and QGenericReturnArgument are internal
  907. helper classes. Because signals and slots can be dynamically
  908. invoked, you must enclose the arguments using the Q_ARG() and
  909. Q_RETURN_ARG() macros. Q_ARG() takes a type name and a
  910. const reference of that type; Q_RETURN_ARG() takes a type name
  911. and a non-const reference.
  912. You only need to pass the name of the signal or slot to this function,
  913. not the entire signature. For example, to asynchronously invoke
  914. the \l{QPushButton::animateClick()}{animateClick()} slot on a
  915. QPushButton, use the following code:
  916. \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 2
  917. With asynchronous method invocations, the parameters must be of
  918. types that are known to Qt's meta-object system, because Qt needs
  919. to copy the arguments to store them in an event behind the
  920. scenes. If you try to use a queued connection and get the error
  921. message
  922. \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 3
  923. call qRegisterMetaType() to register the data type before you
  924. call invokeMethod().
  925. To synchronously invoke the \c compute(QString, int, double) slot on
  926. some arbitrary object \c obj retrieve its return value:
  927. \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 4
  928. If the "compute" slot does not take exactly one QString, one int
  929. and one double in the specified order, the call will fail.
  930. \sa Q_ARG(), Q_RETURN_ARG(), qRegisterMetaType(), QMetaMethod::invoke()
  931. */
  932. bool QMetaObject::invokeMethod(QObject *obj,
  933. const char *member,
  934. Qt::ConnectionType type,
  935. QGenericReturnArgument ret,
  936. QGenericArgument val0,
  937. QGenericArgument val1,
  938. QGenericArgument val2,
  939. QGenericArgument val3,
  940. QGenericArgument val4,
  941. QGenericArgument val5,
  942. QGenericArgument val6,
  943. QGenericArgument val7,
  944. QGenericArgument val8,
  945. QGenericArgument val9)
  946. {
  947. if (!obj)
  948. return false;
  949. QVarLengthArray<char, 512> sig;
  950. int len = qstrlen(member);
  951. if (len <= 0)
  952. return false;
  953. sig.append(member, len);
  954. sig.append('(');
  955. const char *typeNames[] = {ret.name(), val0.name(), val1.name(), val2.name(), val3.name(),
  956. val4.name(), val5.name(), val6.name(), val7.name(), val8.name(),
  957. val9.name()};
  958. int paramCount;
  959. for (paramCount = 1; paramCount < MaximumParamCount; ++paramCount) {
  960. len = qstrlen(typeNames[paramCount]);
  961. if (len <= 0)
  962. break;
  963. sig.append(typeNames[paramCount], len);
  964. sig.append(',');
  965. }
  966. if (paramCount == 1)
  967. sig.append(')'); // no parameters
  968. else
  969. sig[sig.size() - 1] = ')';
  970. sig.append('\0');
  971. int idx = obj->metaObject()->indexOfMethod(sig.constData());
  972. if (idx < 0) {
  973. QByteArray norm = QMetaObject::normalizedSignature(sig.constData());
  974. idx = obj->metaObject()->indexOfMethod(norm.constData());
  975. }
  976. if (idx < 0 || idx >= obj->metaObject()->methodCount()) {
  977. qWarning("QMetaObject::invokeMethod: No such method %s::%s",
  978. obj->metaObject()->className(), sig.constData());
  979. return false;
  980. }
  981. QMetaMethod method = obj->metaObject()->method(idx);
  982. return method.invoke(obj, type, ret,
  983. val0, val1, val2, val3, val4, val5, val6, val7, val8, val9);
  984. }
  985. /*! \fn bool QMetaObject::invokeMethod(QObject *obj, const char *member,
  986. QGenericReturnArgument ret,
  987. QGenericArgument val0 = QGenericArgument(0),
  988. QGenericArgument val1 = QGenericArgument(),
  989. QGenericArgument val2 = QGenericArgument(),
  990. QGenericArgument val3 = QGenericArgument(),
  991. QGenericArgument val4 = QGenericArgument(),
  992. QGenericArgument val5 = QGenericArgument(),
  993. QGenericArgument val6 = QGenericArgument(),
  994. QGenericArgument val7 = QGenericArgument(),
  995. QGenericArgument val8 = QGenericArgument(),
  996. QGenericArgument val9 = QGenericArgument());
  997. \overload invokeMethod()
  998. This overload always invokes the member using the connection type Qt::AutoConnection.
  999. */
  1000. /*! \fn bool QMetaObject::invokeMethod(QObject *obj, const char *member,
  1001. Qt::ConnectionType type,
  1002. QGenericArgument val0 = QGenericArgument(0),
  1003. QGenericArgument val1 = QGenericArgument(),
  1004. QGenericArgument val2 = QGenericArgument(),
  1005. QGenericArgument val3 = QGenericArgument(),
  1006. QGenericArgument val4 = QGenericArgument(),
  1007. QGenericArgument val5 = QGenericArgument(),
  1008. QGenericArgument val6 = QGenericArgument(),
  1009. QGenericArgument val7 = QGenericArgument(),
  1010. QGenericArgument val8 = QGenericArgument(),
  1011. QGenericArgument val9 = QGenericArgument())
  1012. \overload invokeMethod()
  1013. This overload can be used if the return value of the member is of no interest.
  1014. */
  1015. /*!
  1016. \fn bool QMetaObject::invokeMethod(QObject *obj, const char *member,
  1017. QGenericArgument val0 = QGenericArgument(0),
  1018. QGenericArgument val1 = QGenericArgument(),
  1019. QGenericArgument val2 = QGenericArgument(),
  1020. QGenericArgument val3 = QGenericArgument(),
  1021. QGenericArgument val4 = QGenericArgument(),
  1022. QGenericArgument val5 = QGenericArgument(),
  1023. QGenericArgument val6 = QGenericArgument(),
  1024. QGenericArgument val7 = QGenericArgument(),
  1025. QGenericArgument val8 = QGenericArgument(),
  1026. QGenericArgument val9 = QGenericArgument())
  1027. \overload invokeMethod()
  1028. This overload invokes the member using the connection type Qt::AutoConnection and
  1029. ignores return values.
  1030. */
  1031. /*!
  1032. \class QMetaMethod
  1033. \brief The QMetaMethod class provides meta-data about a member
  1034. function.
  1035. \ingroup objectmodel
  1036. A QMetaMethod has a methodType(), a signature(), a list of
  1037. parameterTypes() and parameterNames(), a return typeName(), a
  1038. tag(), and an access() specifier. You can use invoke() to invoke
  1039. the method on an arbitrary QObject.
  1040. \sa QMetaObject, QMetaEnum, QMetaProperty, {Qt's Property System}
  1041. */
  1042. /*!
  1043. \enum QMetaMethod::Attributes
  1044. \internal
  1045. \value Compatibility
  1046. \value Cloned
  1047. \value Scriptable
  1048. */
  1049. /*!
  1050. \fn const QMetaObject *QMetaMethod::enclosingMetaObject() const
  1051. \internal
  1052. */
  1053. /*!
  1054. \enum QMetaMethod::MethodType
  1055. \value Method The function is a plain member function.
  1056. \value Signal The function is a signal.
  1057. \value Slot The function is a slot.
  1058. \value Constructor The function is a constructor.
  1059. */
  1060. /*!
  1061. \fn QMetaMethod::QMetaMethod()
  1062. \internal
  1063. */
  1064. /*!
  1065. Returns the signature of this method (e.g.,
  1066. \c{setValue(double)}).
  1067. \sa parameterTypes(), parameterNames()
  1068. */
  1069. const char *QMetaMethod::signature() const
  1070. {
  1071. if (!mobj)
  1072. return 0;
  1073. return mobj->d.stringdata + mobj->d.data[handle];
  1074. }
  1075. /*!
  1076. Returns a list of parameter types.
  1077. \sa parameterNames(), signature()
  1078. */
  1079. QList<QByteArray> QMetaMethod::parameterTypes() const
  1080. {
  1081. QList<QByteArray> list;
  1082. if (!mobj)
  1083. return list;
  1084. const char *signature = mobj->d.stringdata + mobj->d.data[handle];
  1085. while (*signature && *signature != '(')
  1086. ++signature;
  1087. while (*signature && *signature != ')' && *++signature != ')') {
  1088. const char *begin = signature;
  1089. int level = 0;
  1090. while (*signature && (level > 0 || *signature != ',') && *signature != ')') {
  1091. if (*signature == '<')
  1092. ++level;
  1093. else if (*signature == '>')
  1094. --level;
  1095. ++signature;
  1096. }
  1097. list += QByteArray(begin, signature - begin);
  1098. }
  1099. return list;
  1100. }
  1101. /*!
  1102. Returns a list of parameter names.
  1103. \sa parameterTypes(), signature()
  1104. */
  1105. QList<QByteArray> QMetaMethod::parameterNames() const
  1106. {
  1107. QList<QByteArray> list;
  1108. if (!mobj)
  1109. return list;
  1110. const char *names = mobj->d.stringdata + mobj->d.data[handle + 1];
  1111. if (*names == 0) {
  1112. // do we have one or zero arguments?
  1113. const char *signature = mobj->d.stringdata + mobj->d.data[handle];
  1114. while (*signature && *signature != '(')
  1115. ++signature;
  1116. if (*++signature != ')')
  1117. list += QByteArray();
  1118. } else {
  1119. --names;
  1120. do {
  1121. const char *begin = ++names;
  1122. while (*names && *names != ',')
  1123. ++names;
  1124. list += QByteArray(begin, names - begin);
  1125. } while (*names);
  1126. }
  1127. return list;
  1128. }
  1129. /*!
  1130. Returns the return type of this method, or an empty string if the
  1131. return type is \e void.
  1132. */
  1133. const char *QMetaMethod::typeName() const
  1134. {
  1135. if (!mobj)
  1136. return 0;
  1137. return mobj->d.stringdata + mobj->d.data[handle + 2];
  1138. }
  1139. /*!
  1140. Returns the tag associated with this method.
  1141. Tags are special macros recognized by \c moc that make it
  1142. possible to add extra information about a method. For the moment,
  1143. \c moc doesn't support any special tags.
  1144. */
  1145. const char *QMetaMethod::tag() const
  1146. {
  1147. if (!mobj)
  1148. return 0;
  1149. return mobj->d.stringdata + mobj->d.data[handle + 3];
  1150. }
  1151. /*! \internal */
  1152. int QMetaMethod::attributes() const
  1153. {
  1154. if (!mobj)
  1155. return false;
  1156. return ((mobj->d.data[handle + 4])>>4);
  1157. }
  1158. /*!
  1159. \since 4.6
  1160. Returns this method's index.
  1161. */
  1162. int QMetaMethod::methodIndex() const
  1163. {
  1164. if (!mobj)
  1165. return -1;
  1166. return ((handle - priv(mobj->d.data)->methodData) / 5) + mobj->methodOffset();
  1167. }
  1168. /*!
  1169. Returns the access specification of this method (private,
  1170. protected, or public).
  1171. Signals are always protected, meaning that you can only emit them
  1172. from the class or from a subclass.
  1173. \sa methodType()
  1174. */
  1175. QMetaMethod::Access QMetaMethod::access() const
  1176. {
  1177. if (!mobj)
  1178. return Private;
  1179. return (QMetaMethod::Access)(mobj->d.data[handle + 4] & AccessMask);
  1180. }
  1181. /*!
  1182. Returns the type of this method (signal, slot, or method).
  1183. \sa access()
  1184. */
  1185. QMetaMethod::MethodType QMetaMethod::methodType() const
  1186. {
  1187. if (!mobj)
  1188. return QMetaMethod::Method;
  1189. return (QMetaMethod::MethodType)((mobj->d.data[handle + 4] & MethodTypeMask)>>2);
  1190. }
  1191. /*!
  1192. Invokes this method on the object \a object. Returns true if the member could be invoked.
  1193. Returns false if there is no such member or the parameters did not match.
  1194. The invocation can be either synchronous or asynchronous, depending on the
  1195. \a connectionType:
  1196. \list
  1197. \o If \a connectionType is Qt::DirectConnection, the member will be invoked immediately.
  1198. \o If \a connectionType is Qt::QueuedConnection,
  1199. a QEvent will be posted and the member is invoked as soon as the application
  1200. enters the main event loop.
  1201. \o If \a connectionType is Qt::AutoConnection, the member is invoked
  1202. synchronously if \a object lives in the same thread as the
  1203. caller; otherwise it will invoke the member asynchronously.
  1204. \endlist
  1205. The return value of this method call is placed in \a
  1206. returnValue. If the invocation is asynchronous, the return value cannot
  1207. be evaluated. You can pass up to ten arguments (\a val0, \a val1,
  1208. \a val2, \a val3, \a val4, \a val5, \a val6, \a val7, \a val8,
  1209. and \a val9) to this method call.
  1210. QGenericArgument and QGenericReturnArgument are internal
  1211. helper classes. Because signals and slots can be dynamically
  1212. invoked, you must enclose the arguments using the Q_ARG() and
  1213. Q_RETURN_ARG() macros. Q_ARG() takes a type name and a
  1214. const reference of that type; Q_RETURN_ARG() takes a type name
  1215. and a non-const reference.
  1216. To asynchronously invoke the
  1217. \l{QPushButton::animateClick()}{animateClick()} slot on a
  1218. QPushButton:
  1219. \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 6
  1220. With asynchronous method invocations, the parameters must be of
  1221. types that are known to Qt's meta-object system, because Qt needs
  1222. to copy the arguments to store them in an event behind the
  1223. scenes. If you try to use a queued connection and get the error
  1224. message
  1225. \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 7
  1226. call qRegisterMetaType() to register the data type before you
  1227. call QMetaMethod::invoke().
  1228. To synchronously invoke the \c compute(QString, int, double) slot on
  1229. some arbitrary object \c obj retrieve its return value:
  1230. \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 8
  1231. QMetaObject::normalizedSignature() is used here to ensure that the format
  1232. of the signature is what invoke() expects. E.g. extra whitespace is
  1233. removed.
  1234. If the "compute" slot does not take exactly one QString, one int
  1235. and one double in the specified order, the call will fail.
  1236. \warning this method will not test the validity of the arguments: \a object
  1237. must be an instance of the class of the QMetaObject of which this QMetaMethod
  1238. has been constructed with. The arguments must have the same type as the ones
  1239. expected by the method, else, the behaviour is undefined.
  1240. \sa Q_ARG(), Q_RETURN_ARG(), qRegisterMetaType(), QMetaObject::invokeMethod()
  1241. */
  1242. bool QMetaMethod::invoke(QObject *object,
  1243. Qt::ConnectionType connectionType,
  1244. QGenericReturnArgument returnValue,
  1245. QGenericArgument val0,
  1246. QGenericArgument val1,
  1247. QGenericArgument val2,
  1248. QGenericArgument val3,
  1249. QGenericArgument val4,
  1250. QGenericArgument val5,
  1251. QGenericArgument val6,
  1252. QGenericArgument val7,
  1253. QGenericArgument val8,
  1254. QGenericArgument val9) const
  1255. {
  1256. if (!object || !mobj)
  1257. return false;
  1258. Q_ASSERT(mobj->cast(object));
  1259. // check return type
  1260. if (returnValue.data()) {
  1261. const char *retType = typeName();
  1262. if (qstrcmp(returnValue.name(), retType) != 0) {
  1263. // normalize the return value as well
  1264. // the trick here is to make a function signature out of the return type
  1265. // so that we can call normalizedSignature() and avoid duplicating code
  1266. QByteArray unnormalized;
  1267. int len = qstrlen(returnValue.name());
  1268. unnormalized.reserve(len + 3);
  1269. unnormalized = "_("; // the function is called "_"
  1270. unnormalized.append(returnValue.name());
  1271. unnormalized.append(')');
  1272. QByteArray normalized = QMetaObject::normalizedSignature(unnormalized.constData());
  1273. normalized.truncate(normalized.length() - 1); // drop the ending ')'
  1274. if (qstrcmp(normalized.constData() + 2, retType) != 0)
  1275. return false;
  1276. }
  1277. }
  1278. // check argument count (we don't allow invoking a method if given too few arguments)
  1279. const char *typeNames[] = {
  1280. returnValue.name(),
  1281. val0.name(),
  1282. val1.name(),
  1283. val2.name(),
  1284. val3.name(),
  1285. val4.name(),
  1286. val5.name(),
  1287. val6.name(),
  1288. val7.name(),
  1289. val8.name(),
  1290. val9.name()
  1291. };
  1292. int paramCount;
  1293. for (paramCount = 1; paramCount < MaximumParamCount; ++paramCount) {
  1294. if (qstrlen(typeNames[paramCount]) <= 0)
  1295. break;
  1296. }
  1297. int metaMethodArgumentCount = 0;
  1298. {
  1299. // based on QMetaObject::parameterNames()
  1300. const char *names = mobj->d.stringdata + mobj->d.data[handle + 1];
  1301. if (*names == 0) {
  1302. // do we have one or zero arguments?
  1303. const char *signature = mobj->d.stringdata + mobj->d.data[handle];
  1304. while (*signature && *signature != '(')
  1305. ++signature;
  1306. if (*++signature != ')')
  1307. ++metaMethodArgumentCount;
  1308. } else {
  1309. --names;
  1310. do {
  1311. ++names;
  1312. while (*names && *names != ',')
  1313. ++names;
  1314. ++metaMethodArgumentCount;
  1315. } while (*names);
  1316. }
  1317. }
  1318. if (paramCount <= metaMethodArgumentCount)
  1319. return false;
  1320. // check connection type
  1321. QThread *currentThread = QThread::currentThread();
  1322. QThread *objectThread = object->thread();
  1323. if (connectionType == Qt::AutoConnection) {
  1324. connectionType = currentThread == objectThread
  1325. ? Qt::DirectConnection
  1326. : Qt::QueuedConnection;
  1327. }
  1328. // invoke!
  1329. void *param[] = {
  1330. returnValue.data(),
  1331. val0.data(),
  1332. val1.data(),
  1333. val2.data(),
  1334. val3.data(),
  1335. val4.data(),
  1336. val5.data(),
  1337. val6.data(),
  1338. val7.data(),
  1339. val8.data(),
  1340. val9.data()
  1341. };
  1342. // recompute the methodInde…

Large files files are truncated, but you can click here to view the full file