source: rrlib_jni/type_conversion.h @ 24:d13dfecf897b

Last change on this file since 24:d13dfecf897b was 20:c4f1bedb7be9, checked in by Tobias Föhst <foehst@…>, 7 years ago

Added and updated license information

File size: 5.8 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/type_conversion.h
23 *
24 * \author  Max Reichardt
25 *
26 * \date    2013-05-07
27 *
28 * Functions that convert native C++ types to JNI types and vice versa.
29 *
30 */
31//----------------------------------------------------------------------
32#ifndef __rrlib__jni__jni_type_conversion_h__
33#define __rrlib__jni__jni_type_conversion_h__
34
35//----------------------------------------------------------------------
36// External includes (system with <>, local with "")
37//----------------------------------------------------------------------
38#include <jni.h>
39
40//----------------------------------------------------------------------
41// Internal includes with ""
42//----------------------------------------------------------------------
43#include "rrlib/jni/jvm.h"
44#include "rrlib/jni/tJNIWrappable.h"
45
46//----------------------------------------------------------------------
47// Namespace declaration
48//----------------------------------------------------------------------
49namespace rrlib
50{
51namespace jni
52{
53
54//----------------------------------------------------------------------
55// Forward declarations / typedefs / enums
56//----------------------------------------------------------------------
57
58//----------------------------------------------------------------------
59// Function declarations
60//----------------------------------------------------------------------
61
62///////////////////////////////////////////////////////////////////////////////
63// conversion methods C++ to Java
64///////////////////////////////////////////////////////////////////////////////
65
66template <typename T>
67inline jbyte ToJbyte(T v)
68{
69  return static_cast<jbyte>(v);
70}
71
72template <typename T>
73inline jshort ToJshort(T v)
74{
75  return static_cast<jshort>(v);
76}
77
78template <typename T>
79inline jint ToJint(T v)
80{
81  return static_cast<jint>(v);
82}
83
84inline jlong ToJlong(int64_t v)
85{
86  return static_cast<jlong>(v);
87}
88inline jlong ToJlong(float v)
89{
90  return static_cast<jlong>(v);
91}
92inline jlong ToJlong(double v)
93{
94  return static_cast<jlong>(v);
95}
96inline jlong ToJlong(tJNIWrappable* v)
97{
98  return (jlong)v;  // Pointer conversion
99}
100inline jlong ToJlong(const tJNIWrappable& v)
101{
102  return (jlong)&v;  // Pointer conversion
103}
104
105template <typename T>
106inline jchar ToJchar(T v)
107{
108  return static_cast<jchar>(v);
109}
110
111template <typename T>
112inline jfloat ToJfloat(T v)
113{
114  return static_cast<jfloat>(v);
115}
116
117template <typename T>
118inline jdouble ToJdouble(T v)
119{
120  return static_cast<jdouble>(v);
121}
122
123inline jstring ToJstring(const char* c)
124{
125  jstring js = GetEnv()->NewStringUTF(c);
126  return js;
127}
128inline jstring ToJstring(const std::string& d)
129{
130  return ToJstring(d.c_str());
131}
132
133//inline jobject ToJobject(tJNIWrappable* ptr)
134//{
135//  return ptr->GetJavaWrapper();
136//}
137inline jobject ToJobject(tJNIWrappable& ref)
138{
139  return ref.GetJavaWrapper();
140}
141//template<typename T>
142//inline jobject ToJobject(T& ref)
143//{
144//  return dynamic_cast<tJNIWrappable&>(ref)->GetJavaWrapper();
145//}
146
147
148///////////////////////////////////////////////////////////////////////////////
149// conversion methods Java to C++
150///////////////////////////////////////////////////////////////////////////////
151
152inline int ToInt(jint v)
153{
154  return static_cast<int>(v);
155}
156inline int ToInt(jlong v)
157{
158  return static_cast<int>(v);
159}
160inline int ToInt(jbyte v)
161{
162  return static_cast<int>(v);
163}
164inline int ToInt(jshort v)
165{
166  return static_cast<int>(v);
167}
168
169inline int16_t ToInt16(jint v)
170{
171  return static_cast<int16_t>(v);
172}
173inline int16_t ToInt16(jlong v)
174{
175  return static_cast<int16_t>(v);
176}
177inline int16_t ToInt16(jbyte v)
178{
179  return static_cast<int16_t>(v);
180}
181inline int16_t ToInt16(jshort v)
182{
183  return static_cast<int16_t>(v);
184}
185
186inline int8_t ToInt8(jint v)
187{
188  return static_cast<int8_t>(v);
189}
190inline int8_t ToInt8(jlong v)
191{
192  return static_cast<int8_t>(v);
193}
194inline int8_t ToInt8(jbyte v)
195{
196  return static_cast<int8_t>(v);
197}
198inline int8_t ToInt8(jshort v)
199{
200  return static_cast<int8_t>(v);
201}
202
203inline int64_t ToInt64(jint v)
204{
205  return static_cast<int64_t>(v);
206}
207inline int64_t ToInt64(jlong v)
208{
209  return static_cast<int64_t>(v);
210}
211inline int64_t ToInt64(jbyte v)
212{
213  return static_cast<int64_t>(v);
214}
215inline int64_t ToInt64(jshort v)
216{
217  return static_cast<int64_t>(v);
218}
219
220inline double ToDouble(jdouble v)
221{
222  return static_cast<jdouble>(v);
223}
224inline float ToFloat(jfloat v)
225{
226  return static_cast<jfloat>(v);
227}
228
229inline std::string ToString(jstring js)
230{
231  const char* str = GetEnv()->GetStringUTFChars(js, NULL);
232  assert(str != NULL && "OutOfMemoryError");
233  std::string s(str);
234  GetEnv()->ReleaseStringUTFChars(js, str);
235  return s;
236}
237
238template <typename T>
239inline T* StaticCast(jlong ptr)
240{
241  tJNIWrappable* jw = (tJNIWrappable*)ptr;
242  return static_cast<T>(jw);
243}
244
245//template <typename T>
246//inline T* DynamicCast(jlong ptr)
247//{
248//  tJNIWrappable* jw = (tJNIWrappable*)ptr;
249//  return tCastCache::DynamicCast<T>(jw);
250//}
251
252//----------------------------------------------------------------------
253// End of namespace declaration
254//----------------------------------------------------------------------
255}
256}
257
258
259#endif
Note: See TracBrowser for help on using the repository browser.