Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1494)

Side by Side Diff: base/android/scoped_java_ref.h

Issue 2268553002: Small improvements to JNI references. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Learn about, and then fix, the most vexing parse. :) Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/android/jni_weak_ref.cc ('k') | base/android/scoped_java_ref_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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> 11 #include <type_traits>
12 #include <utility>
12 13
13 #include "base/base_export.h" 14 #include "base/base_export.h"
14 #include "base/logging.h" 15 #include "base/logging.h"
15 #include "base/macros.h" 16 #include "base/macros.h"
16 17
17 namespace base { 18 namespace base {
18 namespace android { 19 namespace android {
19 20
20 // Creates a new local reference frame, in which at least a given number of 21 // Creates a new local reference frame, in which at least a given number of
21 // local references can be created. Note that local references already created 22 // local references can be created. Note that local references already created
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 // Takes ownership of the |obj| reference passed; requires it to be a local 64 // Takes ownership of the |obj| reference passed; requires it to be a local
64 // reference type. 65 // reference type.
65 #if DCHECK_IS_ON() 66 #if DCHECK_IS_ON()
66 // Implementation contains a DCHECK; implement out-of-line when DCHECK_IS_ON. 67 // Implementation contains a DCHECK; implement out-of-line when DCHECK_IS_ON.
67 JavaRef(JNIEnv* env, jobject obj); 68 JavaRef(JNIEnv* env, jobject obj);
68 #else 69 #else
69 // Don't add anything else here; it's inlined. 70 // Don't add anything else here; it's inlined.
70 JavaRef(JNIEnv* env, jobject obj) : obj_(obj) {} 71 JavaRef(JNIEnv* env, jobject obj) : obj_(obj) {}
71 #endif 72 #endif
72 73
74 void swap(JavaRef& other) { std::swap(obj_, other.obj_); }
75
73 // The following are implementation detail convenience methods, for 76 // The following are implementation detail convenience methods, for
74 // use by the sub-classes. 77 // use by the sub-classes.
75 JNIEnv* SetNewLocalRef(JNIEnv* env, jobject obj); 78 JNIEnv* SetNewLocalRef(JNIEnv* env, jobject obj);
76 void SetNewGlobalRef(JNIEnv* env, jobject obj); 79 void SetNewGlobalRef(JNIEnv* env, jobject obj);
77 void ResetLocalRef(JNIEnv* env); 80 void ResetLocalRef(JNIEnv* env);
78 void ResetGlobalRef(); 81 void ResetGlobalRef();
79 jobject ReleaseInternal(); 82 jobject ReleaseInternal();
80 83
81 private: 84 private:
82 jobject obj_; 85 jobject obj_;
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 public: 149 public:
147 ScopedJavaLocalRef() : env_(nullptr) {} 150 ScopedJavaLocalRef() : env_(nullptr) {}
148 151
149 // Non-explicit copy constructor, to allow ScopedJavaLocalRef to be returned 152 // Non-explicit copy constructor, to allow ScopedJavaLocalRef to be returned
150 // by value as this is the normal usage pattern. 153 // by value as this is the normal usage pattern.
151 ScopedJavaLocalRef(const ScopedJavaLocalRef<T>& other) 154 ScopedJavaLocalRef(const ScopedJavaLocalRef<T>& other)
152 : env_(other.env_) { 155 : env_(other.env_) {
153 this->SetNewLocalRef(env_, other.obj()); 156 this->SetNewLocalRef(env_, other.obj());
154 } 157 }
155 158
159 ScopedJavaLocalRef(ScopedJavaLocalRef<T>&& other) : env_(other.env_) {
160 this->swap(other);
161 }
162
156 template <typename U> 163 template <typename U>
157 explicit ScopedJavaLocalRef(const U& other) : env_(nullptr) { 164 explicit ScopedJavaLocalRef(const U& other) : env_(nullptr) {
158 this->Reset(other); 165 this->Reset(other);
159 } 166 }
160 167
161 // Assumes that |obj| is a local reference to a Java object and takes 168 // Assumes that |obj| is a local reference to a Java object and takes
162 // ownership of this local reference. 169 // ownership of this local reference.
163 ScopedJavaLocalRef(JNIEnv* env, T obj) : JavaRef<T>(env, obj), env_(env) {} 170 ScopedJavaLocalRef(JNIEnv* env, T obj) : JavaRef<T>(env, obj), env_(env) {}
164 171
165 ~ScopedJavaLocalRef() { 172 ~ScopedJavaLocalRef() {
166 this->Reset(); 173 this->Reset();
167 } 174 }
168 175
169 // Overloaded assignment operator defined for consistency with the implicit 176 // Overloaded assignment operator defined for consistency with the implicit
170 // copy constructor. 177 // copy constructor.
171 void operator=(const ScopedJavaLocalRef<T>& other) { 178 void operator=(const ScopedJavaLocalRef<T>& other) {
172 this->Reset(other); 179 this->Reset(other);
173 } 180 }
174 181
182 void operator=(ScopedJavaLocalRef<T>&& other) {
183 env_ = other.env_;
184 this->swap(other);
185 }
186
175 void Reset() { 187 void Reset() {
176 this->ResetLocalRef(env_); 188 this->ResetLocalRef(env_);
177 } 189 }
178 190
179 template<typename U> 191 template<typename U>
180 void Reset(const ScopedJavaLocalRef<U>& other) { 192 void Reset(const ScopedJavaLocalRef<U>& other) {
181 // We can copy over env_ here as |other| instance must be from the same 193 // We can copy over env_ here as |other| instance must be from the same
182 // thread as |this| local ref. (See class comment for multi-threading 194 // thread as |this| local ref. (See class comment for multi-threading
183 // limitations, and alternatives). 195 // limitations, and alternatives).
184 this->Reset(other.env_, other.obj()); 196 this->Reset(other.env_, other.obj());
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 // imposed by the underlying Java object that it references). 236 // imposed by the underlying Java object that it references).
225 template<typename T> 237 template<typename T>
226 class ScopedJavaGlobalRef : public JavaRef<T> { 238 class ScopedJavaGlobalRef : public JavaRef<T> {
227 public: 239 public:
228 ScopedJavaGlobalRef() {} 240 ScopedJavaGlobalRef() {}
229 241
230 ScopedJavaGlobalRef(const ScopedJavaGlobalRef<T>& other) { 242 ScopedJavaGlobalRef(const ScopedJavaGlobalRef<T>& other) {
231 this->Reset(other); 243 this->Reset(other);
232 } 244 }
233 245
246 ScopedJavaGlobalRef(ScopedJavaGlobalRef<T>&& other) { this->swap(other); }
247
234 ScopedJavaGlobalRef(JNIEnv* env, T obj) { this->Reset(env, obj); } 248 ScopedJavaGlobalRef(JNIEnv* env, T obj) { this->Reset(env, obj); }
235 249
236 template<typename U> 250 template<typename U>
237 explicit ScopedJavaGlobalRef(const U& other) { 251 explicit ScopedJavaGlobalRef(const U& other) {
238 this->Reset(other); 252 this->Reset(other);
239 } 253 }
240 254
241 ~ScopedJavaGlobalRef() { 255 ~ScopedJavaGlobalRef() {
242 this->Reset(); 256 this->Reset();
243 } 257 }
244 258
245 // Overloaded assignment operator defined for consistency with the implicit 259 // Overloaded assignment operator defined for consistency with the implicit
246 // copy constructor. 260 // copy constructor.
247 void operator=(const ScopedJavaGlobalRef<T>& other) { 261 void operator=(const ScopedJavaGlobalRef<T>& other) {
248 this->Reset(other); 262 this->Reset(other);
249 } 263 }
250 264
265 void operator=(ScopedJavaGlobalRef<T>&& other) { this->swap(other); }
266
251 void Reset() { 267 void Reset() {
252 this->ResetGlobalRef(); 268 this->ResetGlobalRef();
253 } 269 }
254 270
255 template<typename U> 271 template<typename U>
256 void Reset(const U& other) { 272 void Reset(const U& other) {
257 this->Reset(nullptr, other.obj()); 273 this->Reset(nullptr, other.obj());
258 } 274 }
259 275
260 template<typename U> 276 template<typename U>
(...skipping 29 matching lines...) Expand all
290 private: 306 private:
291 T obj_; 307 T obj_;
292 308
293 DISALLOW_COPY_AND_ASSIGN(JavaRefOrBare); 309 DISALLOW_COPY_AND_ASSIGN(JavaRefOrBare);
294 }; 310 };
295 311
296 } // namespace android 312 } // namespace android
297 } // namespace base 313 } // namespace base
298 314
299 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_ 315 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_
OLDNEW
« no previous file with comments | « base/android/jni_weak_ref.cc ('k') | base/android/scoped_java_ref_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698