I have the following C++ class :
class CcdImageList : public std::list<CountedRef<CcdImage> >
The Swig object is correctly transmitted to the Python, but I cannot iterate on the list.
What should I do (in Swig ? ) to make it iterable ?
Did you %include "std_list.i"
(or %include "p_lsstSwig.i"
, which does that for you)?
Did you %template(SomeName) std::list<CountedRef<CcdImage>>
before %include
ing your .h file?
CcdImageList
should inherit its __iter__
method from SomeName
.
I confirm that I have %include “p_lsstSwig.i” and the %template is located before the %include
I don’t understand “CcdImageList should inherit its _iter_ method from SomeName” ? Is “SomeName” the same as the one in “%template(SomeName)” ?
The problem is that I don’t understand what I am doing. Can you recommend some introductory Swig documentation to at least understand the basics ?
You might try http://swig.org/Doc3.0/SWIGPlus.html#SWIGPlus_nn30
But it might take looking at the generated code to figure out what the problem is.
As far as "CcdImageList should inherit its __iter__
method from SomeName
" I think K-T just means that when you use this template then SWIG will stop treating such objects as opaque and will allow you to iterate over them. if std_list.i
works like std_vector.i
then the result is what you want: you can examine C++ lists of this type and for calling functions that take such a list as an argument you may even be able to provide an ordinary python lists whose elements are CountedRef<CcdImage>>
instead of providing a genuine std::list<CountedRef<CcdImage>>
(the vector wrapper certainly allows that). If not, you can construct a “SomeName
” (please pick a better name, such as CcdImageRefList
) and append elements to it one at a time, then pass that.
After several iteration with @ktl and @rowen we finally solved this. The Swig interface should contain:
%include "lsst/meas/simastrom/CountedRef.h"
%template(CcdImageCountedRef) lsst::meas::simastrom::CountedRef<lsst::meas::simastrom::CcdImage>;
%template(CcdImageCountedRefList) std::list<lsst::meas::simastrom::CountedRef<lsst::meas::simastrom::CcdImage> >;
%include "lsst/meas/simastrom/CcdImage.h"
In this order. The bottom line is that Swig is processing the .i file in one single pass, so every “object” should be known before being used.
I will also add that there was unhappiness with using CountedRef instead of a shared_ptr, so @jbosch has kindly agreed to help decide if shared_ptr can do all that is required (and in a way that doesn’t confuse SWIG too badly).