]> sjero.net Git - linphone/blob - tools/my_jni.h
Aac-eld add missing header according to RFC3640 3.3.6
[linphone] / tools / my_jni.h
1 /*
2 my_jni.cc
3 Copyright (C) 2013  Belledonne Communications, Grenoble, France
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  */
19
20 #ifndef __MY_JNI__H
21 #define __MY_JNI__H
22 #include <jni.h>
23 extern "C" {
24 #include "linphonecore_utils.h"
25 }
26
27 #define defCallMethod(Type)                                                                                             \
28 template <typename ReturnType>                                                                                          \
29 static ReturnType call##Type##Method(JNIEnv *env, jobject obj, const char *className, const char *methodName,           \
30                                         const char *methodSignature, ...) {                                             \
31         jclass my_class = env->GetObjectClass(obj);                                                                     \
32         if(my_class == 0) {                                                                                             \
33                 ms_error("Can't get %s JNI class", className);                                                          \
34                 return NULL;                                                                                            \
35         }                                                                                                               \
36         jmethodID my_method = env->GetMethodID(my_class, methodName, methodSignature);                                  \
37         if(my_method == 0) {                                                                                            \
38                 ms_error("Can't get %s %s %s method", className, methodSignature);                                      \
39                 return NULL;                                                                                            \
40         }                                                                                                               \
41         va_list vl;                                                                                                     \
42         va_start(vl, methodSignature);                                                                                  \
43         ReturnType ret = env->Call##Type##MethodV(obj, my_method, vl);                                                  \
44         va_end(vl);                                                                                                     \
45         return ret;                                                                                                     \
46 }                                                                                                                       \
47
48 #define defGetterTypeField(Type, JavaType, JavaStringType)                                                              \
49 template <typename ValueType>                                                                                           \
50 static ValueType get##Type##Field(JNIEnv *env, jobject obj, const char *className, const char *fieldName) {             \
51         jclass my_class = env->GetObjectClass(obj);                                                                     \
52         if(my_class == 0) {                                                                                             \
53                 ms_error("Can't get %s JNI class", className);                                                          \
54                 return NULL;                                                                                            \
55         }                                                                                                               \
56         jfieldID my_field = env->GetFieldID(my_class, fieldName, JavaStringType);                                       \
57         if(my_field == 0) {                                                                                             \
58                 ms_error("Can't get %s %s field", className, fieldName);                                                \
59                 return NULL;                                                                                            \
60         }                                                                                                               \
61         return (ValueType) env->Get##Type##Field(obj, my_field);                                                        \
62 }                                                                                                                       \
63
64 #define defSetterTypeField(Type, JavaType, JavaStringType)                                                              \
65 template <typename ValueType>                                                                                           \
66 static bool set##Type##Field(JNIEnv *env, jobject obj, const char *className, const char *fieldName, ValueType val) {   \
67         jclass my_class = env->GetObjectClass(obj);                                                                     \
68         if(my_class == 0) {                                                                                             \
69                 ms_error("Can't get %s JNI class", className);                                                          \
70                 return false;                                                                                           \
71         }                                                                                                               \
72         jfieldID my_field = env->GetFieldID(my_class, fieldName, JavaStringType);                                       \
73         if(my_field == 0) {                                                                                             \
74                 ms_error("Can't get %s %s field", className, fieldName);                                                \
75                 return false;                                                                                           \
76         }                                                                                                               \
77         env->Set##Type##Field(obj, my_field, (JavaType) val);                                                           \
78         return true;                                                                                                    \
79 }                                                                                                                       \
80
81 #define defGetterAndSetterTypeField(Type, JavaType, JavaStringType)                                                     \
82         defGetterTypeField(Type, JavaType, JavaStringType)                                                              \
83         defSetterTypeField(Type, JavaType, JavaStringType)                                                              \
84
85 namespace my_jni {
86         template <typename ReturnType>
87         static void callVoidMethod(JNIEnv *env, jobject obj, const char *className, const char *methodName,
88                                                 const char *methodSignature, ...) {
89                 jclass my_class = env->GetObjectClass(obj);
90                 if(my_class == 0) {
91                         ms_error("Can't get %s JNI class", className);
92                         return;
93                 }
94                 jmethodID my_method = env->GetMethodID(my_class, methodName, methodSignature);
95                 if(my_method == 0) {
96                         ms_error("Can't get %s %s %s method", className, methodName, methodSignature);
97                         return;
98                 }
99                 va_list vl;
100                 va_start(vl, methodSignature);
101                 env->CallVoidMethodV(obj, my_method, vl);
102                 va_end(vl);
103         }
104         defCallMethod(Object)
105         defGetterAndSetterTypeField(Long, jlong, "J")
106 }
107
108 #endif //__MY_JNI__H