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(is_convertible<U, T>(obj)); |
danakj
2015/09/11 20:44:20
This says "assignable" but implicit_cast does a co
| |
182 env_ = this->SetNewLocalRef(env, obj); | 183 env_ = this->SetNewLocalRef(env, obj); |
183 } | 184 } |
184 | 185 |
185 // Releases the local reference to the caller. The caller *must* delete the | 186 // 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 | 187 // 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. | 188 // is *not* a transfer of ownership and Release() should not be used. |
188 T Release() { | 189 T Release() { |
189 return static_cast<T>(this->ReleaseInternal()); | 190 return static_cast<T>(this->ReleaseInternal()); |
190 } | 191 } |
191 | 192 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
235 this->Reset(NULL, other.obj()); | 236 this->Reset(NULL, other.obj()); |
236 } | 237 } |
237 | 238 |
238 template<typename U> | 239 template<typename U> |
239 void Reset(JNIEnv* env, const JavaParamRef<U>& other) { | 240 void Reset(JNIEnv* env, const JavaParamRef<U>& other) { |
240 this->Reset(env, other.obj()); | 241 this->Reset(env, other.obj()); |
241 } | 242 } |
242 | 243 |
243 template<typename U> | 244 template<typename U> |
244 void Reset(JNIEnv* env, U obj) { | 245 void Reset(JNIEnv* env, U obj) { |
245 implicit_cast<T>(obj); // Ensure U is assignable to T | 246 static_assert(is_convertible<U, T>(obj)); |
246 this->SetNewGlobalRef(env, obj); | 247 this->SetNewGlobalRef(env, obj); |
247 } | 248 } |
248 | 249 |
249 // Releases the global reference to the caller. The caller *must* delete the | 250 // 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 | 251 // 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. | 252 // is *not* a transfer of ownership and Release() should not be used. |
252 T Release() { | 253 T Release() { |
253 return static_cast<T>(this->ReleaseInternal()); | 254 return static_cast<T>(this->ReleaseInternal()); |
254 } | 255 } |
255 }; | 256 }; |
256 | 257 |
257 } // namespace android | 258 } // namespace android |
258 } // namespace base | 259 } // namespace base |
259 | 260 |
260 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_ | 261 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_ |
OLD | NEW |