Qt 4.8
qsignalspy.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtTest module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QSIGNALSPY_H
43 #define QSIGNALSPY_H
44 
45 #include <QtCore/qbytearray.h>
46 #include <QtCore/qlist.h>
47 #include <QtCore/qobject.h>
48 #include <QtCore/qmetaobject.h>
49 #include <QtCore/qvariant.h>
50 
52 
54 
55 QT_MODULE(Test)
56 
57 class QVariant;
58 
59 /* ### Qt5: change the class to use regular BC mechanisms, such that we can
60  * implement things like suggested in task 160192. */
61 class QSignalSpy: public QObject, public QList<QList<QVariant> >
62 {
63 public:
64  QSignalSpy(QObject *obj, const char *aSignal)
65  {
66 #ifdef Q_CC_BOR
67  const int memberOffset = QObject::staticMetaObject.methodCount();
68 #else
69  static const int memberOffset = QObject::staticMetaObject.methodCount();
70 #endif
71  Q_ASSERT(obj);
72  Q_ASSERT(aSignal);
73 
74  if (((aSignal[0] - '0') & 0x03) != QSIGNAL_CODE) {
75  qWarning("QSignalSpy: Not a valid signal, use the SIGNAL macro");
76  return;
77  }
78 
80  const QMetaObject *mo = obj->metaObject();
81  int sigIndex = mo->indexOfMethod(ba.constData());
82  if (sigIndex < 0) {
83  qWarning("QSignalSpy: No such signal: '%s'", ba.constData());
84  return;
85  }
86 
87  if (!QMetaObject::connect(obj, sigIndex, this, memberOffset,
89  qWarning("QSignalSpy: QMetaObject::connect returned false. Unable to connect.");
90  return;
91  }
92  sig = ba;
93  initArgs(mo->method(sigIndex));
94  }
95 
96  inline bool isValid() const { return !sig.isEmpty(); }
97  inline QByteArray signal() const { return sig; }
98 
99 
100  int qt_metacall(QMetaObject::Call call, int methodId, void **a)
101  {
102  methodId = QObject::qt_metacall(call, methodId, a);
103  if (methodId < 0)
104  return methodId;
105 
106  if (call == QMetaObject::InvokeMetaMethod) {
107  if (methodId == 0) {
108  appendArgs(a);
109  }
110  --methodId;
111  }
112  return methodId;
113  }
114 
115 private:
116  void initArgs(const QMetaMethod &member)
117  {
118  QList<QByteArray> params = member.parameterTypes();
119  for (int i = 0; i < params.count(); ++i) {
120  int tp = QMetaType::type(params.at(i).constData());
121  if (tp == QMetaType::Void)
122  qWarning("Don't know how to handle '%s', use qRegisterMetaType to register it.",
123  params.at(i).constData());
124  args << tp;
125  }
126  }
127 
128  void appendArgs(void **a)
129  {
130  QList<QVariant> list;
131  for (int i = 0; i < args.count(); ++i) {
132  QMetaType::Type type = static_cast<QMetaType::Type>(args.at(i));
133  list << QVariant(type, a[i + 1]);
134  }
135  append(list);
136  }
137 
138  // the full, normalized signal name
140  // holds the QMetaType types for the argument list of the signal
142 };
143 
145 
147 
148 #endif
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QMetaObject class contains meta-information about Qt objects.
Definition: qobjectdefs.h:304
int type
Definition: qmetatype.cpp:239
static bool connect(const QObject *sender, int signal_index, const QObject *receiver, int method_index, int type=0, int *types=0)
Definition: qobject.cpp:3194
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static QByteArray normalizedSignature(const char *method)
Normalizes the signature of the given method.
#define QT_MODULE(x)
Definition: qglobal.h:2783
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
bool isValid() const
Definition: qsignalspy.h:96
void appendArgs(void **a)
Definition: qsignalspy.h:128
QList< int > args
Definition: qsignalspy.h:141
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
static const QMetaObject staticMetaObject
This variable stores the meta-object for the class.
Definition: qobject.h:128
long ASN1_INTEGER_get ASN1_INTEGER * a
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
void initArgs(const QMetaMethod &member)
Definition: qsignalspy.h:116
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
void append(const QList< QVariant > &t)
Inserts value at the end of the list.
Definition: qlist.h:507
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
int qt_metacall(QMetaObject::Call call, int methodId, void **a)
Definition: qsignalspy.h:100
Q_CORE_EXPORT void qWarning(const char *,...)
Type
These are the built-in types supported by QMetaType:
Definition: qmetatype.h:64
QSignalSpy(QObject *obj, const char *aSignal)
Definition: qsignalspy.h:64
static int type(const char *typeName)
Returns a handle to the type called typeName, or 0 if there is no such type.
Definition: qmetatype.cpp:607
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
#define QSIGNAL_CODE
Definition: qobjectdefs.h:244
QList< QByteArray > parameterTypes() const
Returns a list of parameter types.
int indexOfMethod(const char *method) const
Finds method and returns its index; otherwise returns -1.
QByteArray signal() const
Definition: qsignalspy.h:97
QByteArray sig
Definition: qsignalspy.h:139
QMetaMethod method(int index) const
Returns the meta-data for the method with the given index.
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
#define QT_END_HEADER
Definition: qglobal.h:137
int methodCount() const
Returns the number of methods known to the meta-object system in this class, including the number of ...
The QMetaMethod class provides meta-data about a member function.
Definition: qmetaobject.h:56
virtual const QMetaObject * metaObject() const
Returns a pointer to the meta-object of this object.
The QList class is a template class that provides lists.
Definition: qdatastream.h:62