[C++-sig] accessing functions at C++ level from one boost.python module in another

Faheem Mitha faheem at email.unc.edu
Sun Oct 10 22:43:55 CEST 2004


Dear People,

I've had modest success using Boost.Python recently. I have however found 
I need to do something for which I am not sure of the best way.

Suppose I have Boost.Python module foo and another one bar. Note that
above code is merely intended as an example/illustration, and may have
errors and/or missing details.

*******************************************************
foo.cc
*******************************************************
#include <boost/python.hpp>

static int state = 0;

void changestate(boost::python::object num)
{
int a = extract<int>(num);
state += a;
}

BOOST_PYTHON_MODULE(foo)
{
def("changestate",changestate);
}
********************************************************

********************************************************
bar.cc
********************************************************
#include <boost/python.hpp>

void accessstate(boost::python::object incr)
{
int a = extract<int>(incr);

// Want to do something like

changestate(a);

// The question is, how do I get my hands on changestate?
}

BOOST_PYTHON_MODULE(bar)
{
def("accessstate", accessstate);
}
**********************************************************

Suppose I load foo into Python. It seems plausible that the function
changestate should be then somehow available for use (with the right
incantation) inside another module bar.

So I can then access the static int state using the function
changestate in bar's C++ code.

Is this possible? If so, how exactly? Actual code would be most helpful.

Please cc me. I'm not subscribed. Thanks.            Faheem.



More information about the Cplusplus-sig mailing list