|
| 1 | +#ifndef FIREBASE_FIRESTORE_CLIENT_CPP_SRC_JNI_OWNERSHIP_H_ |
| 2 | +#define FIREBASE_FIRESTORE_CLIENT_CPP_SRC_JNI_OWNERSHIP_H_ |
| 3 | + |
| 4 | +#include <jni.h> |
| 5 | + |
| 6 | +#include <string> |
| 7 | + |
| 8 | +#include "firestore/src/jni/jni_fwd.h" |
| 9 | +#include "firestore/src/jni/traits.h" |
| 10 | + |
| 11 | +namespace firebase { |
| 12 | +namespace firestore { |
| 13 | +namespace jni { |
| 14 | + |
| 15 | +/** |
| 16 | + * An RAII wrapper for a local JNI reference that automatically deletes the JNI |
| 17 | + * local reference when it goes out of scope. Copies and moves are handled by |
| 18 | + * creating additional references as required. |
| 19 | + * |
| 20 | + * @tparam T A JNI reference wrapper, usually a subclass of `Object`. |
| 21 | + */ |
| 22 | +template <typename T> |
| 23 | +class Local : public T { |
| 24 | + public: |
| 25 | + using jni_type = JniType<T>; |
| 26 | + |
| 27 | + Local() = default; |
| 28 | + |
| 29 | + /** |
| 30 | + * Adopts a local reference that is the result of a JNI invocation. |
| 31 | + */ |
| 32 | + Local(JNIEnv* env, jni_type value) : T(value), env_(env) {} |
| 33 | + |
| 34 | + Local(const Local& other) = delete; |
| 35 | + Local& operator=(const Local&) = delete; |
| 36 | + |
| 37 | + Local(Local&& other) noexcept : T(other.release()), env_(other.env_) {} |
| 38 | + |
| 39 | + Local& operator=(Local&& other) noexcept { |
| 40 | + if (T::object_ != other.get()) { |
| 41 | + EnsureEnv(other.env()); |
| 42 | + env_->DeleteLocalRef(T::object_); |
| 43 | + T::object_ = other.release(); |
| 44 | + } |
| 45 | + return *this; |
| 46 | + } |
| 47 | + |
| 48 | + Local(const Global<T>& other) { |
| 49 | + EnsureEnv(); |
| 50 | + T::object_ = env_->NewLocalRef(other.get()); |
| 51 | + } |
| 52 | + |
| 53 | + Local& operator=(const Global<T>& other) { |
| 54 | + EnsureEnv(); |
| 55 | + env_->DeleteLocalRef(T::object_); |
| 56 | + T::object_ = env_->NewLocalRef(other.get()); |
| 57 | + return *this; |
| 58 | + } |
| 59 | + |
| 60 | + Local(Global<T>&& other) noexcept { |
| 61 | + EnsureEnv(); |
| 62 | + T::object_ = env_->NewLocalRef(other.get()); |
| 63 | + env_->DeleteGlobalRef(other.release()); |
| 64 | + } |
| 65 | + |
| 66 | + Local& operator=(Global<T>&& other) noexcept { |
| 67 | + EnsureEnv(); |
| 68 | + env_->DeleteLocalRef(T::object_); |
| 69 | + T::object_ = env_->NewLocalRef(other.get()); |
| 70 | + env_->DeleteGlobalRef(other.release()); |
| 71 | + return *this; |
| 72 | + } |
| 73 | + |
| 74 | + ~Local() { |
| 75 | + if (env_ && T::object_) { |
| 76 | + env_->DeleteLocalRef(T::object_); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + jni_type get() const override { return static_cast<jni_type>(T::object_); } |
| 81 | + |
| 82 | + jni_type release() { |
| 83 | + jobject result = T::object_; |
| 84 | + T::object_ = nullptr; |
| 85 | + return static_cast<jni_type>(result); |
| 86 | + } |
| 87 | + |
| 88 | + JNIEnv* env() const { return env_; } |
| 89 | + |
| 90 | + private: |
| 91 | + void EnsureEnv(JNIEnv* other = nullptr) { |
| 92 | + if (env_ == nullptr) { |
| 93 | + if (other != nullptr) { |
| 94 | + env_ = other; |
| 95 | + } else { |
| 96 | + env_ = GetEnv(); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + JNIEnv* env_ = nullptr; |
| 102 | +}; |
| 103 | + |
| 104 | +/** |
| 105 | + * Global references are almost always created by promoting local references. |
| 106 | + * Aside from `NewGlobalRef`, there are no JNI APIs that return global |
| 107 | + * references. You can construct a `Global` wrapper with `AdoptExisting::kYes` |
| 108 | + * in the rare case that you're interoperating with other APIs that produce |
| 109 | + * global JNI references. |
| 110 | + */ |
| 111 | +enum class AdoptExisting { kYes }; |
| 112 | + |
| 113 | +/** |
| 114 | + * An RAII wrapper for a global JNI reference, that automatically deletes the |
| 115 | + * JNI global reference when it goes out of scope. Copies and moves are handled |
| 116 | + * by creating additional references as required. |
| 117 | + * |
| 118 | + * @tparam T A JNI reference wrapper, usually a subclass of `Object`. |
| 119 | + */ |
| 120 | +template <typename T> |
| 121 | +class Global : public T { |
| 122 | + public: |
| 123 | + using jni_type = JniType<T>; |
| 124 | + |
| 125 | + Global() = default; |
| 126 | + |
| 127 | + Global(jni_type object, AdoptExisting) : T(object) {} |
| 128 | + |
| 129 | + Global(const Local<T>& other) { |
| 130 | + JNIEnv* env = EnsureEnv(other.env()); |
| 131 | + T::object_ = env->NewGlobalRef(other.get()); |
| 132 | + } |
| 133 | + |
| 134 | + Global& operator=(const Local<T>& other) { |
| 135 | + JNIEnv* env = EnsureEnv(other.env()); |
| 136 | + env->DeleteGlobalRef(T::object_); |
| 137 | + T::object_ = env->NewGlobalRef(other.get()); |
| 138 | + return *this; |
| 139 | + } |
| 140 | + |
| 141 | + Global(const T& other) { |
| 142 | + JNIEnv* env = GetEnv(); |
| 143 | + T::object_ = env->NewGlobalRef(other.get()); |
| 144 | + } |
| 145 | + |
| 146 | + Global& operator=(const T& other) { |
| 147 | + if (T::object_ != other.get()) { |
| 148 | + JNIEnv* env = GetEnv(); |
| 149 | + env->DeleteGlobalRef(T::object_); |
| 150 | + T::object_ = env->NewGlobalRef(other.get()); |
| 151 | + } |
| 152 | + return *this; |
| 153 | + } |
| 154 | + |
| 155 | + // Explicitly declare a copy constructor and copy assignment operator because |
| 156 | + // there's an explicitly declared move constructor for this type. |
| 157 | + // |
| 158 | + // Without this, the implicitly-defined copy constructor would be deleted, and |
| 159 | + // during overload resolution the deleted copy constructor would take priority |
| 160 | + // over the looser match above that takes `const T&`. |
| 161 | + Global(const Global& other) : Global(static_cast<const T&>(other)) {} |
| 162 | + |
| 163 | + Global& operator=(const Global& other) { |
| 164 | + *this = static_cast<const T&>(other); |
| 165 | + return *this; |
| 166 | + } |
| 167 | + |
| 168 | + Global(Global&& other) noexcept : T(other.release()) {} |
| 169 | + |
| 170 | + Global& operator=(Global&& other) noexcept { |
| 171 | + if (T::object_ != other.get()) { |
| 172 | + if (T::object_) { |
| 173 | + JNIEnv* env = GetEnv(); |
| 174 | + env->DeleteGlobalRef(T::object_); |
| 175 | + } |
| 176 | + T::object_ = other.release(); |
| 177 | + } |
| 178 | + return *this; |
| 179 | + } |
| 180 | + |
| 181 | + Global(Local<T>&& other) noexcept { |
| 182 | + JNIEnv* env = EnsureEnv(other.env()); |
| 183 | + T::object_ = env->NewGlobalRef(other.get()); |
| 184 | + env->DeleteLocalRef(other.release()); |
| 185 | + } |
| 186 | + |
| 187 | + Global& operator=(Local<T>&& other) noexcept { |
| 188 | + JNIEnv* env = EnsureEnv(other.env()); |
| 189 | + env->DeleteGlobalRef(T::object_); |
| 190 | + T::object_ = env->NewGlobalRef(other.get()); |
| 191 | + env->DeleteLocalRef(other.release()); |
| 192 | + return *this; |
| 193 | + } |
| 194 | + |
| 195 | + ~Global() { |
| 196 | + if (T::object_) { |
| 197 | + JNIEnv* env = GetEnv(); |
| 198 | + env->DeleteGlobalRef(T::object_); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + jni_type get() const override { return static_cast<jni_type>(T::object_); } |
| 203 | + |
| 204 | + jni_type release() { |
| 205 | + jobject result = T::object_; |
| 206 | + T::object_ = nullptr; |
| 207 | + return static_cast<jni_type>(result); |
| 208 | + } |
| 209 | + |
| 210 | + private: |
| 211 | + JNIEnv* EnsureEnv(JNIEnv* other = nullptr) { |
| 212 | + if (other != nullptr) { |
| 213 | + return other; |
| 214 | + } else { |
| 215 | + return GetEnv(); |
| 216 | + } |
| 217 | + } |
| 218 | +}; |
| 219 | + |
| 220 | +} // namespace jni |
| 221 | +} // namespace firestore |
| 222 | +} // namespace firebase |
| 223 | + |
| 224 | +#endif // FIREBASE_FIRESTORE_CLIENT_CPP_SRC_JNI_OWNERSHIP_H_ |
0 commit comments