|
Re: function pointers in structures and Java: msg#00164programming.swig
Brian Hawley wrote: Like pointers, SWIG can't infer how you want to use these function pointers. This OO design pattern in C is common enough so maybe something automated can be done in the future, but for now you have to take a manual approach to wrapping this. The best solution would be to write a C++ class to wrap the C struct, then get SWIG to wrap the C++ class. There are many other ways which involve tricks and hacks and use of typemaps and SWIG features to bend it into something like a normal Java interface. Also consider using %extend, but that would be very similar to writing a C++ class, except you don't need a C++ compiler. I got this hack together, which might give you some ideas: %inline %{ typedef int Boolean; typedef struct _Object Object; %} %{ Boolean foo(Object *object, char *name) { ... return 1; } // The real struct struct _Object { Boolean (*foo) (Object *object, char *name); }; %} %nodefault; // typemap so that this parameter doesn't get wrapped for use from Java %typemap(in, numinputs=0) Object *object %{ $1 = *(struct _Object **)&jarg1; // serious hack! %} // What SWIG sees struct _Object { Boolean foo (Object *object, char *name); }; %inline %{ Object * new__Object(char *objectName, char *template) { Object *obj = malloc(sizeof(struct _Object)); obj->privateData = obj; obj->foo = foo; return obj; } %} Then you can use it from Java like.... _Object obj = example.new__Object("", ""); int ret = obj.foo("hi"); where example is the name of the SWIG module. You should be able to use %extend so that you can use a Java constructor instead of new__Object. > I am also concerned about the pointer values. For example: > > Object.getString takes the Object *, a char *, and a char **. it > allocates space and returns it in the third argument. the caller is > responsible for deleting it using another function we have called > lfree(). The same with getInt32 which passes a pointer to an > int...although in this case, there is no memory allocated by the > function...it's just filled in. Are these types of things going to work? > You could use %exception to turn the Boolean return into an exception. You really want the get functions to return the value it is getting (a JavaBean getter) and throw an exception if the Boolean return indicates failure. You'll also need to employ lots of typemaps for handling char* and unsigned long* as output parameters if you want to make a decent Java interface. You can do all this with typemaps, but is involved and if you can just write a decent OO C++ interface to your C code, which mirrors the intended Java interface and wrap that. William _______________________________________________ Swig maillist - Swig@xxxxxxxxxxxxxxx http://mailman.cs.uchicago.edu/mailman/listinfo/swig |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Re: Passing pointers to longs: 00164, William S Fulton |
|---|---|
| Next by Date: | STL python typemaps: 00164, yuval |
| Previous by Thread: | function pointers in structures and Javai: 00164, Brian Hawley |
| Next by Thread: | Passing pointers to longs: 00164, Brian Hawley |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |