Merge "Remove ScopedUtfChars.h from global headers."
am: 07c063daaa
Change-Id: If869420fee57167f0a1e6dbb40dae253478933b5
diff --git a/include/nativehelper/ScopedUtfChars.h b/include/nativehelper/ScopedUtfChars.h
index 9cfa9a0..aa8489d 100644
--- a/include/nativehelper/ScopedUtfChars.h
+++ b/include/nativehelper/ScopedUtfChars.h
@@ -22,30 +22,53 @@
// A smart pointer that provides read-only access to a Java string's UTF chars.
// Unlike GetStringUTFChars, we throw NullPointerException rather than abort if
-// passed a null jstring, and c_str will return NULL.
+// passed a null jstring, and c_str will return nullptr.
// This makes the correct idiom very simple:
//
// ScopedUtfChars name(env, java_name);
-// if (name.c_str() == NULL) {
-// return NULL;
+// if (name.c_str() == nullptr) {
+// return nullptr;
// }
class ScopedUtfChars {
public:
ScopedUtfChars(JNIEnv* env, jstring s) : env_(env), string_(s) {
- if (s == NULL) {
- utf_chars_ = NULL;
- jniThrowNullPointerException(env, NULL);
+ if (s == nullptr) {
+ utf_chars_ = nullptr;
+ jniThrowNullPointerException(env, nullptr);
} else {
- utf_chars_ = env->GetStringUTFChars(s, NULL);
+ utf_chars_ = env->GetStringUTFChars(s, nullptr);
}
}
+ ScopedUtfChars(ScopedUtfChars&& rhs) :
+ env_(rhs.env_), string_(rhs.string_), utf_chars_(rhs.utf_chars_) {
+ rhs.env_ = nullptr;
+ rhs.string_ = nullptr;
+ rhs.utf_chars_ = nullptr;
+ }
+
~ScopedUtfChars() {
if (utf_chars_) {
env_->ReleaseStringUTFChars(string_, utf_chars_);
}
}
+ ScopedUtfChars& operator=(ScopedUtfChars&& rhs) {
+ if (this != &rhs) {
+ // Delete the currently owned UTF chars.
+ this->~ScopedUtfChars();
+
+ // Move the rhs ScopedUtfChars and zero it out.
+ env_ = rhs.env_;
+ string_ = rhs.string_;
+ utf_chars_ = rhs.utf_chars_;
+ rhs.env_ = nullptr;
+ rhs.string_ = nullptr;
+ rhs.utf_chars_ = nullptr;
+ }
+ return *this;
+ }
+
const char* c_str() const {
return utf_chars_;
}
@@ -59,8 +82,8 @@
}
private:
- JNIEnv* const env_;
- const jstring string_;
+ JNIEnv* env_;
+ jstring string_;
const char* utf_chars_;
DISALLOW_COPY_AND_ASSIGN(ScopedUtfChars);