22 #ifndef jtie_tconv_idcache_impl_hpp
23 #define jtie_tconv_idcache_impl_hpp
34 #if 0 // XXX ddefines not used at this time
37 #define JTIE_NO_JNI_ID_CACHING 0
38 #define JTIE_WEAK_JNI_ID_CACHING 1
39 #define JTIE_STRONG_JNI_ID_CACHING 2
41 #if !defined(JTIE_JNI_ID_CACHING)
42 # define JNI_ID_CACHING WEAK_CACHING
43 #elif (JTIE_JNI_ID_CACHING == JTIE_NO_JNI_ID_CACHING)
44 # define JNI_ID_CACHING NO_CACHING
45 #elif (JTIE_JNI_ID_CACHING == JTIE_WEAK_JNI_ID_CACHING)
46 # define JNI_ID_CACHING WEAK_CACHING
47 #elif (JTIE_JNI_ID_CACHING == JTIE_STRONG_JNI_ID_CACHING)
48 # define JNI_ID_CACHING STRONG_CACHING
50 # error "Illegal value of JTIE_JNI_ID_CACHING"
53 #endif // XXX ddefines not used at this time
59 template<
typename T >
61 jniGetMemberID(JNIEnv * env,
63 const char *
name,
const char * descriptor);
67 jniGetMemberID< jmethodID >(JNIEnv * env,
69 const char *
name,
const char * descriptor) {
70 return env->GetMethodID(cls,
name, descriptor);
75 jniGetMemberID< jfieldID >(JNIEnv * env,
77 const char *
name,
const char * descriptor) {
78 return env->GetFieldID(cls, name, descriptor);
88 #define JTIE_DEFINE_FIELD_MEMBER_INFO( T ) \
89 JTIE_DEFINE_CLASS_MEMBER_INFO( T, _jfieldID )
94 #define JTIE_DEFINE_METHOD_MEMBER_INFO( T ) \
95 JTIE_DEFINE_CLASS_MEMBER_INFO( T, _jmethodID )
100 #define JTIE_DEFINE_CLASS_MEMBER_INFO( T, IDT ) \
102 static const char * const class_name; \
103 static const char * const member_name; \
104 static const char * const member_descriptor; \
105 typedef IDT * memberID_t; \
111 #define JTIE_INSTANTIATE_CLASS_MEMBER_INFO( T, CN, MN, MD ) \
112 const char * const T::class_name = CN; \
113 const char * const T::member_name = MN; \
114 const char * const T::member_descriptor = MD; \
115 template struct MemberId< T >; \
116 template struct MemberIdCache< T >;
155 template<
typename C >
157 typedef typename C::memberID_t ID_t;
160 static unsigned long nIdLookUps;
174 (void)env; (void)cls;
197 assert(env->ExceptionCheck() == JNI_OK);
198 jclass cls = env->FindClass(C::class_name);
200 assert(env->ExceptionCheck() != JNI_OK);
206 snprintf(m, 256,
"JTie: failed to find Java class '%s'\n",
208 _snprintf(m, 256,
"JTie: failed to find Java class '%s'\n",
210 (C::class_name == NULL ?
"NULL" : C::class_name));
212 env->ExceptionDescribe();
216 assert(env->ExceptionCheck() == JNI_OK);
239 static ID_t
getId(JNIEnv * env, jclass cls) {
243 return jniGetMemberID< ID_t >(env, cls,
244 C::member_name, C::member_descriptor);
256 env->DeleteLocalRef(cls);
263 template<
typename C >
265 typedef typename C::memberID_t ID_t;
267 static ID_t
getId(JNIEnv * env, jclass cls) {
270 assert(env->IsSameObject(gClassRef, NULL) == JNI_FALSE);
271 (void)env; (void)cls;
277 static jclass gClassRef;
287 template<
typename C >
296 Base::gClassRef =
static_cast< jclass
>(env->NewWeakGlobalRef(cls));
306 jclass cls =
static_cast< jclass
>(env->NewLocalRef(Base::gClassRef));
321 env->DeleteLocalRef(cls);
329 template<
typename C >
338 Base::gClassRef =
static_cast< jclass
>(env->NewGlobalRef(cls));
345 jclass cls = Base::gClassRef;
359 (void)env; (void)cls;
367 template<
typename C >
377 jclass cls = Base::gClassRef;
378 assert(env->IsSameObject(cls, NULL) == JNI_FALSE);
384 (void)env; (void)cls;
392 template<
typename C >
402 jclass cls = Base::gClassRef;
403 assert(env->IsSameObject(cls, NULL) == JNI_FALSE);
425 enum JniMemberIdCaching {
429 #if 0 // preloaded caching very tricky, not supported at this time
430 ,WEAK_CACHING_PRELOAD
431 ,STRONG_CACHING_PRELOAD
432 #endif // preloaded caching very tricky, not supported at this time
438 template< JniMemberIdCaching M,
typename C >
441 template<
typename C >
442 struct JniMemberId< NO_CACHING, C >
445 template<
typename C >
446 struct JniMemberId< WEAK_CACHING, C >
449 template<
typename C >
450 struct JniMemberId< STRONG_CACHING, C >
453 #if 0 // preloaded caching very tricky, not supported at this time
454 template<
typename C >
455 struct JniMemberId< WEAK_CACHING_PRELOAD, C >
458 template<
typename C >
459 struct JniMemberId< STRONG_CACHING_PRELOAD, C >
461 #endif // preloaded caching very tricky, not supported at this time
465 #endif // jtie_tconv_idcache_impl_hpp