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

Unified 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: fix typo in comment 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/android/scoped_java_ref.h
diff --git a/base/android/scoped_java_ref.h b/base/android/scoped_java_ref.h
index e5339ee59671e3e1bfc60bf7df50adbe304e31ff..e15ad661e2d85f34a129c5a0a674cc2b847ff94f 100644
--- a/base/android/scoped_java_ref.h
+++ b/base/android/scoped_java_ref.h
@@ -42,6 +42,15 @@ template<typename T> class JavaRef;
template<>
class BASE_EXPORT JavaRef<jobject> {
public:
+ // Allow nullptr to be converted to JavaRef. This avoids having to declare an
+ // empty ScopedJavaLocalRef just to pass null to a function with a JavaRef
+ // parameter, and makes C++ "nullptr" and Java "null" equivalent.
+ JavaRef(std::nullptr_t) : JavaRef() {}
+
+ // Public to allow destruction of temporary JavaRef objects created by the
+ // nullptr conversion. Don't add anything else here; it's inlined.
+ ~JavaRef() {}
+
jobject obj() const { return obj_; }
bool is_null() const { return obj_ == NULL; }
@@ -60,9 +69,6 @@ class BASE_EXPORT JavaRef<jobject> {
JavaRef(JNIEnv* env, jobject obj) : obj_(obj) {}
#endif
- // Don't add anything else here; it's inlined.
- ~JavaRef() {}
-
// The following are implementation detail convenience methods, for
// use by the sub-classes.
JNIEnv* SetNewLocalRef(JNIEnv* env, jobject obj);
@@ -83,11 +89,13 @@ class BASE_EXPORT JavaRef<jobject> {
template<typename T>
class JavaRef : public JavaRef<jobject> {
public:
+ JavaRef(std::nullptr_t) : JavaRef<jobject>(nullptr) {}
+ ~JavaRef() {}
+
T obj() const { return static_cast<T>(JavaRef<jobject>::obj()); }
protected:
JavaRef() {}
- ~JavaRef() {}
JavaRef(JNIEnv* env, T obj) : JavaRef<jobject>(env, obj) {}
@@ -110,7 +118,7 @@ class JavaParamRef : public JavaRef<T> {
// methods directly from C++ and pass null for objects which are not actually
// used by the implementation (e.g. the caller object); allow this to keep
// working.
- JavaParamRef(std::nullptr_t) : JavaRef<T>() {}
+ JavaParamRef(std::nullptr_t) : JavaRef<T>(nullptr) {}
~JavaParamRef() {}
« 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