-
Notifications
You must be signed in to change notification settings - Fork 3
/
method_calls.h
333 lines (289 loc) · 9.65 KB
/
method_calls.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#pragma once
#include "jni_types.h"
#include "object_test.h"
#include "type_signatures.h"
#include "wrappers.h"
#include <peripherals/stl/any_of.h>
namespace jnipp::invocation {
template<typename ArgType, typename InputType>
struct arg_pair
{
using arg_type = ArgType;
using in_type = InputType;
};
namespace arguments {
template<typename T>
requires std::is_same_v<T, jvalue>
inline void get_arg_value(std::vector<jvalue>& values, T arg1)
{
values.push_back(arg1);
}
template<typename T>
requires std::is_same_v<T, java::object>
inline void get_arg_value(std::vector<jvalue>& values, T arg1)
{
values.push_back(arg1.instance);
}
template<typename T>
requires(!std::is_same_v<T, jvalue> && !std::is_same_v<T, java::object>)
inline void get_arg_value(std::vector<jvalue>& values, T arg1)
{
java::type_wrapper<T> wrapper(arg1);
values.push_back(wrapper);
}
template<typename... Args>
inline std::vector<jvalue> get_args(Args... args)
{
std::vector<jvalue> out;
(get_arg_value(out, args), ...);
return out;
}
} // namespace arguments
namespace call {
enum class calling_method
{
instanced_,
static_,
};
void check_exception();
template<return_type Type, calling_method Calling, typename... Args>
inline auto call_no_except(
java::clazz clazz, java::object object, java::method method, Args... args)
{
auto values = arguments::get_args(std::forward<Args>(args)...);
if constexpr(Type == return_type::bool_)
{
return Calling == calling_method::static_
? GetJNI()->CallStaticBooleanMethodA(
clazz, *method, values.data())
: GetJNI()->CallBooleanMethodA(
object, *method, values.data());
} else if constexpr(Type == return_type::byte_)
{
return Calling == calling_method::static_
? GetJNI()->CallStaticByteMethodA(
clazz, *method, values.data())
: GetJNI()->CallByteMethodA(object, *method, values.data());
} else if constexpr(Type == return_type::char_)
{
return Calling == calling_method::static_
? GetJNI()->CallStaticCharMethodA(
clazz, *method, values.data())
: GetJNI()->CallCharMethodA(object, *method, values.data());
} else if constexpr(Type == return_type::short_)
{
return Calling == calling_method::static_
? GetJNI()->CallStaticShortMethodA(
clazz, *method, values.data())
: GetJNI()->CallShortMethodA(object, *method, values.data());
} else if constexpr(Type == return_type::int_)
{
return Calling == calling_method::static_
? GetJNI()->CallStaticIntMethodA(
clazz, *method, values.data())
: GetJNI()->CallIntMethodA(object, *method, values.data());
} else if constexpr(Type == return_type::long_)
{
return Calling == calling_method::static_
? GetJNI()->CallStaticLongMethodA(
clazz, *method, values.data())
: GetJNI()->CallLongMethodA(object, *method, values.data());
} else if constexpr(Type == return_type::float_)
{
return Calling == calling_method::static_
? GetJNI()->CallStaticFloatMethodA(
clazz, *method, values.data())
: GetJNI()->CallFloatMethodA(object, *method, values.data());
} else if constexpr(Type == return_type::double_)
{
return Calling == calling_method::static_
? GetJNI()->CallStaticDoubleMethodA(
clazz, *method, values.data())
: GetJNI()->CallDoubleMethodA(
object, *method, values.data());
} else if constexpr(
Type == return_type::object_ || Type == return_type::object_array_)
{
return java::object{
java::clazz(nullptr),
Calling == calling_method::static_
? GetJNI()->CallStaticObjectMethodA(
clazz, *method, values.data())
: GetJNI()->CallObjectMethodA(object, *method, values.data())};
} else if constexpr(stl_types::one_of(
Type,
return_type::bool_array_,
return_type::char_array_,
return_type::short_array_,
return_type::int_array_,
return_type::long_array_,
return_type::float_array_,
return_type::double_array_))
{
return java::object{
java::clazz(nullptr),
Calling == calling_method::static_
? GetJNI()->CallStaticObjectMethodA(
clazz, *method, values.data())
: GetJNI()->CallObjectMethodA(object, *method, values.data())}
.array()
.value();
} else if constexpr(Type == return_type::void_)
{
if constexpr(Calling == calling_method::static_)
GetJNI()->CallStaticVoidMethodA(clazz, *method, values.data());
else
GetJNI()->CallVoidMethodA(object, *method, values.data());
}
}
template<return_type Type, calling_method Calling, typename... Args>
inline auto call(
java::clazz clazz, java::object obj, java::method method, Args... args);
} // namespace call
template<return_type RType, typename... Args>
struct instance_call
{
instance_call(java::method_reference const& method)
: method(method)
{
}
inline auto operator()(Args... args)
{
if constexpr(RType == return_type::void_)
call::call<RType, call::calling_method::instanced_>(
{},
method.instance,
method.method,
std::forward<Args>(args)...);
else
return call::call<RType, call::calling_method::instanced_>(
{},
method.instance,
method.method,
std::forward<Args>(args)...);
}
java::method_reference method;
};
template<return_type RType, typename... Args>
struct static_call
{
static_call(java::static_method_reference const& method)
: method(method)
{
}
inline auto operator()(Args... args)
{
if constexpr(RType == return_type::void_)
call::call<RType, call::calling_method::static_>(
method.clazz, {}, method.method, std::forward<Args>(args)...);
else
return call::call<RType, call::calling_method::static_>(
method.clazz, {}, method.method, std::forward<Args>(args)...);
}
java::static_method_reference method;
};
template<typename... Args>
struct constructor_call
{
constructor_call(java::static_method_reference const& method)
: method(method)
{
}
inline optional<java::object> operator()(Args... args)
{
std::vector<jvalue> values = arguments::get_args(args...);
auto instance =
GetJNI()->NewObject(method.clazz, *method.method, values.data());
java::object out(method.clazz, instance);
if(!java::objects::not_null(out))
return {};
return java::object{method.clazz, instance};
}
java::static_method_reference method;
};
} // namespace jnipp::invocation
namespace jnipp::wrapping {
template<return_type RType, typename... Args>
struct jmethod
{
jmethod(java::method&& method)
: method(std::move(method))
{
}
template<return_type T2>
/*!
* \brief define a POD or POD array return type
* \return
*/
jmethod<T2, Args...> ret()
{
method.ret(type_signature::to_str<T2>());
return {std::move(method)};
}
/*!
* \brief define an object return type
* \param ret_type Java class name for return type
* \return
*/
jmethod<return_type::object_, Args...> ret(std::string const& ret_type)
{
method.ret(type_signature::classify(ret_type));
method.return_class = ret_type;
return {std::move(method)};
}
template<return_type T2>
requires(T2 == return_type::object_array_)
/*!
* \brief define an object array return type
* \param type Java class name of returned type
* \return
*/
jmethod<return_type::object_array_, Args...> ret(std::string const& type)
{
method.ret(type_signature::to_str<T2>(type));
method.return_class = type;
return {std::move(method)};
}
template<typename T2>
/*!
* \brief define argument with POD or POD array type
* \return
*/
jmethod<RType, Args..., T2> arg()
{
method.arg(type_signature::to_str<T2>());
return {std::move(method)};
}
template<typename T2>
/*!
* \brief define custom argument type, will call on
* jnipp::java::type_wrapper<T2> to translate argument
* \param type Java class name for argument, or JNI POD type
* \return
*/
jmethod<RType, Args..., T2> arg(std::string const& type)
{
method.arg(type_signature::classify(type));
return {std::move(method)};
}
/*!
* \brief define class or POD argument type using jvalue
* \param type
* \return
*/
jmethod<RType, Args..., ::jvalue> arg(std::string const& type)
{
method.arg(type_signature::classify(type));
return {std::move(method)};
}
const char* name() const
{
return method.name.c_str();
}
const char* signature() const
{
return method.signature.c_str();
}
java::method method;
};
} // namespace jnipp::wrapping