| 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 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 // thread, objects of this class must be created, used, and destroyed, on a | 141 // thread, objects of this class must be created, used, and destroyed, on a |
| 142 // single thread. | 142 // single thread. |
| 143 // Therefore, this class should only be used as a stack-based object and from a | 143 // Therefore, this class should only be used as a stack-based object and from a |
| 144 // single thread. If you wish to have the reference outlive the current | 144 // single thread. If you wish to have the reference outlive the current |
| 145 // callstack (e.g. as a class member) or you wish to pass it across threads, | 145 // callstack (e.g. as a class member) or you wish to pass it across threads, |
| 146 // use a ScopedJavaGlobalRef instead. | 146 // use a ScopedJavaGlobalRef instead. |
| 147 template<typename T> | 147 template<typename T> |
| 148 class ScopedJavaLocalRef : public JavaRef<T> { | 148 class ScopedJavaLocalRef : public JavaRef<T> { |
| 149 public: | 149 public: |
| 150 ScopedJavaLocalRef() : env_(nullptr) {} | 150 ScopedJavaLocalRef() : env_(nullptr) {} |
| 151 ScopedJavaLocalRef(std::nullptr_t) : env_(nullptr) {} |
| 151 | 152 |
| 152 // Non-explicit copy constructor, to allow ScopedJavaLocalRef to be returned | 153 // Non-explicit copy constructor, to allow ScopedJavaLocalRef to be returned |
| 153 // by value as this is the normal usage pattern. | 154 // by value as this is the normal usage pattern. |
| 154 ScopedJavaLocalRef(const ScopedJavaLocalRef<T>& other) | 155 ScopedJavaLocalRef(const ScopedJavaLocalRef<T>& other) |
| 155 : env_(other.env_) { | 156 : env_(other.env_) { |
| 156 this->SetNewLocalRef(env_, other.obj()); | 157 this->SetNewLocalRef(env_, other.obj()); |
| 157 } | 158 } |
| 158 | 159 |
| 159 ScopedJavaLocalRef(ScopedJavaLocalRef<T>&& other) : env_(other.env_) { | 160 ScopedJavaLocalRef(ScopedJavaLocalRef<T>&& other) : env_(other.env_) { |
| 160 this->swap(other); | 161 this->swap(other); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 }; | 232 }; |
| 232 | 233 |
| 233 // Holds a global reference to a Java object. The global reference is scoped | 234 // Holds a global reference to a Java object. The global reference is scoped |
| 234 // to the lifetime of this object. This class does not hold onto any JNIEnv* | 235 // to the lifetime of this object. This class does not hold onto any JNIEnv* |
| 235 // passed to it, hence it is safe to use across threads (within the constraints | 236 // passed to it, hence it is safe to use across threads (within the constraints |
| 236 // imposed by the underlying Java object that it references). | 237 // imposed by the underlying Java object that it references). |
| 237 template<typename T> | 238 template<typename T> |
| 238 class ScopedJavaGlobalRef : public JavaRef<T> { | 239 class ScopedJavaGlobalRef : public JavaRef<T> { |
| 239 public: | 240 public: |
| 240 ScopedJavaGlobalRef() {} | 241 ScopedJavaGlobalRef() {} |
| 242 ScopedJavaGlobalRef(std::nullptr_t) {} |
| 241 | 243 |
| 242 ScopedJavaGlobalRef(const ScopedJavaGlobalRef<T>& other) { | 244 ScopedJavaGlobalRef(const ScopedJavaGlobalRef<T>& other) { |
| 243 this->Reset(other); | 245 this->Reset(other); |
| 244 } | 246 } |
| 245 | 247 |
| 246 ScopedJavaGlobalRef(ScopedJavaGlobalRef<T>&& other) { this->swap(other); } | 248 ScopedJavaGlobalRef(ScopedJavaGlobalRef<T>&& other) { this->swap(other); } |
| 247 | 249 |
| 248 ScopedJavaGlobalRef(JNIEnv* env, T obj) { this->Reset(env, obj); } | 250 ScopedJavaGlobalRef(JNIEnv* env, T obj) { this->Reset(env, obj); } |
| 249 | 251 |
| 250 template<typename U> | 252 template<typename U> |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 private: | 308 private: |
| 307 T obj_; | 309 T obj_; |
| 308 | 310 |
| 309 DISALLOW_COPY_AND_ASSIGN(JavaRefOrBare); | 311 DISALLOW_COPY_AND_ASSIGN(JavaRefOrBare); |
| 310 }; | 312 }; |
| 311 | 313 |
| 312 } // namespace android | 314 } // namespace android |
| 313 } // namespace base | 315 } // namespace base |
| 314 | 316 |
| 315 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_ | 317 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_ |
| OLD | NEW |