LibreOffice Module connectivity (master) 1
GlobalRef.hxx
Go to the documentation of this file.
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#pragma once
21
22#include <java/LocalRef.hxx>
23#include <java/lang/Object.hxx>
24
25
26namespace connectivity::jdbc
27{
30 template< typename T >
32 {
33 public:
35 :m_object( nullptr )
36 {
37 }
38
39 GlobalRef( const GlobalRef& _source )
40 :m_object( nullptr )
41 {
42 *this = _source;
43 }
44
45 GlobalRef& operator=( const GlobalRef& _source )
46 {
47 if ( &_source == this )
48 return *this;
49
51 set( t.env(), _source.get() );
52 return *this;
53 }
54
55 ~GlobalRef() COVERITY_NOEXCEPT_FALSE
56 {
57 reset();
58 }
59
60 void reset()
61 {
62 if ( m_object != nullptr )
63 {
65 t.env().DeleteGlobalRef( m_object );
66 m_object = nullptr;
67 }
68 }
69
70 void set( JNIEnv& _environment, T _object )
71 {
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 ) );
77 }
78
79 void set( LocalRef< T >& _object )
80 {
81 set( _object.env(), _object.release() );
82 }
83
84 T get() const
85 {
86 return m_object;
87 }
88
89 bool is() const
90 {
91 return m_object != nullptr;
92 }
93
94 private:
96 };
97
98
99} // namespace connectivity::jdbc
100
101
102/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XPropertyListType t
helper class to hold a local ref to a JNI object
Definition: GlobalRef.hxx:32
GlobalRef & operator=(const GlobalRef &_source)
Definition: GlobalRef.hxx:45
void set(LocalRef< T > &_object)
Definition: GlobalRef.hxx:79
GlobalRef(const GlobalRef &_source)
Definition: GlobalRef.hxx:39
~GlobalRef() COVERITY_NOEXCEPT_FALSE
Definition: GlobalRef.hxx:55
void set(JNIEnv &_environment, T _object)
Definition: GlobalRef.hxx:70
helper class to hold a local ref to a JNI object
Definition: LocalRef.hxx:42
JNIEnv & env() const
Definition: LocalRef.hxx:79