-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqtobjectmanager.cpp
364 lines (299 loc) · 8.89 KB
/
qtobjectmanager.cpp
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include "ruby_cxx.h"
#include "connectruby.h"
#include "qtobjectmanager.h"
// atomic singleton member
namespace _Qom {
// static std::atomic<QtObjectManager*> _inst2(NULL);
static std::atomic<qint64> _objid2(-1);
static std::atomic<qint64> _connid(0);
};
QtObjectManager::QtObjectManager()
{
}
/*
QtObjectManager *QtObjectManager::inst()
{
if (_Qom::_inst2 == NULL) {
_Qom::_inst2 = new QtObjectManager;
// init_fmap();
}
return _Qom::_inst2;
}
*/
bool QtObjectManager::addObject(RB_VALUE rbobj, void *qtobj)
{
ObjectInfo *oi = new ObjectInfo;
oi->objid = ++_Qom::_objid2;
oi->rbobj = rbobj;
oi->qtobj = qtobj;
this->robjs[rbobj] = oi;
this->qobjs[qtobj] = oi;
this->jdobjs[rbobj] = qtobj;
this->idobjs[rb_obj_id(rbobj)] = rbobj;
return false;
}
bool QtObjectManager::delObject(RB_VALUE rbobj)
{
ObjectInfo *oi = NULL;
if (this->jdobjs.contains(rbobj)) {
this->jdobjs.remove(rbobj);
}
if (this->robjs.contains(rbobj)) {
oi = this->robjs.value(rbobj);
this->robjs.remove(rbobj);
this->qobjs.remove(oi->qtobj);
// TODO delete qtobj instance
free(oi->qtobj);
delete oi;
return true;
}
RB_VALUE rbid = rb_obj_id(rbobj);
if (this->idobjs.contains(rbid)) {
this->idobjs.remove(rbid);
}
return false;
}
void *QtObjectManager::getObject(RB_VALUE rbobj)
{
ObjectInfo *oi = NULL;
if (this->robjs.contains(rbobj)) {
oi = this->robjs.value(rbobj);
return oi->qtobj;
}
return NULL;
}
RB_VALUE QtObjectManager::getObject(void *qtobj)
{
for (auto it = this->jdobjs.begin(); it != this->jdobjs.end(); it++) {
if (it.value() == qtobj) {
RB_VALUE rv = it.key();
return rv;
}
}
return 0;
}
RB_VALUE QtObjectManager::getObjectById(RB_VALUE rbid)
{
if (this->idobjs.contains(rbid)) {
return this->idobjs.value(rbid);
}
return 0;
}
// 这里的信号不能重载。
bool QtObjectManager::addSignals(QString rbklass, QVector<QVariant> sigs)
{
for (int i = 0; i < sigs.count(); i ++) {
QString signature = sigs.at(i).toString();
int pos = signature.indexOf('(');
QString method = signature.left(pos);
if (rbsignals.contains(rbklass)) {
if (rbsignals[rbklass].contains(method)) {
qDebug()<<"signal existed:"<<method<<signature<<rbsignals[rbklass][method];
} else {
rbsignals[rbklass][method] = signature;
}
} else {
QHash<QString, QString> tmp;
tmp[method] = signature;
rbsignals[rbklass] = tmp;
}
}
// qDebug()<<rbsignals;
return true;
}
QString QtObjectManager::getSignature(QString rbklass, QString signal)
{
if (rbsignals.contains(rbklass)) {
if (rbsignals[rbklass].contains(signal)) {
return rbsignals[rbklass][signal];
}
}
return QString();
}
// 与rbconnectrb方法对应
bool QtObjectManager::addConnection(QString rbklass, QString rbsignal, QtObjectManager::RubySlot *rbslot)
{
QString key = QString("%1->%2").arg(rbklass).arg(rbsignal);
if (this->rbconnections.contains(key)) {
this->rbconnections[key].append(rbslot);
} else {
QVector<QtObjectManager::RubySlot*> slot = {rbslot};
this->rbconnections[key] = slot;
}
return true;
}
QVector<QtObjectManager::RubySlot*>
QtObjectManager::getConnections(QString rbklass, QString rbsignal)
{
QVector<QtObjectManager::RubySlot*> conns;
QString key = QString("%1->%2").arg(rbklass).arg(rbsignal);
if (this->rbconnections.contains(key)) {
return this->rbconnections.value(key);
}
return conns;
}
// 为什么在QHash中还是不能用呢。
inline uint qHash(const uint128_t & key, uint seed) Q_DECL_NOTHROW
{
uint h = 0;
quint64 hbit, lbit;
memcpy(&hbit, &key, 8);
memcpy(&lbit, ((char*)&key)+8, 8);
uint hh,hl;
hh = qHash(hbit);
hl = qHash(lbit);
quint64 tkey = hh;
tkey = tkey << 32;
hbit = tkey | hl;
h = qHash(hbit);
return h;
}
bool QtObjectManager::addConnection(RB_VALUE rbobj, QString rbsignal, RubySlot *rbslot)
{
QPair<RB_VALUE, QString> ckey = {rbobj, rbsignal};
if (this->rbconnections4.contains(ckey)) {
this->rbconnections4[ckey].append(rbslot);
} else {
QVector<QtObjectManager::RubySlot*> slot = {rbslot};
this->rbconnections4[ckey] = slot;
}
///
RB_VALUE ikey = rbobj;
if (this->rbconnections3.contains(ikey)) {
if (this->rbconnections3[ikey].contains(rbsignal)) {
this->rbconnections3[ikey][rbsignal].append(rbslot);
} else {
QVector<QtObjectManager::RubySlot*> slot = {rbslot};
this->rbconnections3[ikey][rbsignal] = slot;
}
} else {
QVector<QtObjectManager::RubySlot*> slot = {rbslot};
QHash<QString, QVector<QtObjectManager::RubySlot*> > hslots;
hslots[rbsignal] = slot;
this->rbconnections3[ikey] = hslots;
}
return true;
}
QVector<QtObjectManager::RubySlot*>
QtObjectManager::getConnections(RB_VALUE rbobj, QString rbsignal)
{
QPair<RB_VALUE, QString> ckey = {rbobj, rbsignal};
if (this->rbconnections4.contains(ckey)) {
return this->rbconnections4[ckey];
}
// way 2
QVector<QtObjectManager::RubySlot*> conns;
RB_VALUE ikey = rbobj;
if (this->rbconnections3.contains(ikey)) {
if (this->rbconnections3[ikey].contains(rbsignal)) {
conns = this->rbconnections3[ikey][rbsignal];
}
}
return conns;
}
bool QtObjectManager::removeConnection(RB_VALUE rbobj, QString rbsignal,
const QtObjectManager::RubySlot *rbslot)
{
QVector<QtObjectManager::RubySlot*> conns;
conns = this->getConnections(rbobj, rbsignal);
if (conns.count() == 0) {
qDebug()<<"no rbconn:"<<rbobj<<rbsignal;
return true;
}
int rmcnt = 0;
QPair<RB_VALUE, QString> ckey = {rbobj, rbsignal};
RB_VALUE ikey = rbobj;
for (int i = conns.count()-1; i >= 0; i--) {
auto slot = conns.at(i);
if (
// 删除该signal所有的slot
rbslot == NULL ||
// 删除匹配的slot
(slot->receiver == rbslot->receiver && slot->slot == rbslot->slot)) {
this->rbconnections3[ikey][rbsignal].remove(i);
this->rbconnections4[ckey].remove(i);
delete slot;
rmcnt ++;
}
}
qDebug()<<"removed conn3:"<<rmcnt<<rbslot;
return true;
}
//// new connection
bool QtObjectManager::addConnection(RB_VALUE rbobj, QString rbsignal, ConnectAny *rbslot)
{
QPair<RB_VALUE, QString> ckey = {rbobj, rbsignal};
if (this->rbconnections5.contains(ckey)) {
this->rbconnections5[ckey].append(rbslot);
} else {
QVector<ConnectAny*> slot = {rbslot};
this->rbconnections5[ckey] = slot;
}
return true;
}
QVector<ConnectAny*>
QtObjectManager::getConnections5(RB_VALUE rbobj, QString rbsignal)
{
QPair<RB_VALUE, QString> ckey = {rbobj, rbsignal};
if (this->rbconnections5.contains(ckey)) {
return this->rbconnections5[ckey];
}
return QVector<ConnectAny*>();
}
bool QtObjectManager::removeConnection(RB_VALUE rbobj, QString rbsignal,
ConnectAny *rbslot)
{
QVector<ConnectAny*> conns;
conns = this->getConnections5(rbobj, rbsignal);
if (conns.count() == 0) {
qDebug()<<"no rbconn:"<<rbobj<<rbsignal;
return true;
}
int rmcnt = 0;
QPair<RB_VALUE, QString> ckey = {rbobj, rbsignal};
RB_VALUE ikey = rbobj;
for (int i = conns.count()-1; i >= 0; i--) {
auto slot = conns.at(i);
if (
// 删除该signal所有的slot
rbslot == NULL ||
// 删除匹配的slot
// (slot->m_receiver == rbslot->m_receiver && slot->m_slot == rbslot->m_slot)) {
slot->slotEqual(rbslot)) {
this->rbconnections5[ckey].remove(i);
delete slot;
rmcnt ++;
}
}
qDebug()<<"removed conn5:"<<rmcnt<<rbslot;
return true;
}
void QtObjectManager::testParser()
{
// get_class_method("abcdef.h");
}
void QtObjectManager::testIR()
{
// run_code_jit();
}
QDebug &qodebug(QDebug &dbg, void*obj, QString klass)
{
// _Zls6QDebugRK9QBitArray
QString symtpl = "_Zls6QDebugRK%1%2";
QString symname = symtpl.arg(klass.length()).arg(klass);
return dbg.space();
}
/////////////////////
////
/////////////////////
// qint64 ConnectProxy::connid = 1;
ConnectProxy::ConnectProxy()
: QObject()
{
}
qint64 ConnectProxy::addConnection(QMetaObject::Connection conn)
{
qint64 nid = _Qom::_connid++;
conns[nid] = conn;
return nid;
}