comparison Plugin/JavaGlobalReference.cpp @ 7:b14ed1ea3a23

reorganization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 19 Oct 2023 11:14:45 +0200
parents
children 1c407ba1d311
comparison
equal deleted inserted replaced
6:709e5347a390 7:b14ed1ea3a23
1 /**
2 * SPDX-FileCopyrightText: 2023 Sebastien Jodogne, UCLouvain, Belgium
3 * SPDX-License-Identifier: GPL-3.0-or-later
4 */
5
6 /**
7 * Java plugin for Orthanc
8 * Copyright (C) 2023 Sebastien Jodogne, UCLouvain, Belgium
9 *
10 * This program is free software: you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation, either version 3 of the
13 * License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 **/
23
24
25 #include "JavaGlobalReference.h"
26
27 #include "JavaEnvironment.h"
28
29 #include <cassert>
30 #include <stdexcept>
31
32 extern OrthancPluginContext* context_;
33
34
35 JavaGlobalReference::JavaGlobalReference(JavaVirtualMachine& jvm,
36 jobject obj) :
37 jvm_(jvm),
38 obj_(NULL)
39 {
40 if (obj == NULL)
41 {
42 throw std::runtime_error("Null pointer");
43 }
44
45 JavaEnvironment env(jvm);
46
47 obj_ = env.GetValue().NewGlobalRef(obj);
48 if (obj_ == NULL)
49 {
50 throw std::runtime_error("Cannot create global reference");
51 }
52 }
53
54
55 JavaGlobalReference::~JavaGlobalReference()
56 {
57 assert(obj_ != NULL);
58
59 try
60 {
61 JavaEnvironment env(jvm_);
62 env.GetValue().DeleteGlobalRef(obj_);
63 }
64 catch (std::runtime_error& e)
65 {
66 OrthancPluginLogError(context_, e.what());
67 }
68 }
69
70
71 jobject JavaGlobalReference::GetValue()
72 {
73 assert(obj_ != NULL);
74 return obj_;
75 }