cid#1607171 Data race condition
[LibreOffice.git] / connectivity / source / inc / java / GlobalRef.hxx
blob1a97bfb33a45d0c0d646d2372fa9946b445e9d24
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #pragma once
22 #include <java/LocalRef.hxx>
23 #include <java/lang/Object.hxx>
26 namespace connectivity::jdbc
28 /** helper class to hold a local ref to a JNI object
30 template< typename T >
31 class GlobalRef
33 public:
34 GlobalRef()
35 :m_object( nullptr )
39 GlobalRef( const GlobalRef& _source )
40 :m_object( nullptr )
42 *this = _source;
45 GlobalRef& operator=( const GlobalRef& _source )
47 if ( &_source == this )
48 return *this;
50 SDBThreadAttach t;
51 set( t.env(), _source.get() );
52 return *this;
55 ~GlobalRef() COVERITY_NOEXCEPT_FALSE
57 reset();
60 void reset()
62 if ( m_object != nullptr )
64 SDBThreadAttach t;
65 t.env().DeleteGlobalRef( m_object );
66 m_object = nullptr;
70 void set( JNIEnv& _environment, T _object )
72 if ( m_object != nullptr )
73 _environment.DeleteGlobalRef( m_object );
74 m_object = _object;
75 if ( m_object )
76 m_object = static_cast< T >( _environment.NewGlobalRef( m_object ) );
79 void set( LocalRef< T >& _object )
81 set( _object.env(), _object.release() );
84 T get() const
86 return m_object;
89 bool is() const
91 return m_object != nullptr;
94 private:
95 T m_object;
99 } // namespace connectivity::jdbc
102 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */