I have an application written in C++ and a testing system (also in C++). Testing system is pretty complicated and hard to change (I want to make only small changes). My class looks like this:
class Derived : public Base {
public:
void somefunc(const AnotherClass& file) {
}
};
There are several functions inside. My testing system creates Derived class instance and then uses it's methods to do some stuff.
Now I want to be able to write a solution in Python. I need a two-way integration. My idea is to write Python function, which will be executed every time when somefunc
is called. And I don't want to lose variables' values in Python from one launch of function to another. And I also want to be able to use methods which are defined in the Base class instance from python. How can I achieve these things?
I chose Boost.Python for these purposes. For now, I understand, how to use c++ function, or even simple class in Python after some work. But I don't understand how to launch Python function from c++.
The second question - is Boost.Python a good choice? I need something very fast and, at the same time, easy to use.
Thank you for your help.
I would recommend using Cython for this sort of thing. Adapted examples from another question. (Edit: Upon request, I added an extended example that wraps a C++ class, see further below.)
Edit: Simple example, one way (C++ -> Python).
quacker.py:
cquacker.pyx:
main.cpp:
Edit: Extended example, round trip (C++ -> Python -> C++).
quacker.py:
quacker/Duck.hpp
cquacker_defs.pxd:
cquacker.pyx:
main.cpp: