Ocean
ScopedJNIEnvironment.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_NATIVE_SCOPED_JNI_ENVIRONMENT_H
9 #define META_OCEAN_PLATFORM_ANDROID_NATIVE_SCOPED_JNI_ENVIRONMENT_H
10 
12 
13 namespace Ocean
14 {
15 
16 namespace Platform
17 {
18 
19 namespace Android
20 {
21 
22 /**
23  * This class implements a scoped access to a JNI environment with attached current thread.
24  * Do not use/call this object out of two individual threads.
25  * @ingroup platformandroid
26  */
27 class OCEAN_PLATFORM_ANDROID_EXPORT ScopedJNIEnvironment
28 {
29  public:
30 
31  /**
32  * Creates a new scoped object, acquires the JNI environment and attaches the thread.
33  * @param javaVM The virtual Java machine, must be valid
34  */
35  explicit ScopedJNIEnvironment(JavaVM* javaVM);
36 
37  /**
38  * Destructs this object.
39  */
41 
42  /**
43  * Returns the JNI environment which is attached with the current thread.
44  * @return The JNI environment, can be nullptr if invalid
45  */
46  inline JNIEnv* jniEnv() const;
47 
48  /**
49  * Returns whether this scoped object holds a valid JNI environment.
50  * @return True, if so
51  */
52  inline bool isValid() const;
53 
54  /**
55  * Returns the JNI environment which is attached with the current thread.
56  * @return The JNI environment, can be nullptr if invalid
57  */
58  inline JNIEnv* operator->() const;
59 
60  /**
61  * Returns whether this scoped object holds a valid JNI environment.
62  * @return True, if so
63  */
64  explicit inline operator bool() const;
65 
66  protected:
67 
68  /**
69  * Disabled copy constructor.
70  * @param scopedJNIEnvironment Object which would be copied
71  */
72  ScopedJNIEnvironment(const ScopedJNIEnvironment& scopedJNIEnvironment) = delete;
73 
74  /**
75  * Disabled copy operator.
76  * @param scopedJNIEnvironment Object which would be copied
77  * @return Reference to this object
78  */
79  ScopedJNIEnvironment& operator=(const ScopedJNIEnvironment& scopedJNIEnvironment) = delete;
80 
81  protected:
82 
83  /// The virtual Java machine.
84  JavaVM* javaVM_ = nullptr;
85 
86  /// The JNI environment.
87  JNIEnv* jniEnv_ = nullptr;
88 
89  /// True, if the thread has been attached; False, if the thread was attached already.
90  bool threadAttachedExplicitly_ = false;
91 };
92 
93 inline JNIEnv* ScopedJNIEnvironment::jniEnv() const
94 {
95  return jniEnv_;
96 }
97 
98 inline bool ScopedJNIEnvironment::isValid() const
99 {
100  return jniEnv_ != nullptr;
101 }
102 
103 inline JNIEnv* ScopedJNIEnvironment::operator->() const
104 {
105  ocean_assert(isValid());
106 
107  return jniEnv();
108 }
109 
110 inline ScopedJNIEnvironment::operator bool() const
111 {
112  return isValid();
113 }
114 
115 }
116 
117 }
118 
119 }
120 
121 #endif // META_OCEAN_PLATFORM_ANDROID_NATIVE_SCOPED_JNI_ENVIRONMENT_H
This class implements a scoped access to a JNI environment with attached current thread.
Definition: ScopedJNIEnvironment.h:28
JNIEnv * operator->() const
Returns the JNI environment which is attached with the current thread.
Definition: ScopedJNIEnvironment.h:103
ScopedJNIEnvironment & operator=(const ScopedJNIEnvironment &scopedJNIEnvironment)=delete
Disabled copy operator.
bool isValid() const
Returns whether this scoped object holds a valid JNI environment.
Definition: ScopedJNIEnvironment.h:98
ScopedJNIEnvironment(JavaVM *javaVM)
Creates a new scoped object, acquires the JNI environment and attaches the thread.
JNIEnv * jniEnv_
The JNI environment.
Definition: ScopedJNIEnvironment.h:87
JNIEnv * jniEnv() const
Returns the JNI environment which is attached with the current thread.
Definition: ScopedJNIEnvironment.h:93
ScopedJNIEnvironment(const ScopedJNIEnvironment &scopedJNIEnvironment)=delete
Disabled copy constructor.
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15