MPD 0.20.6

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

Ref.hxx
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2010-2011 Max Kellermann <max.kellermann@gmail.com>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the
14 * distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #ifndef JAVA_REF_HXX
31 #define JAVA_REF_HXX
32
33 #include "Global.hxx"
34
35 #include <jni.h>
36
37 #include <assert.h>
38
39 namespace Java {
43 template<typename T>
44 class LocalRef {
45 JNIEnv *const env;
46 const T value;
47
48 public:
52 LocalRef(JNIEnv *_env, T _value):env(_env), value(_value) {
53 assert(env != nullptr);
54 assert(value != nullptr);
55 }
56
57 ~LocalRef() {
58 env->DeleteLocalRef(value);
59 }
60
61 LocalRef(const LocalRef &other) = delete;
62 LocalRef &operator=(const LocalRef &other) = delete;
63
64 T Get() const {
65 return value;
66 }
67
68 operator T() const {
69 return value;
70 }
71 };
72
76 template<typename T>
77 class GlobalRef {
78 T value;
79
80 public:
85 GlobalRef() = default;
86
87 GlobalRef(JNIEnv *env, T _value):value(_value) {
88 assert(env != nullptr);
89 assert(value != nullptr);
90
91 value = (T)env->NewGlobalRef(value);
92 }
93
94 ~GlobalRef() {
95 GetEnv()->DeleteGlobalRef(value);
96 }
97
98 GlobalRef(const GlobalRef &other) = delete;
99 GlobalRef &operator=(const GlobalRef &other) = delete;
100
105 void Set(JNIEnv *env, T _value) {
106 assert(_value != nullptr);
107
108 value = (T)env->NewGlobalRef(_value);
109 }
110
111 T Get() const {
112 return value;
113 }
114
115 operator T() const {
116 return value;
117 }
118 };
119
127 template<typename T>
128 class TrivialRef {
129 T value;
130
131 public:
132 constexpr TrivialRef() {};
133
134 TrivialRef(const TrivialRef &other) = delete;
135 TrivialRef &operator=(const TrivialRef &other) = delete;
136
137 bool IsDefined() const {
138 return value != nullptr;
139 }
140
145 void Set(JNIEnv *env, T _value) {
146 assert(value == nullptr);
147 assert(_value != nullptr);
148
149 value = (T)env->NewGlobalRef(_value);
150 }
151
155 void Clear(JNIEnv *env) {
156 assert(value != nullptr);
157
158 env->DeleteGlobalRef(value);
159 value = nullptr;
160 }
161
166 void ClearOptional(JNIEnv *env) {
167 if (value != nullptr)
168 Clear(env);
169 }
170
171 T Get() const {
172 return value;
173 }
174
175 operator T() const {
176 return value;
177 }
178 };
179 }
180
181 #endif
Java::TrivialRef::Set
void Set(JNIEnv *env, T _value)
Obtain a global reference on the specified object and store it.
Definition: Ref.hxx:145
Java::TrivialRef::ClearOptional
void ClearOptional(JNIEnv *env)
Release the global reference and clear this object.
Definition: Ref.hxx:166
Java::LocalRef::~LocalRef
~LocalRef()
Definition: Ref.hxx:57
Java::GlobalRef
Hold a global reference on a JNI object.
Definition: Ref.hxx:77
Java::TrivialRef::operator=
TrivialRef & operator=(const TrivialRef &other)=delete
Java::GlobalRef::Set
void Set(JNIEnv *env, T _value)
Sets the object, ignoring the previous value.
Definition: Ref.hxx:105
Java::GlobalRef::~GlobalRef
~GlobalRef()
Definition: Ref.hxx:94
Global.hxx
Java::TrivialRef
Container for a global reference to a JNI object that gets initialised and deinitialised explicitly...
Definition: Ref.hxx:128
Java::GlobalRef::GlobalRef
GlobalRef()=default
Constructs an uninitialized object.
Java::LocalRef::Get
T Get() const
Definition: Ref.hxx:64
Java::TrivialRef::IsDefined
bool IsDefined() const
Definition: Ref.hxx:137
Java::GlobalRef::Get
T Get() const
Definition: Ref.hxx:111
Java::TrivialRef::Get
T Get() const
Definition: Ref.hxx:171
Java::GetEnv
static gcc_pure JNIEnv * GetEnv()
Definition: Global.hxx:50
Java::LocalRef::operator=
LocalRef & operator=(const LocalRef &other)=delete
Java::GlobalRef::GlobalRef
GlobalRef(JNIEnv *env, T _value)
Definition: Ref.hxx:87
Java::TrivialRef::Clear
void Clear(JNIEnv *env)
Release the global reference and clear this object.
Definition: Ref.hxx:155
Java
Definition: Class.hxx:37
Java::LocalRef::LocalRef
LocalRef(JNIEnv *_env, T _value)
The local reference is obtained by the caller.
Definition: Ref.hxx:52
Java::TrivialRef::TrivialRef
constexpr TrivialRef()
Definition: Ref.hxx:132
Java::LocalRef
Hold a local reference on a JNI object.
Definition: Ref.hxx:44
Java::GlobalRef::operator=
GlobalRef & operator=(const GlobalRef &other)=delete