/*********************************************************************** * director.swg * * This file contains support for director classes that proxy * method calls from C++ to Java extensions. * * Author : Scott Michel (scottm@aero.org) * * N.B.: This file was adapted from the python director.swg, written by * Mark Rose (mrose@stm.lbl.gov) ************************************************************************/ #ifdef __cplusplus /* director base class */ class __DIRECTOR__ { private: /* pointer to java virtual machine */ JavaVM *__jvm; protected: /* pointer to the wrapped java object */ jobject __self; /* Acquire Java VM environment from Java VM */ JNIEnv *__acquire_jenv() const { JNIEnv *env; assert(__jvm); __jvm->AttachCurrentThread((void **) &env, NULL); return env; } public: /* the default constructor should not be called */ __DIRECTOR__() : __jvm(NULL), __self(NULL) { assert(0); } /* Default constructor */ __DIRECTOR__(JNIEnv *jenv): __jvm((JavaVM *) NULL), __self(NULL) { /* Acquire the Java VM pointer */ jenv->GetJavaVM(&__jvm); } /* Remove the Java object global lock at destruction */ virtual ~__DIRECTOR__() { if (__self) { JNIEnv *jenv; jenv = __acquire_jenv(); jenv->DeleteGlobalRef(__self); __self = (jobject) NULL; } } /* Set __self and get Java global reference on object */ inline void __set_self(JNIEnv *jenv, jobject jself) { __self = jenv->NewGlobalRef(jself); } /* return a pointer to the wrapped java object */ inline jobject __get_self() const { return __self; } }; #endif /* __cplusplus */