|
From: William S F. <ws...@fu...> - 2005-12-18 09:50:26
|
John Koleszar wrote:
> Hi all,
>
> I have a function GetBool(t_bool *) that swig is mapping (in java) to
> GetBool(short []). This same typemap file works for perl, python, and
> php, so it seems like it's a java thing. Seems like Swig should really
> be generating some sort of mutable integer wrapper class and using that
> instead. Currently I have to do something like:
> short x[] = {1};
> test.GetBool(x);
> if(x[0]) do_stuff();
>
> which isn't very nice syntactically. Does anyone have a workaround for
> this? Does this problem exist in 1.3.27?
>
You are specifically asking for this approach by using typemaps.i and
%apply. If you can think of a better way to map a primitive output using
classes in the JDK, let us know. Or you can write your own mutable
number wrapper class as per this in the docs:
http://www.swig.org/Doc1.3/Java.html#using_typemaps_return_arguments
> On another syntax note, and this is more of a feature request than a bug
> report, if I have a typedef like:
> typedef void* handle;
> it'd be nice to be able to reference that type as "handle" or similar
> rather than "SWIGTYPE_p_void". This would provide a little extra type
> safety if you've got multpile types that alias void*.
>
> John
>
> === test.i ===
> %include "typemaps.i"
> %module test
> %{
> typedef unsigned char t_u8;
> typedef t_u8 t_bool;
> static t_bool priv_bool = 0;
>
> int GetBool(t_bool *b) {
> *b=priv_bool;
> printf("C: Returning %d\n",*b);
> return 0;
> };
>
> int SetBool(t_bool b) {
> printf("C: Setting %d\n",b);
> priv_bool = b;
> return 0;
> };
>
> %}
>
> %apply unsigned char *OUTPUT { unsigned char * }
>
> typedef unsigned char t_u8;
> typedef t_u8 t_bool;
> int GetBool(t_bool *b);
> int SetBool(t_bool b);
>
> === Generated test.java (1.3.28) ===
> public class test {
> public static int GetBool(short[] b) {
> return testJNI.GetBool(b);
> }
>
> public static int SetBool(short b) {
> return testJNI.SetBool(b);
> }
> }
>
With regard to the void* typedef, all types are fully resolved as they
are in C++. The implementation is basically doing what the coder
originally intended, eg
void h(handle x);
void v(void *x);
void foo() {
void* p = new int(10);
h(p);
v(p);
}
That is a void* is exactly the same as a handle. Typedefs don't add in
extra type safety. If you want extra type safety, it should be addressed
at the C++ level. If you want to add in extra typesafety in your Java
wrappers that is not present in your C++ layer, you'd need to mess with
some typemaps. Try this:
// modified slightly from the SWIGTYPE* typemaps in java.swg
%typemap(jstype) handle "handle"
%typemap(javain) handle "handle.getCPtr($javainput)"
%typemap(javaout) handle {
long cPtr = $jnicall;
return (cPtr == 0) ? null : new handle(cPtr, $owner);
}
and copy the SWIGTYPE_p_void class to handle.java and change all
'SWIGTYPE_p_void' to 'handle' within this class only. If you have other
code which uses void*, then from Java you'll have to use
SWIGTYPE_p_void, which is what you want, or you can add in more typemaps
like above.
William
|