MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
jtie_tconv_refbyval_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_refbyval_impl.hpp
20  */
21 
22 #ifndef jtie_tconv_refbyval_impl_hpp
23 #define jtie_tconv_refbyval_impl_hpp
24 
25 #include <assert.h> // not using namespaces yet
26 #include <jni.h>
27 
28 #include "jtie_tconv_refbyval.hpp"
29 #include "jtie_tconv_impl.hpp"
30 #include "jtie_tconv_ptrbyval_impl.hpp"
31 #include "jtie_tconv_utils_impl.hpp"
32 #include "helpers.hpp"
33 
34 // ---------------------------------------------------------------------------
35 // ArrayRefParam, ArrayRefResult
36 // ---------------------------------------------------------------------------
37 
38 // XXX document, cleanup
39 
40 // implements the mapping of (1-elem) arrays to reference parameters
41 template< typename J, typename C > struct ArrayRefParam;
42 
43 // implements the mapping of (1-elem) arrays to reference results
44 template< typename J, typename C > struct ArrayRefResult;
45 
46 inline cstatus
47 ensureNonNullArray(jarray ja, JNIEnv * env) {
48  // init return value to error
49  cstatus s = -1;
50 
51  if (ja == NULL) {
52  const char * c = "java/lang/IllegalArgumentException";
53  const char * m = ("JNI wrapper: Java array cannot be null"
54  " when mapped to an object reference type"
55  " (file: " __FILE__ ")");
56  registerException(env, c, m);
57  } else {
58  // ok
59  s = 0;
60  }
61  return s;
62 }
63 
64 template< typename J, typename C >
65 struct ArrayRefParam {
66 
67  static C &
68  convert(cstatus & s, typename J::JA_t * j, JNIEnv * env) {
69  TRACE("C & ArrayRefParam.convert(cstatus &, typename J::JA_t *, JNIEnv *)");
70 
71  // init return value and status to error
72  s = -1;
73  C * c = NULL;
74 
75  if (ensureNonNullArray(j, env) != 0) {
76  // exception pending
77  } else {
78  c = ArrayPtrParam< J, C >::convert(s, j, env);
79  assert(s != 0 || c != NULL);
80  }
81  return *c;
82  }
83 
84  static void
85  release(C & c, typename J::JA_t * j, JNIEnv * env) {
86  TRACE("void ArrayRefParam.release(C &, typename J::JA_t *, JNIEnv *)");
88  }
89 };
90 
91 // actually, there's not much of a point to map a result reference to an
92 // 1-element array as a value-copy holder, for we can return the value
93 // directly, instead; hence, this class is defined for completeness only
94 template< typename J, typename C >
96  static J *
97  convert(C & c, JNIEnv * env) {
98  TRACE("J * ArrayRefResult.convert(C &, JNIEnv *)");
99  // technically, C++ references can be null, hence, no asserts here
100  //assert(&c != NULL);
101  J * j = ArrayPtrResult< J, C >::convert(&c, env);
102  //assert(j != NULL);
103  return j;
104  }
105 };
106 
107 // ---------------------------------------------------------------------------
108 // Specializations for reference type conversions
109 // ---------------------------------------------------------------------------
110 
111 // Avoid mapping types by broad, generic rules, which easily results in
112 // template instantiation ambiguities for non-primitive types. Therefore,
113 // we enumerate all specicializations for primitive type references.
114 
115 // specialize values/value-holders mapped to references:
116 // - const params: map as value copy
117 // - const results: map as value copy
118 // - non-const params: map as value holder (array of length 1)
119 // - non-const results: map as value copy (no use as value holders)
120 #define JTIE_SPECIALIZE_REFERENCE_TYPE_MAPPING( JA, J, C ) \
121  template<> \
122  struct Param< J, const C & > \
123  : Param< J, C > {}; \
124  template<> \
125  struct Result< J, const C & > \
126  : Result< J, C > {}; \
127  template<> \
128  struct Param< JA *, C & > \
129  : ArrayRefParam< _jtie_j_BoundedArray< JA, 1 >, C > {}; \
130  template<> \
131  struct Result< J, C & > \
132  : Result< J, C > {}; \
133 
134 // ---------------------------------------------------------------------------
135 // Specializations for reference to exact-width primitive type conversions
136 // ---------------------------------------------------------------------------
137 
138 JTIE_SPECIALIZE_REFERENCE_TYPE_MAPPING(_jbooleanArray, jboolean, bool)
139 
140 JTIE_SPECIALIZE_REFERENCE_TYPE_MAPPING(_jbyteArray, jbyte, char)
141 JTIE_SPECIALIZE_REFERENCE_TYPE_MAPPING(_jbyteArray, jbyte, signed char)
142 JTIE_SPECIALIZE_REFERENCE_TYPE_MAPPING(_jbyteArray, jbyte, unsigned char)
143 
144 JTIE_SPECIALIZE_REFERENCE_TYPE_MAPPING(_jfloatArray, jfloat, float)
145 JTIE_SPECIALIZE_REFERENCE_TYPE_MAPPING(_jdoubleArray, jdouble, double)
146 
147 // ---------------------------------------------------------------------------
148 // Specializations for reference to variable-width primitive type conversions
149 // ---------------------------------------------------------------------------
150 
151 // jshort in LP32, ILP32, LP64, ILP64, LLP64
152 JTIE_SPECIALIZE_REFERENCE_TYPE_MAPPING(_jshortArray, jshort, signed short)
153 JTIE_SPECIALIZE_REFERENCE_TYPE_MAPPING(_jshortArray, jshort, unsigned short)
154 
155 // jshort in LP32
156 JTIE_SPECIALIZE_REFERENCE_TYPE_MAPPING(_jshortArray, jshort, signed int)
157 JTIE_SPECIALIZE_REFERENCE_TYPE_MAPPING(_jshortArray, jshort, unsigned int)
158 
159 // jint in ILP32, LP64, LLP64
160 JTIE_SPECIALIZE_REFERENCE_TYPE_MAPPING(_jintArray, jint, signed int)
161 JTIE_SPECIALIZE_REFERENCE_TYPE_MAPPING(_jintArray, jint, unsigned int)
162 
163 // jint in LP32, ILP32, LLP64
164 JTIE_SPECIALIZE_REFERENCE_TYPE_MAPPING(_jintArray, jint, signed long)
165 JTIE_SPECIALIZE_REFERENCE_TYPE_MAPPING(_jintArray, jint, unsigned long)
166 
167 // jlong in ILP64
168 JTIE_SPECIALIZE_REFERENCE_TYPE_MAPPING(_jlongArray, jlong, signed int)
169 JTIE_SPECIALIZE_REFERENCE_TYPE_MAPPING(_jlongArray, jlong, unsigned int)
170 
171 // jlong in LP64, ILP64
172 JTIE_SPECIALIZE_REFERENCE_TYPE_MAPPING(_jlongArray, jlong, signed long)
173 JTIE_SPECIALIZE_REFERENCE_TYPE_MAPPING(_jlongArray, jlong, unsigned long)
174 
175 // jlong in LLP64
176 JTIE_SPECIALIZE_REFERENCE_TYPE_MAPPING(_jlongArray, jlong, signed long long)
177 JTIE_SPECIALIZE_REFERENCE_TYPE_MAPPING(_jlongArray, jlong, unsigned long long)
178 
179 // jdouble
180 JTIE_SPECIALIZE_REFERENCE_TYPE_MAPPING(_jdoubleArray, jdouble, long double)
181 
182 // ---------------------------------------------------------------------------
183 
184 #endif // jtie_tconv_refbyval_impl_hpp
185