blob: e17981fad30bfbf4188c64603339bffa24ba8ec7 [file] [log] [blame]
gwt.team.knorton3bee6a42006-12-11 01:16:34 +00001/*
gwt.team.scottb5aa171d2007-04-11 21:11:15 +00002 * Copyright 2007 Google Inc.
gwt.team.knorton3bee6a42006-12-11 01:16:34 +00003 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
gwt.team.scottbab0aa682006-12-06 23:14:19 +000016#ifndef JSTRINGWRAP_H
17#define JSTRINGWRAP_H
18
19#include <jni.h>
20
gwt.team.scottb5aa171d2007-04-11 21:11:15 +000021/*
22 * Wrap a Java String and automatically clean up temporary storage allocated
23 * for accessing its contents.
24 */
gwt.team.scottbab0aa682006-12-06 23:14:19 +000025struct JStringWrap
26{
gwt.team.scottb5aa171d2007-04-11 21:11:15 +000027 JStringWrap(JNIEnv* env, jstring str): env(env), s(str), p(0), jp(0) { }
28 ~JStringWrap() {
29 if (p) env->ReleaseStringUTFChars(s, p);
30 if (jp) env->ReleaseStringChars(s, jp);
31 }
32 const char* str() { if (!p) p = env->GetStringUTFChars(s, 0); return p; }
33 const jchar* jstr() { if (!jp) jp = env->GetStringChars(s, 0); return jp; }
34 jsize length() { return env->GetStringLength(s); }
gwt.team.scottbab0aa682006-12-06 23:14:19 +000035private:
gwt.team.scottb5aa171d2007-04-11 21:11:15 +000036 JNIEnv* env;
37 jstring s;
38 const char* p;
39 const jchar* jp;
gwt.team.scottbab0aa682006-12-06 23:14:19 +000040};
41
42#endif