Passing a C ++ object to C ++ - code through Python?

I wrote some physics modeling code in C ++, and parsing input text files is a bottleneck. As one of the input parameters, the user must specify a mathematical function that will be evaluated many times at runtime. C ++ code has some predefined function classes for this (they are actually quite complicated on the mathematical side) and some limited parsing capabilities, but I am not satisfied with this construction at all.

I need both the algorithm and the function evaluation to remain fast, so it is advisable to save them as compiled code (and, preferably, mathematical functions as objects of C ++ functions). However, I was thinking of gluing the whole simulation together with Python: the user could specify the input parameters in the Python script, as well as implement storage, visualization of the results (matplotlib) and the GUI in Python too.

I know that most of the time you can expose C ++ classes, for example. with SWIG, but I still have a question regarding parsing a user-defined math function in Python:

Is it possible to somehow build a C ++ function object in Python and pass it to the C ++ algorithm? For example. when i call

f = WrappedCPPGaussianFunctionClass(sigma=0.5) WrappedCPPAlgorithm(f) 

in Python, it will return a pointer to a C ++ object, which will then be passed to a C ++ routine that requires such a pointer, or something like that ... (don't ask me about memory management in this case: S)

The thing is, no callback is required in the algorithm for Python . Later, I would like to expand this example to also do simple parsing on the Python side, such as a sum or a product of functions, and return some kind of parsing, such as a C ++ object, but now stay on the basics.

Sorry for the long post and thanks for the suggestions in advance.

+6
c ++ python functor wrapping
source share
1 answer

I am doing the same thing as all this time. The simplest solution that I usually choose is because if nothing else, I'm lazy to smooth your API to a C-like API and then just pass pointers to and from Python (or your other choice language).

Create your classes first

 class MyFunctionClass { public: MyFunctionClass(int Param) ... }; class MyAlgorithmClass { public: MyAlgorithmClass(myfunctionclass& Func) ... }; 

Then create api for functions that create and destroy these classes. I usually leaned in to go void * around, because the languages ​​I use do not in any way preserve type safety. It is just easier. Just be sure to return the correct type before using void *

  void* CreateFunction(int Param) { return new MyFunctionClass(Param); } void DeleteFunction(void* pFunc) { if (pFunc) delete (MyFunctionClass*)pFunc; } void* CreateAlgorithm(void* pFunc) { return new MyAlgorithmClass(*(MyFunctionClass*)pFunc) } void DelteAlgorithm(void* pAlg) { if (pAlg) delete (MyAlgorithmClass*)pAlg; } 

No, all you have to do is get python to call this C-style function. In fact, they can (and probably should) be external c functions to make the link much easier.

+3
source share

All Articles