[PyKDE] SIP and C++ templates

Phil Thompson phil at riverbankcomputing.co.uk
Fri May 6 19:44:03 BST 2005


On Friday 06 May 2005 7:00 pm, Claus, Richard wrote:
> Are there any issues with, or better ways to do, the following:
>
> .h file:
>
> namespace MyNamespace
> {
>   template<unsigned T>
>   class Klass : public BaseKlass<T>
>   {
>   public:
>     Klass();
>     virtual ~Klass();
>
>     void method1();
>
>     ...
>   };
>   typedef Klass<1024> Small
>   typedef Klass<4096> Large
> };
>
> .sip file:
>
> namespace MyNamespace
> {
>   class Small
>   {
> %TypeHeaderCode
> #include "Klass.h"
> %End
>
>   public:
>     Small();
>     virtual ~Small();
>
>     void method1();
>
>     ...
>   };
>
>   class Large
>   {
> %TypeHeaderCode
> #include "Klass.h"
> %End
>
>   public:
>     Large();
>     virtual ~Large();
>
>     void method1();
>
>     ...
>   };
> };
>
>
> This seems to work fine.  Small and Large are visible from Python and
> method1() executes fine.  The methods of BaseKlass are not accessible from
> Python, but that's okay for my purposes since they're used only by methods
> of Klass.
>
> Another solution might be to make Small and Large bona fide classes that
> are derived from Klass<1024> and Klass<4096>, resp., rather than typedefs.
>
> Are these blessed ways to do things or am I climbing through a loophole?

I think that's the right way to do it at the moment. You can always copy 
BaseKlass methods to Small and Large if you want to - the .sip file doesn't 
need to accurately reflect the C++ structure, so long as the generated code 
works.

Better template support is on the TODO list - but the implementation would 
just be a shorthand for what you are doing anyway.

Phil




More information about the PyQt mailing list