| 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 |
| 11 #include <type_traits> |
| 12 |
| 11 #include "base/base_export.h" | 13 #include "base/base_export.h" |
| 12 #include "base/logging.h" | 14 #include "base/logging.h" |
| 13 #include "base/macros.h" | 15 #include "base/macros.h" |
| 14 #include "base/template_util.h" | |
| 15 | 16 |
| 16 namespace base { | 17 namespace base { |
| 17 namespace android { | 18 namespace android { |
| 18 | 19 |
| 19 // Creates a new local reference frame, in which at least a given number of | 20 // Creates a new local reference frame, in which at least a given number of |
| 20 // local references can be created. Note that local references already created | 21 // local references can be created. Note that local references already created |
| 21 // in previous local frames are still valid in the current local frame. | 22 // in previous local frames are still valid in the current local frame. |
| 22 class BASE_EXPORT ScopedJavaLocalFrame { | 23 class BASE_EXPORT ScopedJavaLocalFrame { |
| 23 public: | 24 public: |
| 24 explicit ScopedJavaLocalFrame(JNIEnv* env); | 25 explicit ScopedJavaLocalFrame(JNIEnv* env); |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 | 187 |
| 187 template<typename U> | 188 template<typename U> |
| 188 void Reset(const U& other) { | 189 void Reset(const U& other) { |
| 189 // If |env_| was not yet set (is still NULL) it will be attached to the | 190 // If |env_| was not yet set (is still NULL) it will be attached to the |
| 190 // current thread in SetNewLocalRef(). | 191 // current thread in SetNewLocalRef(). |
| 191 this->Reset(env_, other.obj()); | 192 this->Reset(env_, other.obj()); |
| 192 } | 193 } |
| 193 | 194 |
| 194 template<typename U> | 195 template<typename U> |
| 195 void Reset(JNIEnv* env, U obj) { | 196 void Reset(JNIEnv* env, U obj) { |
| 196 static_assert(base::is_convertible<U, T>::value, | 197 static_assert(std::is_convertible<U, T>::value, |
| 197 "U must be convertible to T"); | 198 "U must be convertible to T"); |
| 198 env_ = this->SetNewLocalRef(env, obj); | 199 env_ = this->SetNewLocalRef(env, obj); |
| 199 } | 200 } |
| 200 | 201 |
| 201 // Releases the local reference to the caller. The caller *must* delete the | 202 // Releases the local reference to the caller. The caller *must* delete the |
| 202 // local reference when it is done with it. Note that calling a Java method | 203 // local reference when it is done with it. Note that calling a Java method |
| 203 // is *not* a transfer of ownership and Release() should not be used. | 204 // is *not* a transfer of ownership and Release() should not be used. |
| 204 T Release() { | 205 T Release() { |
| 205 return static_cast<T>(this->ReleaseInternal()); | 206 return static_cast<T>(this->ReleaseInternal()); |
| 206 } | 207 } |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 this->Reset(NULL, other.obj()); | 258 this->Reset(NULL, other.obj()); |
| 258 } | 259 } |
| 259 | 260 |
| 260 template<typename U> | 261 template<typename U> |
| 261 void Reset(JNIEnv* env, const JavaParamRef<U>& other) { | 262 void Reset(JNIEnv* env, const JavaParamRef<U>& other) { |
| 262 this->Reset(env, other.obj()); | 263 this->Reset(env, other.obj()); |
| 263 } | 264 } |
| 264 | 265 |
| 265 template<typename U> | 266 template<typename U> |
| 266 void Reset(JNIEnv* env, U obj) { | 267 void Reset(JNIEnv* env, U obj) { |
| 267 static_assert(base::is_convertible<U, T>::value, | 268 static_assert(std::is_convertible<U, T>::value, |
| 268 "U must be convertible to T"); | 269 "U must be convertible to T"); |
| 269 this->SetNewGlobalRef(env, obj); | 270 this->SetNewGlobalRef(env, obj); |
| 270 } | 271 } |
| 271 | 272 |
| 272 // Releases the global reference to the caller. The caller *must* delete the | 273 // Releases the global reference to the caller. The caller *must* delete the |
| 273 // global reference when it is done with it. Note that calling a Java method | 274 // global reference when it is done with it. Note that calling a Java method |
| 274 // is *not* a transfer of ownership and Release() should not be used. | 275 // is *not* a transfer of ownership and Release() should not be used. |
| 275 T Release() { | 276 T Release() { |
| 276 return static_cast<T>(this->ReleaseInternal()); | 277 return static_cast<T>(this->ReleaseInternal()); |
| 277 } | 278 } |
| 278 }; | 279 }; |
| 279 | 280 |
| 280 } // namespace android | 281 } // namespace android |
| 281 } // namespace base | 282 } // namespace base |
| 282 | 283 |
| 283 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_ | 284 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_ |
| OLD | NEW |