[C++-sig] Problem with Templated class and Boost.python

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[C++-sig] Problem with Templated class and Boost.python

Mathieu Leduc-Hamel-2
Hi,

I'm trying to create a binding for a small c++ class. I decided to
created directly with Boost.Python and did'nt use Pyste cause it's not
supported on my distro and I cannot make gccxml working.

Now I'm confronted with a class having a template parameter.

The signature is something like this:

template<typename T>
class A : virtual public B<T>
{
    T methodX() {...}
    ...
};

How can I map the template parameter to the boost.python class definition?

Thank you

Mathieu Leduc-Hamel
_______________________________________________
C++-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/c++-sig
Reply | Threaded
Open this post in threaded view
|

Re: [C++-sig] Problem with Templated class and Boost.python

Roman Yakovenko
On 3/31/06, Arrakis Muadib <[hidden email]> wrote:
> Hi,

> Now I'm confronted with a class having a template parameter.
>
> The signature is something like this:
>
> template<typename T>
> class A : virtual public B<T>
> {
>     T methodX() {...}
>     ...
> };
>
> How can I map the template parameter to the boost.python class definition?

You can not export the template itself. You can only export it's instantiations.

> Thank you
>
> Mathieu Leduc-Hamel

--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
_______________________________________________
C++-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/c++-sig
Reply | Threaded
Open this post in threaded view
|

Re: [C++-sig] Problem with Templated class and Boost.python

Ralf W. Grosse-Kunstleve
In reply to this post by Mathieu Leduc-Hamel-2
--- Arrakis Muadib <[hidden email]> wrote:

> I'm trying to create a binding for a small c++ class. I decided to
> created directly with Boost.Python and did'nt use Pyste cause it's not
> supported on my distro and I cannot make gccxml working.
>
> Now I'm confronted with a class having a template parameter.
>
> The signature is something like this:
>
> template<typename T>
> class A : virtual public B<T>
> {
>     T methodX() {...}
>     ...
> };
>
> How can I map the template parameter to the boost.python class definition?

If you have just one A<T> to wrap, simply do it like this:

  class_<A<int> >("A_int")
    .def("method_a", &A<int>::method_a)
  ;

If you have many, write a helper struct like this:

  template <typename T>
  struct A_wrapper
  {
    static void
    wrap(const char* python_name)
    {
      class_<A<T> >(python_name)
        .def("method_a", &A<T>::method_a)
        // etc.
      ;
    }
  };

Later:

  A_wrapper<int>::wrap("A_int");
  A_wrapper<long>::wrap("A_long");
  // etc.

Here is a complete example:

 
http://cvs.sourceforge.net/viewcvs.py/cctbx/scitbx/include/scitbx/stl/vector_wrapper.h?view=markup

http://cvs.sourceforge.net/viewcvs.py/cctbx/scitbx/include/scitbx/stl/vector_ext.cpp?view=markup

Cheers,
        Ralf


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com 
_______________________________________________
C++-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/c++-sig