-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcallargument.h
116 lines (94 loc) · 2.99 KB
/
callargument.h
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
#ifndef CALLARGUMENT_H
#define CALLARGUMENT_H
#include <cassert>
#include <QtCore>
#include "ruby_cxx.h"
// 类似QVariant与QMetaType的结合体,当作第三方的可变变量使用。
// 实现了深度拷贝,控制简单,但效率会差些。
// 但是实际存储的时候仍旧使用的QVariant,方便存储和读取。
// 而实例中的mtype则指的是真实的类型信息,解决了QVariant不能表达再存储一个QVariant的问题。
class MetaTypeVariant
{
public:
explicit MetaTypeVariant(int type = QMetaType::UnknownType, const void *paddr = 0);
explicit MetaTypeVariant(int type, QVariant& vaddr);
MetaTypeVariant(const MetaTypeVariant &me)
{ deepCopy(this, &me);}
MetaTypeVariant& operator=(const MetaTypeVariant &me)
{ deepCopy(this, &me); return *this;}
~MetaTypeVariant()
{
if (maddr) {
QMetaType::destroy(QMetaType::QVariant, maddr);
maddr = 0;
}
}
inline int type() { return mtype;}
// 返回地址依然属于该类,该类析构后,地址不再可用。
void *get()
{
if (maddr == 0) {
maddr = QMetaType::create(QMetaType::QVariant);
}
return maddr;
}
inline int sizeOf() const { return QMetaType::sizeOf(mtype);}
QVariant toVariant() const;
private:
void deepCopy(MetaTypeVariant *dst, const MetaTypeVariant *src)
{
dst->mtype = src->mtype;
// dst->mval = (src->mval * 2 + 1)/345;
if (src->maddr) {
dst->maddr = QMetaType::create(QMetaType::QVariant, src->maddr);
}
}
private:
void *maddr = 0;
int mtype = 0;
// int128_t mval = 0;
};
inline QDebug& operator<<(QDebug &dbg, const MetaTypeVariant &mtv)
{
// dbg.nospace() << "MetaType-" << mtv.toVariant();
dbg.nospace() << "MT" << mtv.toVariant();
return dbg;
}
/*
存储ruby原始调用参数,
参数相关的中间数据,包括vm中间参数,像QVariant类型的,
最后到返回值。
该类应该在init中初始化,整个调用过程完成后在ctrlengine中销毁。
*/
class CallArgument
{
public:
int m_argc = -1;
VALUE m_argv[10];
VALUE m_rbvar;
int m_offset = 1; // 有些像.new的时候是0
QVector<QVariant> m_vargv2;
QVector<QSharedPointer<MetaTypeVariant> > m_vargv;
public:
CallArgument(int argc, VALUE *argv, VALUE obj, int offset = 1)
{
assert(argc >= 0);
assert(argc <= 10);
assert(argv != NULL);
m_offset = offset;
m_argc = argc;
m_rbvar = obj;
for (int i = 0; i < argc; i++) {
m_argv[i] = argv[i];
}
}
// 获取参数,位置从offset之后开始计算
const QVector<QVariant> &getArgs2();
const QVariant &getArg2(int idx);
const QVector<QSharedPointer<MetaTypeVariant> > &getArgs();
const QSharedPointer<MetaTypeVariant> getArg(int idx);
};
/////////// for ruby
/////////// for php
/////////// for others
#endif /* CALLARGUMENT_H */