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_GLOBALREF_HXX
25cdf0e10cSrcweir #define CONNECTIVITY_GLOBALREF_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "java/LocalRef.hxx"
28cdf0e10cSrcweir #include "java/lang/Object.hxx"
29cdf0e10cSrcweir 
30cdf0e10cSrcweir /** === begin UNO includes === **/
31cdf0e10cSrcweir /** === end UNO includes === **/
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <jvmaccess/virtualmachine.hxx>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir //........................................................................
36cdf0e10cSrcweir namespace connectivity { namespace jdbc
37cdf0e10cSrcweir {
38cdf0e10cSrcweir //........................................................................
39cdf0e10cSrcweir 
40cdf0e10cSrcweir 	//====================================================================
41cdf0e10cSrcweir 	//= GlobalRef
42cdf0e10cSrcweir 	//====================================================================
43cdf0e10cSrcweir     /** helper class to hold a local ref to a JNI object
44cdf0e10cSrcweir     */
45cdf0e10cSrcweir     template< typename T >
46cdf0e10cSrcweir     class GlobalRef
47cdf0e10cSrcweir     {
48cdf0e10cSrcweir     public:
GlobalRef()49cdf0e10cSrcweir         GlobalRef()
50cdf0e10cSrcweir             :m_object( NULL )
51cdf0e10cSrcweir         {
52cdf0e10cSrcweir         }
53cdf0e10cSrcweir 
GlobalRef(const GlobalRef & _source)54cdf0e10cSrcweir         GlobalRef( const GlobalRef& _source )
55cdf0e10cSrcweir             :m_object( NULL )
56cdf0e10cSrcweir         {
57cdf0e10cSrcweir             *this = _source;
58cdf0e10cSrcweir         }
59cdf0e10cSrcweir 
operator =(const GlobalRef & _source)60cdf0e10cSrcweir         GlobalRef& operator=( const GlobalRef& _source )
61cdf0e10cSrcweir         {
62cdf0e10cSrcweir             if ( &_source == this )
63cdf0e10cSrcweir                 return *this;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir             SDBThreadAttach t;
66cdf0e10cSrcweir             set( t.env(), _source.get() );
67cdf0e10cSrcweir             return *this;
68cdf0e10cSrcweir         }
69cdf0e10cSrcweir 
~GlobalRef()70cdf0e10cSrcweir         ~GlobalRef()
71cdf0e10cSrcweir         {
72cdf0e10cSrcweir             reset();
73cdf0e10cSrcweir         }
74cdf0e10cSrcweir 
reset()75cdf0e10cSrcweir         void reset()
76cdf0e10cSrcweir         {
77cdf0e10cSrcweir             if ( m_object != NULL )
78cdf0e10cSrcweir             {
79cdf0e10cSrcweir                 SDBThreadAttach t;
80cdf0e10cSrcweir                 t.env().DeleteGlobalRef( m_object );
81cdf0e10cSrcweir                 m_object = NULL;
82cdf0e10cSrcweir             }
83cdf0e10cSrcweir         }
84cdf0e10cSrcweir 
set(JNIEnv & _environment,T _object)85cdf0e10cSrcweir         void set( JNIEnv& _environment, T _object )
86cdf0e10cSrcweir         {
87cdf0e10cSrcweir             if ( m_object != NULL )
88cdf0e10cSrcweir                 _environment.DeleteGlobalRef( m_object );
89cdf0e10cSrcweir             m_object = _object;
90cdf0e10cSrcweir             if ( m_object )
91cdf0e10cSrcweir                 m_object = static_cast< T >( _environment.NewGlobalRef( m_object ) );
92cdf0e10cSrcweir         }
93cdf0e10cSrcweir 
set(LocalRef<T> & _object)94cdf0e10cSrcweir         void set( LocalRef< T >& _object )
95cdf0e10cSrcweir         {
96cdf0e10cSrcweir             set( _object.env(), _object.release() );
97cdf0e10cSrcweir         }
98cdf0e10cSrcweir 
get() const99cdf0e10cSrcweir         T get() const
100cdf0e10cSrcweir         {
101cdf0e10cSrcweir             return m_object;
102cdf0e10cSrcweir         }
103cdf0e10cSrcweir 
is() const104cdf0e10cSrcweir         bool is() const
105cdf0e10cSrcweir         {
106cdf0e10cSrcweir             return m_object != NULL;
107cdf0e10cSrcweir         }
108cdf0e10cSrcweir 
109cdf0e10cSrcweir     private:
110cdf0e10cSrcweir         T   m_object;
111cdf0e10cSrcweir     };
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 
114cdf0e10cSrcweir //........................................................................
115cdf0e10cSrcweir } } // namespace connectivity::jdbc
116cdf0e10cSrcweir //........................................................................
117cdf0e10cSrcweir 
118cdf0e10cSrcweir #endif // CONNECTIVITY_GLOBALREF_HXX
119