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

David Abrahams dave at boost-consulting.com
Fri Dec 6 13:49:31 CET 2002

Niki Spahiev <niki at vintech.bg> writes:

> Nicodemus wrote:
>> Ralf W. Grosse-Kunstleve wrote:
>>> 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++).
>> Actually, I have plans to support it. My basic idea was that the
>> user should simply state:
>> class A
>>    operator [] (key int, type float, range 0-size())
>>    def size
> Why not borrow syntax (and parser) from Pyrex?
> Also maybe swig-xml to interface can work for generating initial contents.
> Whatever it is i have plan how to use it.

I gave this some thought last night.

We could have the following expressions define __getitem__ and
__setitem__, respectively:

   .def(self[unsigned()] = float())

If the class defines a "size()" member function we could use that to
define a __len__ function and to throw an IndexError.

If the class defines a nested key_type type and find() and end()
member functions we could use x.find(key) != x.end() to throw KeyError

Otherwise, the class' __len__ function (if any) could be used to throw
an IndexError, if to the arguments to operator[] are unsigned integral

All this can be done without a code-generating front-end, at least on
conforming compilers. I think that in fact most of it is portable to
all supported platforms.

                       David Abrahams
   dave at boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution

More information about the Cplusplus-sig mailing list