10e7c9d4d3a67c3575e2e76b7830029d48e1f5c6
[platform/upstream/iotivity.git] / service / simulator / java / jni / simulator_common_jni.h
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 #ifndef SIMULATOR_COMMON_JNI_H_
22 #define SIMULATOR_COMMON_JNI_H_
23
24 #include <jni.h>
25
26 typedef struct
27 {
28     jclass classInteger;
29     jclass classDouble;
30     jclass classString;
31     jclass classHashMap;
32     jclass classVector;
33     jclass classSimulatorResource;
34     jclass classSimulatorResourceModel;
35     jclass classSimulatorResourceAttribute;
36     jclass classSimulatorRemoteResource;
37     jclass classSimulatorCallback;
38
39     jmethodID classIntegerCtor;
40     jmethodID classDoubleCtor;
41     jmethodID classHashMapCtor;
42     jmethodID classHashMapPut;
43     jmethodID classVectorCtor;
44     jmethodID classVectorAddElement;
45     jmethodID classSimulatorResourceCtor;
46     jmethodID classSimulatorResourceSetURI;
47     jmethodID classSimulatorResourceSetResourceType;
48     jmethodID classSimulatorResourceSetInterfaceType;
49     jmethodID classSimulatorResourceSetName;
50     jmethodID classSimulatorResourceModelCtor;
51     jmethodID classSimulatorResourceAttributeCtor;
52 } SimulatorClassRefs;
53
54 static jfieldID GetHandleField(JNIEnv *env, jobject jobj)
55 {
56     jclass cls = env->GetObjectClass(jobj);
57     return env->GetFieldID(cls, "nativeHandle", "J");
58 }
59
60 template <typename T>
61 static T *GetHandle(JNIEnv *env, jobject jobj)
62 {
63     jlong handle = env->GetLongField(jobj, GetHandleField(env, jobj));
64     return reinterpret_cast<T *>(handle);
65 }
66
67 template <typename T>
68 static void SetHandle(JNIEnv *env, jobject jobj, T *type)
69 {
70     jlong handle = reinterpret_cast<jlong>(type);
71
72     env->SetLongField(jobj, GetHandleField(env, jobj), handle);
73 }
74
75 JNIEnv *getEnv();
76 void releaseEnv();
77
78 #endif