-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclass_wrapper.h
205 lines (165 loc) · 4.61 KB
/
class_wrapper.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
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
#pragma once
#include "field_access.h"
#include "jni_types.h"
#include "method_calls.h"
#include "type_signatures.h"
namespace jnipp::wrapping {
struct jobject;
struct jclass
{
jclass(::jclass clazz, std::string const& class_name)
: clazz(clazz)
, class_name(class_name)
{
this->clazz.name = type_signature::slashify(class_name);
}
jclass(std::string const& clazz)
: jclass(
GetJNI()->FindClass(type_signature::slashify(clazz).c_str()),
clazz)
{
}
template<typename... Args>
/*!
* \brief Construct an object using a constructor
* Constructors are always called "<init>"
* \param method
* \param args
* \return
*/
jobject construct(
jmethod<return_type::void_, Args...> const& method, Args... args);
/*!
* \brief Object instantiation, wraps an existing object
* \param instance
* \return
*/
jobject operator()(java::object instance);
jobject operator()(::jobject instance);
jobject operator()(java::value instance);
template<return_type RType, typename... Args>
/*!
* \brief Static method calls
* \param method
* \return
*/
invocation::static_call<RType, Args...> operator[](
jmethod<RType, Args...> const& method)
{
auto methodId = GetJNI()->GetStaticMethodID(
clazz, method.name(), method.signature());
invocation::call::check_exception();
return {
java::static_method_reference({
clazz,
java::method(methodId, method.method.return_class),
}),
};
}
template<return_type T>
/*!
* \brief Static fields
* \param field
* \return
*/
field_access::static_field<T> operator[](jfield<T> const& field)
{
auto fieldId =
GetJNI()->GetStaticFieldID(clazz, field.name(), field.signature());
invocation::call::check_exception();
return {
java::static_field_reference({
clazz,
java::field(fieldId, field.field.signature),
}),
};
}
inline operator std::string() const
{
return class_name;
}
java::clazz clazz;
std::string class_name;
};
struct jobject
{
jobject(java::object const& object)
: object(object)
{
// this->object.instance = GetJNI()->NewGlobalRef(object.instance);
}
~jobject()
{
// GetJNI()->DeleteGlobalRef(object.instance);
}
inline jobject cast(jclass const& clazz) const;
template<return_type RType, typename... Args>
invocation::instance_call<RType, Args...> operator[](
jmethod<RType, Args...> const& method)
{
auto methodId = GetJNI()->GetMethodID(
*object.clazz, method.name(), method.signature());
invocation::call::check_exception();
return {java::method_reference{
object,
java::method(methodId, method.method.return_class),
}};
}
template<return_type T>
field_access::instance_field<T> operator[](jfield<T> const& field)
{
auto fieldId = GetJNI()->GetFieldID(
*object.clazz, field.name(), field.signature());
invocation::call::check_exception();
return {java::field_reference{
object,
java::field(fieldId, field.field.signature),
}};
}
inline operator bool() const
{
return static_cast<bool>(object);
}
inline operator ::jvalue() const
{
return ::jvalue{
.l = object,
};
}
inline operator java::object() const
{
return object;
}
java::object object;
};
template<typename... Args>
inline jobject jclass::construct(
jmethod<return_type::void_, Args...> const& method, Args... args)
{
auto constructor =
GetJNI()->GetMethodID(clazz, method.name(), method.signature());
invocation::call::check_exception();
return jobject{java::object{
{clazz},
*invocation::constructor_call({clazz, constructor})(args...),
}};
}
inline jobject jclass::operator()(java::object instance)
{
return jobject(java::object{clazz, instance.instance});
}
inline jobject jclass::operator()(::jobject instance)
{
return (*this)(java::object({}, instance));
}
inline jobject jclass::operator()(java::value instance)
{
return (*this)(java::object({}, instance->l));
}
} // namespace jnipp::wrapping
namespace jnipp::java::objects {
inline bool is_null(wrapping::jobject const& obj)
{
return obj.object.instance;
}
} // namespace jnipp::java::objects