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

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Sat Apr 1 01:16:18 CEST 2006


--- Arrakis Muadib <marrakis at gmail.com> 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 



More information about the Cplusplus-sig mailing list