MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
jtie_tconv_value_impl.hpp
1 /*
2  Copyright 2010 Sun Microsystems, Inc.
3  All rights reserved. Use is subject to license terms.
4 
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.
8 
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.
13 
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
17 */
18 /*
19  * jtie_tconv_value_impl.hpp
20  */
21 
22 #ifndef jtie_tconv_value_impl_hpp
23 #define jtie_tconv_value_impl_hpp
24 
25 #include <assert.h> // not using namespaces yet
26 #include <jni.h>
27 
28 #include "jtie_tconv_value.hpp"
29 #include "jtie_tconv_impl.hpp"
30 #include "helpers.hpp"
31 
32 // ---------------------------------------------------------------------------
33 // Java <-> C basic type conversions
34 // ---------------------------------------------------------------------------
35 
36 // Implements primitive type parameter conversions.
37 template< typename J, typename C >
38 struct ParamBasicT {
39  static C
40  convert(cstatus & s, J j, JNIEnv * env) {
41  TRACE("C ParamBasicT.convert(cstatus &, J, JNIEnv *)");
42  (void)env;
43  s = 0;
44  // XXX assert(static_cast< J >(static_cast< C >(j)) == j);
45  return static_cast< C >(j); // may convert to unsigned type
46  }
47 
48  static void
49  release(C c, J j, JNIEnv * env) {
50  TRACE("void ParamBasicT.release(C, J, JNIEnv *)");
51  (void)c; (void)j; (void)env;
52  }
53 
54 private:
55  // prohibit instantiation
56  ParamBasicT() {
57  // prohibit unsupported template specializations
58  is_valid_primitive_type_mapping< J, C >();
59  }
60 };
61 
62 // Implements primitive type result conversions.
63 template< typename J, typename C >
64 struct ResultBasicT {
65  static J
66  convert(C c, JNIEnv * env) {
67  TRACE("J ResultBasicT.convert(C, JNIEnv *)");
68  (void)env;
69  // XXX assert(static_cast< C >(static_cast< J >(c)) == c);
70  return static_cast< J >(c); // may convert to signed type
71  }
72 
73 private:
74  // prohibit instantiation
75  ResultBasicT() {
76  // prohibit unsupported template specializations
77  is_valid_primitive_type_mapping< J, C >();
78  }
79 };
80 
81 // ---------------------------------------------------------------------------
82 // Specializations for basic type conversions
83 // ---------------------------------------------------------------------------
84 
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.
88 
89 // Lessons learned:
90 //
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 > {};
98 //
99 // Specializations must be defined over intrinsic types, not aliases
100 //
101 // Datatype LP64 ILP64 LLP64 ILP32 LP32
102 // char 8 8 8 8 8
103 // short 16 16 16 16 16
104 // int 32 64 32 32 16
105 // long 64 64 32 32 32
106 // long long 64
107 // pointer 64 64 64 32 32
108 
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 > {};
116 
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 > {};
129 
130 // ---------------------------------------------------------------------------
131 // Specializations for boolean conversions
132 // ---------------------------------------------------------------------------
133 
134 // Implements boolean type parameter conversions.
135 template<>
136 struct ParamBasicT< jboolean, bool > {
137  static bool
138  convert(cstatus & s, jboolean j, JNIEnv * env) {
139  TRACE("bool ParamBasicT.convert(cstatus &, jboolean, JNIEnv *)");
140  (void)env;
141  s = 0;
142  // Java v C: jboolean is unsigned 8-bit, so, beware of truncation
143  return (j == JNI_TRUE);
144  }
145 
146  static void
147  release(bool c, jboolean j, JNIEnv * env) {
148  TRACE("void ParamBasicT.release(bool, jboolean, JNIEnv *)");
149  (void)c; (void)j; (void)env;
150  }
151 };
152 
153 // Implements boolean type result conversions.
154 template<>
155 struct ResultBasicT< jboolean, bool > {
156  static jboolean
157  convert(bool c, JNIEnv * env) {
158  TRACE("jboolean ResultBasicT.convert(bool, JNIEnv *)");
159  (void)env;
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);
163  }
164 };
165 
166 JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jboolean, bool)
167 
168 // ---------------------------------------------------------------------------
169 // Specializations for exact-width number type conversions
170 // ---------------------------------------------------------------------------
171 
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)
175 
176 JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jfloat, float)
177 JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jdouble, double)
178 
179 // ---------------------------------------------------------------------------
180 // Specializations for variable-width number type conversions
181 // ---------------------------------------------------------------------------
182 
183 // Datatype LP32 ILP32 LP64 ILP64 LLP64
184 // char 8 8 8 8 8
185 // short 16 16 16 16 16
186 // int 16 32 32 64 32
187 // long 32 32 64 64 32
188 // long long 64
189 // pointer 32 32 64 64 64
190 
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)
194 
195 // jshort in LP32
196 JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jshort, signed int)
197 JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jshort, unsigned int)
198 
199 // jint in ILP32, LP64, LLP64
200 JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jint, signed int)
201 JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jint, unsigned int)
202 
203 // jint in LP32, ILP32, LLP64
204 JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jint, signed long)
205 JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jint, unsigned long)
206 
207 // jlong in ILP64
208 JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jlong, signed int)
209 JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jlong, unsigned int)
210 
211 // jlong in LP64, ILP64
212 JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jlong, signed long)
213 JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jlong, unsigned long)
214 
215 // jlong in LLP64
216 JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jlong, signed long long)
217 JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jlong, unsigned long long)
218 
219 // jdouble
220 JTIE_SPECIALIZE_BASIC_TYPE_MAPPING(jdouble, long double)
221 
222 // ---------------------------------------------------------------------------
223 
224 #endif // jtie_tconv_value_impl_hpp