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

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

Issue 1492703003: jni: Allow nullptr to be converted to JavaRef. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years 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 | « no previous file | no next file » | 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
(...skipping 24 matching lines...) Expand all
35 35
36 // Forward declare the generic java reference template class. 36 // Forward declare the generic java reference template class.
37 template<typename T> class JavaRef; 37 template<typename T> class JavaRef;
38 38
39 // Template specialization of JavaRef, which acts as the base class for all 39 // Template specialization of JavaRef, which acts as the base class for all
40 // other JavaRef<> template types. This allows you to e.g. pass 40 // other JavaRef<> template types. This allows you to e.g. pass
41 // ScopedJavaLocalRef<jstring> into a function taking const JavaRef<jobject>& 41 // ScopedJavaLocalRef<jstring> into a function taking const JavaRef<jobject>&
42 template<> 42 template<>
43 class BASE_EXPORT JavaRef<jobject> { 43 class BASE_EXPORT JavaRef<jobject> {
44 public: 44 public:
45 // ALlow nullptr to be converted to JavaRef. This avoids having to declare an
Yaron 2015/12/02 19:42:10 Allow
46 // empty ScopedJavaLocalRef just to pass null to a function with a JavaRef
47 // parameter, and makes C++ "nullptr" and Java "null" equivalent.
48 JavaRef(std::nullptr_t) : JavaRef() {}
49
50 // Public to allow destruction of temporary JavaRef objects created by the
51 // nullptr conversion. Don't add anything else here; it's inlined.
52 ~JavaRef() {}
53
45 jobject obj() const { return obj_; } 54 jobject obj() const { return obj_; }
46 55
47 bool is_null() const { return obj_ == NULL; } 56 bool is_null() const { return obj_ == NULL; }
48 57
49 protected: 58 protected:
50 // Initializes a NULL reference. Don't add anything else here; it's inlined. 59 // Initializes a NULL reference. Don't add anything else here; it's inlined.
51 JavaRef() : obj_(NULL) {} 60 JavaRef() : obj_(NULL) {}
52 61
53 // Takes ownership of the |obj| reference passed; requires it to be a local 62 // Takes ownership of the |obj| reference passed; requires it to be a local
54 // reference type. 63 // reference type.
55 #if DCHECK_IS_ON() 64 #if DCHECK_IS_ON()
56 // Implementation contains a DCHECK; implement out-of-line when DCHECK_IS_ON. 65 // Implementation contains a DCHECK; implement out-of-line when DCHECK_IS_ON.
57 JavaRef(JNIEnv* env, jobject obj); 66 JavaRef(JNIEnv* env, jobject obj);
58 #else 67 #else
59 // Don't add anything else here; it's inlined. 68 // Don't add anything else here; it's inlined.
60 JavaRef(JNIEnv* env, jobject obj) : obj_(obj) {} 69 JavaRef(JNIEnv* env, jobject obj) : obj_(obj) {}
61 #endif 70 #endif
62 71
63 // Don't add anything else here; it's inlined.
64 ~JavaRef() {}
65
66 // The following are implementation detail convenience methods, for 72 // The following are implementation detail convenience methods, for
67 // use by the sub-classes. 73 // use by the sub-classes.
68 JNIEnv* SetNewLocalRef(JNIEnv* env, jobject obj); 74 JNIEnv* SetNewLocalRef(JNIEnv* env, jobject obj);
69 void SetNewGlobalRef(JNIEnv* env, jobject obj); 75 void SetNewGlobalRef(JNIEnv* env, jobject obj);
70 void ResetLocalRef(JNIEnv* env); 76 void ResetLocalRef(JNIEnv* env);
71 void ResetGlobalRef(); 77 void ResetGlobalRef();
72 jobject ReleaseInternal(); 78 jobject ReleaseInternal();
73 79
74 private: 80 private:
75 jobject obj_; 81 jobject obj_;
76 82
77 DISALLOW_COPY_AND_ASSIGN(JavaRef); 83 DISALLOW_COPY_AND_ASSIGN(JavaRef);
78 }; 84 };
79 85
80 // Generic base class for ScopedJavaLocalRef and ScopedJavaGlobalRef. Useful 86 // Generic base class for ScopedJavaLocalRef and ScopedJavaGlobalRef. Useful
81 // for allowing functions to accept a reference without having to mandate 87 // for allowing functions to accept a reference without having to mandate
82 // whether it is a local or global type. 88 // whether it is a local or global type.
83 template<typename T> 89 template<typename T>
84 class JavaRef : public JavaRef<jobject> { 90 class JavaRef : public JavaRef<jobject> {
85 public: 91 public:
92 JavaRef(std::nullptr_t) : JavaRef<jobject>(nullptr) {}
93 ~JavaRef() {}
94
86 T obj() const { return static_cast<T>(JavaRef<jobject>::obj()); } 95 T obj() const { return static_cast<T>(JavaRef<jobject>::obj()); }
87 96
88 protected: 97 protected:
89 JavaRef() {} 98 JavaRef() {}
90 ~JavaRef() {}
91 99
92 JavaRef(JNIEnv* env, T obj) : JavaRef<jobject>(env, obj) {} 100 JavaRef(JNIEnv* env, T obj) : JavaRef<jobject>(env, obj) {}
93 101
94 private: 102 private:
95 DISALLOW_COPY_AND_ASSIGN(JavaRef); 103 DISALLOW_COPY_AND_ASSIGN(JavaRef);
96 }; 104 };
97 105
98 // Holds a local reference to a JNI method parameter. 106 // Holds a local reference to a JNI method parameter.
99 // Method parameters should not be deleted, and so this class exists purely to 107 // Method parameters should not be deleted, and so this class exists purely to
100 // wrap them as a JavaRef<T> in the JNI binding generator. Do not create 108 // wrap them as a JavaRef<T> in the JNI binding generator. Do not create
101 // instances manually. 109 // instances manually.
102 template<typename T> 110 template<typename T>
103 class JavaParamRef : public JavaRef<T> { 111 class JavaParamRef : public JavaRef<T> {
104 public: 112 public:
105 // Assumes that |obj| is a parameter passed to a JNI method from Java. 113 // Assumes that |obj| is a parameter passed to a JNI method from Java.
106 // Does not assume ownership as parameters should not be deleted. 114 // Does not assume ownership as parameters should not be deleted.
107 JavaParamRef(JNIEnv* env, T obj) : JavaRef<T>(env, obj) {} 115 JavaParamRef(JNIEnv* env, T obj) : JavaRef<T>(env, obj) {}
108 116
109 // Allow nullptr to be converted to JavaParamRef. Some unit tests call JNI 117 // Allow nullptr to be converted to JavaParamRef. Some unit tests call JNI
110 // methods directly from C++ and pass null for objects which are not actually 118 // methods directly from C++ and pass null for objects which are not actually
111 // used by the implementation (e.g. the caller object); allow this to keep 119 // used by the implementation (e.g. the caller object); allow this to keep
112 // working. 120 // working.
113 JavaParamRef(std::nullptr_t) : JavaRef<T>() {} 121 JavaParamRef(std::nullptr_t) : JavaRef<T>(nullptr) {}
114 122
115 ~JavaParamRef() {} 123 ~JavaParamRef() {}
116 124
117 // TODO(torne): remove this cast once we're using JavaRef consistently. 125 // TODO(torne): remove this cast once we're using JavaRef consistently.
118 // http://crbug.com/506850 126 // http://crbug.com/506850
119 operator T() const { return JavaRef<T>::obj(); } 127 operator T() const { return JavaRef<T>::obj(); }
120 128
121 private: 129 private:
122 DISALLOW_COPY_AND_ASSIGN(JavaParamRef); 130 DISALLOW_COPY_AND_ASSIGN(JavaParamRef);
123 }; 131 };
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 // is *not* a transfer of ownership and Release() should not be used. 268 // is *not* a transfer of ownership and Release() should not be used.
261 T Release() { 269 T Release() {
262 return static_cast<T>(this->ReleaseInternal()); 270 return static_cast<T>(this->ReleaseInternal());
263 } 271 }
264 }; 272 };
265 273
266 } // namespace android 274 } // namespace android
267 } // namespace base 275 } // namespace base
268 276
269 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_ 277 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698