-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathqmetaobject_rust.hpp
More file actions
121 lines (108 loc) · 4.3 KB
/
Copy pathqmetaobject_rust.hpp
File metadata and controls
121 lines (108 loc) · 4.3 KB
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
/* Copyright (C) 2018 Olivier Goffart <ogoffart@woboq.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <QtCore/QObject>
#include <QtCore/QEvent>
#include <QtCore/QDebug>
struct TraitObject {
void *a;
void *b;
explicit operator bool() const { return a && b; }
};
extern "C" QMetaObject *RustObject_metaObject(TraitObject);
extern "C" void RustObject_destruct(TraitObject);
template <typename Base>
struct RustObject : Base {
TraitObject rust_object;
void (*extra_destruct)(QObject *);
const QMetaObject *metaObject() const override {
return RustObject_metaObject(rust_object);
}
int qt_metacall(QMetaObject::Call _c, int _id, void **_a) override {
_id = Base::qt_metacall(_c, _id, _a);
if (_id < 0)
return _id;
const QMetaObject *mo = metaObject();
if (_c == QMetaObject::InvokeMetaMethod || _c == QMetaObject::RegisterMethodArgumentMetaType) {
int methodCount = mo->methodCount();
if (_id < methodCount)
mo->d.static_metacall(this, _c, _id, _a);
_id -= methodCount;
} else if ((_c >= QMetaObject::ReadProperty && _c <= QMetaObject::QueryPropertyUser)
|| _c == QMetaObject::RegisterPropertyMetaType) {
int propertyCount = mo->propertyCount();
if (_id < propertyCount)
mo->d.static_metacall(this, _c, _id, _a);
_id -= propertyCount;
}
return _id;
}
bool event(QEvent *event) override {
if (rust_object && event->type() == 513) {
// "513 reserved for Qt Jambi's DeleteOnMainThread event"
// This event is sent by rust when we are deleted.
rust_object = { nullptr, nullptr }; // so the destructor don't recurse
delete this;
return true;
}
return Base::event(event);
}
~RustObject() {
if (extra_destruct)
extra_destruct(this);
if (rust_object) {
auto r = rust_object;
rust_object = { nullptr, nullptr };
RustObject_destruct(r);
}
}
};
struct RustObjectDescription {
size_t size;
const QMetaObject *baseMetaObject;
QObject *(*create)(const TraitObject*);
void (*qmlConstruct)(void*, const TraitObject*, void (*extra_destruct)(QObject *));
};
template<typename T>
const RustObjectDescription *rustObjectDescription() {
static RustObjectDescription desc {
sizeof(T),
&T::staticMetaObject,
[](const TraitObject *self) -> QObject* {
auto q = new T();
q->rust_object = *self;
return q;
},
[](void *data, const TraitObject *self, void (*extra_destruct)(QObject *)) {
auto *q = new (data) T();
q->rust_object = *self;
q->extra_destruct = extra_destruct;
},
};
return &desc;
}
using CreatorFunction = void (*)(void *);
// Hack to access QMetaType::registerConverterFunction which is private, but ConverterFunctor
// is a friend
namespace QtPrivate {
template<>
struct ConverterFunctor<TraitObject, TraitObject, TraitObject> : public AbstractConverterFunction
{
using AbstractConverterFunction::AbstractConverterFunction;
bool registerConverter(int from, int to) {
return QMetaType::registerConverterFunction(this, from, to);
}
};
}