PageRenderTime 60ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  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 methodIndex by reversing the arithmetic in QMetaObject::property()
  1343. int methodIndex = ((handle - priv(mobj->d.data)->methodData) / 5) + mobj->methodOffset();
  1344. if (connectionType == Qt::DirectConnection) {
  1345. return QMetaObject::metacall(object, QMetaObject::InvokeMetaMethod, methodIndex, param) < 0;
  1346. } else {
  1347. if (returnValue.data()) {
  1348. qWarning("QMetaMethod::invoke: Unable to invoke methods with return values in "
  1349. "queued connections");
  1350. return false;
  1351. }
  1352. int nargs = 1; // include return type
  1353. void **args = (void **) qMalloc(paramCount * sizeof(void *));
  1354. Q_CHECK_PTR(args);
  1355. int *types = (int *) qMalloc(paramCount * sizeof(int));
  1356. Q_CHECK_PTR(types);
  1357. types[0] = 0; // return type
  1358. args[0] = 0;
  1359. for (int i = 1; i < paramCount; ++i) {
  1360. types[i] = QMetaType::type(typeNames[i]);
  1361. if (types[i]) {
  1362. args[i] = QMetaType::construct(types[i], param[i]);
  1363. ++nargs;
  1364. } else if (param[i]) {
  1365. qWarning("QMetaMethod::invoke: Unable to handle unregistered datatype '%s'",
  1366. typeNames[i]);
  1367. for (int x = 1; x < i; ++x) {
  1368. if (types[x] && args[x])
  1369. QMetaType::destroy(types[x], args[x]);
  1370. }
  1371. qFree(types);
  1372. qFree(args);
  1373. return false;
  1374. }
  1375. }
  1376. if (connectionType == Qt::QueuedConnection) {
  1377. QCoreApplication::postEvent(object, new QMetaCallEvent(methodIndex,
  1378. 0,
  1379. -1,
  1380. nargs,
  1381. types,
  1382. args));
  1383. } else {
  1384. if (currentThread == objectThread) {
  1385. qWarning("QMetaMethod::invoke: Dead lock detected in "
  1386. "BlockingQueuedConnection: Receiver is %s(%p)",
  1387. mobj->className(), object);
  1388. }
  1389. // blocking queued connection
  1390. #ifdef QT_NO_THREAD
  1391. QCoreApplication::postEvent(object, new QMetaCallEvent(methodIndex,
  1392. 0,
  1393. -1,
  1394. nargs,
  1395. types,
  1396. args));
  1397. #else
  1398. QSemaphore semaphore;
  1399. QCoreApplication::postEvent(object, new QMetaCallEvent(methodIndex,
  1400. 0,
  1401. -1,
  1402. nargs,
  1403. types,
  1404. args,
  1405. &semaphore));
  1406. semaphore.acquire();
  1407. #endif // QT_NO_THREAD
  1408. }
  1409. }
  1410. return true;
  1411. }
  1412. /*! \fn bool QMetaMethod::invoke(QObject *object,
  1413. QGenericReturnArgument returnValue,
  1414. QGenericArgument val0 = QGenericArgument(0),
  1415. QGenericArgument val1 = QGenericArgument(),
  1416. QGenericArgument val2 = QGenericArgument(),
  1417. QGenericArgument val3 = QGenericArgument(),
  1418. QGenericArgument val4 = QGenericArgument(),
  1419. QGenericArgument val5 = QGenericArgument(),
  1420. QGenericArgument val6 = QGenericArgument(),
  1421. QGenericArgument val7 = QGenericArgument(),
  1422. QGenericArgument val8 = QGenericArgument(),
  1423. QGenericArgument val9 = QGenericArgument()) const
  1424. \overload invoke()
  1425. This overload always invokes this method using the connection type Qt::AutoConnection.
  1426. */
  1427. /*! \fn bool QMetaMethod::invoke(QObject *object,
  1428. Qt::ConnectionType connectionType,
  1429. QGenericArgument val0 = QGenericArgument(0),
  1430. QGenericArgument val1 = QGenericArgument(),
  1431. QGenericArgument val2 = QGenericArgument(),
  1432. QGenericArgument val3 = QGenericArgument(),
  1433. QGenericArgument val4 = QGenericArgument(),
  1434. QGenericArgument val5 = QGenericArgument(),
  1435. QGenericArgument val6 = QGenericArgument(),
  1436. QGenericArgument val7 = QGenericArgument(),
  1437. QGenericArgument val8 = QGenericArgument(),
  1438. QGenericArgument val9 = QGenericArgument()) const
  1439. \overload invoke()
  1440. This overload can be used if the return value of the member is of no interest.
  1441. */
  1442. /*!
  1443. \fn bool QMetaMethod::invoke(QObject *object,
  1444. QGenericArgument val0 = QGenericArgument(0),
  1445. QGenericArgument val1 = QGenericArgument(),
  1446. QGenericArgument val2 = QGenericArgument(),
  1447. QGenericArgument val3 = QGenericArgument(),
  1448. QGenericArgument val4 = QGenericArgument(),
  1449. QGenericArgument val5 = QGenericArgument(),
  1450. QGenericArgument val6 = QGenericArgument(),
  1451. QGenericArgument val7 = QGenericArgument(),
  1452. QGenericArgument val8 = QGenericArgument(),
  1453. QGenericArgument val9 = QGenericArgument()) const
  1454. \overload invoke()
  1455. This overload invokes this method using the
  1456. connection type Qt::AutoConnection and ignores return values.
  1457. */
  1458. /*!
  1459. \class QMetaEnum
  1460. \brief The QMetaEnum class provides meta-data about an enumerator.
  1461. \ingroup objectmodel
  1462. Use name() for the enumerator's name. The enumerator's keys (names
  1463. of each enumerated item) are returned by key(); use keyCount() to find
  1464. the number of keys. isFlag() returns whether the enumerator is
  1465. meant to be used as a flag, meaning that its values can be combined
  1466. using the OR operator.
  1467. The conversion functions keyToValue(), valueToKey(), keysToValue(),
  1468. and valueToKeys() allow conversion between the integer
  1469. representation of an enumeration or set value and its literal
  1470. representation. The scope() function returns the class scope this
  1471. enumerator was declared in.
  1472. \sa QMetaObject, QMetaMethod, QMetaProperty
  1473. */
  1474. /*!
  1475. \fn bool QMetaEnum::isValid() const
  1476. Returns true if this enum is valid (has a name); otherwise returns
  1477. false.
  1478. \sa name()
  1479. */
  1480. /*!
  1481. \fn const QMetaObject *QMetaEnum::enclosingMetaObject() const
  1482. \internal
  1483. */
  1484. /*!
  1485. \fn QMetaEnum::QMetaEnum()
  1486. \internal
  1487. */
  1488. /*!
  1489. Returns the name of the enumerator (without the scope).
  1490. For example, the Qt::AlignmentFlag enumeration has \c
  1491. AlignmentFlag as the name and \l Qt as the scope.
  1492. \sa isValid(), scope()
  1493. */
  1494. const char *QMetaEnum::name() const
  1495. {
  1496. if (!mobj)
  1497. return 0;
  1498. return mobj->d.stringdata + mobj->d.data[handle];
  1499. }
  1500. /*!
  1501. Returns the number of keys.
  1502. \sa key()
  1503. */
  1504. int QMetaEnum::keyCount() const
  1505. {
  1506. if (!mobj)
  1507. return 0;
  1508. return mobj->d.data[handle + 2];
  1509. }
  1510. /*!
  1511. Returns the key with the given \a index, or 0 if no such key exists.
  1512. \sa keyCount(), value(), valueToKey()
  1513. */
  1514. const char *QMetaEnum::key(int index) const
  1515. {
  1516. if (!mobj)
  1517. return 0;
  1518. int count = mobj->d.data[handle + 2];
  1519. int data = mobj->d.data[handle + 3];
  1520. if (index >= 0 && index < count)
  1521. return mobj->d.stringdata + mobj->d.data[data + 2*index];
  1522. return 0;
  1523. }
  1524. /*!
  1525. Returns the value with the given \a index; or returns -1 if there
  1526. is no such value.
  1527. \sa keyCount(), key(), keyToValue()
  1528. */
  1529. int QMetaEnum::value(int index) const
  1530. {
  1531. if (!mobj)
  1532. return 0;
  1533. int count = mobj->d.data[handle + 2];
  1534. int data = mobj->d.data[handle + 3];
  1535. if (index >= 0 && index < count)
  1536. return mobj->d.data[data + 2*index + 1];
  1537. return -1;
  1538. }
  1539. /*!
  1540. Returns true if this enumerator is used as a flag; otherwise returns
  1541. false.
  1542. When used as flags, enumerators can be combined using the OR
  1543. operator.
  1544. \sa keysToValue(), valueToKeys()
  1545. */
  1546. bool QMetaEnum::isFlag() const
  1547. {
  1548. return mobj && mobj->d.data[handle + 1];
  1549. }
  1550. /*!
  1551. Returns the scope this enumerator was declared in.
  1552. For example, the Qt::AlignmentFlag enumeration has \c Qt as
  1553. the scope and \c AlignmentFlag as the name.
  1554. \sa name()
  1555. */
  1556. const char *QMetaEnum::scope() const
  1557. {
  1558. return mobj?mobj->d.stringdata : 0;
  1559. }
  1560. /*!
  1561. Returns the integer value of the given enumeration \a key, or -1
  1562. if \a key is not defined.
  1563. For flag types, use keysToValue().
  1564. \sa valueToKey(), isFlag(), keysToValue()
  1565. */
  1566. int QMetaEnum::keyToValue(const char *key) const
  1567. {
  1568. if (!mobj || !key)
  1569. return -1;
  1570. uint scope = 0;
  1571. const char *qualified_key = key;
  1572. const char *s = key + qstrlen(key);
  1573. while (s > key && *s != ':')
  1574. --s;
  1575. if (s > key && *(s-1)==':') {
  1576. scope = s - key - 1;
  1577. key += scope + 2;
  1578. }
  1579. int count = mobj->d.data[handle + 2];
  1580. int data = mobj->d.data[handle + 3];
  1581. for (int i = 0; i < count; ++i)
  1582. if ((!scope || (qstrlen(mobj->d.stringdata) == scope && strncmp(qualified_key, mobj->d.stringdata, scope) == 0))
  1583. && strcmp(key, mobj->d.stringdata + mobj->d.data[data + 2*i]) == 0)
  1584. return mobj->d.data[data + 2*i + 1];
  1585. return -1;
  1586. }
  1587. /*!
  1588. Returns the string that is used as the name of the given
  1589. enumeration \a value, or 0 if \a value is not defined.
  1590. For flag types, use valueToKeys().
  1591. \sa isFlag(), valueToKeys()
  1592. */
  1593. const char* QMetaEnum::valueToKey(int value) const
  1594. {
  1595. if (!mobj)
  1596. return 0;
  1597. int count = mobj->d.data[handle + 2];
  1598. int data = mobj->d.data[handle + 3];
  1599. for (int i = 0; i < count; ++i)
  1600. if (value == (int)mobj->d.data[data + 2*i + 1])
  1601. return mobj->d.stringdata + mobj->d.data[data + 2*i];
  1602. return 0;
  1603. }
  1604. /*!
  1605. Returns the value derived from combining together the values of
  1606. the \a keys using the OR operator, or -1 if \a keys is not
  1607. defined. Note that the strings in \a keys must be '|'-separated.
  1608. \sa isFlag(), valueToKey(), valueToKeys()
  1609. */
  1610. int QMetaEnum::keysToValue(const char *keys) const
  1611. {
  1612. if (!mobj)
  1613. return -1;
  1614. QStringList l = QString::fromLatin1(keys).split(QLatin1Char('|'));
  1615. //#### TODO write proper code, do not use QStringList
  1616. int value = 0;
  1617. int count = mobj->d.data[handle + 2];
  1618. int data = mobj->d.data[handle + 3];
  1619. for (int li = 0; li < l.size(); ++li) {
  1620. QString trimmed = l.at(li).trimmed();
  1621. QByteArray qualified_key = trimmed.toLatin1();
  1622. const char *key = qualified_key.constData();
  1623. uint scope = 0;
  1624. const char *s = key + qstrlen(key);
  1625. while (s > key && *s != ':')
  1626. --s;
  1627. if (s > key && *(s-1)==':') {
  1628. scope = s - key - 1;
  1629. key += scope + 2;
  1630. }
  1631. int i;
  1632. for (i = count-1; i >= 0; --i)
  1633. if ((!scope || (qstrlen(mobj->d.stringdata) == scope && strncmp(qualified_key.constData(), mobj->d.stringdata, scope) == 0))
  1634. && strcmp(key, mobj->d.stringdata + mobj->d.data[data + 2*i]) == 0) {
  1635. value |= mobj->d.data[data + 2*i + 1];
  1636. break;
  1637. }
  1638. if (i < 0)
  1639. value |= -1;
  1640. }
  1641. return value;
  1642. }
  1643. /*!
  1644. Returns a byte array of '|'-separated keys that represents the
  1645. given \a value.
  1646. \sa isFlag(), valueToKey(), keysToValue()
  1647. */
  1648. QByteArray QMetaEnum::valueToKeys(int value) const
  1649. {
  1650. QByteArray keys;
  1651. if (!mobj)
  1652. return keys;
  1653. int count = mobj->d.data[handle + 2];
  1654. int data = mobj->d.data[handle + 3];
  1655. int v = value;
  1656. for(int i = 0; i < count; i++) {
  1657. int k = mobj->d.data[data + 2*i + 1];
  1658. if ((k != 0 && (v & k) == k ) || (k == value)) {
  1659. v = v & ~k;
  1660. if (!keys.isEmpty())
  1661. keys += '|';
  1662. keys += mobj->d.stringdata + mobj->d.data[data + 2*i];
  1663. }
  1664. }
  1665. return keys;
  1666. }
  1667. static QByteArray qualifiedName(const QMetaEnum &e)
  1668. {
  1669. return QByteArray(e.scope()) + "::" + e.name();
  1670. }
  1671. /*!
  1672. \class QMetaProperty
  1673. \brief The QMetaProperty class provides meta-data about a property.
  1674. \ingroup objectmodel
  1675. Property meta-data is obtained from an object's meta-object. See
  1676. QMetaObject::property() and QMetaObject::propertyCount() for
  1677. details.
  1678. \section1 Property Meta-Data
  1679. A property has a name() and a type(), as well as various
  1680. attributes that specify its behavior: isReadable(), isWritable(),
  1681. isDesignable(), isScriptable(), and isStored().
  1682. If the property is an enumeration, isEnumType() returns true; if the
  1683. property is an enumeration that is also a flag (i.e. its values
  1684. can be combined using the OR operator), isEnumType() and
  1685. isFlagType() both return true. The enumerator for these types is
  1686. available from enumerator().
  1687. The property's values are set and retrieved with read(), write(),
  1688. and reset(); they can also be changed through QObject's set and get
  1689. functions. See QObject::setProperty() and QObject::property() for
  1690. details.
  1691. \section1 Copying and Assignment
  1692. QMetaProperty objects can be copied by value. However, each copy will
  1693. refer to the same underlying property meta-data.
  1694. \sa QMetaObject, QMetaEnum, QMetaMethod, {Qt's Property System}
  1695. */
  1696. /*!
  1697. \fn bool QMetaProperty::isValid() const
  1698. Returns true if this property is valid (readable); otherwise
  1699. returns false.
  1700. \sa isReadable()
  1701. */
  1702. /*!
  1703. \fn const QMetaObject *QMetaProperty::enclosingMetaObject() const
  1704. \internal
  1705. */
  1706. /*!
  1707. \internal
  1708. */
  1709. QMetaProperty::QMetaProperty()
  1710. : mobj(0), handle(0), idx(0)
  1711. {
  1712. }
  1713. /*!
  1714. Returns this property's name.
  1715. \sa type(), typeName()
  1716. */
  1717. const char *QMetaProperty::name() const
  1718. {
  1719. if (!mobj)
  1720. return 0;
  1721. int handle = priv(mobj->d.data)->propertyData + 3*idx;
  1722. return mobj->d.stringdata + mobj->d.data[handle];
  1723. }
  1724. /*!
  1725. Returns the name of this property's type.
  1726. \sa type(), name()
  1727. */
  1728. const char *QMetaProperty::typeName() const
  1729. {
  1730. if (!mobj)
  1731. return 0;
  1732. int handle = priv(mobj->d.data)->propertyData + 3*idx;
  1733. return mobj->d.stringdata + mobj->d.data[handle + 1];
  1734. }
  1735. /*!
  1736. Returns this property's type. The return value is one
  1737. of the values of the QVariant::Type enumeration.
  1738. \sa userType(), typeName(), name()
  1739. */
  1740. QVariant::Type QMetaProperty::type() const
  1741. {
  1742. if (!mobj)
  1743. return QVariant::Invalid;
  1744. int handle = priv(mobj->d.data)->propertyData + 3*idx;
  1745. uint flags = mobj->d.data[handle + 2];
  1746. uint type = flags >> 24;
  1747. if (type == 0xff) // special value for QVariant
  1748. type = QVariant::LastType;
  1749. if (type)
  1750. return QVariant::Type(type);
  1751. if (isEnumType()) {
  1752. int enumMetaTypeId = QMetaType::type(qualifiedName(menum));
  1753. if (enumMetaTypeId == 0)
  1754. return QVariant::Int;
  1755. }
  1756. #ifdef QT_COORD_TYPE
  1757. // qreal metatype must be resolved at runtime.
  1758. if (strcmp(typeName(), "qreal") == 0)
  1759. return QVariant::Type(qMetaTypeId<qreal>());
  1760. #endif
  1761. return QVariant::UserType;
  1762. }
  1763. /*!
  1764. \since 4.2
  1765. Returns this property's user type. The return value is one
  1766. of the values that are registered with QMetaType, or 0 if
  1767. the type is not registered.
  1768. \sa type(), QMetaType, typeName()
  1769. */
  1770. int QMetaProperty::userType() const
  1771. {
  1772. QVariant::Type tp = type();
  1773. if (tp != QVariant::UserType)
  1774. return tp;
  1775. if (isEnumType()) {
  1776. int enumMetaTypeId = QMetaType::type(qualifiedName(menum));
  1777. return enumMetaTypeId;
  1778. }
  1779. return QMetaType::type(typeName());
  1780. }
  1781. /*!
  1782. \since 4.6
  1783. Returns this property's index.
  1784. */
  1785. int QMetaProperty::propertyIndex() const
  1786. {
  1787. if (!mobj)
  1788. return -1;
  1789. return idx + mobj->propertyOffset();
  1790. }
  1791. /*!
  1792. Returns true if the property's type is an enumeration value that
  1793. is used as a flag; otherwise returns false.
  1794. Flags can be combined using the OR operator. A flag type is
  1795. implicitly also an enum type.
  1796. \sa isEnumType(), enumerator(), QMetaEnum::isFlag()
  1797. */
  1798. bool QMetaProperty::isFlagType() const
  1799. {
  1800. return isEnumType() && menum.isFlag();
  1801. }
  1802. /*!
  1803. Returns true if the property's type is an enumeration value;
  1804. otherwise returns false.
  1805. \sa enumerator(), isFlagType()
  1806. */
  1807. bool QMetaProperty::isEnumType() const
  1808. {
  1809. if (!mobj)
  1810. return false;
  1811. int handle = priv(mobj->d.data)->propertyData + 3*idx;
  1812. int flags = mobj->d.data[handle + 2];
  1813. return (flags & EnumOrFlag) && menum.name();
  1814. }
  1815. /*!
  1816. \internal
  1817. Returns true if the property has a C++ setter function that
  1818. follows Qt's standard "name" / "setName" pattern. Designer and uic
  1819. query hasStdCppSet() in order to avoid expensive
  1820. QObject::setProperty() calls. All properties in Qt [should] follow
  1821. this pattern.
  1822. */
  1823. bool QMetaProperty::hasStdCppSet() const
  1824. {
  1825. if (!mobj)
  1826. return false;
  1827. int handle = priv(mobj->d.data)->propertyData + 3*idx;
  1828. int flags = mobj->d.data[handle + 2];
  1829. return (flags & StdCppSet);
  1830. }
  1831. /*!
  1832. Returns the enumerator if this property's type is an enumerator
  1833. type; otherwise the returned value is undefined.
  1834. \sa isEnumType(), isFlagType()
  1835. */
  1836. QMetaEnum QMetaProperty::enumerator() const
  1837. {
  1838. return menum;
  1839. }
  1840. /*!
  1841. Reads the property's value from the given \a object. Returns the value
  1842. if it was able to read it; otherwise returns an invalid variant.
  1843. \sa write(), reset(), isReadable()
  1844. */
  1845. QVariant QMetaProperty::read(const QObject *object) const
  1846. {
  1847. if (!object || !mobj)
  1848. return QVariant();
  1849. uint t = QVariant::Int;
  1850. if (isEnumType()) {
  1851. /*
  1852. try to create a QVariant that can be converted to this enum
  1853. type (only works if the enum has already been registered
  1854. with QMetaType)
  1855. */
  1856. int enumMetaTypeId = QMetaType::type(qualifiedName(menum));
  1857. if (enumMetaTypeId != 0)
  1858. t = enumMetaTypeId;
  1859. } else {
  1860. int handle = priv(mobj->d.data)->propertyData + 3*idx;
  1861. uint flags = mobj->d.data[handle + 2];
  1862. const char *typeName = mobj->d.stringdata + mobj->d.data[handle + 1];
  1863. t = (flags >> 24);
  1864. if (t == 0xff) // special value for QVariant
  1865. t = QVariant::LastType;
  1866. if (t == QVariant::Invalid)
  1867. t = QMetaType::type(typeName);
  1868. if (t == QVariant::Invalid)
  1869. t = QVariant::nameToType(typeName);
  1870. if (t == QVariant::Invalid || t == QVariant::UserType) {
  1871. if (t == QVariant::Invalid)
  1872. qWarning("QMetaProperty::read: Unable to handle unregistered datatype '%s' for property '%s::%s'", typeName, mobj->className(), name());
  1873. return QVariant();
  1874. }
  1875. }
  1876. // the status variable is changed by qt_metacall to indicate what it did
  1877. // this feature is currently only used by QtDBus and should not be depended
  1878. // upon. Don't change it without looking into QDBusAbstractInterface first
  1879. // -1 (unchanged): normal qt_metacall, result stored in argv[0]
  1880. // changed: result stored directly in value
  1881. int status = -1;
  1882. QVariant value;
  1883. void *argv[] = { 0, &value, &status };
  1884. if (t == QVariant::LastType) {
  1885. argv[0] = &value;
  1886. } else {
  1887. value = QVariant(t, (void*)0);
  1888. argv[0] = value.data();
  1889. }
  1890. QMetaObject::metacall(const_cast<QObject*>(object), QMetaObject::ReadProperty,
  1891. idx + mobj->propertyOffset(), argv);
  1892. if (status != -1)
  1893. return value;
  1894. if (t != QVariant::LastType && argv[0] != value.data())
  1895. // pointer or reference
  1896. return QVariant((QVariant::Type)t, argv[0]);
  1897. return value;
  1898. }
  1899. /*!
  1900. Writes \a value as the property's value to the given \a object. Returns
  1901. true if the write succeeded; otherwise returns false.
  1902. \sa read(), reset(), isWritable()
  1903. */
  1904. bool QMetaProperty::write(QObject *object, const QVariant &value) const
  1905. {
  1906. if (!object || !isWritable())
  1907. return false;
  1908. QVariant v = value;
  1909. uint t = QVariant::Invalid;
  1910. if (isEnumType()) {
  1911. if (v.type() == QVariant::String
  1912. #ifdef QT3_SUPPORT
  1913. || v.type() == QVariant::CString
  1914. #endif
  1915. ) {
  1916. if (isFlagType())
  1917. v = QVariant(menum.keysToValue(value.toByteArray()));
  1918. else
  1919. v = QVariant(menum.keyToValue(value.toByteArray()));
  1920. } else if (v.type() != QVariant::Int && v.type() != QVariant::UInt) {
  1921. int enumMetaTypeId = QMetaType::type(qualifiedName(menum));
  1922. if ((enumMetaTypeId == 0) || (v.userType() != enumMetaTypeId) || !v.constData())
  1923. return false;
  1924. v = QVariant(*reinterpret_cast<const int *>(v.constData()));
  1925. }
  1926. v.convert(QVariant::Int);
  1927. } else {
  1928. int handle = priv(mobj->d.data)->propertyData + 3*idx;
  1929. uint flags = mobj->d.data[handle + 2];
  1930. t = flags >> 24;
  1931. if (t == 0xff) // special value for QVariant
  1932. t = QVariant::LastType;
  1933. if (t == QVariant::Invalid) {
  1934. const char *typeName = mobj->d.stringdata + mobj->d.data[handle + 1];
  1935. const char *vtypeName = value.typeName();
  1936. if (vtypeName && strcmp(typeName, vtypeName) == 0)
  1937. t = value.userType();
  1938. else
  1939. t = QVariant::nameToType(typeName);
  1940. }
  1941. if (t == QVariant::Invalid)
  1942. return false;
  1943. if (t != QVariant::LastType && t != (uint)value.userType() && (t < QMetaType::User && !v.convert((QVariant::Type)t)))
  1944. return false;
  1945. }
  1946. // the status variable is changed by qt_metacall to indicate what it did
  1947. // this feature is currently only used by QtDBus and should not be depended
  1948. // upon. Don't change it without looking into QDBusAbstractInterface first
  1949. // -1 (unchanged): normal qt_metacall, result stored in argv[0]
  1950. // changed: result stored directly in value, return the value of status
  1951. int status = -1;
  1952. // the flags variable is used by the declarative module to implement
  1953. // interception of property writes.
  1954. int flags = 0;
  1955. void *argv[] = { 0, &v, &status, &flags };
  1956. if (t == QVariant::LastType)
  1957. argv[0] = &v;
  1958. else
  1959. argv[0] = v.data();
  1960. QMetaObject::metacall(object, QMetaObject::WriteProperty, idx + mobj->propertyOffset(), argv);
  1961. return status;
  1962. }
  1963. /*!
  1964. Resets the property for the given \a object with a reset method.
  1965. Returns true if the reset worked; otherwise returns false.
  1966. Reset methods are optional; only a few properties support them.
  1967. \sa read(), write()
  1968. */
  1969. bool QMetaProperty::reset(QObject *object) const
  1970. {
  1971. if (!object || !mobj || !isResettable())
  1972. return false;
  1973. void *argv[] = { 0 };
  1974. QMetaObject::metacall(object, QMetaObject::ResetProperty, idx + mobj->propertyOffset(), argv);
  1975. return true;
  1976. }
  1977. /*!
  1978. Returns true if this property can be reset to a default value; otherwise
  1979. returns false.
  1980. \sa reset()
  1981. */
  1982. bool QMetaProperty::isResettable() const
  1983. {
  1984. if (!mobj)
  1985. return false;
  1986. int flags = mobj->d.data[handle + 2];
  1987. return flags & Resettable;
  1988. }
  1989. /*!
  1990. Returns true if this property is readable; otherwise returns false.
  1991. \sa isWritable(), read(), isValid()
  1992. */
  1993. bool QMetaProperty::isReadable() const
  1994. {
  1995. if (!mobj)
  1996. return false;
  1997. int flags = mobj->d.data[handle + 2];
  1998. return flags & Readable;
  1999. }
  2000. /*!
  2001. Returns true if this property has a corresponding change notify signal;
  2002. otherwise returns false.
  2003. \sa notifySignal()
  2004. */
  2005. bool QMetaProperty::hasNotifySignal() const
  2006. {
  2007. if (!mobj)
  2008. return false;
  2009. int flags = mobj->d.data[handle + 2];
  2010. return flags & Notify;
  2011. }
  2012. /*!
  2013. \since 4.5
  2014. Returns the QMetaMethod instance of the property change notifying signal if
  2015. one was specified, otherwise returns an invalid QMetaMethod.
  2016. \sa hasNotifySignal()
  2017. */
  2018. QMetaMethod QMetaProperty::notifySignal() const
  2019. {
  2020. int id = notifySignalIndex();
  2021. if (id != -1)
  2022. return mobj->method(id);
  2023. else
  2024. return QMetaMethod();
  2025. }
  2026. /*!
  2027. \since 4.6
  2028. Returns the index of the property change notifying signal if one was
  2029. specified, otherwise returns -1.
  2030. \sa hasNotifySignal()
  2031. */
  2032. int QMetaProperty::notifySignalIndex() const
  2033. {
  2034. if (hasNotifySignal()) {
  2035. int offset = priv(mobj->d.data)->propertyData +
  2036. priv(mobj->d.data)->propertyCount * 3 + idx;
  2037. return mobj->d.data[offset] + mobj->methodOffset();
  2038. } else {
  2039. return -1;
  2040. }
  2041. }
  2042. /*!
  2043. Returns true if this property is writable; otherwise returns
  2044. false.
  2045. \sa isReadable(), write()
  2046. */
  2047. bool QMetaProperty::isWritable() const
  2048. {
  2049. if (!mobj)
  2050. return false;
  2051. int flags = mobj->d.data[handle + 2];
  2052. return flags & Writable;
  2053. }
  2054. /*!
  2055. Returns true if this property is designable for the given \a object;
  2056. otherwise returns false.
  2057. If no \a object is given, the function returns false if the
  2058. \c{Q_PROPERTY()}'s \c DESIGNABLE attribute is false; otherwise
  2059. returns true (if the attribute is true or is a function or expression).
  2060. \sa isScriptable(), isStored()
  2061. */
  2062. bool QMetaProperty::isDesignable(const QObject *object) const
  2063. {
  2064. if (!mobj)
  2065. return false;
  2066. int flags = mobj->d.data[handle + 2];
  2067. bool b = flags & Designable;
  2068. if (object) {
  2069. void *argv[] = { &b };
  2070. QMetaObject::metacall(const_cast<QObject*>(object), QMetaObject::QueryPropertyDesignable,
  2071. idx + mobj->propertyOffset(), argv);
  2072. }
  2073. return b;
  2074. }
  2075. /*!
  2076. Returns true if the property is scriptable for the given \a object;
  2077. otherwise returns false.
  2078. If no \a object is given, the function returns false if the
  2079. \c{Q_PROPERTY()}'s \c SCRIPTABLE attribute is false; otherwise returns
  2080. true (if the attribute is true or is a function or expression).
  2081. \sa isDesignable(), isStored()
  2082. */
  2083. bool QMetaProperty::isScriptable(const QObject *object) const
  2084. {
  2085. if (!mobj)
  2086. return false;
  2087. int flags = mobj->d.data[handle + 2];
  2088. bool b = flags & Scriptable;
  2089. if (object) {
  2090. void *argv[] = { &b };
  2091. QMetaObject::metacall(const_cast<QObject*>(object), QMetaObject::QueryPropertyScriptable,
  2092. idx + mobj->propertyOffset(), argv);
  2093. }
  2094. return b;
  2095. }
  2096. /*!
  2097. Returns true if the property is stored for \a object; otherwise returns
  2098. false.
  2099. If no \a object is given, the function returns false if the
  2100. \c{Q_PROPERTY()}'s \c STORED attribute is false; otherwise returns
  2101. true (if the attribute is true or is a function or expression).
  2102. \sa isDesignable(), isScriptable()
  2103. */
  2104. bool QMetaProperty::isStored(const QObject *object) const
  2105. {
  2106. if (!mobj)
  2107. return false;
  2108. int flags = mobj->d.data[handle + 2];
  2109. bool b = flags & Stored;
  2110. if (object) {
  2111. void *argv[] = { &b };
  2112. QMetaObject::metacall(const_cast<QObject*>(object), QMetaObject::QueryPropertyStored,
  2113. idx + mobj->propertyOffset(), argv);
  2114. }
  2115. return b;
  2116. }
  2117. /*!
  2118. Returns true if this property is designated as the \c USER
  2119. property, i.e., the one that the user can edit for \a object or
  2120. that is significant in some other way. Otherwise it returns
  2121. false. e.g., the \c text property is the \c USER editable property
  2122. of a QLineEdit.
  2123. If \a object is null, the function returns false if the \c
  2124. {Q_PROPERTY()}'s \c USER attribute is false. Otherwise it returns
  2125. true.
  2126. \sa QMetaObject::userProperty(), isDesignable(), isScriptable()
  2127. */
  2128. bool QMetaProperty::isUser(const QObject *object) const
  2129. {
  2130. if (!mobj)
  2131. return false;
  2132. int flags = mobj->d.data[handle + 2];
  2133. bool b = flags & User;
  2134. if (object) {
  2135. void *argv[] = { &b };
  2136. QMetaObject::metacall(const_cast<QObject*>(object), QMetaObject::QueryPropertyUser,
  2137. idx + mobj->propertyOffset(), argv);
  2138. }
  2139. return b;
  2140. }
  2141. /*!
  2142. \since 4.6
  2143. Returns true if the property is constant; otherwise returns false.
  2144. A property is constant if the \c{Q_PROPERTY()}'s \c CONSTANT attribute
  2145. is set.
  2146. */
  2147. bool QMetaProperty::isConstant() const
  2148. {
  2149. if (!mobj)
  2150. return false;
  2151. int flags = mobj->d.data[handle + 2];
  2152. return flags & Constant;
  2153. }
  2154. /*!
  2155. \since 4.6
  2156. Returns true if the property is final; otherwise returns false.
  2157. A property is final if the \c{Q_PROPERTY()}'s \c FINAL attribute
  2158. is set.
  2159. */
  2160. bool QMetaProperty::isFinal() const
  2161. {
  2162. if (!mobj)
  2163. return false;
  2164. int flags = mobj->d.data[handle + 2];
  2165. return flags & Final;
  2166. }
  2167. /*!
  2168. \obsolete
  2169. Returns true if the property is editable for the given \a object;
  2170. otherwise returns false.
  2171. If no \a object is given, the function returns false if the
  2172. \c{Q_PROPERTY()}'s \c EDITABLE attribute is false; otherwise returns
  2173. true (if the attribute is true or is a function or expression).
  2174. \sa isDesignable(), isScriptable(), isStored()
  2175. */
  2176. bool QMetaProperty::isEditable(const QObject *object) const
  2177. {
  2178. if (!mobj)
  2179. return false;
  2180. int flags = mobj->d.data[handle + 2];
  2181. bool b = flags & Editable;
  2182. if (object) {
  2183. void *argv[] = { &b };
  2184. QMetaObject::metacall(const_cast<QObject*>(object), QMetaObject::QueryPropertyEditable,
  2185. idx + mobj->propertyOffset(), argv);
  2186. }
  2187. return b;
  2188. }
  2189. /*!
  2190. \class QMetaClassInfo
  2191. \brief The QMetaClassInfo class provides additional information
  2192. about a class.
  2193. \ingroup objectmodel
  2194. Class information items are simple \e{name}--\e{value} pairs that
  2195. are specified using Q_CLASSINFO() in the source code. The
  2196. information can be retrieved using name() and value(). For example:
  2197. \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 5
  2198. This mechanism is free for you to use in your Qt applications. Qt
  2199. doesn't use it for any of its classes.
  2200. \sa QMetaObject
  2201. */
  2202. /*!
  2203. \fn QMetaClassInfo::QMetaClassInfo()
  2204. \internal
  2205. */
  2206. /*!
  2207. \fn const QMetaObject *QMetaClassInfo::enclosingMetaObject() const
  2208. \internal
  2209. */
  2210. /*!
  2211. Returns the name of this item.
  2212. \sa value()
  2213. */
  2214. const char *QMetaClassInfo::name() const
  2215. {
  2216. if (!mobj)
  2217. return 0;
  2218. return mobj->d.stringdata + mobj->d.data[handle];
  2219. }
  2220. /*!
  2221. Returns the value of this item.
  2222. \sa name()
  2223. */
  2224. const char* QMetaClassInfo::value() const
  2225. {
  2226. if (!mobj)
  2227. return 0;
  2228. return mobj->d.stringdata + mobj->d.data[handle + 1];
  2229. }
  2230. /*!
  2231. \macro QGenericArgument Q_ARG(Type, const Type &value)
  2232. \relates QMetaObject
  2233. This macro takes a \a Type and a \a value of that type and
  2234. returns a \l QGenericArgument object that can be passed to
  2235. QMetaObject::invokeMethod().
  2236. \sa Q_RETURN_ARG()
  2237. */
  2238. /*!
  2239. \macro QGenericReturnArgument Q_RETURN_ARG(Type, Type &value)
  2240. \relates QMetaObject
  2241. This macro takes a \a Type and a non-const reference to a \a
  2242. value of that type and returns a QGenericReturnArgument object
  2243. that can be passed to QMetaObject::invokeMethod().
  2244. \sa Q_ARG()
  2245. */
  2246. /*!
  2247. \class QGenericArgument
  2248. \brief The QGenericArgument class is an internal helper class for
  2249. marshalling arguments.
  2250. This class should never be used directly. Please use the \l Q_ARG()
  2251. macro instead.
  2252. \sa Q_ARG(), QMetaObject::invokeMethod(), QGenericReturnArgument
  2253. */
  2254. /*!
  2255. \fn QGenericArgument::QGenericArgument(const char *name, const void *data)
  2256. Constructs a QGenericArgument object with the given \a name and \a data.
  2257. */
  2258. /*!
  2259. \fn QGenericArgument::data () const
  2260. Returns the data set in the constructor.
  2261. */
  2262. /*!
  2263. \fn QGenericArgument::name () const
  2264. Returns the name set in the constructor.
  2265. */
  2266. /*!
  2267. \class QGenericReturnArgument
  2268. \brief The QGenericReturnArgument class is an internal helper class for
  2269. marshalling arguments.
  2270. This class should never be used directly. Please use the
  2271. Q_RETURN_ARG() macro instead.
  2272. \sa Q_RETURN_ARG(), QMetaObject::invokeMethod(), QGenericArgument
  2273. */
  2274. /*!
  2275. \fn QGenericReturnArgument::QGenericReturnArgument(const char *name, void *data)
  2276. Constructs a QGenericReturnArgument object with the given \a name
  2277. and \a data.
  2278. */
  2279. /*! \internal
  2280. If the local_method_index is a cloned method, return the index of the original.
  2281. Example: if the index of "destroyed()" is passed, the index of "destroyed(QObject*)" is returned
  2282. */
  2283. int QMetaObjectPrivate::originalClone(const QMetaObject *mobj, int local_method_index)
  2284. {
  2285. Q_ASSERT(local_method_index < get(mobj)->methodCount);
  2286. int handle = get(mobj)->methodData + 5 * local_method_index;
  2287. while (mobj->d.data[handle + 4] & MethodCloned) {
  2288. Q_ASSERT(local_method_index > 0);
  2289. handle -= 5;
  2290. local_method_index--;
  2291. }
  2292. return local_method_index;
  2293. }
  2294. QT_END_NAMESPACE