blob: b108a6b98fda16baecd59aa973bf4224ea9b1fda [file] [log] [blame]
Elliott Hughesd5344fe2010-01-28 12:18:39 -08001/*
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_JAVA_UNICODE_STRING_H_included
18#define SCOPED_JAVA_UNICODE_STRING_H_included
19
20#include "JNIHelp.h"
Elliott Hughesa656b242010-03-01 16:28:05 -080021#include "unicode/unistr.h"
Elliott Hughesd5344fe2010-01-28 12:18:39 -080022
23// A smart pointer that provides access to an ICU UnicodeString given a JNI
24// jstring. We give ICU a direct pointer to the characters on the Java heap.
Elliott Hughes9de899c2010-04-12 18:32:50 -070025// It's clever enough to copy-on-write if necessary.
Elliott Hughesd5344fe2010-01-28 12:18:39 -080026class ScopedJavaUnicodeString {
27public:
28 ScopedJavaUnicodeString(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
29 mChars = env->GetStringChars(mString, NULL);
30 const int32_t charCount = env->GetStringLength(mString);
31 mUnicodeString.setTo(false, mChars, charCount);
32 }
33
34 ~ScopedJavaUnicodeString() {
35 mEnv->ReleaseStringChars(mString, mChars);
36 }
37
Elliott Hughes9de899c2010-04-12 18:32:50 -070038 const UnicodeString& unicodeString() const {
39 return mUnicodeString;
40 }
41
42 UnicodeString& unicodeString() {
Elliott Hughesd5344fe2010-01-28 12:18:39 -080043 return mUnicodeString;
44 }
45
46private:
47 JNIEnv* mEnv;
48 jstring mString;
49 const UChar* mChars;
50 UnicodeString mUnicodeString;
Elliott Hughes7ca6fd02010-03-29 20:51:48 -070051
52 // Disallow copy and assignment.
53 ScopedJavaUnicodeString(const ScopedJavaUnicodeString&);
54 void operator=(const ScopedJavaUnicodeString&);
Elliott Hughesd5344fe2010-01-28 12:18:39 -080055};
56
57#endif // SCOPED_JAVA_UNICODE_STRING_H_included