Re: [C++-SIG] How to use CXX...
"Barry Scott" <barry@scottb.demon.co.uk> writes: [...]
4. I haven't gotten to trying to wrap containers yet. What if your C++ type has a method like const std::list<foo>& get_foos() const; where the type exposes a nested container of some other type. That looks like you could coax some Pythonic behavior out of the pattern, but how would you go about wrapping it? Do you have to copy the whole list for each request for the Python-wrapped sequence?
No copy needed. You can implement the methods required for a sequence type and be a proxy between Python and the std::list.
Looking at the sequence methods more closely, it looks like Python imposes random-access requirements on the underlying sequence. The sequence_item() method allows the caller to fetch an element at a particular index. Has anyone tried to adapt this to something like std::list? It seems like you _could_ find an item by iterating from begin() a given number of times, making sure that you're not at end(), but that's horribly inefficient. You could try to cache the last index request and say, "If the requested index is just one higher than the last one, then just bump my iterator forward by one." This could get into some weird situations if the underlying list changed - like if some elements got inserted before your "current" position. The list iterators would be stable, but the indexes would not be. An alternate idea is to copy the list iterators into a vector. That could also be costly if the proxy sequence has to be constructed again and again. Has anyone tried to create such a Standard C++ container proxy? [...] -- Steven E. Harris Primus Knowledge Solutions, Inc. http://www.primus.com
It is clear that a Python sequence type is a random access container. The class Sequence in CXX is too. If you want to wrap a class that is not a random access container, then it isn't a Sequence. You can invent a new class for it that is a child of Object in the same way that Sequence is, with whatever access rules you think are appropriate, but if your can't do x[i] in reasonable time it is simply deceptive to have such an operator. One model that may work is the Eiffel LIST; in Eiffel, most containers have a cursor as part of their state. The cursor can be moved forward, placed at the start, tested as whether it is off the end, etc. The access is then x.item (which would be x.item() in C++) to get the value under the cursor.
participants (2)
-
Paul F. Dubois -
Steve Harris