source: rrlib_jni/java_native_utility_functions_jni.cpp @ 24:d13dfecf897b

Last change on this file since 24:d13dfecf897b was 22:5f7083ce4cf1, checked in by Max Reichardt <mreichardt@…>, 7 years ago

Adapted to moving jni functions to rrlib_jni-java

File size: 6.9 KB
Line 
1//
2// You received this file as part of RRLib
3// Robotics Research Library
4//
5// Copyright (C) Finroc GbR (finroc.org)
6//
7// This program is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation; either version 2 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License along
18// with this program; if not, write to the Free Software Foundation, Inc.,
19// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20//
21//----------------------------------------------------------------------
22/*!\file    rrlib/jni/java_native_utility_functions_jni.cpp
23 *
24 * \author  Max Reichardt
25 *
26 * \date    2013-05-07
27 *
28 * Native utility functions for java code (e.g. get Pointers to native byte buffers)
29 */
30//----------------------------------------------------------------------
31
32//----------------------------------------------------------------------
33// External includes (system with <>, local with "")
34//----------------------------------------------------------------------
35#include <jni.h>
36#include <cstring>
37
38//----------------------------------------------------------------------
39// Internal includes with ""
40//----------------------------------------------------------------------
41#include "rrlib/jni/tJNIWrappable.h"
42
43//----------------------------------------------------------------------
44// Debugging
45//----------------------------------------------------------------------
46#include <cassert>
47
48//----------------------------------------------------------------------
49// Namespace usage
50//----------------------------------------------------------------------
51
52//----------------------------------------------------------------------
53// Namespace declaration
54//----------------------------------------------------------------------
55namespace rrlib
56{
57namespace jni
58{
59
60//----------------------------------------------------------------------
61// Forward declarations / typedefs / enums
62//----------------------------------------------------------------------
63
64//----------------------------------------------------------------------
65// Const values
66//----------------------------------------------------------------------
67
68//----------------------------------------------------------------------
69// Implementation
70//----------------------------------------------------------------------
71
72extern "C"
73{
74  JNIEXPORT void JNICALL Java_org_rrlib_jni_JNICalls_deleteJNIWrappable(JNIEnv* env, jclass class_, jlong pointer)
75  {
76    rrlib::jni::tJNIWrappable* obj = (rrlib::jni::tJNIWrappable*)pointer;
77    obj->SetJavaWrapper(NULL, false); // avoids that Java "destructor" is invoked (again)
78    delete obj;
79  }
80  JNIEXPORT jlong JNICALL Java_org_rrlib_jni_JNICalls_getBufferPointer(JNIEnv* env, jclass class_, jobject buf)
81  {
82    return (jlong)env->GetDirectBufferAddress(buf);
83  }
84  JNIEXPORT jbyte JNICALL Java_org_rrlib_jni_JNICalls_getByte(JNIEnv* env, jclass class_, jlong ptr)
85  {
86    return *((jbyte*)ptr);
87  }
88  JNIEXPORT jobject JNICALL Java_org_rrlib_jni_JNICalls_getCByteBuffer(JNIEnv* env, jclass class_, jlong ptr, jint size)
89  {
90    jobject result;
91    result = env->NewDirectByteBuffer((void*)ptr, size);
92    return result;
93  }
94  JNIEXPORT jdouble JNICALL Java_org_rrlib_jni_JNICalls_getDouble(JNIEnv* env, jclass class_, jlong ptr)
95  {
96    return *((jdouble*)ptr);
97  }
98  JNIEXPORT jfloat JNICALL Java_org_rrlib_jni_JNICalls_getFloat(JNIEnv* env, jclass class_, jlong ptr)
99  {
100    return *((jfloat*)ptr);
101  }
102  JNIEXPORT jint JNICALL Java_org_rrlib_jni_JNICalls_getInt(JNIEnv* env, jclass class_, jlong ptr)
103  {
104    return *((jint*)ptr);
105  }
106  JNIEXPORT jlong JNICALL Java_org_rrlib_jni_JNICalls_getJavaVM(JNIEnv* env, jclass class_)
107  {
108    JavaVM* jvm = rrlib::jni::GetJavaVM();
109    if (jvm != NULL)
110    {
111      return (jlong)jvm;
112    }
113    env->GetJavaVM(&jvm);
114    assert(jvm != NULL && "Error initializing JavaVM pointer");
115    rrlib::jni::SetJavaVM(jvm);
116    return (jlong)jvm;
117  }
118  JNIEXPORT jlong JNICALL Java_org_rrlib_jni_JNICalls_getLong(JNIEnv* env, jclass class_, jlong ptr)
119  {
120    return *((jlong*)ptr);
121  }
122  JNIEXPORT jlong JNICALL Java_org_rrlib_jni_JNICalls_getPointer(JNIEnv* env, jclass class_, jlong address, jint index)
123  {
124    void** array = (void**)address;
125    return (jlong)array[index];
126  }
127  JNIEXPORT jshort JNICALL Java_org_rrlib_jni_JNICalls_getShort(JNIEnv* env, jclass class_, jlong ptr)
128  {
129    return *((jshort*)ptr);
130  }
131  JNIEXPORT void JNICALL Java_org_rrlib_jni_JNICalls_memcpy(JNIEnv* env, jclass class_, jlong dest, jlong src, jint length)
132  {
133    memcpy((void*)src, (void*)dest, (int)length);
134  }
135  JNIEXPORT void JNICALL Java_org_rrlib_jni_JNICalls_setByte(JNIEnv* env, jclass class_, jlong ptr, jbyte val)
136  {
137    *((jbyte*)ptr) = val;
138  }
139  JNIEXPORT void JNICALL Java_org_rrlib_jni_JNICalls_setDouble(JNIEnv* env, jclass class_, jlong ptr, jdouble val)
140  {
141    *((jdouble*)ptr) = val;
142  }
143  JNIEXPORT void JNICALL Java_org_rrlib_jni_JNICalls_setFloat(JNIEnv* env, jclass class_, jlong ptr, jfloat val)
144  {
145    *((jfloat*)ptr) = val;
146  }
147  JNIEXPORT void JNICALL Java_org_rrlib_jni_JNICalls_setInt(JNIEnv* env, jclass class_, jlong ptr, jint val)
148  {
149    *((jint*)ptr) = val;
150  }
151  JNIEXPORT void JNICALL Java_org_rrlib_jni_JNICalls_setJavaObject(JNIEnv* env, jclass class_, jlong pointer, jobject object, jboolean java_responsible)
152  {
153    rrlib::jni::tJNIWrappable* obj = (rrlib::jni::tJNIWrappable*)pointer;
154    obj->SetJavaWrapper(object, !java_responsible);
155  }
156  JNIEXPORT void JNICALL Java_org_rrlib_jni_JNICalls_setLong(JNIEnv* env, jclass class_, jlong ptr, jlong val)
157  {
158    *((jlong*)ptr) = val;
159  }
160  JNIEXPORT void JNICALL Java_org_rrlib_jni_JNICalls_setPointer(JNIEnv* env, jclass class_, jlong arraypointer, jint index, jlong pointer)
161  {
162    void** array = (void**)arraypointer;
163    array[index] = (void*)pointer;
164  }
165  JNIEXPORT void JNICALL Java_org_rrlib_jni_JNICalls_setShort(JNIEnv* env, jclass class_, jlong ptr, jshort val)
166  {
167    *((jshort*)ptr) = val;
168  }
169  JNIEXPORT jint JNICALL Java_org_rrlib_jni_JNICalls_sizeOfPointer(JNIEnv* env, jclass class_)
170  {
171    return sizeof(void*);
172  }
173  JNIEXPORT jint JNICALL Java_org_rrlib_jni_JNICalls_strlen(JNIEnv* env, jclass class_, jlong ptr)
174  {
175    return strlen((char*)ptr);
176  }
177  JNIEXPORT jstring JNICALL Java_org_rrlib_jni_JNICalls_toString(JNIEnv* env, jclass class_, jlong pointer)
178  {
179    jstring result;
180    result = env->NewStringUTF((char*)pointer);
181    return result;
182  }
183
184} // extern C
185
186//----------------------------------------------------------------------
187// End of namespace declaration
188//----------------------------------------------------------------------
189}
190}
Note: See TracBrowser for help on using the repository browser.