-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathjobject_reference.h
More file actions
116 lines (99 loc) · 4.6 KB
/
Copy pathjobject_reference.h
File metadata and controls
116 lines (99 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FIREBASE_APP_SRC_JOBJECT_REFERENCE_H_
#define FIREBASE_APP_SRC_JOBJECT_REFERENCE_H_
#include <jni.h>
#include "app/src/include/firebase/internal/common.h"
namespace firebase {
namespace internal {
// Creates an alias of internal::JObjectReference named classname.
// This is useful when defining the implementation of a forward declared class
// using JObjectReference.
#define JOBJECT_REFERENCE(classname) \
class classname : public firebase::internal::JObjectReference { \
public: \
explicit classname(JNIEnv* env) \
: firebase::internal::JObjectReference(env) {} \
explicit classname(const firebase::internal::JObjectReference& obj) \
: firebase::internal::JObjectReference(obj) {} \
explicit classname(firebase::internal::JObjectReference&& obj) \
: firebase::internal::JObjectReference(obj) {} \
classname(JNIEnv* env, jobject obj) \
: firebase::internal::JObjectReference(env, obj) {} \
classname& operator=(const firebase::internal::JObjectReference& rhs) { \
firebase::internal::JObjectReference::operator=(rhs); \
return *this; \
} \
classname& operator=(firebase::internal::JObjectReference&& rhs) { \
firebase::internal::JObjectReference::operator=(rhs); \
return *this; \
} \
}
// Creates and holds a global reference to a Java object.
class JObjectReference {
public:
JObjectReference();
explicit JObjectReference(JNIEnv* env);
// Create a reference to a java object.
JObjectReference(JNIEnv* env, jobject object);
// Copy
JObjectReference(const JObjectReference& reference);
// Move
#ifdef FIREBASE_USE_MOVE_OPERATORS
JObjectReference(JObjectReference&& reference) noexcept;
#endif // FIREBASE_USE_MOVE_OPERATORS
// Delete the reference to the java object.
~JObjectReference();
// Copy this reference.
JObjectReference& operator=(const JObjectReference& reference);
// Move this reference.
#ifdef FIREBASE_USE_MOVE_OPERATORS
JObjectReference& operator=(JObjectReference&& reference) noexcept;
#endif // FIREBASE_USE_MOVE_OPERATORS
// Add a global reference to the specified object, removing the reference
// to the object currently referenced by this class. If jobject_reference
// is null, the existing reference is removed.
void Set(jobject jobject_reference);
void Set(JNIEnv* env, jobject jobject_reference);
// Get a JNIEnv from the JavaVM associated with this class.
JNIEnv* GetJNIEnv() const;
// Get the JavaVM associated with this class.
JavaVM* java_vm() const { return java_vm_; }
// Get the global reference to the Java object without incrementing the
// reference count.
jobject object() const { return object_; }
// Get a local reference to the object. The returned reference must be
// deleted after use with DeleteLocalRef().
jobject GetLocalRef() const;
// Same as object()
jobject operator*() const { return object(); }
// Convert a local reference to a JObjectReference, deleting the local
// reference.
static JObjectReference FromLocalReference(JNIEnv* env,
jobject local_reference);
private:
// Initialize this instance by adding a reference to the specified Java
// object.
void Initialize(JavaVM* jvm, JNIEnv* env, jobject jobject_reference);
// Get JavaVM from a JNIEnv.
static JavaVM* GetJavaVM(JNIEnv* env);
private:
JavaVM* java_vm_;
jobject object_;
};
} // namespace internal
} // namespace firebase
#endif // FIREBASE_APP_SRC_JOBJECT_REFERENCE_H_