|
Boost Users : |
From: alex_at_[hidden]
Date: 2007-08-15 22:27:25
Hello,
I am new in this list. I would like to ask a quesiton about the linkage
between BOOST and Python. I want to use C++ to trigger a Python code so that
when I change anything inside Python, my C++ can know the changes and get the
result back.
So how can I pass an object from C++ to Python? and return the result?
Here is the code that embedded the Class.
class CppClass {
public:
int getNum() {
return 7;
}
};
#include <boost/python.hpp>
using namespace boost::python;
int main( int argc, char ** argv ) {
try {
Py_Initialize();
object main_module((
handle<>(borrowed(PyImport_AddModule("__main__")))));
object main_namespace = main_module.attr("__dict__");
main_namespace["CppClass"] = class_<CppClass>("CppClass")
.def("getNum",&CppClass::getNum);
handle<> ignored(( PyRun_String( ""cpp = CppClass()\n"
"print cpp.getNum()\n"",
Py_file_input,
main_namespace.ptr(),
main_namespace.ptr() ) ));
} catch( error_already_set ) {
PyErr_Print();
}
}
Thanks
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