Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : : #ifndef CONNECTIVITY_GLOBALREF_HXX
21 : : #define CONNECTIVITY_GLOBALREF_HXX
22 : :
23 : : #include "java/LocalRef.hxx"
24 : : #include "java/lang/Object.hxx"
25 : :
26 : : #include <jvmaccess/virtualmachine.hxx>
27 : :
28 : : //........................................................................
29 : : namespace connectivity { namespace jdbc
30 : : {
31 : : //........................................................................
32 : :
33 : : //====================================================================
34 : : //= GlobalRef
35 : : //====================================================================
36 : : /** helper class to hold a local ref to a JNI object
37 : : */
38 : : template< typename T >
39 : : class GlobalRef
40 : : {
41 : : public:
42 : 0 : GlobalRef()
43 : 0 : :m_object( NULL )
44 : : {
45 : 0 : }
46 : :
47 : 0 : GlobalRef( const GlobalRef& _source )
48 : 0 : :m_object( NULL )
49 : : {
50 : 0 : *this = _source;
51 : 0 : }
52 : :
53 : 0 : GlobalRef& operator=( const GlobalRef& _source )
54 : : {
55 : 0 : if ( &_source == this )
56 : 0 : return *this;
57 : :
58 : 0 : SDBThreadAttach t;
59 : 0 : set( t.env(), _source.get() );
60 : 0 : return *this;
61 : : }
62 : :
63 : 0 : ~GlobalRef()
64 : : {
65 : 0 : reset();
66 : 0 : }
67 : :
68 : 0 : void reset()
69 : : {
70 : 0 : if ( m_object != NULL )
71 : : {
72 : 0 : SDBThreadAttach t;
73 : 0 : t.env().DeleteGlobalRef( m_object );
74 : 0 : m_object = NULL;
75 : : }
76 : 0 : }
77 : :
78 : 0 : void set( JNIEnv& _environment, T _object )
79 : : {
80 : 0 : if ( m_object != NULL )
81 : 0 : _environment.DeleteGlobalRef( m_object );
82 : 0 : m_object = _object;
83 : 0 : if ( m_object )
84 : 0 : m_object = static_cast< T >( _environment.NewGlobalRef( m_object ) );
85 : 0 : }
86 : :
87 : 0 : void set( LocalRef< T >& _object )
88 : : {
89 : 0 : set( _object.env(), _object.release() );
90 : 0 : }
91 : :
92 : 0 : T get() const
93 : : {
94 : 0 : return m_object;
95 : : }
96 : :
97 : 0 : bool is() const
98 : : {
99 : 0 : return m_object != NULL;
100 : : }
101 : :
102 : : private:
103 : : T m_object;
104 : : };
105 : :
106 : :
107 : : //........................................................................
108 : : } } // namespace connectivity::jdbc
109 : : //........................................................................
110 : :
111 : : #endif // CONNECTIVITY_GLOBALREF_HXX
112 : :
113 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|