AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
GlobalRef.h
1/*
2 * AUI Framework - Declarative UI toolkit for modern C++20
3 * Copyright (C) 2020-2024 Alex2772 and Contributors
4 *
5 * SPDX-License-Identifier: MPL-2.0
6 *
7 * This Source Code Form is subject to the terms of the Mozilla Public
8 * License, v. 2.0. If a copy of the MPL was not distributed with this
9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 */
11
12#pragma once
13
14#include "Globals.h"
15
16
17namespace aui::jni {
18
23 class GlobalRef {
24 private:
25 jobject mObject;
26 bool mLocal = false;
27
28 public:
29 GlobalRef():
30 mObject(nullptr) {
31
32 }
33 GlobalRef(jobject object):
34 mObject(object ? aui::jni::env()->NewGlobalRef(object) : nullptr) {
35
36 }
37 GlobalRef(const GlobalRef& object):
38 mObject(object.mObject ? aui::jni::env()->NewGlobalRef(object.mObject) : nullptr) {
39
40 }
41 ~GlobalRef() {
42 if (!mObject) return;
43 if (mLocal) {
44 aui::jni::env()->DeleteLocalRef(mObject);
45 } else {
46 aui::jni::env()->DeleteGlobalRef(mObject);
47 }
48 }
49
50 bool operator!() const {
51 return !mObject;
52 }
53
54 operator bool() const {
55 return mObject;
56 }
57
58
59 [[nodiscard]]
60 jobject asObject() const noexcept {
61 return mObject;
62 }
63
64 [[nodiscard]]
65 jclass asClass() const noexcept {
66 return (jclass)mObject;
67 }
68
69 static void assignLocalRef(GlobalRef& target, jobject value) {
70 AUI_ASSERT(target.mObject == nullptr);
71 target.mObject = value;
72 target.mLocal = true;
73 }
74 };
75}
Global ref.
Definition: GlobalRef.h:23
JNIEnv * env()
Definition: Globals.h:38
#define AUI_ASSERT(condition)
Asserts that the passed condition evaluates to true.
Definition: Assert.h:55