#include <boost/python.hpp>
class MyClass
{
public:
MyClass() = default;
void method1();
void method2();
void executePython();
};
#include <iostream>
void MyClass::method1()
{
std::cout << "Method 1 called" << std::endl;
}
void MyClass::method2()
{
std::cout << "Method 2 called" << std::endl;
}
void MyClass::executePython()
{
try
{
Py_Initialize();
// Expose the current instance (self) to the embedded Python script
boost::python::object main_namespace;
main_namespace["self"] = boost::python::ptr(this);
// Execute Python code that calls method1 and method2 on self
boost::python::exec("self.method1()\nself.method2()", main_namespace);
Py_Finalize();
}
catch (boost::python::error_already_set&)
{
PyErr_Print();
}
}
int main()
{
MyClass mc;
mc.executePython();
return 0;
}