-
Notifications
You must be signed in to change notification settings - Fork 0
/
JavaConvert.cpp
444 lines (389 loc) · 18.9 KB
/
JavaConvert.cpp
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// Copyright (c) 2013-2013 Josh Blum
// 2019 Nicholas Corgan
// SPDX-License-Identifier: BSL-1.0
#include <Pothos/Plugin.hpp>
#include "JavaProxy.hpp"
#include <Poco/Bugcheck.h>
#include <cstdint>
#include <cassert>
#include <cstring>
#include <type_traits>
/***********************************************************************
* Null
**********************************************************************/
static Pothos::Proxy convertNullToJNull(Pothos::ProxyEnvironment::Sptr env, const Pothos::NullObject &)
{
return std::dynamic_pointer_cast<JavaProxyEnvironment>(env)->makeHandle((jobject)nullptr);
}
static Pothos::NullObject convertJNullToNull(const Pothos::Proxy &)
{
return Pothos::NullObject();
}
pothos_static_block(pothosRegisterJavaNullConversions)
{
Pothos::PluginRegistry::addCall("/proxy/converters/java/null_to_jnull",
&convertNullToJNull);
Pothos::PluginRegistry::add("/proxy/converters/java/jnull_to_null",
Pothos::ProxyConvertPair("", &convertJNullToNull));
}
/***********************************************************************
* jvalue templated helpers
**********************************************************************/
template <char sig, typename T>
static jvalue numToJvalue(const T &num)
{
jvalue value;
switch (sig) //a good compiler should simplify out the switch
{
case 'Z': value.z = jboolean(num); return value;
case 'C': value.c = jchar(num); return value;
case 'B': value.b = jbyte(num); return value;
case 'S': value.s = jshort(num); return value;
case 'I': value.i = jint(num); return value;
case 'J': value.j = jlong(num); return value;
case 'F': value.f = jfloat(num); return value;
case 'D': value.d = jdouble(num); return value;
}
poco_bugcheck_msg(std::string(1, sig).c_str()); throw;
}
template <char sig, typename T>
static T jvalueToNum(const jvalue &value)
{
switch (sig) //a good compiler should simplify out the switch
{
case 'Z': return T(value.z);
case 'C': return T(value.c);
case 'B': return T(value.b);
case 'S': return T(value.s);
case 'I': return T(value.i);
case 'J': return T(value.j);
case 'F': return T(value.f);
case 'D': return T(value.d);
}
poco_bugcheck_msg(std::string(1, sig).c_str()); throw;
}
/***********************************************************************
* Convert to Java Primitives
**********************************************************************/
template <char sig, typename T>
static Pothos::Proxy convertNumToJPrimitive(Pothos::ProxyEnvironment::Sptr env, const T &num)
{
return std::dynamic_pointer_cast<JavaProxyEnvironment>(env)->makeHandle(numToJvalue<sig>(num), sig);
}
template <char sig, typename T>
static T convertJPrimitiveToNum(const Pothos::Proxy &proxy)
{
auto handle = std::dynamic_pointer_cast<JavaProxyHandle>(proxy.getHandle());
assert(sig == handle->sig);
return jvalueToNum<sig, T>(handle->value);
}
pothos_static_block(pothosRegisterJavaPrimitiveConversions)
{
Pothos::PluginRegistry::addCall("/proxy/converters/java/bool_to_jboolean",
&convertNumToJPrimitive<'Z', bool>);
Pothos::PluginRegistry::add("/proxy/converters/java/jboolean_to_bool",
Pothos::ProxyConvertPair("Z", &convertJPrimitiveToNum<'Z', bool>));
Pothos::PluginRegistry::addCall("/proxy/converters/java/char_to_jchar",
&convertNumToJPrimitive<'C', char>);
Pothos::PluginRegistry::add("/proxy/converters/java/jchar_to_char",
Pothos::ProxyConvertPair("C", &convertJPrimitiveToNum<'C', char>));
Pothos::PluginRegistry::addCall("/proxy/converters/java/schar_to_jbyte",
&convertNumToJPrimitive<'B', signed char>);
Pothos::PluginRegistry::addCall("/proxy/converters/java/uchar_to_jbyte",
&convertNumToJPrimitive<'B', unsigned char>);
Pothos::PluginRegistry::add("/proxy/converters/java/jbyte_to_int8",
Pothos::ProxyConvertPair("B", &convertJPrimitiveToNum<'B', int8_t>));
Pothos::PluginRegistry::addCall("/proxy/converters/java/sshort_to_jshort",
&convertNumToJPrimitive<'S', signed short>);
Pothos::PluginRegistry::addCall("/proxy/converters/java/ushort_to_jshort",
&convertNumToJPrimitive<'S', unsigned short>);
Pothos::PluginRegistry::add("/proxy/converters/java/jshort_to_int16",
Pothos::ProxyConvertPair("S", &convertJPrimitiveToNum<'S', int16_t>));
Pothos::PluginRegistry::addCall("/proxy/converters/java/sint_to_jint",
&convertNumToJPrimitive<'I', signed int>);
Pothos::PluginRegistry::addCall("/proxy/converters/java/uint_to_jint",
&convertNumToJPrimitive<'I', unsigned int>);
Pothos::PluginRegistry::add("/proxy/converters/java/jint_to_int32",
Pothos::ProxyConvertPair("I", &convertJPrimitiveToNum<'I', int32_t>));
#ifndef POCO_LONG_IS_64_BIT
Pothos::PluginRegistry::addCall("/proxy/converters/java/slong_to_jint",
&convertNumToJPrimitive<'I', signed long>);
Pothos::PluginRegistry::addCall("/proxy/converters/java/ulong_to_jint",
&convertNumToJPrimitive<'I', unsigned long>);
#else
Pothos::PluginRegistry::addCall("/proxy/converters/java/slong_to_jlong",
&convertNumToJPrimitive<'J', signed long>);
Pothos::PluginRegistry::addCall("/proxy/converters/java/ulong_to_jlong",
&convertNumToJPrimitive<'J', unsigned long>);
#endif
Pothos::PluginRegistry::addCall("/proxy/converters/java/sllong_to_jlong",
&convertNumToJPrimitive<'J', signed long long>);
Pothos::PluginRegistry::addCall("/proxy/converters/java/ullong_to_jlong",
&convertNumToJPrimitive<'J', unsigned long long>);
Pothos::PluginRegistry::add("/proxy/converters/java/jlong_to_int64",
Pothos::ProxyConvertPair("J", &convertJPrimitiveToNum<'J', int64_t>));
Pothos::PluginRegistry::addCall("/proxy/converters/java/float_to_jfloat",
&convertNumToJPrimitive<'F', float>);
Pothos::PluginRegistry::add("/proxy/converters/java/jfloat_to_float",
Pothos::ProxyConvertPair("F", &convertJPrimitiveToNum<'F', float>));
Pothos::PluginRegistry::addCall("/proxy/converters/java/double_to_jdouble",
&convertNumToJPrimitive<'D', double>);
Pothos::PluginRegistry::add("/proxy/converters/java/jdouble_to_double",
Pothos::ProxyConvertPair("D", &convertJPrimitiveToNum<'D', double>));
}
/***********************************************************************
* String
**********************************************************************/
static Pothos::Proxy convertStringToJString(Pothos::ProxyEnvironment::Sptr env, const std::string &s)
{
auto jenv = std::dynamic_pointer_cast<JavaProxyEnvironment>(env);
return jenv->makeHandle(jenv->env->NewStringUTF(s.c_str()));
}
static std::string convertJStringToString(const Pothos::Proxy &proxy)
{
auto handle = std::dynamic_pointer_cast<JavaProxyHandle>(proxy.getHandle());
auto env = std::dynamic_pointer_cast<JavaProxyEnvironment>(proxy.getEnvironment());
return env->jstringToString(handle->value.l);
}
pothos_static_block(pothosRegisterJavaStringConversions)
{
Pothos::PluginRegistry::addCall("/proxy/converters/java/string_to_jstring",
&convertStringToJString);
Pothos::PluginRegistry::add("/proxy/converters/java/jstring_to_string",
Pothos::ProxyConvertPair("java.lang.String", &convertJStringToString));
}
/***********************************************************************
* Convert to Java Arrays
**********************************************************************/
// Utility functions
template <typename ctype>
using IsTypeBool = std::is_same<ctype, bool>;
template <typename ctype, typename jtype>
using DoTypesMatch = std::is_same<ctype, jtype>;
// If the C++ vector and the Java array are the same type under the hood,
// we can directly memcpy the values. This does not hold for bool, as
// std::vector<bool>'s specialization does not provide data().
template <typename ctype, typename jtype, typename retType>
using EnableForMemcpy = typename std::enable_if<DoTypesMatch<ctype, jtype>::value && !IsTypeBool<ctype>::value, retType>;
// If the C++ vector and the Java array are different types under the hood,
// or if the C++ type is a bool, we cannot use the memcpy optimization and must
// copy and cast each element individually.
template <typename ctype, typename jtype, typename retType>
using EnableForStaticCast = typename std::enable_if<!DoTypesMatch<ctype, jtype>::value || IsTypeBool<ctype>::value, retType>;
template <typename ctype, typename jtype>
static typename EnableForMemcpy<ctype, jtype, void>::type copyNumericVectorToJArray(
jtype* jElems,
const std::vector<ctype>& cppVec)
{
std::memcpy(
jElems,
const_cast<std::vector<ctype>&>(cppVec).data(),
(sizeof(ctype) * cppVec.size()));
}
template <typename ctype, typename jtype>
static typename EnableForStaticCast<ctype, jtype, void>::type copyNumericVectorToJArray(
jtype* jElems,
const std::vector<ctype>& cppVec)
{
for (size_t i = 0; i < cppVec.size(); i++)
{
jElems[i] = static_cast<jtype>(cppVec[i]);
}
}
template <typename ctype, typename jtype>
static typename EnableForMemcpy<ctype, jtype, void>::type copyJArrayToNumericVector(
std::vector<ctype>& cppVec,
const jtype* jElems)
{
std::memcpy(
cppVec.data(),
jElems,
(sizeof(ctype) * cppVec.size()));
}
template <typename ctype, typename jtype>
static typename EnableForStaticCast<ctype, jtype, void>::type copyJArrayToNumericVector(
std::vector<ctype>& cppVec,
const jtype* jElems)
{
for (size_t i = 0; i < cppVec.size(); i++)
{
cppVec[i] = static_cast<ctype>(jElems[i]);
}
}
// Functions used by Pothos for conversion
template <char sig, typename T>
Pothos::Proxy convertNumericVectorToJArray(Pothos::ProxyEnvironment::Sptr env, const std::vector<T> &vec)
{
auto jenv = std::dynamic_pointer_cast<JavaProxyEnvironment>(env);
switch (sig)
{
#define cnvtja(PrimitiveType, jtype) \
{ \
auto ar = jenv->env->New ## PrimitiveType ## Array(vec.size()); \
jtype *elems = jenv->env->Get ## PrimitiveType ## ArrayElements(ar, nullptr); \
copyNumericVectorToJArray<T, jtype>(elems, vec); \
jenv->env->Release ## PrimitiveType ## ArrayElements(ar, elems, 0 /*mode*/); \
return jenv->makeHandle(ar); \
}
case 'Z': cnvtja(Boolean, jboolean)
case 'C': cnvtja(Char, jchar)
case 'B': cnvtja(Byte, jbyte)
case 'S': cnvtja(Short, jshort)
case 'I': cnvtja(Int, jint)
case 'J': cnvtja(Long, jlong)
case 'F': cnvtja(Float, jfloat)
case 'D': cnvtja(Double, jdouble)
}
poco_bugcheck_msg(std::string(1, sig).c_str()); throw;
}
template <char sig, typename T>
std::vector<T> convertJArrayToNumericVector(const Pothos::Proxy &proxy)
{
auto jenv = std::dynamic_pointer_cast<JavaProxyEnvironment>(proxy.getEnvironment());
switch (sig)
{
#define cjatnv(PrimitiveType, jtype) \
{ \
auto ar = (jtype ## Array) std::dynamic_pointer_cast<JavaProxyHandle>(proxy.getHandle())->value.l; \
std::vector<T> vec(jenv->env->GetArrayLength(ar)); \
jtype *elems = jenv->env->Get ## PrimitiveType ## ArrayElements(ar, nullptr); \
copyJArrayToNumericVector<T, jtype>(vec, elems); \
jenv->env->Release ## PrimitiveType ## ArrayElements(ar, elems, JNI_ABORT); \
return vec; \
}
case 'Z': cjatnv(Boolean, jboolean)
case 'C': cjatnv(Char, jchar)
case 'B': cjatnv(Byte, jbyte)
case 'S': cjatnv(Short, jshort)
case 'I': cjatnv(Int, jint)
case 'J': cjatnv(Long, jlong)
case 'F': cjatnv(Float, jfloat)
case 'D': cjatnv(Double, jdouble)
}
poco_bugcheck_msg(std::string(1, sig).c_str()); throw;
}
pothos_static_block(pothosRegisterJavaArrayConversions)
{
Pothos::PluginRegistry::addCall("/proxy/converters/java/vecbool_to_jbooleanarray",
&convertNumericVectorToJArray<'Z', bool>);
Pothos::PluginRegistry::add("/proxy/converters/java/jbooleanarray_to_vecbool",
Pothos::ProxyConvertPair("[Z", &convertJArrayToNumericVector<'Z', bool>));
Pothos::PluginRegistry::addCall("/proxy/converters/java/vecchar_to_jchararray",
&convertNumericVectorToJArray<'C', char>);
Pothos::PluginRegistry::add("/proxy/converters/java/jchararray_to_vecchar",
Pothos::ProxyConvertPair("[C", &convertJArrayToNumericVector<'C', char>));
Pothos::PluginRegistry::addCall("/proxy/converters/java/vecschar_to_jbytearray",
&convertNumericVectorToJArray<'B', signed char>);
Pothos::PluginRegistry::addCall("/proxy/converters/java/vecuchar_to_jbytearray",
&convertNumericVectorToJArray<'B', unsigned char>);
Pothos::PluginRegistry::add("/proxy/converters/java/jbytearray_to_vecint8",
Pothos::ProxyConvertPair("[B", &convertJArrayToNumericVector<'B', int8_t>));
Pothos::PluginRegistry::addCall("/proxy/converters/java/vecsshort_to_jshortarray",
&convertNumericVectorToJArray<'S', signed short>);
Pothos::PluginRegistry::addCall("/proxy/converters/java/vecushort_to_jshortarray",
&convertNumericVectorToJArray<'S', unsigned short>);
Pothos::PluginRegistry::add("/proxy/converters/java/jshortarray_to_vecint16",
Pothos::ProxyConvertPair("[S", &convertJArrayToNumericVector<'S', int16_t>));
Pothos::PluginRegistry::addCall("/proxy/converters/java/vecsint_to_jintarray",
&convertNumericVectorToJArray<'I', signed int>);
Pothos::PluginRegistry::addCall("/proxy/converters/java/vecuint_to_jintarray",
&convertNumericVectorToJArray<'I', unsigned int>);
Pothos::PluginRegistry::add("/proxy/converters/java/jintarray_to_vecint32",
Pothos::ProxyConvertPair("[I", &convertJArrayToNumericVector<'I', int32_t>));
#ifndef POCO_LONG_IS_64_BIT
Pothos::PluginRegistry::addCall("/proxy/converters/java/vecslong_to_jintarray",
&convertNumericVectorToJArray<'I', signed long>);
Pothos::PluginRegistry::addCall("/proxy/converters/java/veculong_to_jintarray",
&convertNumericVectorToJArray<'I', unsigned long>);
#else
Pothos::PluginRegistry::addCall("/proxy/converters/java/vecslong_to_jlongarray",
&convertNumericVectorToJArray<'J', signed long>);
Pothos::PluginRegistry::addCall("/proxy/converters/java/veculong_to_jlongarray",
&convertNumericVectorToJArray<'J', unsigned long>);
#endif
Pothos::PluginRegistry::addCall("/proxy/converters/java/vecsllong_to_jlongarray",
&convertNumericVectorToJArray<'J', signed long long>);
Pothos::PluginRegistry::addCall("/proxy/converters/java/vecullong_to_jlongarray",
&convertNumericVectorToJArray<'J', unsigned long long>);
Pothos::PluginRegistry::add("/proxy/converters/java/jlongarray_to_vecint64",
Pothos::ProxyConvertPair("[J", &convertJArrayToNumericVector<'J', int64_t>));
Pothos::PluginRegistry::addCall("/proxy/converters/java/vecfloat_to_jfloatarray",
&convertNumericVectorToJArray<'F', float>);
Pothos::PluginRegistry::add("/proxy/converters/java/jfloatarray_to_vecfloat",
Pothos::ProxyConvertPair("[F", &convertJArrayToNumericVector<'F', float>));
Pothos::PluginRegistry::addCall("/proxy/converters/java/vecdouble_to_jdoublearray",
&convertNumericVectorToJArray<'D', double>);
Pothos::PluginRegistry::add("/proxy/converters/java/jdoublearray_to_vecdouble",
Pothos::ProxyConvertPair("[D", &convertJArrayToNumericVector<'D', double>));
}
/***********************************************************************
* Object Arrays
**********************************************************************/
/*
static Pothos::Proxy convertVectorToJObjectArray(Pothos::ProxyEnvironment::Sptr env, const Pothos::ProxyVector &vec)
{
auto jenv = std::dynamic_pointer_cast<JavaProxyEnvironment>(env);
auto ar = (jobjectArray) jenv->env->NewObjectArray(vec.size(), jenv->FindClass("java/lang/Object"), nullptr);
for (size_t i = 0; i < vec.size(); i++)
{
jenv->env->SetObjectArrayElement(ar, i, jenv->getHandle(vec[i])->toJobject());
}
return jenv->makeHandle(ar);
}
*/
static Pothos::ProxyVector convertJObjectArrayToVector(const Pothos::Proxy &proxy)
{
auto handle = std::dynamic_pointer_cast<JavaProxyHandle>(proxy.getHandle());
auto jenv = std::dynamic_pointer_cast<JavaProxyEnvironment>(proxy.getEnvironment());
auto ar = (jobjectArray) handle->value.l;
Pothos::ProxyVector vec(jenv->env->GetArrayLength(ar));
for (size_t i = 0; i < vec.size(); i++)
{
vec[i] = jenv->makeHandle(jenv->env->GetObjectArrayElement(ar, i));
}
return vec;
}
pothos_static_block(pothosRegisterJavaObjectArrayConversions)
{
//Pothos::PluginRegistry::addCall("/proxy/converters/java/vector_to_jarray",
// &convertVectorToJObjectArray);
Pothos::PluginRegistry::add("/proxy/converters/java/jarray_to_vector",
Pothos::ProxyConvertPair("[Ljava.lang.Object;", &convertJObjectArrayToVector));
}
/***********************************************************************
* Primitive Object representations
**********************************************************************/
template <typename T>
static T javaPrimitiveObjectToType(
const std::string &name,
const Pothos::Proxy &proxy)
{
return proxy.call<T>(name);
}
pothos_static_block(pothosRegisterJavaDefaultConversions)
{
Pothos::PluginRegistry::add("/proxy/converters/java/javalangboolean_to_bool",
Pothos::ProxyConvertPair("java.lang.Boolean", Pothos::Callable(
&javaPrimitiveObjectToType<bool>).bind("booleanValue", 0)));
Pothos::PluginRegistry::add("/proxy/converters/java/javalangcharacter_to_char",
Pothos::ProxyConvertPair("java.lang.Character", Pothos::Callable(
&javaPrimitiveObjectToType<char>).bind("charValue", 0)));
Pothos::PluginRegistry::add("/proxy/converters/java/javalangbyte_to_signed_char",
Pothos::ProxyConvertPair("java.lang.Byte", Pothos::Callable(
&javaPrimitiveObjectToType<signed char>).bind("byteValue", 0)));
Pothos::PluginRegistry::add("/proxy/converters/java/javalangshort_to_short",
Pothos::ProxyConvertPair("java.lang.Short", Pothos::Callable(
&javaPrimitiveObjectToType<signed short>).bind("shortValue", 0)));
Pothos::PluginRegistry::add("/proxy/converters/java/javalanginteger_to_int",
Pothos::ProxyConvertPair("java.lang.Integer", Pothos::Callable(
&javaPrimitiveObjectToType<signed int>).bind("intValue", 0)));
Pothos::PluginRegistry::add("/proxy/converters/java/javalanglong_to_long_long",
Pothos::ProxyConvertPair("java.lang.Long", Pothos::Callable(
&javaPrimitiveObjectToType<signed long long>).bind("longValue", 0)));
Pothos::PluginRegistry::add("/proxy/converters/java/javalangfloat_to_float",
Pothos::ProxyConvertPair("java.lang.Float", Pothos::Callable(
&javaPrimitiveObjectToType<float>).bind("floatValue", 0)));
Pothos::PluginRegistry::add("/proxy/converters/java/javalangdouble_to_double",
Pothos::ProxyConvertPair("java.lang.Double", Pothos::Callable(
&javaPrimitiveObjectToType<double>).bind("doubleValue", 0)));
}