From: Andrey S. <dar...@ma...> - 2008-01-29 13:04:26
|
Hendrik Maryns wrote: > I was recently pointed to the %extend C_class mechanism, and loving it: > no more need for a self-constructed intermediate C file, I can directly > use the third-party C code now. > > However, I encountered a problem there, which the documentation does not > warn for: invoking such a ‘virtual’ function recursively does not work, > one has to use the synthesized SWIG name in the recursive call. > > E.g. I have a function to print a recursive struct: > > struct mgTreeNode { /* tree node */ > mA a; /* alphabet element */ > struct mgTreeNode *left, *right; /* successors */ > mgId id; /* state space id */ > mgState state; /* automaton state */ > }; > > %extend mgTreeNode { > > char *printTreeNode(int labelLength) { > // reformat the label > char *label = calloc(labelLength+1, sizeof(char)); > // … > // the daughters > char *leftDaughter = 0; > if ($self->left) { > leftDaughter = mgTreeNode_printTreeNode($self->left, labelLength); > } > char *rightDaughter = 0; > if ($self->right) { > rightDaughter = mgTreeNode_printTreeNode($self->right, labelLength); > } > // … print all stuff into buffer > return buffer; > } > }; > > This works, but note the > mgTreeNode_printTreeNode($self->right, > It would be much nicer if there were some kind of syntax to recursively > call the function. I tried > $self->right.mgTreeNode_printTreeNode( > but that doesn’t work. > > So I guess this is an RFE, a suggestion to enhance the documentation, > and a call for help: is there a nicer way to do this? May be nicer way to perform this task is to form recursion in separate function and call it from %extend method, like this: %{ static char* printTreeNodeRecursively( mgTreeNode* self, int labelLength ) { // ... } %} %extend mgTreeNode { char* printTreeNode( int labelLength ) { return printTreeNodeRecursively( $self, labelLength ); } } In this case you will not use names 'mangled' by SWIG. -- WBR, Andrey Starodubtsev mailto:dar...@ma... |