Boost logo

Boost Users :

Subject: [Boost-users] return std::vector<> as boost::python::list
From: Andy Falanga (afalanga) (afalanga_at_[hidden])
Date: 2012-02-21 17:00:10


Hi everyone,

I'd originally worked out this issue by following the guidelines at the python wiki for exposing C++ containers into python. Basically, I made my vector act like a python list. This, however, created a problem for our teams build environment, specifically, that we didn't have python_d.lib to link with. This irritation cannot be tolerated in our environment so, I'm tasked with moving that vector to a boost::python::list object. However, all of my searches haven't returned much in terms of how to use this list object.

The vector is of a simple structure that doesn't have anything but primitive types. Because of this, returning the list by value isn't a problem. Could I simply make a wrapper around the function that returns the vector with function that returns the list? For example:

std::vector<myStruct>& GetVector() {
    // important stuff
    return vector;
}

boost::python::list GetPythonList(std::vector<myStruct>& structure) {
    boost::python::list boostPyList;
    for(int i(0); i < structure.size(); i++) {
        // copy each element into the python list
    }

    return boostPyList;
}

Would this work? Is it that simple?

Thanks,
Andy



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net