PageRenderTime 54ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/src/corelib/kernel/qmetaobject.cpp

https://bitbucket.org/ultra_iter/qt-vtl
C++ | 2783 lines | 1371 code | 197 blank | 1215 comment | 433 complexity | d3716d0f8550989b9c5bbdaa8f8e8cf1 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-3.0, BSD-3-Clause, CC0-1.0, CC-BY-SA-4.0, LGPL-2.1, GPL-3.0, Apache-2.0

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

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

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