views:

347

answers:

1

Hello,

I made some bindings from my C++ app for python.

The problem is that I use pointers to members (It's for computing shortest path and giving the property to minimize as parameter).

This is the C++ signature:

std::vector<Path> martins(int start, int dest, MultimodalGraph & g, float Edge::*)

This is what I did (from what I understood in the doc):

%constant float Edge::* distance = &Edge::distance;

This is how I call my function from python:

foo.martins(0, 1000, g, foo.distance)

And this is the error I get:

NotImplementedError: Wrong number of arguments for overloaded function 'martins'.
Possible C/C++ prototypes are:
martins(int,int,MultimodalGraph &,float Edge::*)

I have an overloaded method that uses a default 4th paramaters, and it works perfectly.

So is it possible to use pointers to members with swig? If yes, what's the trick? If no, what would be the most elegant way to work arround?

Thank you for your help!

UPDATE: if someone knows if Boost::python does it for sure, I'll switch to it.

+2  A: 

Don't know about SWIG, but in boost::python you can write a wrapper:

bool foo(int x, float* result);

boost::python::tuple foo_wrapper(int x)
{
    float v;
    bool result = foo(x, &v);
    return boost::python::make_tuple(result, v);
}

BOOST_PYTHON_MODULE(foomodule)
{
    def("foo", &foo_wrapper);
}

And in python you would:

result, v = foomodule.foo(x)

Since in Python floats are immutable, you can't actually pass one to a method and expect the method to change the float value, so we adapt the code to return a tuple of values instead.

Bruno Oliveira
Tristram Gräbener
Bruno Oliveira
That's what I did. But I'm not satisfied. The big idea is to have a graph with various costs and I want an algorithm to run with the cost passed as a parameter (for every edge, the algorithm will pick the good variable according to the parameters).I think I was too ambitious. Most likely python is too low level and doesn't allow the abstraction that offers C++ with templates and member pointers.I still accept the answer. Thanks for the effort :)
Tristram Gräbener