[C++-sig] Re: will custom lvalue converters be supported?
Ralf W. Grosse-Kunstleve
rwgk at yahoo.com
Wed Jul 21 20:32:02 CEST 2004
--- Igor Lapshin <igor at pcigeomatics.com> wrote:
> That's exactly the case: there are already hundreds of functions
> written with that "write-back" requirement long before we decided to use
> Python and it seems to me that writing thin wrappers for all of them is
> quite problematic. Moreover, the new functions are still being
In that case I'd wrap your list-like classes like this:
This makes your std::vector derived classes look and work almost exactly like a
native Python list. You can even write foo([1,2,3]) directly to call
foo(std::vector<int> const&). Only if you want the write-back you have to be a
bit more conscious:
array = stl.vector.int([1,2,3])
This works because
- std::vector<int> is wrapped with boost::python::class_<>
- there is a custom rvalue converter from any Python sequence
(list, tuple, any iterable) to std::vector<int>
- the copy constructor for std::vector<int> is wrapped explicitly
About my implementation:
- I am trying to keep it simple to keep compile times down.
- All required header files are open source and quite small.
The most important header file is container_conversions.h
which has no other dependencies. You can easily copy and
paste code from slice.h and utils.h.
- You will not be interested in ref_from_array.h. Simply remove
the include and the three corresponding lines near the bottom.
- If you are not interested in pickling you can reduce compile
times some more by removing .enable_pickling() and the getinitargs
- For use examples, see:
You may want to wrap the required std::vector instantiations in the way
outlined above, and your derived classes with class_<your_derived,
bases<std::vector<int> > >.
It may be better to use Jonathan's newer boost::python::slice instead of my
older code for handling slices. As a start you could simply remove the
functions involving my slice objects. Maybe Jonathan can suggest replacement
Do you Yahoo!?
New and Improved Yahoo! Mail - 100MB free storage!
More information about the Cplusplus-sig