21 #ifndef jtie_tconv_object_impl_hpp
22 #define jtie_tconv_object_impl_hpp
28 #include "jtie_tconv_object.hpp"
29 #include "jtie_tconv_impl.hpp"
30 #include "jtie_tconv_idcache_impl.hpp"
31 #include "jtie_tconv_utils_impl.hpp"
32 #include "helpers.hpp"
41 JTIE_DEFINE_FIELD_MEMBER_INFO(_Wrapper_cdelegate)
45 typedef JniMemberId< WEAK_CACHING, _Wrapper_cdelegate > Wrapper_cdelegate;
56 template< typename J, typename C >
60 template< typename J, typename C >
64 template< typename J, typename C >
68 template< typename C >
71 convert(cstatus & s, _jtie_Object * j, JNIEnv * env) {
72 TRACE(
"C * ObjectParam.convert(cstatus &, _jtie_Object *, JNIEnv *)");
82 jclass cls = Wrapper_cdelegate::getClass(env);
87 jfieldID fid = Wrapper_cdelegate::getId(env, cls);
92 jlong p = env->GetLongField(j, fid);
96 = (
"JTie: Java wrapper object must have a"
97 " non-zero delegate when used as target or"
98 " argument in a method call"
99 " (file: " __FILE__
")");
100 registerException(env,
"java/lang/AssertionError", m);
103 intptr_t ip =
static_cast< intptr_t
>(p);
104 assert(static_cast< jlong >(ip) == p);
105 c =
reinterpret_cast< C *
>(ip);
113 Wrapper_cdelegate::releaseRef(env, cls);
120 release(C * c, _jtie_Object * j, JNIEnv * env) {
121 TRACE(
"void ObjectParam.release(C *, _jtie_Object *, JNIEnv *)");
123 (void)c; (void)j; (void)env;
128 template<
typename C >
132 TRACE(
"C & ObjectParam.convert(cstatus &, _jtie_Object *, JNIEnv *)");
140 = (
"JTie: Java argument must not be null when mapped"
141 " to a C reference (file: " __FILE__
")");
142 registerException(env,
"java/lang/IllegalArgumentException", msg);
144 c = ObjectParam< _jtie_Object *, C * >::convert(s, j, env);
145 assert(s != 0 || c != NULL);
154 TRACE(
"void ObjectParam.release(C &, _jtie_Object *, JNIEnv *)");
155 ObjectParam< _jtie_Object *, C * >::release(&c, j, env);
160 template<
typename C >
164 TRACE(
"C & Target.convert(cstatus &, _jtie_Object *, JNIEnv *)");
171 const char *
msg = (
"JTie: Java target object of a method call"
172 " must not be null (file: " __FILE__
")");
173 registerException(env,
"java/lang/NullPointerException", msg);
178 c = ObjectParam< _jtie_Object *, C * >::convert(s, j, env);
179 assert(s != 0 || c != NULL);
188 TRACE(
"void Target.release(C &, _jtie_Object *, JNIEnv *)");
190 ObjectParam< _jtie_Object *, C * >::release(&c, j, env);
195 template<
typename J,
typename C >
196 struct ObjectResult< J *, C * > {
199 typedef JniMemberId< WEAK_CACHING, J > J_ctor;
203 convert(C * c, JNIEnv * env) {
204 TRACE(
"J * ObjectResult.convert(JNIEnv *, C *)");
211 jclass cls = J_ctor::getClass(env);
216 jmethodID cid = J_ctor::getId(env, cls);
220 J * jo = wrapAsJavaObject(cls, cid, c, env);
229 J_ctor::releaseRef(env, cls);
238 wrapAsJavaObject(jclass cls, jmethodID cid, C * c, JNIEnv * env);
242 template<
typename J ,
typename C >
243 struct ObjectResult< J *, C & > {
245 convert(C & c, JNIEnv * env) {
246 TRACE(
"J * ObjectResult.convert(JNIEnv *, C &)");
252 = (
"JTie: returned C reference must not be null"
253 " (e.g., check if memory allocation has failed without"
254 " raising an exception, as can happen with older C++"
255 " compilers?) (file: " __FILE__
")");
256 registerException(env,
"java/lang/AssertionError", msg);
259 j = ObjectResult< J *, C * >::convert(p, env);
276 jclass cls = Wrapper_cdelegate::getClass(env);
281 jfieldID fid = Wrapper_cdelegate::getId(env, cls);
288 jlong p =
static_cast< jlong
>(ip);
289 assert(static_cast< intptr_t >(p) == ip);
292 env->SetLongField(jo, fid, p);
295 Wrapper_cdelegate::releaseRef(env, cls);
300 template<
typename J,
typename C >
302 ObjectResult< J *, C * >::
303 wrapAsJavaObject(jclass cls, jmethodID cid, C * c, JNIEnv * env) {
309 jclass cls0 = Wrapper_cdelegate::getClass(env);
314 jfieldID fid = Wrapper_cdelegate::getId(env, cls0);
319 jobject jo = env->NewObject(cls, cid);
325 intptr_t ip =
reinterpret_cast< intptr_t
>(c);
326 jlong p =
static_cast< jlong
>(ip);
327 assert(static_cast< intptr_t >(p) == ip);
330 env->SetLongField(jo, fid, p);
333 j =
static_cast< J *
>(jo);
337 Wrapper_cdelegate::releaseRef(env, cls0);
347 template<
typename J,
typename C >
350 template<
typename J,
typename C >
357 template<
typename J,
typename C >
362 template<
typename J,
typename C >
367 template<
typename J,
typename C >
375 template<
typename J,
typename C >
377 : ObjectResult< _jtie_ObjectMapper< J > *, C * > {};
380 template<
typename J,
typename C >
382 : ObjectResult< _jtie_ObjectMapper< J > *, C & > {};
386 #endif // jtie_tconv_object_impl_hpp