[C++-sig] Boost.Python code generator

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Thu Dec 5 07:34:19 CET 2002


--- Nicodemus <nicodemus at globalite.com.br> wrote:
> In this case I see a real benefit, if you consider that you have over
> 100+ classes to convert. 8)

Oh, I guess now I have another argument for exclusively using Python to handle
runtime polymorphism. But I understand not everybody can afford the luxury of
being so stubborn with this opinion :-)

Do you have a plan for dealing with "Python only" member functions? How would
you deal with, for example, a __getitem__ that does range checking and raises
the proper Python exception if necessary (assume there is no range-checking
member function in C++). Another example is pickle support where you might want
to def a combination of __getinitargs__, __getstate__ and __setstate__. Where
would I put the C++ source for these functions?

An ad hoc idea is to have some kind of "inline" or "verbatim" statement, e.g.:

<verbatim>
  boost::python::tuple
  getinitargs(any const& o)
  {
    // code here for building the tuple
  }
</verbatim>

  class any
    def foo # any plain member function
    def __getinitargs__=free(getinitargs)

But what if you have several classes with pickle support or getitem?
I got frustrated inventing function names like

  any_getinitargs(/*...*/);

  any_other_getinitargs((/*...*/);

  etc_getinitargs(/*...*/);

Therefore I started a scheme like this:

  struct really_long_class_name_wrapper
  {
    // serves as a namespace for short identifiers
  
    typedef really_long_class_name w_t;

    static some_type
    getitem(w_t const& o, long key)
    {
      // code here
    }
    
    static void
    wrap()
    {
      class_<w_t>("any")
        def("foo", &w_t::foo)
        def("__getitem__", getitem)  
      ;
    }
  };

This scales very nicely and keeps the wrapper code neat and tidy (you can even
have the BOOST_PYTHON_*FUNCTION_OVERLOADS macros inside the wrapper structs).

This leads to the idea of turning the "inline" approach upside-down:
expect C++ code with embedded pyste fragments. Can you see this working for
your runtime-polymorphic types?

Ralf


__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com




More information about the Cplusplus-sig mailing list