1*caf5cd79SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*caf5cd79SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*caf5cd79SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*caf5cd79SAndrew Rist  * distributed with this work for additional information
6*caf5cd79SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*caf5cd79SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*caf5cd79SAndrew Rist  * "License"); you may not use this file except in compliance
9*caf5cd79SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*caf5cd79SAndrew Rist  *
11*caf5cd79SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*caf5cd79SAndrew Rist  *
13*caf5cd79SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*caf5cd79SAndrew Rist  * software distributed under the License is distributed on an
15*caf5cd79SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*caf5cd79SAndrew Rist  * KIND, either express or implied.  See the License for the
17*caf5cd79SAndrew Rist  * specific language governing permissions and limitations
18*caf5cd79SAndrew Rist  * under the License.
19*caf5cd79SAndrew Rist  *
20*caf5cd79SAndrew Rist  *************************************************************/
21*caf5cd79SAndrew Rist 
22*caf5cd79SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef CONNECTIVITY_LOCALREF_HXX
25cdf0e10cSrcweir #define CONNECTIVITY_LOCALREF_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir /** === begin UNO includes === **/
28cdf0e10cSrcweir /** === end UNO includes === **/
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <jvmaccess/virtualmachine.hxx>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir //........................................................................
33cdf0e10cSrcweir namespace connectivity { namespace jdbc
34cdf0e10cSrcweir {
35cdf0e10cSrcweir //........................................................................
36cdf0e10cSrcweir 
37cdf0e10cSrcweir 	//====================================================================
38cdf0e10cSrcweir 	//= LocalRef
39cdf0e10cSrcweir 	//====================================================================
40cdf0e10cSrcweir     /** helper class to hold a local ref to a JNI object
41cdf0e10cSrcweir 
42cdf0e10cSrcweir         Note that this class never actually calls NewLocalRef. It is assumed that all objects
43cdf0e10cSrcweir         passed are already acquired with a local ref (as it usually is the case if you obtain
44cdf0e10cSrcweir         the object from an JNI method).
45cdf0e10cSrcweir     */
46cdf0e10cSrcweir     template< typename T >
47cdf0e10cSrcweir     class LocalRef
48cdf0e10cSrcweir     {
49cdf0e10cSrcweir     public:
LocalRef(JNIEnv & environment)50cdf0e10cSrcweir         explicit LocalRef( JNIEnv& environment )
51cdf0e10cSrcweir             :m_environment( environment )
52cdf0e10cSrcweir             ,m_object( NULL )
53cdf0e10cSrcweir         {
54cdf0e10cSrcweir         }
55cdf0e10cSrcweir 
LocalRef(JNIEnv & environment,T object)56cdf0e10cSrcweir         LocalRef( JNIEnv& environment, T object )
57cdf0e10cSrcweir             :m_environment( environment )
58cdf0e10cSrcweir             ,m_object( object )
59cdf0e10cSrcweir         {
60cdf0e10cSrcweir         }
61cdf0e10cSrcweir 
~LocalRef()62cdf0e10cSrcweir         ~LocalRef()
63cdf0e10cSrcweir         {
64cdf0e10cSrcweir             reset();
65cdf0e10cSrcweir         }
66cdf0e10cSrcweir 
release()67cdf0e10cSrcweir         T release()
68cdf0e10cSrcweir         {
69cdf0e10cSrcweir             T t = m_object;
70cdf0e10cSrcweir             m_object = NULL;
71cdf0e10cSrcweir             return t;
72cdf0e10cSrcweir         }
73cdf0e10cSrcweir 
set(T object)74cdf0e10cSrcweir         void set( T object ) { reset(); m_object = object; }
75cdf0e10cSrcweir 
reset()76cdf0e10cSrcweir         void reset()
77cdf0e10cSrcweir         {
78cdf0e10cSrcweir             if ( m_object != NULL )
79cdf0e10cSrcweir             {
80cdf0e10cSrcweir                 m_environment.DeleteLocalRef( m_object );
81cdf0e10cSrcweir                 m_object = NULL;
82cdf0e10cSrcweir             }
83cdf0e10cSrcweir         }
84cdf0e10cSrcweir 
env() const85cdf0e10cSrcweir         JNIEnv& env() const { return m_environment; }
get() const86cdf0e10cSrcweir         T       get() const { return m_object; }
is() const87cdf0e10cSrcweir         bool    is()  const { return m_object != NULL; }
88cdf0e10cSrcweir 
89cdf0e10cSrcweir     private:
90cdf0e10cSrcweir         LocalRef(LocalRef &); // not defined
91cdf0e10cSrcweir         void operator =(LocalRef &); // not defined
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     protected:
94cdf0e10cSrcweir         JNIEnv& m_environment;
95cdf0e10cSrcweir         T       m_object;
96cdf0e10cSrcweir     };
97cdf0e10cSrcweir 
98cdf0e10cSrcweir //........................................................................
99cdf0e10cSrcweir } } // namespace connectivity::jdbc
100cdf0e10cSrcweir //........................................................................
101cdf0e10cSrcweir 
102cdf0e10cSrcweir #endif // CONNECTIVITY_LOCALREF_HXX
103