MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
jtie_tconv_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_impl.hpp
20  */
21 
22 #ifndef jtie_tconv_impl_hpp
23 #define jtie_tconv_impl_hpp
24 
25 #include <jni.h>
26 
27 #include "helpers.hpp"
28 
29 // ---------------------------------------------------------------------------
30 // Java <-> C type conversions
31 // ---------------------------------------------------------------------------
32 
33 // Design rationale:
34 //
35 // The classes here only document prototypes, no functions are declared
36 // or defined here, in order to have undefined type mappings result in a
37 // compilation error. A better option might be to use static_assert()
38 // when made available in upcoming C++0x. Alternatively, other patterns
39 // could be looked at for specifying and mandating specialization
40 // (a possible candidate: the Curiosly Recurring Template Pattern).
41 
42 /*
43  * A type conversion status type.
44  */
45 typedef int cstatus;
46 
54 template < typename J, typename C > struct is_supported_type_mapping;
55 
64 template< typename J, typename C >
65 struct Param
66 #if 0 // only document class template, to be defined by specialization
67 {
87  static C
88  convert(cstatus & s, J j, JNIEnv * env) {
89  TRACE("C Param.convert(cstatus &, J, JNIEnv *)");
90  (void)j; (void)env;
91  s = 1;
92  static_assert(false, "missing specialization of parameter conversion");
93  return 0;
94  }
95 
120  static void
121  release(C c, J j, JNIEnv * env) {
122  TRACE("void Param.release(C, J, JNIEnv *)");
123  (void)c; (void)j; (void)env;
124  static_assert(false, "missing specialization of parameter conversion");
125  }
126 }
127 #endif // only document class template, to be defined by specialization
128 ;
129 
143 template< typename J, typename C >
144 struct Target
145 #if 0 // only document class template, to be defined by specialization
146 {
153  static C &
154  convert(cstatus & s, J j, JNIEnv * env) {
155  TRACE("C & Target.convert(cstatus &, J, JNIEnv *)");
156  (void)j; (void)env;
157  s = 1;
158  static_assert(false, "missing specialization of target conversion");
159  return 0;
160  }
161 
167  static void
168  release(C & c, J j, JNIEnv * env) {
169  TRACE("void Target.release(C &, J, JNIEnv *)");
170  (void)c; (void)j; (void)env;
171  static_assert(false, "missing specialization of target conversion");
172  }
173 }
174 #endif // only document class template, to be defined by specialization
175 ;
176 
185 template< typename J, typename C >
186 struct Result
187 #if 0 // only document class template, to be defined by specialization
188 {
202  static J
203  convert(C c, JNIEnv * env) {
204  TRACE("J Result.convert(C, JNIEnv *)");
205  (void)c; (void)env;
206  static_assert(false, "missing specialization of result conversion");
207  return 0;
208  }
209 }
210 #endif // only document class template, to be defined by specialization
211 ;
212 
213 // Lessons learned:
214 //
215 // Basing the type conversion code on class templates rather than loose
216 // function templates allows for:
217 // - defining a uniform type converter interface (no overloading ambiguities)
218 // - writing generic conversion rules using partial template specialization
219 // - inheriting implementations from type-specific conversion classes
220 // - separating J->C from C->J conversion expressing convert/release asymmetry
221 
222 // ---------------------------------------------------------------------------
223 // Generic specializations for Java <-> C type conversions
224 // ---------------------------------------------------------------------------
225 
226 // The set of explicit template specializations in the type conversion
227 // definitions can be reduced by generifying some type mapping rules.
228 // We need to be careful, though, to avoid template instantiation clutter
229 // and resolution ambiguities by too (many) broad rules.
230 
231 // For values (primitive types and pointers), which are always copied,
232 // it is safe to specialize const values/pointers to non-const ones.
233 //
234 // Examples: 'int const', 'A * const' (does not apply to 'const A *')
235 
236 // XXX ambigous with enums
237 //template< typename J, typename C >
238 //struct Param< J, C const > : Param< J, C > {};
239 //
240 //template< typename J, typename C >
241 //struct Result< J, C const > : Result< J, C > {};
242 
243 // XXX untested
244 template< typename J, typename C >
245 struct Param< J const, C > : Param< J, C > {};
246 
247 template< typename J, typename C >
248 struct Result< J const, C > : Result< J, C > {};
249 
250 // ---------------------------------------------------------------------------
251 // formal <-> actual parameter/result type casts
252 // ---------------------------------------------------------------------------
253 
257 template< typename T, typename S >
258 inline T
259 cast(S s) {
260  TRACE("T cast(S)");
261  return static_cast< T >(s); // support base->derived class pointer casts
262 }
263 
264 // Design note:
265 //
266 // XXX Rationale for cast<>...
267 //
268 // No class template needed (in contrast to Param/Result type conversion),
269 // a function template is sufficient for actual/formal casts.
270 
271 // ---------------------------------------------------------------------------
272 
273 #endif // jtie_tconv_impl_hpp