MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
jtie_tconv_utils_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_utils_impl.hpp
19  */
20 
21 #ifndef jtie_tconv_utils_impl_hpp
22 #define jtie_tconv_utils_impl_hpp
23 
24 #include <jni.h>
25 
26 //#include "helpers.hpp"
27 
28 // ---------------------------------------------------------------------------
29 // Java <-> C type conversion utilities
30 // ---------------------------------------------------------------------------
31 
32 // provides some (meta) predicates on types
33 template< typename C >
34 struct TypeInfo {
35  // indicates whether a type is const
36  static bool isMutable() {
37  return true;
38  }
39 };
40 
41 // partial specialization for const types
42 template< typename C >
43 struct TypeInfo< const C > {
44  static bool isMutable() {
45  return false;
46  }
47 };
48 
49 // registers an exception with JNI for this thread
50 inline void
51 registerException(JNIEnv * env, const char * jvmClassName, const char * msg)
52 {
53  jclass ec = env->FindClass(jvmClassName);
54  if (ec == NULL) {
55  // exception pending
56  } else {
57  env->ThrowNew(ec, msg);
58  env->DeleteLocalRef(ec);
59  // exception pending
60  }
61 }
62 
63 // returns a value in big endian format
64 template<typename C>
65 C
66 big_endian(C c)
67 {
68  // test if big or little endian
69  C r = 1;
70  if(*(char *)&r == 0) {
71  // big endian, ok
72  return c;
73  }
74 
75  // little-endian, reverse byte order (better: use optimized swap macros)
76  const size_t n = sizeof(C);
77  char * s = (char *)&c;
78  char * t = (char *)&r;
79  for (int i = n-1, j = 0; i >= 0; i--, j++)
80  t[j] = s[i];
81  return r;
82 }
83 
84 // ---------------------------------------------------------------------------
85 
86 #endif // jtie_tconv_utils_impl_hpp