Dispose method override for a specific class hierarchy

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Dispose method override for a specific class hierarchy

julvar
Hi,
I am working on a C# wrapper interface over a pre-existing C++ library.
There are several class hierarchies in the C++ codebase and I want to overrride the Dispose method in just a certain subset of the class hierarchies.
and I have not been able to figure out a convenient way to do that.

when I try this, it overrides it for all classes (which is not what I want)
%typemap(csdestruct, methodname="Dispose", methodmodifiers="public") SWIGTYPE {
...
}

%typemap(csdestruct_derived, methodname="Dispose", methodmodifiers="public") SWIGTYPE {
...
}

On the other hand, I'd prefer not to have to give the directive explicitly for every class that I want to override (there are thousands of classes).
%typemap(csdestruct, methodname="Dispose", methodmodifiers="public") MyBaseClass {
...
}

%typemap(csdestruct_derived, methodname="Dispose", methodmodifiers="public") MyDerivedClass {
...
}

I would like to know if there is a way for me to specify the directive just for the specific base class that I am intersted in, and the override would apply for all classes derived from it.

Much thanks in advance,
Julian.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Dispose method override for a specific class hierarchy

William S Fulton
On 29 July 2016 at 17:30, julvar <[hidden email]> wrote:

> Hi,
> I am working on a C# wrapper interface over a pre-existing C++ library.
> There are several class hierarchies in the C++ codebase and I want to
> overrride the Dispose method in /just a certain subset /of the class
> hierarchies.
> and I have not been able to figure out a convenient way to do that.
>
> when I try this, it overrides it for all classes /(which is not what I
> want)/
> %typemap(csdestruct, methodname="Dispose", methodmodifiers="public")
> SWIGTYPE {
> ...
> }
>
> %typemap(csdestruct_derived, methodname="Dispose", methodmodifiers="public")
> SWIGTYPE {
> ...
> }
>
> On the other hand, I'd prefer not to have to give the directive explicitly
> for every class that I want to override (there are thousands of classes).
> %typemap(csdestruct, methodname="Dispose", methodmodifiers="public")
> MyBaseClass {
> ...
> }
>
> %typemap(csdestruct_derived, methodname="Dispose", methodmodifiers="public")
> MyDerivedClass {
> ...
> }
>
> I would like to know if there is a way for me to specify the directive just
> for the specific base class that I am intersted in, and the override would
> apply for all classes derived from it.
>
I'm afraid not.

Alternative approaches are to repeatedly change the SWIGTYPE typemap
and possibly re-arrange your header files. Say you want your custom
typemap to apply to all the classes in header files AA.h AB.h AC.h but
no B.h, then use:

%typemap(csdestruct_derived, methodname="Dispose", methodmodifiers="public")
SWIGTYPE {
// typemap for classes in A*.h ...
}
%include "AA.h"
%include "AB.h"
%include "AC.h"

%typemap(csdestruct_derived, methodname="Dispose", methodmodifiers="public")
SWIGTYPE {
// another typemap for the header files below ...
}
%include B.h
%include ...

Use macros to cutdown on repetitive code.

Another approach is to run swig -xml over your interface files to
generate an xml file containing a dump of the the classes. Then write
a simple script to parse it and extract the C++ class hierarchies to
generate an interface file with the list of classes you want to apply
your typemap to. Then run swig normally for your target language to
use the additional interface file.

William

------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Dispose method override for a specific class hierarchy

julvar
Hello, 

Thanks for your response, William. 
So maybe I didn't understand the correct usage/behavior of "csdestruct_derived" ... Can you please clarify if the following statements are correct?

The following typemap would apply to any derived class that is included after the statement.
%typemap(csdestruct_derived, methodname="Dispose", methodmodifiers="public")
SWIGTYPE {
// another typemap for the header files below ...
}

and the following typemap would apply only to MYTYPE class (which has to be included after the statement).
%typemap(csdestruct_derived, methodname="Dispose", methodmodifiers="public")
MYTYPE {
// another typemap for the header files below ...
}

My initial impression was that it would work on any class derived from MYTYPE. Is that an incorrect assumption?

Thanks in advance,
Julian.

On Tue, Aug 16, 2016 at 12:15 AM, William S Fulton <[hidden email]> wrote:
On 29 July 2016 at 17:30, julvar <[hidden email]> wrote:
> Hi,
> I am working on a C# wrapper interface over a pre-existing C++ library.
> There are several class hierarchies in the C++ codebase and I want to
> overrride the Dispose method in /just a certain subset /of the class
> hierarchies.
> and I have not been able to figure out a convenient way to do that.
>
> when I try this, it overrides it for all classes /(which is not what I
> want)/
> %typemap(csdestruct, methodname="Dispose", methodmodifiers="public")
> SWIGTYPE {
> ...
> }
>
> %typemap(csdestruct_derived, methodname="Dispose", methodmodifiers="public")
> SWIGTYPE {
> ...
> }
>
> On the other hand, I'd prefer not to have to give the directive explicitly
> for every class that I want to override (there are thousands of classes).
> %typemap(csdestruct, methodname="Dispose", methodmodifiers="public")
> MyBaseClass {
> ...
> }
>
> %typemap(csdestruct_derived, methodname="Dispose", methodmodifiers="public")
> MyDerivedClass {
> ...
> }
>
> I would like to know if there is a way for me to specify the directive just
> for the specific base class that I am intersted in, and the override would
> apply for all classes derived from it.
>
I'm afraid not.

Alternative approaches are to repeatedly change the SWIGTYPE typemap
and possibly re-arrange your header files. Say you want your custom
typemap to apply to all the classes in header files AA.h AB.h AC.h but
no B.h, then use:

%typemap(csdestruct_derived, methodname="Dispose", methodmodifiers="public")
SWIGTYPE {
// typemap for classes in A*.h ...
}
%include "AA.h"
%include "AB.h"
%include "AC.h"

%typemap(csdestruct_derived, methodname="Dispose", methodmodifiers="public")
SWIGTYPE {
// another typemap for the header files below ...
}
%include B.h
%include ...

Use macros to cutdown on repetitive code.

Another approach is to run swig -xml over your interface files to
generate an xml file containing a dump of the the classes. Then write
a simple script to parse it and extract the C++ class hierarchies to
generate an interface file with the list of classes you want to apply
your typemap to. Then run swig normally for your target language to
use the additional interface file.

William


------------------------------------------------------------------------------

_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Loading...