Elliott Hughes | bcf7c66 | 2010-03-26 11:30:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef SCOPED_UTF_CHARS_H_included |
| 18 | #define SCOPED_UTF_CHARS_H_included |
| 19 | |
| 20 | #include "JNIHelp.h" |
Elliott Hughes | a5d0294 | 2010-06-01 23:02:53 -0700 | [diff] [blame] | 21 | #include <string.h> |
Elliott Hughes | bcf7c66 | 2010-03-26 11:30:00 -0700 | [diff] [blame] | 22 | |
| 23 | // A smart pointer that provides read-only access to a Java string's UTF chars. |
Elliott Hughes | 0596087 | 2010-05-26 17:45:07 -0700 | [diff] [blame] | 24 | // Unlike GetStringUTFChars, we throw NullPointerException rather than abort if |
| 25 | // passed a null jstring, and c_str will return NULL. |
| 26 | // This makes the correct idiom very simple: |
| 27 | // |
| 28 | // ScopedUtfChars name(env, javaName); |
| 29 | // if (name.c_str() == NULL) { |
| 30 | // return NULL; |
| 31 | // } |
Elliott Hughes | bcf7c66 | 2010-03-26 11:30:00 -0700 | [diff] [blame] | 32 | class ScopedUtfChars { |
| 33 | public: |
| 34 | ScopedUtfChars(JNIEnv* env, jstring s) |
Elliott Hughes | 0596087 | 2010-05-26 17:45:07 -0700 | [diff] [blame] | 35 | : mEnv(env), mString(s) |
Elliott Hughes | bcf7c66 | 2010-03-26 11:30:00 -0700 | [diff] [blame] | 36 | { |
Elliott Hughes | 0596087 | 2010-05-26 17:45:07 -0700 | [diff] [blame] | 37 | if (s == NULL) { |
| 38 | mUtfChars = NULL; |
| 39 | jniThrowNullPointerException(env, NULL); |
| 40 | } else { |
| 41 | mUtfChars = env->GetStringUTFChars(s, NULL); |
| 42 | } |
Elliott Hughes | bcf7c66 | 2010-03-26 11:30:00 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | ~ScopedUtfChars() { |
| 46 | if (mUtfChars) { |
| 47 | mEnv->ReleaseStringUTFChars(mString, mUtfChars); |
| 48 | } |
| 49 | } |
| 50 | |
Elliott Hughes | 0808cae | 2010-04-23 16:15:38 -0700 | [diff] [blame] | 51 | const char* c_str() const { |
Elliott Hughes | bcf7c66 | 2010-03-26 11:30:00 -0700 | [diff] [blame] | 52 | return mUtfChars; |
| 53 | } |
| 54 | |
Elliott Hughes | 753dcd8 | 2010-06-01 18:07:56 -0700 | [diff] [blame] | 55 | size_t size() const { |
| 56 | return strlen(mUtfChars); |
| 57 | } |
| 58 | |
Elliott Hughes | bcf7c66 | 2010-03-26 11:30:00 -0700 | [diff] [blame] | 59 | // Element access. |
| 60 | const char& operator[](size_t n) const { |
| 61 | return mUtfChars[n]; |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | JNIEnv* mEnv; |
| 66 | jstring mString; |
| 67 | const char* mUtfChars; |
| 68 | |
| 69 | // Disallow copy and assignment. |
| 70 | ScopedUtfChars(const ScopedUtfChars&); |
| 71 | void operator=(const ScopedUtfChars&); |
| 72 | }; |
| 73 | |
| 74 | #endif // SCOPED_UTF_CHARS_H_included |