I have a C++ object and wish to pass it by reference/pointer to an embedded Python interpreter so that Python can make changes to it which are visible to both C++ and Python. I am using Boost.Python for interop.
For sake of example, here is a class Foo, its Boost.Python wrapper, and a Foo object in C++:
mymodule.h:
struct Foo
{
int bar;
Foo(int num): bar(num) {}
};
mymodule.cpp:
#include "mymodule.h"
#include <boost/python.hpp>
BOOST_PYTHON_MODULE(mymodule)
{
using namespace boost::python;
class_<Foo>("Foo", init<int>())
.def_readwrite("bar", &Foo::bar)
;
}
main.cpp:
#include <iostream>
#include <boost/python.hpp>
#include "mymodule.h"
using namespace boost::python;
int main()
{
Py_Initialize();
object main_module = import("__main__");
object main_namespace = main_module.attr("__dict__");
import("mymodule");
Foo foo(42);
// ... somehow use Boost.Python to send reference to interpreter ...
object result = exec_file("myscript.py", main_namespace);
std::cout << foo.bar << std::endl;
}
So how do I send a reference of foo to Python so both C++ and Python can see its contents?