This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
Re: Patch for Review: JvGetStringUTF and JvTempCString
- From: Mohan Embar <gnustuff at thisiscool dot com>
- To: Andrew Haley <aph at redhat dot com>
- Cc: Bryce McKinlay <bryce at mckinlay dot net dot nz>, tromey at redhat dot com, java-patches at gcc dot gnu dot org
- Date: Tue, 12 Aug 2003 14:02:25 -0500
- Subject: Re: Patch for Review: JvGetStringUTF and JvTempCString
- Reply-to: gnustuff at thisiscool dot com
Hi Andrew,
> > How would I parameterize the 256 in this class? (I'm sure you wouldn't
> > want to make this a template class....)
>
>Use a macro. Create the temp and pass it to the constructor.
That's not as easy as it sounds, especially if we want the helper class
to make sense outside of the context of the macro.
Here is what I came up with which tries to encapsulate the gore as
much as possible while still providing a useful macro and a useful
helper class that can be used with or without the macro. It results in
JvGetStringUTFLength() being called twice for the non-stack-based
case. It seems more trouble than it's worth for the 256 bytes, but I'll let
you all be the judge of that. I'm taking this for a spin right now....
-- Mohan
http://www.thisiscool.com/
http://www.animalsong.org/
-----------------------------------------------8<----------------------------------------------
class _Jv_TempUTFString
{
public:
_Jv_TempUTFString(jstring jstr, char* buf=0);
~_Jv_TempUTFString();
// Accessors
operator const char*() const
{
return buf_;
}
const char* buf() const
{
return buf_;
}
char* buf()
{
return buf_;
}
private:
char* buf_;
bool heapAllocated_;
};
inline _Jv_TempUTFString::_Jv_TempUTFString (jstring jstr, char* buf)
: buf_(0), heapAllocated_(false)
{
if (!jstr) return;
if (buf)
{
buf_ = buf;
}
else
{
jsize len = JvGetStringUTFLength (jstr);
buf_ = (char*) _Jv_Malloc (len+1);
heapAllocated_ = true;
}
JvGetStringUTFChars (jstr, buf_);
}
inline _Jv_TempUTFString::~_Jv_TempUTFString ()
{
if (heapAllocated_)
_Jv_Free (buf_);
}
#define JV_TEMP_UTF_STRING(jstr, buf) \
jsize buf##_len = JvGetStringUTFLength (jstr) + 1; \
char buf##_temp[buf##_len <= 256 ? buf##_len : 0]; \
_Jv_TempUTFString buf(jstr, sizeof(buf##_temp)==0 ? 0 : buf##_temp);
-----------------------------------------------8<----------------------------------------------