Swig wrapping C++ Polymorphism

James Carroll mrmaple at gmail.com
Thu Jun 23 07:51:42 EDT 2005


Hi, I asked this on the SWIG mailing list, but it's pretty dead over there...
I'm trying to get Python to pass a subclass of a C++ object to another
C++ object...

I have three C++ classes,

TiledImageSource

ZoomifyReaderWx  which ISA  TiledImageSource

TiffWriter which has a method which takes a TiledImageSource

reader = bright.ZoomifyReaderWx()
writer = bright.TiledTiffWriter()
writer.SetImageSource(reader)  <- doesn't work, error below.

TiffWriter has a SetImageSource method that takes a pointer to a
TiledImageSource.
When I try to pass an instance of ZoomifyReaderWx to TiffWriter, SWIG
can't hook the two up.

How can I fix this?  I think I need a typemap, but I'm not sure of the syntax.

Thanks!
-Jim


Test script
----------------------------

reader = bright.ZoomifyReaderWx()
reader.Open("C:/Slides/test.pff")

writer = bright.TiledTiffWriter()
writer.SetImageSource(reader)
writer.CreateFile("C:/Slides/test.tif")

while not writer.IsFinished():
   writer.ProcessABit()
   print ".",

Exception
----------------------------
C:\jimc\prj\bright\scripts\vsEdit>python piplineTest.py
Traceback (most recent call last):
 File "piplineTest.py", line 11, in ?
   writer.SetImageSource(reader)
 File "C:\jimc\prj\bright\scripts\vsEdit\bright.py", line 88, in SetImageSource

   def SetImageSource(*args): return _bright.TiledTiffWriter_SetImageSource(*ar
gs)
TypeError: argument number 2: a 'TiledImageSource *' is expected, 'PySwigObject(
_p_ZoomifyReaderWx)' is received


Interface files (exceprts)
----------------------------

class ZoomifyReaderWx: public TiledImageSource
{
public:
   bool Open(wxString filename);
   void Close();

   ZoomifyReaderWx();
   //%pragma(python) addtomethod = "__init__:self._setOORInfo(self)"

   // necessary for ZoomableImageReader
   virtual int GetWidth(int reduction = 0);
   virtual int GetHeight(int reduction = 0);
...


class TiledTiffWriter
{
public:
   TiledTiffWriter();
   //%pragma(python) addtomethod = "__init__:self._setOORInfo(self)"

   void SetImageSource(TiledImageSource *source);
   void SetJpegQuality(int quality) { m_iJpegQuality = quality; }
   void CreateFile(char *filename);
...

class TiledImageSource
{
public:
   //%pragma(python) addtomethod = "__init__:self._setOORInfo(self)"

       virtual int GetWidth(int reduction = 0);
       virtual int GetHeight(int reduction = 0);



More information about the Python-list mailing list