MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
jtie_tconv_string_impl.hpp
1 /*
2  Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 /*
18  * jtie_tconv_string_impl.hpp
19  */
20 
21 #ifndef jtie_tconv_string_impl_hpp
22 #define jtie_tconv_string_impl_hpp
23 
24 #include <assert.h> // not using namespaces yet
25 #include <jni.h>
26 
27 #include "jtie_tconv_string.hpp"
28 #include "jtie_tconv_impl.hpp"
29 #include "helpers.hpp"
30 
31 // ---------------------------------------------------------------------------
32 // Java String <-> const char * type conversion
33 // ---------------------------------------------------------------------------
34 
35 // comments: to support UCS2 and also locale encodings...
36 // - class _jstring can be subclassed (analog to bytebuffer mappings)
37 // - see JNU_NewStringNative (§8.2.1) and JNU_GetStringNativeChars (§8.2.2)
38 // in JNI Programming Guide & Tutorial...)
39 // - beware that GetStringChars() etc does not deliver not null-terminated
40 // character strings; some OS (e.g., Windows) expect two trailing zero byte
41 // values to terminate Unicode strings.
42 
43 // Implements the mapping of Java String parameters.
44 // declared as template to support other specializations (e.g. char *)
45 template< typename J, typename C >
46 struct ParamStringT;
47 
48 // Implements the mapping of Java String results.
49 // declared as template to support other specializations (e.g. char *)
50 template< typename J, typename C >
51 struct ResultStringT;
52 
53 template<>
54 struct ParamStringT< jstring, const char * > {
55  static const char *
56  convert(cstatus & s, jstring j, JNIEnv * env) {
57  TRACE("const char * ParamStringT.convert(cstatus &, jstring, JNIEnv *)");
58 
59  // init return value and status to error
60  s = -1;
61  const char * c = NULL;
62 
63  // return a C string from a Java String
64  if (j == NULL) {
65  // ok
66  s = 0;
67  } else {
68  // get a UTF-8 string, to be released by ReleaseStringUTFChars()
69  // ignore whether C string is pinned or a copy of Java string
70  c = env->GetStringUTFChars(j, NULL);
71  if (c == NULL) {
72  // exception pending
73  } else {
74  // ok
75  s = 0;
76  }
77  }
78  return c;
79  }
80 
81  static void
82  release(const char * c, jstring j, JNIEnv * env) {
83  TRACE("void ParamStringT.release(const char *, jstring, JNIEnv *)");
84  if (c == NULL) {
85  assert(j == NULL);
86  } else {
87  assert(j);
88  // release the UTF-8 string allocated by GetStringUTFChars()
89  env->ReleaseStringUTFChars(j, c);
90  }
91  }
92 };
93 
94 template<>
95 struct ResultStringT< jstring, const char * > {
96  static jstring
97  convert(const char * c, JNIEnv * env) {
98  TRACE("jstring ResultStringT.convert(const char *, JNIEnv *)");
99  if (c == NULL)
100  return NULL;
101 
102  // construct a String object from a UTF-8 C string
103  return env->NewStringUTF(c);
104  }
105 };
106 
107 // ---------------------------------------------------------------------------
108 // Specializations for Java String <-> [const] char * type conversion
109 // ---------------------------------------------------------------------------
110 
111 // extend String specializations to const pointers
112 template< typename C >
113 struct Param< jstring, C * const >
115 template< typename C >
116 struct Result< jstring, C * const >
118 
119 // specialize Java Strings mapped to 'const char *'
120 template<>
121 struct Param< jstring, const char * >
123 template<>
124 struct Result< jstring, const char * >
126 
127 // specialize Java Strings mapped to 'char *' (only result mapping!)
128 // no parameter mapping desirable
129 // template<>
130 // struct Param< jstring, char * >
131 // : ParamStringT< jstring, const char * > {};
132 // result mapping of compatible with 'const char*'
133 template<>
134 struct Result< jstring, char * >
136 
137 // ---------------------------------------------------------------------------
138 
139 #endif // jtie_tconv_string_impl_hpp