Ocean
NativeInterfaceManager.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_INTERFACE_MANAGER_H
9 #define META_OCEAN_PLATFORM_ANDROID_NATIVE_INTERFACE_MANAGER_H
10 
13 
14 #include "ocean/base/Lock.h"
15 #include "ocean/base/Singleton.h"
16 
17 namespace Ocean
18 {
19 
20 namespace Platform
21 {
22 
23 namespace Android
24 {
25 
26 /**
27  * This class implements the manager of the java native interface as singleton.
28  * @ingroup platformandroid
29  */
30 class OCEAN_PLATFORM_ANDROID_EXPORT NativeInterfaceManager : public Singleton<NativeInterfaceManager>
31 {
32  friend class Singleton<NativeInterfaceManager>;
33 
34  private:
35 
36  /**
37  * Definition of a map mapping thread ids to Java native interface environments.
38  */
39  using ThreadEnvironmentMap = std::unordered_map<pid_t, JNIEnv*>;
40 
41  public:
42 
43  /**
44  * Returns the JNI virtual machine object.
45  * @return The JNI virtual machine object
46  */
47  JavaVM* virtualMachine();
48 
49  /**
50  * Returns the java native interface environment object for the current calling thread.
51  * @return JNI environment object.
52  */
53  JNIEnv* environment();
54 
55  /**
56  * Returns the current activity.
57  * @return The current activity, may be nullptr if no activity is set
58  */
59  jobject currentActivity();
60 
61  /**
62  * Sets the virtual machine object of this manager.
63  * @param virtualMachine Virtual machine to be set
64  * @return True, if succeeded
65  */
66  bool setVirtualMachine(JavaVM* virtualMachine);
67 
68  /**
69  * Sets or changes the current activity.
70  * @param activity The current activity to be set or updated, can be nullptr to remove the previously set activity
71  */
72  void setCurrentActivity(jobject activity);
73 
74  protected:
75 
76  /**
77  * Creates a new manger object.
78  * This object will be created indirectly by the singleton object.
79  * @see Singleton::get().
80  */
82 
83  /**
84  * Destructs a manger object.
85  */
87 
88  protected:
89 
90  /// Manager lock.
92 
93  /// JNI virtual machine object.
94  JavaVM* virtualMachine_ = nullptr;
95 
96  /// The JNI object of the current activity with global reference, nullptr if not set.
98 
99  /// Map holding Java native environments individually for each thread.
101 };
102 
103 }
104 
105 }
106 
107 }
108 
109 #endif // META_OCEAN_PLATFORM_ANDROID_NATIVE_INTERFACE_MANAGER_H
This class implements a recursive lock object.
Definition: Lock.h:31
This class implements the manager of the java native interface as singleton.
Definition: NativeInterfaceManager.h:31
jobject currentActivity()
Returns the current activity.
ThreadEnvironmentMap threadEnvironmentMap_
Map holding Java native environments individually for each thread.
Definition: NativeInterfaceManager.h:100
bool setVirtualMachine(JavaVM *virtualMachine)
Sets the virtual machine object of this manager.
ScopedJObject currentActivity_
The JNI object of the current activity with global reference, nullptr if not set.
Definition: NativeInterfaceManager.h:97
JavaVM * virtualMachine()
Returns the JNI virtual machine object.
std::unordered_map< pid_t, JNIEnv * > ThreadEnvironmentMap
Definition of a map mapping thread ids to Java native interface environments.
Definition: NativeInterfaceManager.h:39
NativeInterfaceManager()
Creates a new manger object.
JNIEnv * environment()
Returns the java native interface environment object for the current calling thread.
Lock lock_
Manager lock.
Definition: NativeInterfaceManager.h:91
void setCurrentActivity(jobject activity)
Sets or changes the current activity.
virtual ~NativeInterfaceManager()
Destructs a manger object.
This template class is the base class for all singleton objects.
Definition: Singleton.h:71
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15