update dev300-m58
[ooovba.git] / connectivity / source / inc / java / LocalRef.hxx
blobde6e11e250af1cc6a06c0fa1cd77683077387f16
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: LocalRef.hxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef CONNECTIVITY_LOCALREF_HXX
32 #define CONNECTIVITY_LOCALREF_HXX
34 /** === begin UNO includes === **/
35 /** === end UNO includes === **/
37 #include <jvmaccess/virtualmachine.hxx>
39 //........................................................................
40 namespace connectivity { namespace jdbc
42 //........................................................................
44 //====================================================================
45 //= LocalRef
46 //====================================================================
47 /** helper class to hold a local ref to a JNI object
49 Note that this class never actually calls NewLocalRef. It is assumed that all objects
50 passed are already acquired with a local ref (as it usually is the case if you obtain
51 the object from an JNI method).
53 template< typename T >
54 class LocalRef
56 public:
57 explicit LocalRef( JNIEnv& environment )
58 :m_environment( environment )
59 ,m_object( NULL )
63 LocalRef( JNIEnv& environment, T object )
64 :m_environment( environment )
65 ,m_object( object )
69 ~LocalRef()
71 reset();
74 T release()
76 T t = m_object;
77 m_object = NULL;
78 return t;
81 void set( T object ) { reset(); m_object = object; }
83 void reset()
85 if ( m_object != NULL )
87 m_environment.DeleteLocalRef( m_object );
88 m_object = NULL;
92 JNIEnv& env() const { return m_environment; }
93 T get() const { return m_object; }
94 bool is() const { return m_object != NULL; }
96 private:
97 LocalRef(LocalRef &); // not defined
98 void operator =(LocalRef &); // not defined
100 protected:
101 JNIEnv& m_environment;
102 T m_object;
105 //........................................................................
106 } } // namespace connectivity::jdbc
107 //........................................................................
109 #endif // CONNECTIVITY_LOCALREF_HXX