logo       

function pointers in structures and Java: msg#00158

programming.swig

Subject: function pointers in structures and Java


So far, I've been very impressed with SWIG.

We have a large collection of libraries, implemented in C.

To provide for some runtime overloading, and to make the code a bit more "object" like, we do the following:


TYPEDEF(struct _Object, Object)
struct _Object
{
void *privateData;
void *typePrivateData;

PROTOTYPE(Boolean, (*getString), (Object *object, char *name, char **
value_p));
PROTOTYPE(Boolean, (*getInt32), (Object *object, char *name, unsigned lo
ng *value_p));
PROTOTYPE(Boolean, (*getBoolean), (Object *object, char *name, Boolean
*value_p));
PROTOTYPE(Boolean, (*setString), (Object *object, char *name, char *v
alue));
PROTOTYPE(Boolean, (*setInt32), (Object *object, char *name, unsigned lo
ng value));
PROTOTYPE(Boolean, (*setBoolean), (Object *object, char *name, Boolean
value));
PROTOTYPE(Boolean, (*serialize), (Object *object, char **buffer_p, si
ze_t *bufferSize_p));
PROTOTYPE(Boolean, (*getName), (Object *object, char **buffer_p));
};


PROTOTYPE(Object *, new__Object, (char *objectName, char *template));
PROTOTYPE(Boolean, delete__Object, (Object *object));

The PROTOTYPE and TYPEDEF macros help deal with issues where typedef isn't available on a platform's C compiler, or it isn't ANSI-C.

Now, when new__Object is called, it allocates the private data structures, and sets up the function pointers in the structure appropriately.

We are running SWIG with the -no_default option, so it doesn't collide with the new__ and delete__ functions we have.

We would like to be able to use libraries like this from Java applications.

SWIG does just fine, and code like this works fine:

public class Main {
public static void main(String argv[]) {
_Object op;
Object o;
long value;
System.loadLibrary("base");
System.loadLibrary("object");
// TT t1 = new TT();
_TT tt = new _TT();

op = Object.new__Object("abc", "msg.xml");

However, there doesn't seem to be a way to use op, or even Object to access the member functions.

I'm open to suggestions on how this could be accomplished. Ultimately, I'd like to be able to do something like: op.getString(...), but even Object.getString(op,...) would
be fine.

One thought I had was to define actual C functions: Object__getString outside of the struct, and have that Object__getString routine in turn, reference the op argument, and make the actual call....

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?

Thanks for any help, tips, pointers (no pun intended), etc.

-- Brian

+==========================================================+
Brian N. Hawley bhawley@xxxxxxxxxxx
Luminex Software, Inc. http://www.luminex.com
PO Box 5908 voice: 909-781-4100 x112
Riverside, CA 92517 fax: 909-781-4105
+==========================================================+


_______________________________________________
Swig maillist - Swig@xxxxxxxxxxxxxxx
http://mailman.cs.uchicago.edu/mailman/listinfo/swig



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise