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

David Abrahams dave at boost-consulting.com
Fri Dec 6 15:09:14 CET 2002


Nicodemus <nicodemus at globalite.com.br> writes:

> David Abrahams wrote:
>
>>I gave this some thought last night.
>>
>>We could have the following expressions define __getitem__ and
>>__setitem__, respectively:
>>
>>   .def(self[unsigned()])
>>   .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
>>types.
>>
>>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.
>>
>
> Very nice!
>
> The only problem I see is classes that don't follow standard names
> (Size instead of size, for instance), but even then a thin wrapper
> class could be used without problems, right?

Not really "without problems". Thin wrapper classes introduce the
problem of having to write forwarding constructors, which is often
tedious enough to make it not worth the trouble.

Probably there should be another interface for defining these
operations as well. It shouldn't be too hard to imagine what that
might look like. Ideas?

(P.S. consider the usefulness of function objects produced by
Boost.Bind or Boost.Lambda)

-Dave

-- 
                       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