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 INCLUDED_CONNECTIVITY_SOURCE_INC_JAVA_GLOBALREF_HXX
21 : #define INCLUDED_CONNECTIVITY_SOURCE_INC_JAVA_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 : /** helper class to hold a local ref to a JNI object
32 : */
33 : template< typename T >
34 : class GlobalRef
35 : {
36 : public:
37 9 : GlobalRef()
38 9 : :m_object( NULL )
39 : {
40 9 : }
41 :
42 229 : GlobalRef( const GlobalRef& _source )
43 229 : :m_object( NULL )
44 : {
45 229 : *this = _source;
46 229 : }
47 :
48 229 : GlobalRef& operator=( const GlobalRef& _source )
49 : {
50 229 : if ( &_source == this )
51 0 : return *this;
52 :
53 229 : SDBThreadAttach t;
54 229 : set( t.env(), _source.get() );
55 229 : return *this;
56 : }
57 :
58 238 : ~GlobalRef()
59 : {
60 238 : reset();
61 238 : }
62 :
63 247 : void reset()
64 : {
65 247 : if ( m_object != NULL )
66 : {
67 238 : SDBThreadAttach t;
68 238 : t.env().DeleteGlobalRef( m_object );
69 238 : m_object = NULL;
70 : }
71 247 : }
72 :
73 238 : void set( JNIEnv& _environment, T _object )
74 : {
75 238 : if ( m_object != NULL )
76 0 : _environment.DeleteGlobalRef( m_object );
77 238 : m_object = _object;
78 238 : if ( m_object )
79 238 : m_object = static_cast< T >( _environment.NewGlobalRef( m_object ) );
80 238 : }
81 :
82 9 : void set( LocalRef< T >& _object )
83 : {
84 9 : set( _object.env(), _object.release() );
85 9 : }
86 :
87 467 : T get() const
88 : {
89 467 : return m_object;
90 : }
91 :
92 238 : bool is() const
93 : {
94 238 : return m_object != NULL;
95 : }
96 :
97 : private:
98 : T m_object;
99 : };
100 :
101 :
102 :
103 : } } // namespace connectivity::jdbc
104 :
105 :
106 : #endif // INCLUDED_CONNECTIVITY_SOURCE_INC_JAVA_GLOBALREF_HXX
107 :
108 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|