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