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 "base/base_export.h" | 11 #include "base/base_export.h" |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/template_util.h" |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 namespace android { | 17 namespace android { |
17 | 18 |
18 // Creates a new local reference frame, in which at least a given number of | 19 // Creates a new local reference frame, in which at least a given number of |
19 // local references can be created. Note that local references already created | 20 // local references can be created. Note that local references already created |
20 // in previous local frames are still valid in the current local frame. | 21 // in previous local frames are still valid in the current local frame. |
21 class BASE_EXPORT ScopedJavaLocalFrame { | 22 class BASE_EXPORT ScopedJavaLocalFrame { |
22 public: | 23 public: |
23 explicit ScopedJavaLocalFrame(JNIEnv* env); | 24 explicit ScopedJavaLocalFrame(JNIEnv* env); |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 | 172 |
172 template<typename U> | 173 template<typename U> |
173 void Reset(const U& other) { | 174 void Reset(const U& other) { |
174 // If |env_| was not yet set (is still NULL) it will be attached to the | 175 // If |env_| was not yet set (is still NULL) it will be attached to the |
175 // current thread in SetNewLocalRef(). | 176 // current thread in SetNewLocalRef(). |
176 this->Reset(env_, other.obj()); | 177 this->Reset(env_, other.obj()); |
177 } | 178 } |
178 | 179 |
179 template<typename U> | 180 template<typename U> |
180 void Reset(JNIEnv* env, U obj) { | 181 void Reset(JNIEnv* env, U obj) { |
181 implicit_cast<T>(obj); // Ensure U is assignable to T | 182 static_assert(base::is_convertible<U, T>::value, |
| 183 "U must be convertible to T"); |
182 env_ = this->SetNewLocalRef(env, obj); | 184 env_ = this->SetNewLocalRef(env, obj); |
183 } | 185 } |
184 | 186 |
185 // Releases the local reference to the caller. The caller *must* delete the | 187 // Releases the local reference to the caller. The caller *must* delete the |
186 // local reference when it is done with it. Note that calling a Java method | 188 // local reference when it is done with it. Note that calling a Java method |
187 // is *not* a transfer of ownership and Release() should not be used. | 189 // is *not* a transfer of ownership and Release() should not be used. |
188 T Release() { | 190 T Release() { |
189 return static_cast<T>(this->ReleaseInternal()); | 191 return static_cast<T>(this->ReleaseInternal()); |
190 } | 192 } |
191 | 193 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 this->Reset(NULL, other.obj()); | 237 this->Reset(NULL, other.obj()); |
236 } | 238 } |
237 | 239 |
238 template<typename U> | 240 template<typename U> |
239 void Reset(JNIEnv* env, const JavaParamRef<U>& other) { | 241 void Reset(JNIEnv* env, const JavaParamRef<U>& other) { |
240 this->Reset(env, other.obj()); | 242 this->Reset(env, other.obj()); |
241 } | 243 } |
242 | 244 |
243 template<typename U> | 245 template<typename U> |
244 void Reset(JNIEnv* env, U obj) { | 246 void Reset(JNIEnv* env, U obj) { |
245 implicit_cast<T>(obj); // Ensure U is assignable to T | 247 static_assert(base::is_convertible<U, T>::value, |
| 248 "U must be convertible to T"); |
246 this->SetNewGlobalRef(env, obj); | 249 this->SetNewGlobalRef(env, obj); |
247 } | 250 } |
248 | 251 |
249 // Releases the global reference to the caller. The caller *must* delete the | 252 // Releases the global reference to the caller. The caller *must* delete the |
250 // global reference when it is done with it. Note that calling a Java method | 253 // global reference when it is done with it. Note that calling a Java method |
251 // is *not* a transfer of ownership and Release() should not be used. | 254 // is *not* a transfer of ownership and Release() should not be used. |
252 T Release() { | 255 T Release() { |
253 return static_cast<T>(this->ReleaseInternal()); | 256 return static_cast<T>(this->ReleaseInternal()); |
254 } | 257 } |
255 }; | 258 }; |
256 | 259 |
257 } // namespace android | 260 } // namespace android |
258 } // namespace base | 261 } // namespace base |
259 | 262 |
260 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_ | 263 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_ |
OLD | NEW |