Ocean
Loading...
Searching...
No Matches
ScopedJNIObject.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_PLATFORM_ANDROID_SCOPED_JNI_OBJECT_H
9#define META_OCEAN_PLATFORM_ANDROID_SCOPED_JNI_OBJECT_H
10
13
14namespace Ocean
15{
16
17namespace Platform
18{
19
20namespace Android
21{
22
23// Forward declaration.
24template <typename T> class ScopedJNIObject;
25
26/**
27 * Definition of a scoped object encapsulating a jclass object.
28 * @see ScopedJNIObject
29 * @ingroup platformandroid
30 */
32
33/**
34 * Definition of a scoped object encapsulating a jobject object.
35 * @see ScopedJNIObject
36 * @ingroup platformandroid
37 */
39
40/**
41 * Definition of a scoped object encapsulating a jobjectArray object.
42 * @see ScopedJNIObject
43 * @ingroup platformandroid
44 */
46
47/**
48 * Definition of a scoped object encapsulating a jstring object.
49 * @see ScopedJNIObject
50 * @ingroup platformandroid
51 */
53
54/**
55 * This class implements a scoped JNI jobject/jclass/jstring.
56 * The scoped object encapsulates a jobject and deletes the local reference when the scoped ends.
57 * @tparam T The data type of the JNI object e.g., jobject, jclass, jstring
58 * @ingroup platformandroid
59 */
60template <typename T>
62{
63 public:
64
65 /**
66 * Default constructor.
67 */
68 ScopedJNIObject() = default;
69
70 /**
71 * Move constructor.
72 * @param object The object to be moved
73 */
75
76 /**
77 * Creates a new scoped object for a given jobject.
78 * Beware: This object must not live longer than the provided scoped JNI environment!
79 * @param scopedJNIEnvironment The associated scoped JNI environment
80 * @param object The jobject to be encapsulated, can be nullptr
81 */
82 inline ScopedJNIObject(const ScopedJNIEnvironment& scopedJNIEnvironment, T object);
83
84 /**
85 * Creates a new scoped object for a given jobject.
86 */
87 inline ScopedJNIObject(JNIEnv& jniEnvironment, T object);
88
89 /**
90 * Destructs this scoped object and deletes the local reference to the jobject.
91 */
93
94 /**
95 * Makes this scoped object a global object.
96 * Global objects can be access from any thread.
97 */
98 inline void makeGlobal();
99
100 /**
101 * Releases this encapsulated jobject and deletes the local reference to the jobject.
102 */
103 inline void release();
104
105 /**
106 * Returns whether this scoped object holds a valid jobject.
107 * @return True, if so
108 */
109 inline bool isValid() const;
110
111 /**
112 * Returns whether this object is global.
113 * @return True, if so
114 */
115 inline bool isGlobal() const;
116
117 /**
118 * Returns the pointer to the encapsulated object.
119 * @return The encapsulated object, nullptr if no object is encapsulated
120 */
121 inline const T& object() const;
122
123 /**
124 * Cast operator.
125 * @return The encapsulated object, nullptr if no object is encapsulated
126 */
127 inline operator T() const;
128
129 /**
130 * Returns the pointer to the encapsulated object, must not be called if no object is encapsulated.
131 * @see isValid().
132 * @return The encapsulated object, nullptr if no object is encapsulated
133 */
134 inline const T& operator->() const;
135
136 /**
137 * Returns the pointer to the encapsulated object, must not be called if no object is encapsulated.
138 * @see isValid().
139 * @return The encapsulated object, nullptr if no object is encapsulated
140 */
141 inline const T& operator*() const;
142
143 /**
144 * Returns whether this scoped object holds a valid jobject.
145 * @return True, if so
146 */
147 explicit inline operator bool() const;
148
149 /**
150 * Move operator.
151 * @param object The object to be moved
152 * @return Reference to this object
153 */
155
156 protected:
157
158 /**
159 * Disables copy constructor.
160 * @param object The object which would be copied
161 */
162 ScopedJNIObject(const ScopedJNIObject& object) = delete;
163
164 /**
165 * Disables copy operator.
166 * @param object The object which would be copied
167 * @return Reference to this object
168 */
169 ScopedJNIObject& operator=(const ScopedJNIObject& object) = delete;
170
171 protected:
172
173 /// The corresponding JNI environment.
174 JNIEnv* jniEnvironment_ = nullptr;
175
176 /// The encapsulated object.
177 T object_ = T();
178
179 /// The encapsulated global object.
181};
182
183template <typename T>
185{
186 *this = std::move(object);
187}
188
189template <typename T>
190inline ScopedJNIObject<T>::ScopedJNIObject(const ScopedJNIEnvironment& scopedJNIEnvironment, T object) :
191 jniEnvironment_(scopedJNIEnvironment.jniEnv()),
192 object_(object)
193{
194 // nothing to do here
195}
196
197template <typename T>
198inline ScopedJNIObject<T>::ScopedJNIObject(JNIEnv& jniEnvironment, T object) :
199 jniEnvironment_(&jniEnvironment),
200 object_(object)
201{
202 // nothing to do here
203}
204
205template <typename T>
207{
208 release();
209}
210
211template <typename T>
213{
214 ocean_assert(isValid());
215
216 if (globalObject_ == nullptr)
217 {
218 globalObject_ = T(jniEnvironment_->NewGlobalRef(object_));
219 }
220}
221
222template <typename T>
224{
225 if (globalObject_ != nullptr)
226 {
227 ocean_assert(jniEnvironment_ != nullptr);
228 jniEnvironment_->DeleteGlobalRef(globalObject_);
229 globalObject_ = nullptr;
230 }
231
232 if (object_)
233 {
234 ocean_assert(jniEnvironment_ != nullptr);
235 jniEnvironment_->DeleteLocalRef(object_);
236 object_ = nullptr;
237 }
238}
239
240template <typename T>
242{
243 return object_ != nullptr;
244}
245
246template <typename T>
248{
249 return globalObject_ != nullptr;
250}
251
252template <typename T>
253inline const T& ScopedJNIObject<T>::object() const
254{
255 if (globalObject_ != nullptr)
256 {
257 ocean_assert(object_ != nullptr);
258 return globalObject_;
259 }
260
261 return object_;
262}
263
264template <typename T>
266{
267 return object();
268}
269
270template <typename T>
271inline const T& ScopedJNIObject<T>::operator->() const
272{
273 ocean_assert(isValid());
274
275 return object();
276}
277
278template <typename T>
279inline const T& ScopedJNIObject<T>::operator*() const
280{
281 ocean_assert(isValid());
282
283 return object();
284}
285
286template <typename T>
288{
289 return isValid();
290}
291
292template <typename T>
294{
295 if (this != &object)
296 {
297 release();
298
299 jniEnvironment_ = object.jniEnvironment_;
300 object_ = std::move(object.object_);
301 globalObject_ = std::move(object.globalObject_);
302
303 object.jniEnvironment_ = nullptr;
304 object.object_ = T();
305 object.globalObject_ = T();
306 }
307
308 return *this;
309}
310
311}
312
313}
314
315}
316
317#endif // META_OCEAN_PLATFORM_ANDROID_SCOPED_JNI_OBJECT_H
This class implements a scoped access to a JNI environment with attached current thread.
Definition ScopedJNIEnvironment.h:28
This class implements a scoped JNI jobject/jclass/jstring.
Definition ScopedJNIObject.h:62
const T & operator*() const
Returns the pointer to the encapsulated object, must not be called if no object is encapsulated.
Definition ScopedJNIObject.h:279
ScopedJNIObject()=default
Default constructor.
ScopedJNIObject< T > & operator=(ScopedJNIObject< T > &&object)
Move operator.
Definition ScopedJNIObject.h:293
T object_
The encapsulated object.
Definition ScopedJNIObject.h:177
void makeGlobal()
Makes this scoped object a global object.
Definition ScopedJNIObject.h:212
ScopedJNIObject(const ScopedJNIObject &object)=delete
Disables copy constructor.
~ScopedJNIObject()
Destructs this scoped object and deletes the local reference to the jobject.
Definition ScopedJNIObject.h:206
bool isValid() const
Returns whether this scoped object holds a valid jobject.
Definition ScopedJNIObject.h:241
bool isGlobal() const
Returns whether this object is global.
Definition ScopedJNIObject.h:247
ScopedJNIObject(JNIEnv &jniEnvironment, T object)
Creates a new scoped object for a given jobject.
Definition ScopedJNIObject.h:198
void release()
Releases this encapsulated jobject and deletes the local reference to the jobject.
Definition ScopedJNIObject.h:223
ScopedJNIObject(ScopedJNIObject< T > &&object)
Move constructor.
Definition ScopedJNIObject.h:184
const T & operator->() const
Returns the pointer to the encapsulated object, must not be called if no object is encapsulated.
Definition ScopedJNIObject.h:271
ScopedJNIObject(const ScopedJNIEnvironment &scopedJNIEnvironment, T object)
Creates a new scoped object for a given jobject.
Definition ScopedJNIObject.h:190
ScopedJNIObject & operator=(const ScopedJNIObject &object)=delete
Disables copy operator.
const T & object() const
Returns the pointer to the encapsulated object.
Definition ScopedJNIObject.h:253
JNIEnv * jniEnvironment_
The corresponding JNI environment.
Definition ScopedJNIObject.h:174
T globalObject_
The encapsulated global object.
Definition ScopedJNIObject.h:180
ScopedJNIObject< jclass > ScopedJClass
Definition of a scoped object encapsulating a jclass object.
Definition ScopedJNIObject.h:31
ScopedJNIObject< jobjectArray > ScopedJObjectArray
Definition of a scoped object encapsulating a jobjectArray object.
Definition ScopedJNIObject.h:45
ScopedJNIObject< jstring > ScopedJString
Definition of a scoped object encapsulating a jstring object.
Definition ScopedJNIObject.h:52
ScopedJNIObject< jobject > ScopedJObject
Definition of a scoped object encapsulating a jobject object.
Definition ScopedJNIObject.h:38
The namespace covering the entire Ocean framework.
Definition Accessor.h:15