| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_ANDROID_SCOPED_JAVA_REF_H_ | 5 #ifndef BASE_ANDROID_SCOPED_JAVA_REF_H_ |
| 6 #define BASE_ANDROID_SCOPED_JAVA_REF_H_ | 6 #define BASE_ANDROID_SCOPED_JAVA_REF_H_ |
| 7 | 7 |
| 8 #include <jni.h> | 8 #include <jni.h> |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 | 10 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 // empty ScopedJavaLocalRef just to pass null to a function with a JavaRef | 47 // empty ScopedJavaLocalRef just to pass null to a function with a JavaRef |
| 48 // parameter, and makes C++ "nullptr" and Java "null" equivalent. | 48 // parameter, and makes C++ "nullptr" and Java "null" equivalent. |
| 49 JavaRef(std::nullptr_t) : JavaRef() {} | 49 JavaRef(std::nullptr_t) : JavaRef() {} |
| 50 | 50 |
| 51 // Public to allow destruction of temporary JavaRef objects created by the | 51 // Public to allow destruction of temporary JavaRef objects created by the |
| 52 // nullptr conversion. Don't add anything else here; it's inlined. | 52 // nullptr conversion. Don't add anything else here; it's inlined. |
| 53 ~JavaRef() {} | 53 ~JavaRef() {} |
| 54 | 54 |
| 55 jobject obj() const { return obj_; } | 55 jobject obj() const { return obj_; } |
| 56 | 56 |
| 57 bool is_null() const { return obj_ == NULL; } | 57 bool is_null() const { return obj_ == nullptr; } |
| 58 | 58 |
| 59 protected: | 59 protected: |
| 60 // Initializes a NULL reference. Don't add anything else here; it's inlined. | 60 // Initializes a null reference. Don't add anything else here; it's inlined. |
| 61 JavaRef() : obj_(NULL) {} | 61 JavaRef() : obj_(nullptr) {} |
| 62 | 62 |
| 63 // Takes ownership of the |obj| reference passed; requires it to be a local | 63 // Takes ownership of the |obj| reference passed; requires it to be a local |
| 64 // reference type. | 64 // reference type. |
| 65 #if DCHECK_IS_ON() | 65 #if DCHECK_IS_ON() |
| 66 // Implementation contains a DCHECK; implement out-of-line when DCHECK_IS_ON. | 66 // Implementation contains a DCHECK; implement out-of-line when DCHECK_IS_ON. |
| 67 JavaRef(JNIEnv* env, jobject obj); | 67 JavaRef(JNIEnv* env, jobject obj); |
| 68 #else | 68 #else |
| 69 // Don't add anything else here; it's inlined. | 69 // Don't add anything else here; it's inlined. |
| 70 JavaRef(JNIEnv* env, jobject obj) : obj_(obj) {} | 70 JavaRef(JNIEnv* env, jobject obj) : obj_(obj) {} |
| 71 #endif | 71 #endif |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 // destroyed. Therefore, since a JNIEnv is only suitable for use on a single | 137 // destroyed. Therefore, since a JNIEnv is only suitable for use on a single |
| 138 // thread, objects of this class must be created, used, and destroyed, on a | 138 // thread, objects of this class must be created, used, and destroyed, on a |
| 139 // single thread. | 139 // single thread. |
| 140 // Therefore, this class should only be used as a stack-based object and from a | 140 // Therefore, this class should only be used as a stack-based object and from a |
| 141 // single thread. If you wish to have the reference outlive the current | 141 // single thread. If you wish to have the reference outlive the current |
| 142 // callstack (e.g. as a class member) or you wish to pass it across threads, | 142 // callstack (e.g. as a class member) or you wish to pass it across threads, |
| 143 // use a ScopedJavaGlobalRef instead. | 143 // use a ScopedJavaGlobalRef instead. |
| 144 template<typename T> | 144 template<typename T> |
| 145 class ScopedJavaLocalRef : public JavaRef<T> { | 145 class ScopedJavaLocalRef : public JavaRef<T> { |
| 146 public: | 146 public: |
| 147 ScopedJavaLocalRef() : env_(NULL) {} | 147 ScopedJavaLocalRef() : env_(nullptr) {} |
| 148 | 148 |
| 149 // Non-explicit copy constructor, to allow ScopedJavaLocalRef to be returned | 149 // Non-explicit copy constructor, to allow ScopedJavaLocalRef to be returned |
| 150 // by value as this is the normal usage pattern. | 150 // by value as this is the normal usage pattern. |
| 151 ScopedJavaLocalRef(const ScopedJavaLocalRef<T>& other) | 151 ScopedJavaLocalRef(const ScopedJavaLocalRef<T>& other) |
| 152 : env_(other.env_) { | 152 : env_(other.env_) { |
| 153 this->SetNewLocalRef(env_, other.obj()); | 153 this->SetNewLocalRef(env_, other.obj()); |
| 154 } | 154 } |
| 155 | 155 |
| 156 template<typename U> | 156 template <typename U> |
| 157 explicit ScopedJavaLocalRef(const U& other) | 157 explicit ScopedJavaLocalRef(const U& other) : env_(nullptr) { |
| 158 : env_(NULL) { | |
| 159 this->Reset(other); | 158 this->Reset(other); |
| 160 } | 159 } |
| 161 | 160 |
| 162 // Assumes that |obj| is a local reference to a Java object and takes | 161 // Assumes that |obj| is a local reference to a Java object and takes |
| 163 // ownership of this local reference. | 162 // ownership of this local reference. |
| 164 ScopedJavaLocalRef(JNIEnv* env, T obj) : JavaRef<T>(env, obj), env_(env) {} | 163 ScopedJavaLocalRef(JNIEnv* env, T obj) : JavaRef<T>(env, obj), env_(env) {} |
| 165 | 164 |
| 166 ~ScopedJavaLocalRef() { | 165 ~ScopedJavaLocalRef() { |
| 167 this->Reset(); | 166 this->Reset(); |
| 168 } | 167 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 180 template<typename U> | 179 template<typename U> |
| 181 void Reset(const ScopedJavaLocalRef<U>& other) { | 180 void Reset(const ScopedJavaLocalRef<U>& other) { |
| 182 // We can copy over env_ here as |other| instance must be from the same | 181 // We can copy over env_ here as |other| instance must be from the same |
| 183 // thread as |this| local ref. (See class comment for multi-threading | 182 // thread as |this| local ref. (See class comment for multi-threading |
| 184 // limitations, and alternatives). | 183 // limitations, and alternatives). |
| 185 this->Reset(other.env_, other.obj()); | 184 this->Reset(other.env_, other.obj()); |
| 186 } | 185 } |
| 187 | 186 |
| 188 template<typename U> | 187 template<typename U> |
| 189 void Reset(const U& other) { | 188 void Reset(const U& other) { |
| 190 // If |env_| was not yet set (is still NULL) it will be attached to the | 189 // If |env_| was not yet set (is still null) it will be attached to the |
| 191 // current thread in SetNewLocalRef(). | 190 // current thread in SetNewLocalRef(). |
| 192 this->Reset(env_, other.obj()); | 191 this->Reset(env_, other.obj()); |
| 193 } | 192 } |
| 194 | 193 |
| 195 template<typename U> | 194 template<typename U> |
| 196 void Reset(JNIEnv* env, U obj) { | 195 void Reset(JNIEnv* env, U obj) { |
| 197 static_assert(std::is_convertible<U, T>::value, | 196 static_assert(std::is_convertible<U, T>::value, |
| 198 "U must be convertible to T"); | 197 "U must be convertible to T"); |
| 199 env_ = this->SetNewLocalRef(env, obj); | 198 env_ = this->SetNewLocalRef(env, obj); |
| 200 } | 199 } |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 void operator=(const ScopedJavaGlobalRef<T>& other) { | 247 void operator=(const ScopedJavaGlobalRef<T>& other) { |
| 249 this->Reset(other); | 248 this->Reset(other); |
| 250 } | 249 } |
| 251 | 250 |
| 252 void Reset() { | 251 void Reset() { |
| 253 this->ResetGlobalRef(); | 252 this->ResetGlobalRef(); |
| 254 } | 253 } |
| 255 | 254 |
| 256 template<typename U> | 255 template<typename U> |
| 257 void Reset(const U& other) { | 256 void Reset(const U& other) { |
| 258 this->Reset(NULL, other.obj()); | 257 this->Reset(nullptr, other.obj()); |
| 259 } | 258 } |
| 260 | 259 |
| 261 template<typename U> | 260 template<typename U> |
| 262 void Reset(JNIEnv* env, const JavaParamRef<U>& other) { | 261 void Reset(JNIEnv* env, const JavaParamRef<U>& other) { |
| 263 this->Reset(env, other.obj()); | 262 this->Reset(env, other.obj()); |
| 264 } | 263 } |
| 265 | 264 |
| 266 template<typename U> | 265 template<typename U> |
| 267 void Reset(JNIEnv* env, U obj) { | 266 void Reset(JNIEnv* env, U obj) { |
| 268 static_assert(std::is_convertible<U, T>::value, | 267 static_assert(std::is_convertible<U, T>::value, |
| 269 "U must be convertible to T"); | 268 "U must be convertible to T"); |
| 270 this->SetNewGlobalRef(env, obj); | 269 this->SetNewGlobalRef(env, obj); |
| 271 } | 270 } |
| 272 | 271 |
| 273 // Releases the global reference to the caller. The caller *must* delete the | 272 // Releases the global reference to the caller. The caller *must* delete the |
| 274 // global reference when it is done with it. Note that calling a Java method | 273 // global reference when it is done with it. Note that calling a Java method |
| 275 // is *not* a transfer of ownership and Release() should not be used. | 274 // is *not* a transfer of ownership and Release() should not be used. |
| 276 T Release() { | 275 T Release() { |
| 277 return static_cast<T>(this->ReleaseInternal()); | 276 return static_cast<T>(this->ReleaseInternal()); |
| 278 } | 277 } |
| 279 }; | 278 }; |
| 280 | 279 |
| 280 // Temporary type for parameters to Java functions, to allow incremental |
| 281 // migration from bare jobject to JavaRef. Don't use outside JNI generator. |
| 282 template <typename T> |
| 283 class JavaRefOrBare { |
| 284 public: |
| 285 JavaRefOrBare(std::nullptr_t) : obj_(nullptr) {} |
| 286 JavaRefOrBare(const JavaRef<T>& ref) : obj_(ref.obj()) {} |
| 287 JavaRefOrBare(T obj) : obj_(obj) {} |
| 288 T obj() const { return obj_; } |
| 289 |
| 290 private: |
| 291 T obj_; |
| 292 |
| 293 DISALLOW_COPY_AND_ASSIGN(JavaRefOrBare); |
| 294 }; |
| 295 |
| 281 } // namespace android | 296 } // namespace android |
| 282 } // namespace base | 297 } // namespace base |
| 283 | 298 |
| 284 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_ | 299 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_ |
| OLD | NEW |