1

I have wrapped class in c++ and expose to python and when I call function member it work, problem is that I cannot access data (public member result in class Path has type std::list

BOOST_PYTHON_MODULE(map) // module name
{
     class_<Path>("Path")
        .def_readonly("result", &Path::result)
        .def("findPathFromTo", &Path::findPathFromTo);

     class_<Position>("Position")
        .def(init<float, float, float>())
        .def_readwrite("x", &Position::x)
        .def_readwrite("y", &Position::y)
        .def_readwrite("z", &Position::z);
};

and I call like

import map
path = map.Path()
path.findPathFromTo(2, 3, 34 ,34)
path.resultPath

last line throws exception

TypeError: No Python class registered for C++ class std::list<Position, std::allocator<Position> >
PaolaJ.
  • 10,872
  • 22
  • 73
  • 111
  • 1
    See [wrapping a list of structs with boost.python](http://stackoverflow.com/q/6776888/222914) – Janne Karila Dec 17 '13 at 11:01
  • Also worth reading: http://stackoverflow.com/questions/6157409/stdvector-to-boostpythonlist –  Dec 17 '13 at 11:04

1 Answers1

2

Since you are returning a list, you should expose it:

class_<std::list<Position>>("list_position")
    .def("__iter__", iterator<std::list<Position> >())
;

Then you can iterate over the elements:

import map
path = map.Path()
path.findPathFromTo(2, 3, 34 ,34)
for p in path.resultPath: # is resultPath a std::list<Position>? I'm supposing YES.
    print p.x, p.y, p.z

As you can notice in the link that @janne-karila pointed out, it's possible improve the way python interacts with std::list, providing methods to clear, append, remove, etc, elements of the list. In the case you want to access the elements using an index, I would like to suggest you to use a std::vector (it's just a suggestion, not sure if it is suitable for your needs).

Hugo Corrá
  • 14,546
  • 3
  • 27
  • 39