JNIReferences.h

00001 /*
00002  * @(#)JNIReferences.h  0.2.0 / 2007-10-05
00003  *
00004  * Copyright (c) 2007, ETF and contributors. All rights reserved.
00005  *
00006  * This software is licensed under the LGPL, version 2.1, license.
00007  * A copy of the license is included in the file LICENSE-LGPL.txt.
00008  */
00009 #ifndef JNIREFERENCES_H
00010 #define JNIREFERENCES_H
00011 
00012 #include "JNIWrapper.h"
00013 
00014 /*^**************************************************************************^*/
00015 /*- GlobalReference Template.                                                -*/
00016 /*^**************************************************************************^*/
00017 
00024 template<typename T>
00025 class GlobalReference
00026 {
00027 public:
00028   GlobalReference(JNIEnv *env, T t);
00029   virtual ~GlobalReference();
00030 private:
00031   GlobalReference(const GlobalReference &);
00032 public:
00033   inline T ref() const;
00034 protected:
00035   JNIEnv *m_env;
00036   T m_obj;
00037 };
00038 
00039 /*^**************************************************************************^*/
00040 /*- GlobalReference :: Public Constructor.                                   -*/
00041 /*^**************************************************************************^*/
00042 
00049 template<typename T>
00050 GlobalReference<T>::GlobalReference(JNIEnv *env, T t)
00051 {
00052   if (t) {
00053     m_obj = static_cast<T>(JNIWrapper::newGlobalRef(env, t));
00054     Q_ASSERT(env);
00055     m_env = env;
00056   } else {
00057     m_obj = 0;
00058   }
00059 }
00060 
00061 /*^**************************************************************************^*/
00062 /*- GlobalReference :: Public Destructor.                                    -*/
00063 /*^**************************************************************************^*/
00064 
00068 template<typename T>
00069 GlobalReference<T>::~GlobalReference()
00070 {
00071   if (m_obj) {
00072     m_env->DeleteGlobalRef(m_obj);
00073   }
00074 }
00075 
00076 /*^**************************************************************************^*/
00077 /*- GlobalReference :: Private Constructor.                                  -*/
00078 /*^**************************************************************************^*/
00079 
00080 template<typename T>
00081 GlobalReference<T>::GlobalReference(const GlobalReference &)
00082 {
00083   // Copy constructor is disabled.
00084 }
00085 
00086 /*^**************************************************************************^*/
00087 /*- GlobalReference :: Public Inline Member Function.                        -*/
00088 /*^**************************************************************************^*/
00089 
00095 template<typename T>
00096 inline T GlobalReference<T>::ref() const
00097 {
00098   return m_obj;
00099 }
00100 
00101 /*^**************************************************************************^*/
00102 /*- GlobalReference :: Explicit Template Instantiations.                     -*/
00103 /*^**************************************************************************^*/
00104 
00105 template class GlobalReference<jobject>;
00106 template class GlobalReference<jstring>;
00107 template class GlobalReference<jclass>;
00108 template class GlobalReference<jbyteArray>;
00109 
00110 /*^**************************************************************************^*/
00111 /*- GlobalReference :: Template Specializaions.                              -*/
00112 /*^**************************************************************************^*/
00113 
00114 typedef GlobalReference<jobject> GlobalJObject;
00115 typedef GlobalReference<jstring> GlobalJString;
00116 typedef GlobalReference<jclass> GlobalJClass;
00117 typedef GlobalReference<jbyteArray> GlobalJByteArray;
00118 
00119 #endif //JNIREFERENCES_H
00120 
00121 /*^*****************************************************************************
00122   File History:
00123 
00124  - 2007-10-05 09:57:10 Slobodan
00125    Initial version
00126 
00127 *****************************************************************************^*/

Copyright © 2007 ETF and contributors. All Rights Reserved.