3
* ====================================================================
4
* Licensed to the Apache Software Foundation (ASF) under one
5
* or more contributor license agreements. See the NOTICE file
6
* distributed with this work for additional information
7
* regarding copyright ownership. The ASF licenses this file
8
* to you under the Apache License, Version 2.0 (the
9
* "License"); you may not use this file except in compliance
10
* with the License. You may obtain a copy of the License at
12
* http://www.apache.org/licenses/LICENSE-2.0
14
* Unless required by applicable law or agreed to in writing,
15
* software distributed under the License is distributed on an
16
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17
* KIND, either express or implied. See the License for the
18
* specific language governing permissions and limitations
20
* ====================================================================
24
#ifndef SVN_JAVAHL_JNIWRAPPER_STRING_HPP
25
#define SVN_JAVAHL_JNIWRAPPER_STRING_HPP
30
#include <apr_pools.h>
32
#include "jni_object.hpp"
37
* Object wrapper for @c java.lang.String.
39
* The associated JNI class reference is stored for the lifetime of
40
* the JVM in the global class cache.
48
* Constructs a wrapper around an existing string @a str.
50
explicit String(Env env, jstring str)
56
* Constructs a new string and wrapper from @a text.
58
explicit String(Env env, const char* text)
60
m_jthis(env.NewStringUTF(text))
64
* Constructs a new string and wrapper from @a text.
66
explicit String(Env env, const std::string& text)
68
m_jthis(env.NewStringUTF(text.c_str()))
72
* Returns the wrapped JNI object reference.
80
* Returns the wrapped enviromnment reference.
88
* Returns the number of Unicode characters in the string.
92
return m_env.GetStringLength(get());
96
* Returns the length of the modified UTF-8 representation of the
99
jsize utf8_length() const
101
return m_env.GetStringUTFLength(get());
105
* Copies the contents of the modified UTF-8 representation of the
106
* string into @a pool.
108
const char* strdup(apr_pool_t* pool) const;
111
* Accessor class for the contents of the string.
113
* Objects of this class should be created within the scope where
114
* the raw C string is required. They will create an immutable
115
* modified UTF-8 representation of the string contents. The data
116
* will be released by the destructor.
122
* Constructs an immutable string contents accessor.
124
explicit Contents(const String& str)
126
m_text(!str.get() ? NULL
127
: str.m_env.GetStringUTFChars(str.get(), NULL)),
128
m_length(m_text ? jsize(::std::strlen(m_text)) : 0)
132
* Releases the string contents, possibly committing changes to the JVM.
137
m_str.m_env.ReleaseStringUTFChars(m_str.get(), NULL);
141
* Returns the C representation of the string contents.
143
const char* c_str() const
149
* Returns the length of the C representation of the string.
151
jsize utf8_length() const
163
* Accessor class for the contents of the string.
165
* Behaves like the #Contents class, but the representation is
166
* considered mutable and can be assigned a new value, which will be
167
* subsequently committed to the JVM.
169
class MutableContents : protected Contents
173
* Constructs a mutable string contents accessor.
175
explicit MutableContents(String& str)
181
* Releases the string contents, possibly committing changes to the JVM.
187
// Prevent double-release by the Contents destructor.
189
m_str.m_env.ReleaseStringUTFChars(m_str.get(), m_new_text);
194
* Returns the C representation of the string contents.
196
const char* c_str() const
200
return Contents::c_str();
204
* Returns the length of the C representation of the string.
206
jsize utf8_length() const
208
return Contents::utf8_length();
212
* Sets a new value for the string, to be committed to the JVM
213
* when the accessor object is destroyed.
214
* @throw std::invalid_argument if the @a new_text is @c null
215
* @throw std::logic_error if this is a @c null or immutable string
217
void set_value(const char* new_text);
220
const char* m_new_text;
225
const jstring m_jthis;
228
* This object's implementation details.
230
class ClassImpl : public Object::ClassImpl
232
friend class ClassCacheImpl;
235
explicit ClassImpl(Env env, jclass cls)
236
: Object::ClassImpl(env, cls)
240
virtual ~ClassImpl();
243
friend class Contents;
244
friend class MutableContents;
245
friend class ClassCacheImpl;
246
static const char* const m_class_name;
251
#endif // SVN_JAVAHL_JNIWRAPPER_STRING_HPP