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