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, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is also distributed with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24 /*
25 * jtie_tconv_refbybb_impl.hpp
26 */
27
28 #ifndef jtie_tconv_refbybb_impl_hpp
29 #define jtie_tconv_refbybb_impl_hpp
30
31 #include <assert.h> // not using namespaces yet
32 #include <jni.h>
33
34 #include "jtie_tconv_refbybb.hpp"
35 #include "jtie_tconv_impl.hpp"
36 #include "jtie_tconv_ptrbybb_impl.hpp"
37 #include "jtie_tconv_utils_impl.hpp"
38 #include "helpers.hpp"
39
40 // ---------------------------------------------------------------------------
41 // ByteBufferRefParam, ByteBufferRefResult
42 // ---------------------------------------------------------------------------
43
44 // XXX document, cleanup
45
46 // implements the mapping of ByteBuffers to reference parameters
47 template< typename J, typename C > struct ByteBufferRefParam;
48
49 // implements the mapping of ByteBuffers to reference results
50 template< typename J, typename C > struct ByteBufferRefResult;
51
52 inline cstatus
ensureNonNullBuffer(jtie_j_n_ByteBuffer jbb,JNIEnv * env)53 ensureNonNullBuffer(jtie_j_n_ByteBuffer jbb, JNIEnv * env) {
54 // init return value to error
55 cstatus s = -1;
56
57 if (jbb == NULL) {
58 const char * c = "java/lang/IllegalArgumentException";
59 const char * m = ("JTie: java.nio.ByteBuffer cannot be null"
60 " when mapped to an object reference type"
61 " (file: " __FILE__ ")");
62 registerException(env, c, m);
63 } else {
64 // ok
65 s = 0;
66 }
67 return s;
68 }
69
70 template< typename J, typename C >
71 struct ByteBufferRefParam {
72
73 static C &
convertByteBufferRefParam74 convert(cstatus & s, jtie_j_n_ByteBuffer j, JNIEnv * env) {
75 TRACE("C & ByteBufferRefParam.convert(cstatus &, jtie_j_n_ByteBuffer, JNIEnv *)");
76
77 // init return value and status to error
78 s = -1;
79 C * c = NULL;
80
81 if (ensureNonNullBuffer(j, env) != 0) {
82 // exception pending
83 } else {
84 c = ByteBufferPtrParam< J, C >::convert(s, j, env);
85 assert(s != 0 || c != NULL);
86 }
87 return *c;
88 }
89
90 static void
releaseByteBufferRefParam91 release(C & c, jtie_j_n_ByteBuffer j, JNIEnv * env) {
92 TRACE("void ByteBufferRefParam.release(C &, jtie_j_n_ByteBuffer, JNIEnv *)");
93 ByteBufferPtrParam< J, C >::release(&c, j, env);
94 }
95 };
96
97 template< typename J, typename C >
98 struct ByteBufferRefResult {
99 static J *
convertByteBufferRefResult100 convert(C & c, JNIEnv * env) {
101 TRACE("J * ByteBufferRefResult.convert(C &, JNIEnv *)");
102 // technically, C++ references can be null, hence, no asserts here
103 //assert(&c != NULL);
104 J * j = ByteBufferPtrResult< J, C >::convert(&c, env);
105 //assert(j != NULL);
106 return j;
107 }
108 };
109
110 // ---------------------------------------------------------------------------
111 // Specializations for ByteBuffer type conversions
112 // ---------------------------------------------------------------------------
113
114 // specialize ByteBuffers mapped to references:
115 // - params: require a minimum buffer capacity of the size of the base type
116 // - results: allocate buffer with a capacity of the size of the base type
117 template< typename C >
118 struct Param< jtie_j_n_ByteBuffer, C & >
119 : ByteBufferRefParam< _jtie_j_n_BoundedByteBuffer< sizeof(C) >, C > {};
120 template< typename C >
121 struct Result< jtie_j_n_ByteBuffer, C & >
122 : ByteBufferRefResult< _jtie_j_n_BoundedByteBuffer< sizeof(C) >, C > {};
123
124 // ---------------------------------------------------------------------------
125
126 #endif // jtie_tconv_refbybb_impl_hpp
127