forked from smartdevicelink/sdl_atf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qtdynamic.cc
208 lines (186 loc) · 6.31 KB
/
qtdynamic.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#line 66 "dynamic_object.nw"
#include "qtdynamic.h"
#include <QObject>
#include <QDebug>
#line 5 "main.nw"
extern "C" {
#include <lua5.2/lua.h>
#include <lua5.2/lualib.h>
#include <lua5.2/lauxlib.h>
}
#line 70 "dynamic_object.nw"
DynamicObject::DynamicObject(QObject *parent)
: QObject(parent) { }
static QList<QByteArray> typesFromString(const char *str, const char *end)
{
QList<QByteArray> result;
const char *p = str;
while (p != end)
{
if (*p == ',') ++p;
const char *b = p;
while (p != end && *p++ != ',');
if (p > b)
result.append(QByteArray(b, p - b));
}
return result;
}
static int *queuedConnectionTypes(const QList<QByteArray> &typeNames)
{
int *types = new int [typeNames.count() + 1];
Q_CHECK_PTR(types);
for (int i = 0; i < typeNames.count(); ++i) {
const QByteArray typeName = typeNames.at(i);
if (typeName.endsWith('*'))
types[i] = QMetaType::VoidStar;
else
types[i] = QMetaType::type(typeName);
if (!types[i]) {
qWarning("QObject::connect: Cannot queue arguments of type '%s'\n"
"(Make sure '%s' is registered using qRegisterMetaType().)",
typeName.constData(), typeName.constData());
delete [] types;
return 0;
}
}
types[typeNames.count()] = 0;
return types;
}
bool DynamicObject::connectDynamicSlot(QObject *obj, const char *signal, const char *slot,
DynamicSlot *s)
{
#line 189 "dynamic_object.nw"
QByteArray theSignal = QMetaObject::normalizedSignature(signal);
QByteArray theSlot = QMetaObject::normalizedSignature(slot);
if (!QMetaObject::checkConnectArgs(theSignal, theSlot)) {
qWarning() << "Cannot connect signal" << theSignal << "to slot" << theSlot;
return false;
}
#line 117 "dynamic_object.nw"
int signalId = obj->metaObject()->indexOfSignal(theSignal);
if (signalId < 0) {
qWarning() << "No such signal " << theSignal;
return false;
}
int slotId = slotIndices.value(theSlot, -1);
if (slotId < 0) {
slotId = slotList.size();
slotIndices[theSlot] = slotId;
slotList.append(s);
}
return QMetaObject::connect(obj, signalId,
this, slotId + metaObject()->methodCount(),
Qt::QueuedConnection,
queuedConnectionTypes(typesFromString(theSlot.constData() + theSlot.indexOf('(') + 1,
theSlot.constData() + theSlot.indexOf(')'))));
}
bool DynamicObject::connectDynamicSignal(const char *signal, QObject *obj, const char *slot)
{
#line 189 "dynamic_object.nw"
QByteArray theSignal = QMetaObject::normalizedSignature(signal);
QByteArray theSlot = QMetaObject::normalizedSignature(slot);
if (!QMetaObject::checkConnectArgs(theSignal, theSlot)) {
qWarning() << "Cannot connect signal" << theSignal << "to slot" << theSlot;
return false;
}
#line 141 "dynamic_object.nw"
int slotId = obj->metaObject()->indexOfSlot(theSlot);
if (slotId < 0) {
qWarning() << "Cannot find slot " << theSlot;
return false;
}
int signalId = signalIndices.value(theSignal, -1);
if (signalId < 0) {
signalId = signalIndices.size();
signalIndices[theSignal] = signalId;
}
return QMetaObject::connect(this, signalId + metaObject()->methodCount(), obj, slotId,
Qt::QueuedConnection,
queuedConnectionTypes(typesFromString(theSignal.constData() + theSignal.indexOf('(') + 1,
theSignal.constData() + theSignal.indexOf(')'))));
}
bool DynamicObject::connectDynamicSignalToDynamicSlot(
DynamicObject* sender,
const char *signal,
DynamicObject* receiver,
const char *slot,
DynamicSlot *s)
{
#line 189 "dynamic_object.nw"
QByteArray theSignal = QMetaObject::normalizedSignature(signal);
QByteArray theSlot = QMetaObject::normalizedSignature(slot);
if (!QMetaObject::checkConnectArgs(theSignal, theSlot)) {
qWarning() << "Cannot connect signal" << theSignal << "to slot" << theSlot;
return false;
}
#line 167 "dynamic_object.nw"
int signalId = sender->signalIndices.value(theSignal, -1);
if (signalId < 0) {
signalId = sender->signalIndices.size();
sender->signalIndices[theSignal] = signalId;
}
int slotId = receiver->slotIndices.value(theSlot, -1);
if (slotId < 0) {
slotId = receiver->slotList.size();
receiver->slotIndices[theSlot] = slotId;
receiver->slotList.append(s);
}
return QMetaObject::connect(sender,
signalId + sender->metaObject()->methodCount(),
receiver,
slotId + receiver->metaObject()->methodCount(),
Qt::QueuedConnection,
queuedConnectionTypes(typesFromString(theSignal.constData() + theSignal.indexOf('(') + 1,
theSignal.constData() + theSignal.indexOf(')'))));
}
#line 196 "dynamic_object.nw"
int DynamicObject::qt_metacall(QMetaObject::Call c, int id, void **arguments)
{
id = QObject::qt_metacall(c, id, arguments);
if (id < 0 || c != QMetaObject::InvokeMetaMethod)
return id;
Q_ASSERT(id < slotList.size());
slotList[id]->call(sender(), arguments);
return -1;
}
bool DynamicObject::emitDynamicSignal(const char *signal, void **arguments)
{
QByteArray theSignal = QMetaObject::normalizedSignature(signal);
int signalId = signalIndices.value(theSignal, -1);
if (signalId >= 0) {
QMetaObject::activate(this, metaObject(), signalId + metaObject()->methodCount(),
arguments);
return true;
} else {
return false;
}
}
DynamicSlot::DynamicSlot(lua_State *L, int objidx, const char *signature)
: lua_state(L),
objidx_(objidx)
{
QByteArray theSignal = QMetaObject::normalizedSignature(signature);
marshallers_ = get_marshalling_list(theSignal);
QByteArray slotName = signature;
slot_ = QString(QByteArray(slotName, slotName.indexOf('(')));
}
void DynamicSlot::call(QObject *sender, void **arguments)
{
(void)sender;
lua_rawgeti(lua_state, LUA_REGISTRYINDEX, objidx_);
lua_getfield(lua_state, -1, slot_.toUtf8().constData());
if (lua_isfunction(lua_state, -1)) {
lua_pushnil(lua_state);
lua_copy(lua_state, -3, -1);
++arguments; // skip return value
int argc = 1;
for (auto m : marshallers_) {
m->Unmarshal(*arguments++, lua_state);
++argc;
}
lua_call(lua_state, argc, 0);
} else {
lua_pop(lua_state, 1); // Remove slot from stack
}
lua_pop(lua_state, 1); // Remove object from stack
}