-
Notifications
You must be signed in to change notification settings - Fork 2
/
qtregister.h
95 lines (77 loc) · 2.57 KB
/
qtregister.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
#ifndef QTREGISTER_H
#define QTREGISTER_H
#include <cassert>
#include <QtCore>
#include "macrolib.h"
#include "ruby_cxx.h"
#include "entry.h"
class QtRegister;
static QtRegister *qrti = NULL;
/*
管理Qt类在ruby中的注册
auto rger = QtRegister::inst();
*/
class QtRegister : public Singleton<QtRegister>
{
public:
friend class Singleton;
public:
QtRegister() { qrti = this; }
void setModuleQt(VALUE cMoudleQt) { m_cModuleQt = cMoudleQt;}
bool isRegisted(QString klass)
{
return m_classes.contains(klass);
}
VALUE getClass(QString klass)
{
assert(isRegisted(klass));
VALUE cQtClass = Qnil;
cQtClass = m_classes.value(klass);
return cQtClass;
}
bool registClass(QString klass)
{
if (m_cModuleQt == 0) {
qDebug()<<"uninit ModuleQt";
return false;
}
if (isRegisted(klass)) return false;
const char *cname = klass.toLatin1().data();
VALUE module = this->m_cModuleQt;
VALUE cQtClass = rb_define_class_under(module, cname, rb_cObject);
rb_define_method(cQtClass, "initialize", (VALUE (*) (...)) x_Qt_class_init_jit, -1);
rb_define_method(cQtClass, "to_s", (VALUE (*) (...)) x_Qt_meta_class_to_s, -1);
rb_define_method(cQtClass, "method_missing", (VALUE (*) (...)) x_Qt_class_method_missing_jit, -1);
rb_define_singleton_method(cQtClass, "const_missing",
(VALUE (*) (...)) x_Qt_class_const_missing_jit, -1);
rb_define_singleton_method(cQtClass, "method_missing",
(VALUE (*) (...)) x_Qt_class_singleton_method_missing_jit, -1);
this->fixConflict(cQtClass, klass, "display");
m_classes.insert(klass, cQtClass);
return true;
}
bool unregistClass(QString klass)
{
if (isRegisted(klass)) {
VALUE cQtClass = m_classes.value(klass);
ID cid = rb_to_id(cQtClass);
rb_undef_method(cQtClass, "method_missing");
rb_undef_method(cQtClass, "to_s");
rb_undef_method(cQtClass, "initialize");
rb_undef(cQtClass, cid);
return true;
}
return true;
}
bool fixConflict(VALUE cQtClass, QString klass, QString method)
{
if (klass == "QLCDNumber") {
rb_undef_method(cQtClass, method.toLatin1().data());
}
return true;
}
protected:
QHash<QString, VALUE> m_classes;
VALUE m_cModuleQt = 0;
};
#endif /* QTREGISTER_H */