On Wed, 20 Mar 2013 16:53:55 -0000, Alex Leach <beamesleach@gmail.com> wrote:
On Wed, 20 Mar 2013 16:34:00 -0000, Jim Bosch <talljimbo@gmail.com> wrote:
[...] I'm curious what you're using to wrap std::list, as you clearly have methods that return std::list, and the standard library can be tricky to translate to Python due to things like iterator invalidation.
Heh, not yet anyway! Although I've laid out the foundations, I haven't yet implemented my attempt at a solution, nor tested it,
I've since got around to fixing the std::list wrapper I think I sent around before. Lots of errors in that version! Now, I've got it working, though; its sort and reverse functions use C++ STL algorithms instead of Python's, but that's probably a good thing; results appear to be consistent with those on a normal Python list. That said, I haven't profiled it for speed and compared performance against Python lists. I attach a complete example in the attached zip archive; just run 'python test_make_list.py' to compile an example extension, which exposes std::list<int> and std::list<std::string>. Unit-tests are then run to compare those against identical Python lists. make_list.zip contents:- ./test_make_list.py // Compiles example code and runs unit-tests ./test_make_list.cpp // example showing how to expose list<string> and list<int> ./boost_helpers/make_list.hpp // defines boost::python::list_indexing_suite ./boost_helpers/make_callback.hpp // needed for sort and reverse optional binary predicate ./util.py // helper functions for compiling with distutils A couple things to mention:- 1. make_callback.hpp isn't as tidy as make_list.hpp; it probably shouldn't even be necessary, but I needed something to help calling back into Python code, and this seemed to work. I wrote quite a nice template implementation using variadic templates, but the C++98 alternative isn't great. 2. I haven't (yet) exposed a good initialiser for the list classes, so it's not possible to do, e.g. >>> int_list = test_make_list.IntList([1, 2, 3]) Instead, I've been populating lists like this:- >>> int_list = test_make_list.IntList() >>> int_list[:] = [1, 2, 3] Obviously, that's not quite ideal. Any help or advice with making a templatised initialiser would be very welcome though! Kind regards, Alex