diff options
author | Elliott Hughes <enh@google.com> | 2010-03-26 11:30:00 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2010-03-26 13:24:10 -0700 |
commit | bcf7c66e617ad0c33bb320184bb2401def517342 (patch) | |
tree | d7bf28cd5bab40431ad4a5dc2fbb98cc4e099367 /include/ScopedUtfChars.h | |
parent | a5cdf4f808ecb46f6f348fce5873f1691587d9ea (diff) | |
download | libcore-bcf7c66e617ad0c33bb320184bb2401def517342.zip libcore-bcf7c66e617ad0c33bb320184bb2401def517342.tar.gz libcore-bcf7c66e617ad0c33bb320184bb2401def517342.tar.bz2 |
Start cleaning up the Charset implementation.
This was going to be https://issues.apache.org/jira/browse/HARMONY-6461,
but I couldn't resist cleaning up some of the surrounding code, and ended
up cleaning up some of our native code too. In the course of the afternoon
I spent on this, I lost my conviction that the upstream change makes
sense, so I reverted that, leaving this change just pure cleanup.
(Note that the cleanup work is incomplete. This is an improvement, but
there's plenty left to do. I just don't want to get too distracted until
all the Java 6 changes are done.)
Change-Id: I56841db5f6c038bbf7942e83a148dca546519269
Diffstat (limited to 'include/ScopedUtfChars.h')
-rw-r--r-- | include/ScopedUtfChars.h | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/include/ScopedUtfChars.h b/include/ScopedUtfChars.h new file mode 100644 index 0000000..8bc3e66 --- /dev/null +++ b/include/ScopedUtfChars.h @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SCOPED_UTF_CHARS_H_included +#define SCOPED_UTF_CHARS_H_included + +#include "JNIHelp.h" + +// A smart pointer that provides read-only access to a Java string's UTF chars. +class ScopedUtfChars { +public: + ScopedUtfChars(JNIEnv* env, jstring s) + : mEnv(env), mString(s), mUtfChars(NULL) + { + mUtfChars = env->GetStringUTFChars(s, NULL); + } + + ~ScopedUtfChars() { + if (mUtfChars) { + mEnv->ReleaseStringUTFChars(mString, mUtfChars); + } + } + + const char* data() const { + return mUtfChars; + } + + // Element access. + const char& operator[](size_t n) const { + return mUtfChars[n]; + } + +private: + JNIEnv* mEnv; + jstring mString; + const char* mUtfChars; + + // Disallow copy and assignment. + ScopedUtfChars(const ScopedUtfChars&); + void operator=(const ScopedUtfChars&); +}; + +#endif // SCOPED_UTF_CHARS_H_included |