-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmarshallruby.cpp
282 lines (256 loc) · 8.66 KB
/
marshallruby.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
#include "qtobjectmanager.h"
#include "marshallruby.h"
/*
数字类型
字符串类型
对象类型
数组类型(元素为数字,字符串,对象)
*/
QVariant MarshallRuby::VALUE2Variant(VALUE v)
{
QVariant rv;
QString str, str2;
void *ci = NULL;
QObject *obj = NULL;
VALUE v1;
VALUE v2;
VALUE v3;
switch (TYPE(v)) {
case T_NONE: rv = QVariant(); break;
case T_FIXNUM: rv = (int)FIX2INT(v); break;
case T_STRING: rv = RSTRING_PTR(v); break;
case T_FLOAT: rv = RFLOAT_VALUE(v); break;
case T_NIL: rv = 0; break;
case T_TRUE: rv = true; break;
case T_FALSE: rv = false; break;
case T_OBJECT:
str = QString(rb_class2name(RBASIC_CLASS(v)));
ci = Qom::inst()->getObject(v);
// obj = dynamic_cast<QObject*>(ci);
qDebug()<<"unimpl VALUE:"<<str<<ci<<obj;
// rv = QVariant(QMetaType::VoidStar, ci);
rv = QVariant::fromValue(ci);
break;
case T_ARRAY: {
QStringList ary;
// ary << "a123" << "b3456";
qDebug()<<RARRAY_LEN(v)<<QT_VERSION;
for (int i = 0; i < RARRAY_LEN(v); i++) {
ary << VALUE2Variant(rb_ary_entry(v, i)).toString();
}
rv = QVariant(ary);
}; break;
case T_STRUCT:
qDebug()<<"use VALUE2Variant2 function.";
break;
case T_CLASS:
default:
qDebug()<<"unknown VALUE type:"<<TYPE(v);
break;
}
return (rv);
}
// Range类型支持
QVector<QVariant> MarshallRuby::VALUE2Variant2(VALUE v)
{
QVector<QVariant> rvs(1);
QVariant rv;
QString str, str2;
void *ci = NULL;
QObject *obj = NULL;
VALUE v1;
VALUE v2;
VALUE v3;
switch (TYPE(v)) {
case T_NONE: rv = QVariant(); break;
case T_FIXNUM: rv = (int)FIX2INT(v); break;
case T_STRING: rv = RSTRING_PTR(v); break;
case T_FLOAT: rv = RFLOAT_VALUE(v); break;
case T_NIL: rv = 0; break;
case T_TRUE: rv = true; break;
case T_FALSE: rv = false; break;
case T_OBJECT:
str = QString(rb_class2name(RBASIC_CLASS(v)));
ci = Qom::inst()->getObject(v);
// obj = dynamic_cast<QObject*>(ci);
qDebug()<<"unimpl VALUE:"<<str<<ci<<obj;
// rv = QVariant(QMetaType::VoidStar, ci);
rv = QVariant::fromValue(ci);
break;
case T_ARRAY: {
QStringList ary;
// ary << "a123" << "b3456";
qDebug()<<RARRAY_LEN(v)<<QT_VERSION;
for (int i = 0; i < RARRAY_LEN(v); i++) {
// FIXME: 也可能是对象数组,如Qt5::QString,但toString方法不好用。
// FIXME: 如,[Qt5::QApplication.translate("MainWindow", "acbc", nil), "efgggggg", "hijjjjjjjj"]
ary << VALUE2Variant(rb_ary_entry(v, i)).toString();
}
rv = QVariant(ary);
}; break;
case T_STRUCT: {
str = rb_class2name(RBASIC_CLASS(v));
if (str == "Range") {
// qDebug()<<"Range is struct???"<<BUILTIN_TYPE(v)
// <<rb_class2name(RBASIC_CLASS(v))
// <<RSTRUCT_LEN(v);
v1 = RSTRUCT_GET(v, 0);
v2 = RSTRUCT_GET(v, 1);
v3 = RSTRUCT_GET(v, 2);
// qDebug()<<TYPE(v1)<<TYPE(v2)<<TYPE(v3);
// qDebug()<<FIX2INT(v1)<<FIX2INT(v2);
rv = QVariant(FIX2INT(v1));
rvs.append(QVariant(FIX2INT(v2)));
} else {
qDebug()<<"unsupported struct type:"<<str;
}
}; break;
case T_CLASS:
default:
qDebug()<<"unknown VALUE type:"<<TYPE(v);
break;
}
rvs[0] = rv;
return (rvs);
}
QVector<QVariant> MarshallRuby::ARGV2Variant(int argc, VALUE *argv, int start)
{
QVector<QVariant> args;
for (int i = start; i < argc; i ++) {
// if (i == 0) continue; // for ctor 0 is arg0
if (i >= argc) break;
qDebug()<<"i == "<< i << (i<argc) << (i>argc);
QVector<QVariant> targs = VALUE2Variant2(argv[i]);
for (auto v: targs) args << v;
qDebug()<<"i == "<< i << (i<argc) << (i>argc)<<targs;
}
return (args);
}
// static
VALUE MarshallRuby::Variant2VALUE(void *v, int type)
{
VALUE obj = Qom::inst()->getObject(v);
if (obj != 0) {
return obj;
}
int pty = type;
void *arg = v;
VALUE rv = Qnil;
qDebug()<<arg<<pty<<QString(QMetaType::typeName(pty));
switch(pty) {
case QMetaType::QString: {
QString s(*(QString*)arg);
qDebug()<<s;
qDebug()<<Qom::inst()->getObject(arg)<<Qnil;
rv = rb_str_new2(((QString*)arg)->toLatin1().data());
} break;
default:
qDebug()<<"unknown type:"<<pty<<QString(QMetaType::typeName(pty));
break;
}
return rv;
}
/////////////////////
// static
QVector<QSharedPointer<MetaTypeVariant> >
MarshallRuby::VALUE2MTVariant(VALUE v)
{
QVector<QSharedPointer<MetaTypeVariant> > rvs(1);
QSharedPointer<MetaTypeVariant> rv;
QString str, str2;
void *ci = NULL;
QObject *obj = NULL;
VALUE v1, v2, v3;
switch (TYPE(v)) {
case T_NONE: rv = QSharedPointer<MetaTypeVariant>(new MetaTypeVariant()); break;
case T_FIXNUM: {
QVariant num = (int)FIX2INT(v);
rv = QSharedPointer<MetaTypeVariant>(new MetaTypeVariant(QMetaType::Int, &num));
}; break;
case T_STRING: {
QVariant str = QString(RSTRING_PTR(v));
rv = QSharedPointer<MetaTypeVariant>(new MetaTypeVariant(QMetaType::QString, &str));
}; break;
case T_FLOAT: {
QVariant num = RFLOAT_VALUE(v);
rv = QSharedPointer<MetaTypeVariant>(new MetaTypeVariant(QMetaType::Double, &num));
}; break;
case T_NIL: {
QVariant num = 0;
rv = QSharedPointer<MetaTypeVariant>(new MetaTypeVariant(QMetaType::Int, &num));
}; break;
case T_TRUE: {
QVariant ok = true;
rv = QSharedPointer<MetaTypeVariant>(new MetaTypeVariant(QMetaType::Bool, &ok));
}; break;
case T_FALSE: {
QVariant ok = false;
rv = QSharedPointer<MetaTypeVariant>(new MetaTypeVariant(QMetaType::Bool, &ok));
}; break;
case T_OBJECT: {
str = QString(rb_class2name(RBASIC_CLASS(v)));
ci = Qom::inst()->getObject(v);
// obj = dynamic_cast<QObject*>(ci);
qDebug()<<"unimpl VALUE:"<<str<<ci<<obj;
// rv = QVariant(QMetaType::VoidStar, ci);
// rv = QVariant::fromValue(ci);
QVariant vrv = QVariant::fromValue(ci);
rv = QSharedPointer<MetaTypeVariant>(new MetaTypeVariant(QMetaType::VoidStar, &vrv));
}; break;
case T_ARRAY: {
QStringList ary;
// ary << "a123" << "b3456";
qDebug()<<RARRAY_LEN(v)<<QT_VERSION;
for (int i = 0; i < RARRAY_LEN(v); i++) {
// FIXME: 也可能是对象数组,如Qt5::QString,但toString方法不好用。
// FIXME: 如,[Qt5::QApplication.translate("MainWindow", "acbc", nil), "efgggggg", "hijjjjjjjj"]
ary << VALUE2Variant(rb_ary_entry(v, i)).toString();
}
// rv = QVariant(ary);
QVariant vary(ary);
rv = QSharedPointer<MetaTypeVariant>(new MetaTypeVariant(QMetaType::QStringList, &vary));
}; break;
case T_STRUCT: { // for ruby range
str = rb_class2name(RBASIC_CLASS(v));
if (str == "Range") {
// qDebug()<<"Range is struct???"<<BUILTIN_TYPE(v)
// <<rb_class2name(RBASIC_CLASS(v))
// <<RSTRUCT_LEN(v);
v1 = RSTRUCT_GET(v, 0);
v2 = RSTRUCT_GET(v, 1);
v3 = RSTRUCT_GET(v, 2);
// qDebug()<<TYPE(v1)<<TYPE(v2)<<TYPE(v3);
// qDebug()<<FIX2INT(v1)<<FIX2INT(v2);
// rv = QVariant(FIX2INT(v1));
// rvs.append(QVariant(FIX2INT(v2)));
QVariant num1 = FIX2INT(v1);
QVariant num2 = FIX2INT(v2);
rv = QSharedPointer<MetaTypeVariant>(new MetaTypeVariant(QMetaType::Int, &num1));
QSharedPointer<MetaTypeVariant> mtv(new MetaTypeVariant(QMetaType::Int, &num2));
rvs.append(mtv);
} else {
qDebug()<<"unsupported struct type:"<<str;
}
}; break;
case T_CLASS:
default:
qDebug()<<"unknown VALUE type:"<<TYPE(v);
break;
}
rvs[0] = rv;
return (rvs);
}
// static
QVector<QSharedPointer<MetaTypeVariant> >
MarshallRuby::ARGV2MTVariant(int argc, VALUE *argv, int start)
{
QVector<QSharedPointer<MetaTypeVariant> > args;
for (int i = start; i < argc; i ++) {
if (i >= argc) break;
qDebug()<<"i == "<< i << (i<argc) << (i>argc);
QVector<QSharedPointer<MetaTypeVariant> > targs = VALUE2MTVariant(argv[i]);
for (auto &v: targs) args << v;
qDebug()<<"i == "<< i << (i<argc) << (i>argc)<<targs;
}
return (args);
}