This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Patch for Review: JvGetStringUTF and JvTempCString


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<----------------------------------------------





Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]