-
Notifications
You must be signed in to change notification settings - Fork 3
/
jni_types.h
279 lines (229 loc) · 4.96 KB
/
jni_types.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
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
#pragma once
#ifndef FORCEDINLINE
#define FORCEDINLINE inline
#endif
#include <jni.h>
#include <string>
namespace jnipp {
template<typename T>
struct is_jarray
{
static constexpr bool value =
std::is_base_of<_jarray, typename std::remove_pointer<T>::type>::value;
};
enum class return_type
{
byte_,
char_,
short_,
int_,
long_,
bool_,
float_,
double_,
byte_array_,
char_array_,
short_array_,
int_array_,
long_array_,
bool_array_,
float_array_,
double_array_,
object_array_,
array_,
object_,
void_,
};
extern JNIEnv* GetJNI();
template<typename T>
using optional = std::optional<T>;
namespace java {
struct clazz
{
clazz(std::string const& name)
: name(name)
{
}
clazz(::jclass ref)
: class_ref(ref)
{
}
clazz()
{
}
operator const char*() const
{
return name->c_str();
}
operator ::jclass() const
{
return *class_ref;
}
optional<std::string> name;
optional<::jclass> class_ref;
};
struct method
{
method(std::string const& name)
: name(name)
, signature("()V")
{
}
method(
::jmethodID method_id,
optional<std::string> return_class = std::nullopt)
: method_id(method_id)
, return_class(return_class)
{
}
inline std::string returnType()
{
auto returnSplit = signature.find(")") + 1;
return signature.substr(returnSplit, std::string::npos);
}
inline std::string argList()
{
auto returnSplit = signature.find("(");
auto endSplit = signature.find(")");
return signature.substr(returnSplit + 1, endSplit - returnSplit - 1);
}
void ret(std::string const& retType)
{
auto returnSplit = signature.find(")");
signature = signature.substr(0, returnSplit + 1) + retType;
}
void arg(std::string const& argType)
{
auto endSplit = signature.find(")");
auto begin = signature.substr(0, endSplit);
auto end = signature.substr(endSplit);
signature = begin + argType + end;
}
::jmethodID operator*() const
{
return *method_id;
}
std::string name;
std::string signature;
std::optional<std::string> return_class;
optional<::jmethodID> method_id;
};
struct field
{
field(std::string const& name)
: name(name)
{
}
field(::jfieldID field_id, optional<std::string> field_class = std::nullopt)
: field_id(field_id)
, signature(field_class.value_or(std::string()))
{
}
field& withType(std::string const& sig)
{
signature = sig;
return *this;
}
::jfieldID operator*() const
{
return *field_id;
}
std::string name;
std::string signature;
optional<::jfieldID> field_id;
};
struct array
{
::jarray instance;
::jclass value_class;
std::string value_type;
operator ::jarray() const
{
return instance;
}
jlong length() const
{
return GetJNI()->GetArrayLength(instance);
}
template<typename T>
static array of(T* arrayType)
{
return array{reinterpret_cast<::jarray>(arrayType)};
}
};
struct object
{
object(java::clazz clazz, ::jobject instance)
: clazz(*clazz.class_ref)
, instance(instance)
{
}
object()
{
}
optional<::jclass> clazz;
::jobject instance;
operator ::jobject() const
{
return instance;
}
optional<java::array> array() const
{
if(!instance)
return {};
return java::array{reinterpret_cast<::jarray>(instance)};
}
inline operator bool() const
{
return instance;
}
};
using value = optional<::jvalue>;
template<typename T>
inline value make_value(T val)
{
::jvalue out;
if constexpr(std::is_same_v<T, jboolean>)
out.z = val;
else if constexpr(std::is_same_v<T, jbyte>)
out.b = val;
else if constexpr(std::is_same_v<T, jchar>)
out.b = val;
else if constexpr(std::is_same_v<T, jshort>)
out.s = val;
else if constexpr(std::is_same_v<T, jint>)
out.i = val;
else if constexpr(std::is_same_v<T, jlong>)
out.j = val;
else if constexpr(std::is_same_v<T, jfloat>)
out.f = val;
else if constexpr(std::is_same_v<T, jdouble>)
out.d = val;
else if constexpr(std::is_same_v<T, jobject>)
out.l = val;
else
return {};
return out;
}
struct method_reference
{
java::object instance;
java::method method;
};
struct static_method_reference
{
java::clazz clazz;
java::method method;
};
struct field_reference
{
java::object instance;
java::field field;
};
struct static_field_reference
{
java::clazz clazz;
java::field field;
};
} // namespace java
} // namespace jnipp