Binding of C code
Roberto Cavada
cavada at irst.itc.it
Fri Jun 20 05:48:02 EDT 2003
Hi all,
I need to bind C code written with a object-oriented style.
For example suppose you need to bind this two "classes" (sorry, it's a
bit long, but easy to read):
/* helpers */
typedef struct ClassA_TAG* ClassA_ptr;
typedef struct ClassB_TAG* ClassB_ptr;
#define CLASS_A(x) ((ClassA_ptr) x)
#define CLASS_B(x) ((ClassB_ptr) x)
/* Base class */
typedef struct ClassA_TAG
{
/* data members here */
/* VIRTUAL TABLE: for semplicity we let it to stay here instead of
putting it into a meta-class which collects all parts common
to all instances */
void (*finalize)(ClassA_ptr self); /* virtual destructor */
} ClassA;
/* Derived class */
typedef struct ClassB_TAG
{
/* base class */
ClassA _parent;
/* data members here */
} ClassB;
/* ----------------------------------------------------------- */
static void class_a_finalize(ClassA_ptr self);
static void class_b_finalize(ClassA_ptr object);
/* Public methods: */
ClassA_ptr ClassA_create()
{
ClassA_ptr self;
self = CLASS_A( malloc(sizeof(ClassA)) );
self->finalize = class_a_finalize;
printf("Constructor of A\n");
return self;
}
/* virtual destructor for ClassA and any derived class */
void ClassA_destroy(ClassA_ptr object)
{
object->finalize(object);
}
ClassB_ptr ClassB_create()
{
ClassB_ptr self;
self = CLASS_B( malloc(sizeof(ClassB)) );
CLASS_A(self)->finalize = class_b_finalize;
printf("Constructor of B\n");
return self;
}
static void class_a_finalize(ClassA_ptr self)
{
printf("Destructor of A\n");
free(self);
}
static void class_b_finalize(ClassA_ptr object)
{
ClassB_ptr self = CLASS_B(object);
printf("Destructor of B\n");
free(self);
}
/* example of use */
int main()
{
ClassA_ptr a;
ClassB_ptr b;
a = ClassA_create();
b = ClassB_create();
ClassA_destroy(a);
ClassA_destroy(CLASS_A(b));
return 0;
}
/* ----------------------------------------- */
How can I bind these structures into two equivalent python classes?
The idea here is that I can use Class A methods on instances of Class B.
I know the gobject library, but I'd like to avoid to depend on it.
I think that SWIG cannot handle this stuff, am I wrong?
Is there a tool that can generate the correct binding? If there is
not, how would it be possible to write one?
Thanks,
rob
--
_/_/_/ _/_/_/ Roberto Cavada
_/ _/ _/ ITC-irst http://www.irst.itc.it
_/ _/ _/ Automated Reasoning Systems - Formal Methods Group
/_/_/ _/ Via Sommarive, 18 - 38050 Povo (TN) - Italy
_/ _/ Tel: +39 0461 314 328 Fax: +39 0461 302 040
_/ _/_/ cavada at irst.itc.it http://sra.itc.it/people/cavada
More information about the Python-list
mailing list