← Back to team overview

dolfin team mailing list archive

Re: PyDOLFIN, SWIG and overloaded constructor

 

On Sunday 26 October 2008 13:34:42 Evan Lezar wrote:
> Hi
>
> I have added a constructor to PETScMatrix to allow me to control whether
> the matrix is to be created in parallel or not.  The declarations are given
> below
>
> /// Create empty matrix
> explicit PETScMatrix(Type type=default_matrix);
>
> /// Create an empty matrix explicitly setting the parallel flag
> explicit PETScMatrix(bool is_parallel, Type type=default_matrix);
>
> The idea is that I use
>
> S = PETScMatrix(False)
>
> If I want to create a matrix that is local.  The problem is that swig can't
> differentiate between Type and bool (and uint for the PETScMatrix(uint M,
> uint N, Type type=default_matrix); constructor).  I have seen that it is
> possible to rename PETScMatrix(bool) to PETScMatrix_bool(bool) for example
> so that the python call would be
>
> S = PETScMatrix_bool(False),
>
> but I can't seem to get this working.  Any suggestions?

I have very limited experience with parrallel programming, and of course even 
less with parrallel dolfin, so I cannot give you any specific respons on the 
changes you have suggested to the dolfin interface, but I can maybee help you 
out with the wrapper thing.

I have no experience of overloading enums, and bools, where the results 
compete with, e.g., uint, uint. But I can see how this is a problem, as enums 
are wrapped as int by swig, bool in python is int, and there is no 
fundamental type called uint in python, they are wrapped as int by swig.

You can rename almost any c++ function in swig and then extend the python 
interface to call the renamed function, to customize the python interface. 
This is done in many places in the code, but I do not think you can rename 
the constructor function. This function need to be called through the 
__init__ function in python, and this has to call the wrapped constructors. 
If you rename one constructor function, this wont be accessed by the __init__ 
function anymore, and would probably be invisible. 

You could consider use a string argument as your extra arguement, instead of 
bool.


Johan



References