What Is The Cleanest Way to Call A Python Function From C++ with a SWIG Wrapped Object

后端 未结 3 552
Happy的楠姐
Happy的楠姐 2021-01-31 05:54

I have the following code, which implements a simple C++ class (ObjWithPyCallback) with a Python callback function. The idea is to call the Python function with \"this\" as the

3条回答
  •  伪装坚强ぢ
    2021-01-31 06:49

    I would use SWIG's mechanisms for handling inheritance and have a callback class with a virtual function void call(). Then you use SWIG to enable that class to be derived from in Python.

    In Python, you simply make sure that where the callback is set, you wrap it in an instance of a Python class derived from the C++ callback class, and make it's call member function execute the callback. That's also where you'd do the test to see if it's callable. Then you would call the setCallback function with this wrapper object.

提交回复
热议问题