summaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2013-05-01 16:34:03 -0700
committerElliott Hughes <enh@google.com>2013-05-01 16:34:03 -0700
commit60b1855c2229c2b87d2e392061df55b065d2a70e (patch)
treecc6117a660e23c4fd13f1bc12b2fa63128031380 /include
parentad86c68a7ee81bc2f1c42d3c33364b2c74132803 (diff)
downloadlibcore-60b1855c2229c2b87d2e392061df55b065d2a70e.zip
libcore-60b1855c2229c2b87d2e392061df55b065d2a70e.tar.gz
libcore-60b1855c2229c2b87d2e392061df55b065d2a70e.tar.bz2
Fix ScopedStringChars to behave like ScopedUtfChars.
Also reformat in Google C++ style and remove a couple of unnecessary #includes. Bug: https://code.google.com/p/android/issues/detail?id=45724 Change-Id: I6d1a78b3b1f72e7015baba64811d9749f330fba7
Diffstat (limited to 'include')
-rw-r--r--include/ScopedStringChars.h61
-rw-r--r--include/ScopedUtfChars.h63
2 files changed, 72 insertions, 52 deletions
diff --git a/include/ScopedStringChars.h b/include/ScopedStringChars.h
index b59b786..cfbd247 100644
--- a/include/ScopedStringChars.h
+++ b/include/ScopedStringChars.h
@@ -20,32 +20,55 @@
#include "JNIHelp.h"
// A smart pointer that provides access to a jchar* given a JNI jstring.
+// Unlike GetStringChars, we throw NullPointerException rather than abort if
+// passed a null jstring, and get will return NULL.
+// This makes the correct idiom very simple:
+//
+// ScopedStringChars name(env, java_name);
+// if (name.get() == NULL) {
+// return NULL;
+// }
class ScopedStringChars {
-public:
- ScopedStringChars(JNIEnv* env, jstring s) : mEnv(env), mString(s), mSize(0) {
- mChars = env->GetStringChars(mString, NULL);
- if (mChars != NULL) {
- mSize = env->GetStringLength(mString);
- }
+ public:
+ ScopedStringChars(JNIEnv* env, jstring s) : env_(env), string_(s), size_(0) {
+ if (s == NULL) {
+ chars_ = NULL;
+ jniThrowNullPointerException(env, NULL);
+ } else {
+ chars_ = env->GetStringChars(string_, NULL);
+ if (chars_ != NULL) {
+ size_ = env->GetStringLength(string_);
+ }
}
+ }
- ~ScopedStringChars() {
- mEnv->ReleaseStringChars(mString, mChars);
+ ~ScopedStringChars() {
+ if (chars_ != NULL) {
+ env_->ReleaseStringChars(string_, chars_);
}
+ }
- const jchar* get() const { return mChars; }
- const jchar& operator[](size_t n) const { return mChars[n]; }
- size_t size() const { return mSize; }
+ const jchar* get() const {
+ return chars_;
+ }
-private:
- JNIEnv* mEnv;
- jstring mString;
- const jchar* mChars;
- size_t mSize;
+ size_t size() const {
+ return size_;
+ }
- // Disallow copy and assignment.
- ScopedStringChars(const ScopedStringChars&);
- void operator=(const ScopedStringChars&);
+ const jchar& operator[](size_t n) const {
+ return chars_[n];
+ }
+
+ private:
+ JNIEnv* env_;
+ jstring string_;
+ const jchar* chars_;
+ size_t size_;
+
+ // Disallow copy and assignment.
+ ScopedStringChars(const ScopedStringChars&);
+ void operator=(const ScopedStringChars&);
};
#endif // SCOPED_STRING_CHARS_H_included
diff --git a/include/ScopedUtfChars.h b/include/ScopedUtfChars.h
index 7492a0c..7761450 100644
--- a/include/ScopedUtfChars.h
+++ b/include/ScopedUtfChars.h
@@ -25,50 +25,47 @@
// passed a null jstring, and c_str will return NULL.
// This makes the correct idiom very simple:
//
-// ScopedUtfChars name(env, javaName);
+// ScopedUtfChars name(env, java_name);
// if (name.c_str() == NULL) {
-// return NULL;
+// return NULL;
// }
class ScopedUtfChars {
-public:
- ScopedUtfChars(JNIEnv* env, jstring s)
- : mEnv(env), mString(s)
- {
- if (s == NULL) {
- mUtfChars = NULL;
- jniThrowNullPointerException(env, NULL);
- } else {
- mUtfChars = env->GetStringUTFChars(s, NULL);
- }
+ public:
+ ScopedUtfChars(JNIEnv* env, jstring s) : env_(env), string_(s) {
+ if (s == NULL) {
+ utf_chars_ = NULL;
+ jniThrowNullPointerException(env, NULL);
+ } else {
+ utf_chars_ = env->GetStringUTFChars(s, NULL);
}
+ }
- ~ScopedUtfChars() {
- if (mUtfChars) {
- mEnv->ReleaseStringUTFChars(mString, mUtfChars);
- }
+ ~ScopedUtfChars() {
+ if (utf_chars_) {
+ env_->ReleaseStringUTFChars(string_, utf_chars_);
}
+ }
- const char* c_str() const {
- return mUtfChars;
- }
+ const char* c_str() const {
+ return utf_chars_;
+ }
- size_t size() const {
- return strlen(mUtfChars);
- }
+ size_t size() const {
+ return strlen(utf_chars_);
+ }
- // Element access.
- const char& operator[](size_t n) const {
- return mUtfChars[n];
- }
+ const char& operator[](size_t n) const {
+ return utf_chars_[n];
+ }
-private:
- JNIEnv* mEnv;
- jstring mString;
- const char* mUtfChars;
+ private:
+ JNIEnv* env_;
+ jstring string_;
+ const char* utf_chars_;
- // Disallow copy and assignment.
- ScopedUtfChars(const ScopedUtfChars&);
- void operator=(const ScopedUtfChars&);
+ // Disallow copy and assignment.
+ ScopedUtfChars(const ScopedUtfChars&);
+ void operator=(const ScopedUtfChars&);
};
#endif // SCOPED_UTF_CHARS_H_included