1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org 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 Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 #ifndef CONNECTIVITY_LOCALREF_HXX 29 #define CONNECTIVITY_LOCALREF_HXX 30 31 /** === begin UNO includes === **/ 32 /** === end UNO includes === **/ 33 34 #include <jvmaccess/virtualmachine.hxx> 35 36 //........................................................................ 37 namespace connectivity { namespace jdbc 38 { 39 //........................................................................ 40 41 //==================================================================== 42 //= LocalRef 43 //==================================================================== 44 /** helper class to hold a local ref to a JNI object 45 46 Note that this class never actually calls NewLocalRef. It is assumed that all objects 47 passed are already acquired with a local ref (as it usually is the case if you obtain 48 the object from an JNI method). 49 */ 50 template< typename T > 51 class LocalRef 52 { 53 public: 54 explicit LocalRef( JNIEnv& environment ) 55 :m_environment( environment ) 56 ,m_object( NULL ) 57 { 58 } 59 60 LocalRef( JNIEnv& environment, T object ) 61 :m_environment( environment ) 62 ,m_object( object ) 63 { 64 } 65 66 ~LocalRef() 67 { 68 reset(); 69 } 70 71 T release() 72 { 73 T t = m_object; 74 m_object = NULL; 75 return t; 76 } 77 78 void set( T object ) { reset(); m_object = object; } 79 80 void reset() 81 { 82 if ( m_object != NULL ) 83 { 84 m_environment.DeleteLocalRef( m_object ); 85 m_object = NULL; 86 } 87 } 88 89 JNIEnv& env() const { return m_environment; } 90 T get() const { return m_object; } 91 bool is() const { return m_object != NULL; } 92 93 private: 94 LocalRef(LocalRef &); // not defined 95 void operator =(LocalRef &); // not defined 96 97 protected: 98 JNIEnv& m_environment; 99 T m_object; 100 }; 101 102 //........................................................................ 103 } } // namespace connectivity::jdbc 104 //........................................................................ 105 106 #endif // CONNECTIVITY_LOCALREF_HXX 107