Re: [PyQt] downcast a base class to its template subclass
On Tue, 2 Nov 2010 10:55:51 -0700, Qin Shen wrote: > Hi Phil, > > Phil Thompson wrote: >> On Mon, 1 Nov 2010 17:26:41 -0700, Qin Shen wrote: >> >>> Hi, >>> >>> In my non-template base class' .sip file, I used %ConvertToSubClassCode >>> directive >>> to downcast the base class type to its sub-class. If the sub-class is >>> not a template, >>> it all works fine. Now I need to derive a template sub-class from the >>> base class, >>> the sipFindType() won't work because there isn't any Python class >>> implemented >>> for the templated sub-class. What should I do in this case? >>> >> >> So how is the template sub-class implemented in Python? Your >> %ConvertToSubClassCode will need to explicitly check for the template >> sub-class and return the appropriate Python type. >> > The .sip file of the template sub-class is based on its C++ .h header file. > Its implementation in the .sip file has been omitted since I thought I > don't > need to put any implementation in the .sip file as its C++ header file has > been properly included. > > When you asked how is the template sub-class implemented in Python, did you > mean that I need to put the C++ style implementation in .sip as well? > > Do I need to use %MappedType to convert the template sub-class to/from a > Python > type. For a non-template class, sip usually generates a .cpp file for it > as well > as a corresponding sipType_BLAH. In the case of a template class, neither > was generated. I don't see a direct type match between a Python type and > any > specialization of the template sub-class, and no sipType_ is generated for > the template class, how am I supposed to convert the type? Any C++ template must eventually be implemented using a %MappedType - that's where you decide what Python type will be used to represent the C++ type. > Do you have any example on this? There are lots of examples in PyQt - qlist.sip is probably the most comprehensive. Phil ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] downcast a base class to its template subclass
Hi Phil, Phil Thompson wrote: On Mon, 1 Nov 2010 17:26:41 -0700, Qin Shen wrote: Hi, In my non-template base class' .sip file, I used %ConvertToSubClassCode directive to downcast the base class type to its sub-class. If the sub-class is not a template, it all works fine. Now I need to derive a template sub-class from the base class, the sipFindType() won't work because there isn't any Python class implemented for the templated sub-class. What should I do in this case? So how is the template sub-class implemented in Python? Your %ConvertToSubClassCode will need to explicitly check for the template sub-class and return the appropriate Python type. The .sip file of the template sub-class is based on its C++ .h header file. Its implementation in the .sip file has been omitted since I thought I don't need to put any implementation in the .sip file as its C++ header file has been properly included. When you asked how is the template sub-class implemented in Python, did you mean that I need to put the C++ style implementation in .sip as well? Do I need to use %MappedType to convert the template sub-class to/from a Python type. For a non-template class, sip usually generates a .cpp file for it as well as a corresponding sipType_BLAH. In the case of a template class, neither was generated. I don't see a direct type match between a Python type and any specialization of the template sub-class, and no sipType_ is generated for the template class, how am I supposed to convert the type? Do you have any example on this? Thanks a lot, -Jean ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] downcast a base class to its template subclass
On Mon, 1 Nov 2010 17:26:41 -0700, Qin Shen wrote: > Hi, > > In my non-template base class' .sip file, I used %ConvertToSubClassCode > directive > to downcast the base class type to its sub-class. If the sub-class is > not a template, > it all works fine. Now I need to derive a template sub-class from the > base class, > the sipFindType() won't work because there isn't any Python class > implemented > for the templated sub-class. What should I do in this case? > > > The files look like this: > > In [BaseEntity.sip] > > class BaseEntity > { > %TypeHeaderCode > #include > %End > > %ConvertToSubClassCode > sipType = sipFindType(sipCpp->classType()); > %End > > public: > > > } > > > > [CustomEntity.h] > --- > template > class CustomEntity : BaseEntity > { > public: > CustomEntity(); > virtual ~CustomEntity(); > > std::string classType() const > { > std::ostringstream nameStream; > nameStream << "CustomEntity" << std::setfill('0') << > std::setw(2) << ID; > return nameStream.str(); > } > > ... // more stuff here > } > > > [CustomEntity.sip] > --- > template > class CustomEntity : BaseEntity > { > %TypeHeaderCode > #include > %End > > public: > CustomEntity(); > virtual ~CustomEntity(); > > std::string classType() const; > > ... // more stuff here > } So how is the template sub-class implemented in Python? Your %ConvertToSubClassCode will need to explicitly check for the template sub-class and return the appropriate Python type. Phil ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt