Java typemap question

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Java typemap question

Matt Weiss
Hey All,

I'm struggling with a typemap problem in java.  I currently have the
following typemap declared in java:

%typemap(jni) void *, const void * "jbyteArray"
%typemap(jtype) void *, const void * "byte []"
%typemap(jstype) void *, const void * "byte []"

My goal is to treat a void * as if it were a byte [].  Naturally they
aren't identically equivalent because one must know the length of the
data block to copy bytes to/from the java byte [] array.. And I do.

So I proceeded to write the following typemaps:

        %typemap(in) void * , const void *
        %{
       
                jsize jiSendBufferLen = jenv->GetArrayLength( $input );
                char* pchBuffer = new char[ jiSendBufferLen];

          if (!pchBuffer)
                {
    SWIG_JavaThrowException(jenv,
SWIG_JavaOutOfMemoryError,
                                "memory allocation failed");
                }

                memset( pchBuffer, 0, jiSendBufferLen);
       
                jenv->GetByteArrayRegion( $input, 0, jiSendBufferLen,
(jbyte*)pchBuffer );
                $1 = pchBuffer;
        %}

This works great!  I suspect that I can use one of the swig generated
functions to shorten this.  

My out typemap looks like this:

        %typemap(out) void * , const void *
        %{
                int ilength = arg1->length();

                $result = SWIG_JavaArrayOutSchar(jenv,
                                (signed char *) $1, (jint) ilength);

        %}

This works great 99% of the time.  My problem is that there are two
objects that use this  typemap.  One type is a struct, and one is a
class.  In my struct, length is a member variable, and in my class,
length is a function, thus I need both "length()" and "length" syntax.  

How can I resolve this problem?  I looked at extending the class with
the %extend syntax to add a length() function to my struct, but that
appears to only extend the class/struct in the java interface, and
doesn't help me in the .jni interface.  Can I do something similar at
the JNI level?

Thanks in advance,
Matt
--
Matt Weiss - Software Journeyman
X-Rite, Incorporated
3100 44th Street SW
Grandville, MI 49418


-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: Java typemap question

William S Fulton
Matt Weiss wrote:

> Hey All,
>
> I'm struggling with a typemap problem in java.  I currently have the
> following typemap declared in java:
>
> %typemap(jni) void *, const void * "jbyteArray"
> %typemap(jtype) void *, const void * "byte []"
> %typemap(jstype) void *, const void * "byte []"
>
> My goal is to treat a void * as if it were a byte [].  Naturally they
> aren't identically equivalent because one must know the length of the
> data block to copy bytes to/from the java byte [] array.. And I do.
>
> So I proceeded to write the following typemaps:
>
> %typemap(in) void * , const void *
> %{
>
> jsize jiSendBufferLen = jenv->GetArrayLength( $input );
> char* pchBuffer = new char[ jiSendBufferLen];
>
>   if (!pchBuffer)
> {
>     SWIG_JavaThrowException(jenv,
> SWIG_JavaOutOfMemoryError,
> "memory allocation failed");
> }
>
> memset( pchBuffer, 0, jiSendBufferLen);
>
> jenv->GetByteArrayRegion( $input, 0, jiSendBufferLen,
> (jbyte*)pchBuffer );
> $1 = pchBuffer;
> %}
>
> This works great!  I suspect that I can use one of the swig generated
> functions to shorten this.  
>
> My out typemap looks like this:
>
> %typemap(out) void * , const void *
> %{
> int ilength = arg1->length();
>
> $result = SWIG_JavaArrayOutSchar(jenv,
> (signed char *) $1, (jint) ilength);
>
> %}
>
> This works great 99% of the time.  My problem is that there are two
> objects that use this  typemap.  One type is a struct, and one is a
> class.  In my struct, length is a member variable, and in my class,
> length is a function, thus I need both "length()" and "length" syntax.  
>
> How can I resolve this problem?  I looked at extending the class with
> the %extend syntax to add a length() function to my struct, but that
> appears to only extend the class/struct in the java interface, and
> doesn't help me in the .jni interface.  Can I do something similar at
> the JNI level?
>

Write two different typemaps targetting the names of the functions:

%typemap(out) void * returnclass
%{
   int ilength = arg1->length();
   ...
%}
%typemap(out) void * returnstruct
%{
   int ilength = arg1->length;
   ...
%}

void * returnclass(myclass*);
void * returnstruct(mystruct*);

or write overloaded helper functions so that you keep just the one typemap:

%typemap(out) void * returnclass
%{
   int ilength = get_length(arg1);
   ...
%}

%{
inline int get_length(myclass *s) { return s->length(); }
inline int get_length(mystruct *s) { return s->length; }
%}

William


-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Java typemap question

Matt Weiss
In reply to this post by Matt Weiss
Hey All,

I'm struggling with a typemap problem in java.  I currently have the
following typemap declared in java:

%typemap(jni) void *, const void * "jbyteArray"
%typemap(jtype) void *, const void * "byte []"
%typemap(jstype) void *, const void * "byte []"

My goal is to treat a void * as if it were a byte [].  Naturally they
aren't identically equivalent because one must know the length of the
data block to copy bytes to/from the java byte [] array.. And I do.

So I proceeded to write the following typemaps:

        %typemap(in) void * , const void *
        %{
       
                jsize jiSendBufferLen = jenv->GetArrayLength( $input );
                char* pchBuffer = new char[ jiSendBufferLen];

          if (!pchBuffer)
                {
    SWIG_JavaThrowException(jenv,
SWIG_JavaOutOfMemoryError,
                                "memory allocation failed");
                }

                memset( pchBuffer, 0, jiSendBufferLen);
       
                jenv->GetByteArrayRegion( $input, 0, jiSendBufferLen,
(jbyte*)pchBuffer );
                $1 = pchBuffer;
        %}

This works great!  I suspect that I can use one of the swig generated
functions to shorten this.  

My out typemap looks like this:

        %typemap(out) void * , const void *
        %{
                int ilength = arg1->length();

                $result = SWIG_JavaArrayOutSchar(jenv,
                                (signed char *) $1, (jint) ilength);

        %}

This works great 99% of the time.  My problem is that there are two
objects that use this  typemap.  One type is a struct, and one is a
class.  In my struct, length is a member variable, and in my class,
length is a function, thus I need both "length()" and "length" syntax.  

How can I resolve this problem?  I looked at extending the class with
the %extend syntax to add a length() function to my struct, but that
appears to only extend the class/struct in the java interface, and
doesn't help me in the .jni interface.  Can I do something similar at
the JNI level?

Thanks in advance,
Matt
--
Matt Weiss - Software Journeyman
X-Rite, Incorporated
3100 44th Street SW
Grandville, MI 49418


-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user