ug4
type_converter.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2014: Steinbeis Forschungszentrum (STZ Ölbronn)
3  * Author: Michael Hoffer
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef TYPE_CONVERTER_H
34 #define TYPE_CONVERTER_H
35 
36 #include <jni.h>
37 #include <string>
38 #include <vector>
39 #include "registry/class.h"
40 #include "registry/registry.h"
41 #include "messaging.h"
42 
43 
44 // we are not sure whether NULL is equivalent to null object in Java.
45 // thus, we use this define that allows us to change that easily in the future
46 #define JNULL NULL;
47 
48 namespace ug {
49 namespace vrl {
50 
51 // added by Christian Poliwoda
52 // christian.poliwoda@gcsc.uni-frankfurt.de
53 // y 13 m 10 d 31
59 struct TypeAndArray {
61  bool isArray;
62 };
63 
70 jstring stringC2J(JNIEnv *env, const char* s);
71 
84 std::string stringJ2C(JNIEnv *env, jstring const& s);
85 
92 jobjectArray stringArrayC2J(JNIEnv *env, const std::string* strings);
93 
100 jobjectArray stringArrayC2J(
101  JNIEnv *env,
102  std::vector<std::string> const& strings);
103 
116 std::vector<std::string> stringArrayJ2C(
117  JNIEnv *env, jobjectArray const& array);
118 
124 inline bool boolJ2C(jboolean b) {
125  return b == JNI_TRUE;
126 }
127 
133 inline jboolean boolC2J(bool b) {
134  return b ? JNI_TRUE : JNI_FALSE;
135 }
136 
143 jobject boolean2JObject(JNIEnv *env, jboolean value);
144 
151 jboolean jObject2Boolean(JNIEnv *env, jobject obj);
152 
159 jobject int2JObject(JNIEnv *env, jint value);
160 
167 jint jObject2Int(JNIEnv *env, jobject obj);
168 
175 jobject double2JObject(JNIEnv *env, jdouble value);
176 
183 jdouble jObject2Double(JNIEnv *env, jobject obj);
184 
191 jobject string2JObject(JNIEnv *env, const char* value);
192 
199 std::string jObject2String(JNIEnv *env, jobject obj);
200 
207 jobject pointer2JObject(JNIEnv *env, void* value);
208 
215 jobject constPointer2JObject(JNIEnv *env, const void* value);
216 
223 jobject smartPointer2JObject(JNIEnv *env, SmartPtr<void> value);
224 
231 jobject constSmartPointer2JObject(JNIEnv *env, ConstSmartPtr<void> value);
232 
241 bool isJSmartPointerConst(JNIEnv *env, jobject ptr);
242 
249 void invalidateJSmartPointer(JNIEnv *env, jobject obj);
250 
257 void invalidateJConstSmartPointer(JNIEnv *env, jobject obj);
258 
265 bool isConstJPtr(JNIEnv* env, jobject obj);
266 
273 void* jObject2Pointer(JNIEnv *env, jobject obj);
274 
281 std::string jPointerGetName(JNIEnv *env, jobject obj);
282 
283 
284 // added by Christian Poliwoda
285 // christian.poliwoda@gcsc.uni-frankfurt.de
286 // y 13 m 05 d 28
287 bool isJObjectAnArray(JNIEnv *env, jobject value);
288 
289 void jObject2BoolVector(JNIEnv *env, jobject object, std::vector<bool>& bv);
290 void jObject2IntVector(JNIEnv *env, jobject object, std::vector<int>& iv);
291 void jObject2SizetVector(JNIEnv *env, jobject object, std::vector<size_t>& stv);
292 void jObject2NumberVector(JNIEnv *env, jobject object, std::vector<number>& nv);
293 void jObject2stdStringVector(JNIEnv *env, jobject object, std::vector<std::string>& sv);
295 (
296  JNIEnv *env,
297  jobject object,
298  ug::Variant::Type jo_type,
300  std::vector<std::pair<void*, const ug::bridge::ClassNameNode*> >& pv
301 );
303 (
304  JNIEnv *env,
305  jobject object,
307  std::vector<std::pair<SmartPtr<void>, const ug::bridge::ClassNameNode*> >& pv
308 );
310 (
311  JNIEnv *env,
312  jobject object,
313  ug::Variant::Type jo_type,
315  std::vector<std::pair<ConstSmartPtr<void>, const ug::bridge::ClassNameNode*> >& pv
316 );
317 
324 jobject boolVector2JObject(JNIEnv* env, const std::vector<bool>& bv);
325 
332 jobject intVector2JObject(JNIEnv* env, const std::vector<int>& iv);
333 
340 jobject sizetVector2JObject(JNIEnv* env, const std::vector<size_t>& iv);
341 
348 jobject numberVector2JObject(JNIEnv* env, const std::vector<number>& nv);
349 
356 jobject cStringVector2JObject(JNIEnv* env, const std::vector<const char*>& sv);
357 
364 jobject stdStringVector2JObject(JNIEnv* env, const std::vector<std::string>& sv);
365 
372 jobject ptrVector2JObject(JNIEnv* env, const std::vector<void*>& pv);
373 
380 jobject constPtrVector2JObject(JNIEnv* env, const std::vector<const void*>& pv);
381 
388 jobject smartPtrVector2JObject(JNIEnv* env, const std::vector<SmartPtr<void> >& pv);
389 
396 jobject constSmartPtrVector2JObject(JNIEnv* env, const std::vector<ConstSmartPtr<void> >& pv);
397 
407 void jobjectArray2ParamStack(JNIEnv *env,
409  ug::bridge::ParameterStack& paramsOut,
410  const ug::bridge::ParameterInfo& paramsTemplate,
411  jobjectArray const& array);
412 
420 jobjectArray createEmptyJavaArray(
421  JNIEnv *env, std::string className);
422 
430 jobjectArray createEmptyJavaArray(
431  JNIEnv *env, jclass elementClass);
432 
440 jobject param2JObject(JNIEnv *env,
441  ug::bridge::ParameterStack& params, size_t index);
442 
449 jobject getClass(JNIEnv *env, jobject obj);
450 
457 std::string getClassName(JNIEnv *env, jobject obj);
458 
465 std::string getParamClassName(JNIEnv *env, jobject obj);
466 
475 TypeAndArray paramClass2ParamType(JNIEnv *env, jobject obj);
476 
488 bool compareParamTypes(JNIEnv *env, jobjectArray params,
490  const ug::bridge::ParameterInfo& paramStack,
491  bool allowSmartToRawPtrConversion = false);
492 
499 const std::vector<const ug::bridge::IExportedClass*> getParentClasses(
501  const ug::bridge::IExportedClass* clazz);
502 
503 
510 jobject registry2NativeAPI(JNIEnv *env, ug::bridge::Registry* reg);
511 
516 std::vector<const char*> getBaseClassNames(const ug::bridge::ClassNameNode* node);
517 
522 std::string getParamTypeAsString(const uint type);
523 
528 std::string getParamTypesAsString(JNIEnv *env, jobjectArray const& array);
529 
536 void throwUgErrorAsJavaException(JNIEnv *env, ug::UGError error);
537 
544 void throwUgErrorAsJavaException(JNIEnv *env, std::string error);
545 
546 }// end vrl::
547 }// end ug::
548 
549 #endif /* TYPE_CONVERTER_H */
550 
Definition: smart_pointer.h:650
Definition: smart_pointer.h:525
Instances of this class or of derived classes are thrown if errors arise.
Definition: error.h:104
Type
Definition: variant.h:89
node for class names
Definition: class_name_provider.h:65
Base class for exported Classes.
Definition: class.h:499
a stack holding parameter infos about a parameter stack
Definition: parameter_stack.h:73
A stack that can hold values together with their type-id.
Definition: parameter_stack.h:270
Registry for functions and classes that are exported to scripts and visualizations.
Definition: registry.h:138
unsigned int uint
Definition: types.h:114
std::vector< std::string > stringArrayJ2C(JNIEnv *env, jobjectArray const &array)
Definition: type_converter.cpp:146
bool boolJ2C(jboolean b)
Definition: type_converter.h:124
bool isJSmartPointerConst(JNIEnv *env, jobject ptr)
Definition: type_converter.cpp:420
std::string getParamClassName(JNIEnv *env, jobject obj)
Definition: type_converter.cpp:532
void invalidateJSmartPointer(JNIEnv *env, jobject obj)
Definition: type_converter.cpp:312
jobject intVector2JObject(JNIEnv *env, const std::vector< int > &iv)
Definition: type_converter.cpp:1460
bool compareParamTypes(JNIEnv *env, jobjectArray params, ug::bridge::Registry *reg, ug::bridge::ParameterInfo const &paramStack, bool allowSmartToRawPtrConversion)
Definition: type_converter.cpp:813
std::string getParamTypeAsString(const uint type)
Definition: type_converter.cpp:622
void jObject2stdStringVector(JNIEnv *env, jobject object, std::vector< std::string > &sv)
Definition: type_converter.cpp:1326
void jObject2IntVector(JNIEnv *env, jobject object, std::vector< int > &iv)
Definition: type_converter.cpp:1284
const std::vector< const ug::bridge::IExportedClass * > getParentClasses(ug::bridge::Registry *reg, const ug::bridge::IExportedClass *clazz)
Definition: type_converter.cpp:164
void jObject2SizetVector(JNIEnv *env, jobject object, std::vector< size_t > &stv)
Definition: type_converter.cpp:1298
jobject sizetVector2JObject(JNIEnv *env, const std::vector< size_t > &iv)
Definition: type_converter.cpp:1474
jobject boolean2JObject(JNIEnv *env, jboolean value)
Definition: type_converter.cpp:205
jobjectArray stringArrayC2J(JNIEnv *env, const std::string *strings, const size_t array_length)
Definition: type_converter.cpp:59
jobject boolVector2JObject(JNIEnv *env, const std::vector< bool > &bv)
Definition: type_converter.cpp:1445
jobject getClass(JNIEnv *env, jobject obj)
Definition: type_converter.cpp:508
std::string stringJ2C(JNIEnv *env, jstring const &s)
Definition: type_converter.cpp:52
jobject constSmartPtrVector2JObject(JNIEnv *env, const std::vector< ConstSmartPtr< void > > &pv)
Definition: type_converter.cpp:1572
jobject numberVector2JObject(JNIEnv *env, const std::vector< number > &nv)
Definition: type_converter.cpp:1488
jdouble jObject2Double(JNIEnv *env, jobject obj)
Definition: type_converter.cpp:199
std::vector< const char * > getBaseClassNames(const ug::bridge::ClassNameNode *node)
Definition: type_converter.cpp:217
std::string jPointerGetName(JNIEnv *env, jobject obj)
Definition: type_converter.cpp:249
jint jObject2Int(JNIEnv *env, jobject obj)
Definition: type_converter.cpp:187
void jObject2BoolVector(JNIEnv *env, jobject object, std::vector< bool > &bv)
Definition: type_converter.cpp:1270
jobject int2JObject(JNIEnv *env, jint value)
Definition: type_converter.cpp:181
jobject constSmartPointer2JObject(JNIEnv *env, ConstSmartPtr< void > value)
Definition: type_converter.cpp:397
bool isJObjectAnArray(JNIEnv *env, jobject value)
Definition: type_converter.cpp:1257
void * jObject2Pointer(JNIEnv *env, jobject obj)
Definition: type_converter.cpp:236
jobject smartPointer2JObject(JNIEnv *env, SmartPtr< void > value)
Definition: type_converter.cpp:374
std::string getClassName(JNIEnv *env, jobject obj)
Definition: type_converter.cpp:517
jboolean jObject2Boolean(JNIEnv *env, jobject obj)
Definition: type_converter.cpp:211
jobject pointer2JObject(JNIEnv *env, void *value)
Definition: type_converter.cpp:352
jobject param2JObject(JNIEnv *env, ug::bridge::ParameterStack &params, size_t index)
Definition: type_converter.cpp:1192
jstring stringC2J(JNIEnv *env, const char *s)
Definition: type_converter.cpp:48
jobject constPointer2JObject(JNIEnv *env, const void *value)
Definition: type_converter.cpp:363
void throwUgErrorAsJavaException(JNIEnv *env, ug::UGError error)
Definition: type_converter.cpp:447
jboolean boolC2J(bool b)
Definition: type_converter.h:133
void jobjectArray2ParamStack(JNIEnv *env, ug::bridge::Registry *reg, ug::bridge::ParameterStack &paramsOut, const ug::bridge::ParameterInfo &paramsTemplate, jobjectArray const &array)
Definition: type_converter.cpp:956
jobject smartPtrVector2JObject(JNIEnv *env, const std::vector< SmartPtr< void > > &pv)
Definition: type_converter.cpp:1558
void jObject2ConstSmartPtrVector(JNIEnv *env, jobject object, ug::Variant::Type jo_type, ug::bridge::Registry *reg, std::vector< std::pair< ConstSmartPtr< void >, const ug::bridge::ClassNameNode * > > &pv)
Definition: type_converter.cpp:1409
jobject stdStringVector2JObject(JNIEnv *env, const std::vector< std::string > &sv)
Definition: type_converter.cpp:1516
void jObject2PtrVector(JNIEnv *env, jobject object, ug::Variant::Type jo_type, ug::bridge::Registry *reg, std::vector< std::pair< void *, const ug::bridge::ClassNameNode * > > &pv)
Definition: type_converter.cpp:1341
jobject cStringVector2JObject(JNIEnv *env, const std::vector< const char * > &sv)
Definition: type_converter.cpp:1502
jobject registry2NativeAPI(JNIEnv *env, ug::bridge::Registry *reg)
Definition: type_converter.cpp:2169
jobject constPtrVector2JObject(JNIEnv *env, const std::vector< const void * > &pv)
Definition: type_converter.cpp:1544
void jObject2SmartPtrVector(JNIEnv *env, jobject object, ug::bridge::Registry *reg, std::vector< std::pair< SmartPtr< void >, const ug::bridge::ClassNameNode * > > &pv)
Definition: type_converter.cpp:1382
jobject double2JObject(JNIEnv *env, jdouble value)
Definition: type_converter.cpp:193
void jObject2NumberVector(JNIEnv *env, jobject object, std::vector< number > &nv)
Definition: type_converter.cpp:1312
jobject ptrVector2JObject(JNIEnv *env, const std::vector< void * > &pv)
Definition: type_converter.cpp:1530
void invalidateJConstSmartPointer(JNIEnv *env, jobject obj)
Definition: type_converter.cpp:332
jobjectArray createEmptyJavaArray(JNIEnv *env, std::string className)
Definition: type_converter.cpp:91
TypeAndArray paramClass2ParamType(JNIEnv *env, jobject obj)
Definition: type_converter.cpp:677
bool isConstJPtr(JNIEnv *env, jobject obj)
Definition: type_converter.cpp:226
std::string getParamTypesAsString(JNIEnv *env, jobjectArray const &array)
Definition: type_converter.cpp:922
jobject string2JObject(JNIEnv *env, const char *value)
Definition: type_converter.cpp:439
std::string jObject2String(JNIEnv *env, jobject obj)
Definition: type_converter.cpp:443
the ug namespace
Definition: type_converter.h:59
bool isArray
Definition: type_converter.h:61
ug::Variant::Type type
Definition: type_converter.h:60