Re: %factory and templates

<[email protected]>
Newsgroups gmane.comp.programming.swig
Message-ID <[email protected]>
From: William S Fulton <[email protected]> 
Sent: 09 October 2020 19:03



 

On Fri, 9 Oct 2020 at 16:42, <[email protected] <mailto:[email protected]> > wrote:

Hi

 

We are trying to use %factory (see https://github.com/swig/swig/blob/master/Lib/typemaps/factory.swg) to let SWIG do automatic downcasting to the actual types, as opposed to just returning the base class. However, at present it isn’t working for us. Our hunch is that this is due to templates and typemaps (which I don’t understand well enough). We haven’t boiled it down to a simple example yet, but I thought maybe would know the answer straight-away 😊

 

We have a hierarchy like this (apologies for long names, but I kept the ones in our actual code)

 

stir::GeneralisedPrior<TargetT >

stir::QuadraticPrior<elemT> : public stir::GeneralisedPrior<DiscretisedDensity<3,elemT> >

stir::RelativeDifferencePrior<elemT> : public stir::GeneralisedPrior<DiscretisedDensity<3,elemT> >

 

(cutting out some intermediate classes)

 

We then have some other templated class that has a member

 

stir::GeneralisedPrior<TargetT > *GeneralisedObjectiveFunction<TargetT>::get_prior_ptr()

 

So, in our SWIG interface file we say

 

%newobject *::get_prior_ptr;

%factory(stir::GeneralisedPrior *stir::GeneralisedObjectiveFunction::get_prior_ptr, stir::QuadraticPrior, stir::PLSPrior, stir::RelativeDifferencePrior);

 

i.e. without any template stuff at all, but it seems that the typemap generated by %factory isn’t picked up.

 

Anyone any suggestions?

 

 

 

You need to instantiate your templates with %template if you aren't already.

 

William

 

Thanks William,

 

Sorry to have posted incomplete code, but it is rather long due to the somewhat complicated hierarchy. We can try to make a reduced example. The %templates are there. Indeed, all the classes are callable from Python. It is just that the “factory” (get_prior_ptr) still returns a base object (GeneralisedPrior), as opposed to one of the leaf classes (QuadraticPrior etc).

 

Relevant bits of our interface file are:

*	%newobject, %factor, %include at https://github.com/UCL/STIR/blob/9c72b9ee8d584f4fd5729f496b7927fec8f55e4e/src/swig/stir.i#L1582-L1595
*	%template stuff at https://github.com/UCL/STIR/blob/9c72b9ee8d584f4fd5729f496b7927fec8f55e4e/src/swig/stir.i#L1634-L1650

 

I’m not sure how to debug the typemap question but will have a look at the debug switches of SWIG, unless there are any easier suggestions.

 

Thanks again

Kris

_______________________________________________
Swig-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/swig-user
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.