2
Copyright 2010 Sun Microsystems, Inc.
3
All rights reserved. Use is subject to license terms.
5
This program is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation; version 2 of the License.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
* jtie_tconv_value_impl.hpp
22
#ifndef jtie_tconv_value_impl_hpp
23
#define jtie_tconv_value_impl_hpp
25
#include <assert.h> // not using namespaces yet
28
#include "jtie_tconv_value.hpp"
29
#include "jtie_tconv_impl.hpp"
30
#include "helpers.hpp"
32
// ---------------------------------------------------------------------------
33
// Java <-> C basic type conversions
34
// ---------------------------------------------------------------------------
36
// Implements primitive type parameter conversions.
37
template< typename J, typename C >
40
convert(cstatus & s, J j, JNIEnv * env) {
41
TRACE("C ParamBasicT.convert(cstatus &, J, JNIEnv *)");
44
// XXX assert(static_cast< J >(static_cast< C >(j)) == j);
45
return static_cast< C >(j); // may convert to unsigned type
49
release(C c, J j, JNIEnv * env) {
50
TRACE("void ParamBasicT.release(C, J, JNIEnv *)");
51
(void)c; (void)j; (void)env;
55
// prohibit instantiation
57
// prohibit unsupported template specializations
58
is_valid_primitive_type_mapping< J, C >();
62
// Implements primitive type result conversions.
63
template< typename J, typename C >
66
convert(C c, JNIEnv * env) {
67
TRACE("J ResultBasicT.convert(C, JNIEnv *)");
69
// XXX assert(static_cast< C >(static_cast< J >(c)) == c);
70
return static_cast< J >(c); // may convert to signed type
74
// prohibit instantiation
76
// prohibit unsupported template specializations
77
is_valid_primitive_type_mapping< J, C >();
81
// ---------------------------------------------------------------------------
82
// Specializations for basic type conversions
83
// ---------------------------------------------------------------------------
85
// Avoid mapping types by broad, generic rules, which easily results in
86
// template instantiation ambiguities for non-primitive types. Therefore,
87
// we enumerate all specicializations for primitive types.
91
// Cannot extend Param/Result specializations for const types by a generic
92
// rule on the base class (no template match with this indirection)
93
// template<> struct ParamBasicT< J, C const > : ParamBasicT< J, C > {};
94
// template<> struct ResultBasicT< J, C const > : ResultBasicT< J, C > {};
95
// but have to specialize Param/Result directly
96
// template<> struct Param< J, C const > : ParamBasicT< J, C > {};
97
// template<> struct Result< J, C const > : ResultBasicT< J, C > {};
99
// Specializations must be defined over intrinsic types, not aliases
101
// Datatype LP64 ILP64 LLP64 ILP32 LP32
103
// short 16 16 16 16 16
104
// int 32 64 32 32 16
105
// long 64 64 32 32 32
107
// pointer 64 64 64 32 32
109
// extend set of valid primitive type mappings for const value specializations
110
template < typename J, typename C >
111
struct is_valid_primitive_type_mapping< const J, C > {};
112
template < typename J, typename C >
113
struct is_valid_primitive_type_mapping< J, const C > {};
114
template < typename J, typename C >
115
struct is_valid_primitive_type_mapping< const J, const C > {};
117
// also provides specializations for 'const'
118
// template clutter can be reduced a bit: const value types do not need extra
119
// specializations of their implementation
120
// ... : ParamBasicT< J, C const > {};
121
// ... : ResultBasicT< J, C const > {};
122
// but can be derived from their non-const specializations
123
#define JTIE_SPECIALIZE_BASIC_TYPE_MAPPING( J, C ) \
124
template<> struct is_valid_primitive_type_mapping< J, C > {}; \
125
template<> struct Param< J, C > : ParamBasicT< J, C > {}; \
126
template<> struct Result< J, C > : ResultBasicT< J, C > {}; \
127
template<> struct Param< J, C const > : ParamBasicT< J, C > {}; \
128
template<> struct Result< J, C const > : ResultBasicT< J, C > {};
130
// ---------------------------------------------------------------------------
131
// Specializations for boolean conversions
132
// ---------------------------------------------------------------------------
134
// Implements boolean type parameter conversions.
136
struct ParamBasicT< jboolean, bool > {
138
convert(cstatus & s, jboolean j, JNIEnv * env) {
139
TRACE("bool ParamBasicT.convert(cstatus &, jboolean, JNIEnv *)");
142
// Java v C: jboolean is unsigned 8-bit, so, beware of truncation
143
return (j == JNI_TRUE);
147
release(bool c, jboolean j, JNIEnv * env) {
148
TRACE("void ParamBasicT.release(bool, jboolean, JNIEnv *)");
149
(void)c; (void)j; (void)env;
153
// Implements boolean type result conversions.
155
struct ResultBasicT< jboolean, bool > {
157
convert(bool c, JNIEnv * env) {
158
TRACE("jboolean ResultBasicT.convert(bool, JNIEnv *)");
160
// Java v C: jboolean is unsigned 8-bit, so, beware of truncation
161
// on some platforms, JNI_TRUE/FALSE seems top be defined as int
162
return static_cast< jboolean >(c ? JNI_TRUE : JNI_FALSE);
166
JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jboolean, bool)
168
// ---------------------------------------------------------------------------
169
// Specializations for exact-width number type conversions
170
// ---------------------------------------------------------------------------
172
JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jbyte, char)
173
JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jbyte, signed char)
174
JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jbyte, unsigned char)
176
JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jfloat, float)
177
JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jdouble, double)
179
// ---------------------------------------------------------------------------
180
// Specializations for variable-width number type conversions
181
// ---------------------------------------------------------------------------
183
// Datatype LP32 ILP32 LP64 ILP64 LLP64
185
// short 16 16 16 16 16
186
// int 16 32 32 64 32
187
// long 32 32 64 64 32
189
// pointer 32 32 64 64 64
191
// jshort in LP32, ILP32, LP64, ILP64, LLP64
192
JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jshort, signed short)
193
JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jshort, unsigned short)
196
JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jshort, signed int)
197
JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jshort, unsigned int)
199
// jint in ILP32, LP64, LLP64
200
JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jint, signed int)
201
JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jint, unsigned int)
203
// jint in LP32, ILP32, LLP64
204
JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jint, signed long)
205
JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jint, unsigned long)
208
JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jlong, signed int)
209
JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jlong, unsigned int)
211
// jlong in LP64, ILP64
212
JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jlong, signed long)
213
JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jlong, unsigned long)
216
JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jlong, signed long long)
217
JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jlong, unsigned long long)
220
JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jdouble, long double)
222
// ---------------------------------------------------------------------------
224
#endif // jtie_tconv_value_impl_hpp