danomatika / swig-openframeworks

a SWIG interface for openFrameworks with included Makefile, submodule this in your language wrapper addons
Other
39 stars 11 forks source link

Callbacks #4

Closed chaosct closed 9 years ago

chaosct commented 9 years ago

I am trying to implement some sort of callbacks to dynamically call C++ code from the embedded Python ( chaosct/ofxPython#3 ) . I created the interface CallBack that specialized callbacks can implement. Then I extend its code in python to manage the possible arguments passed to the callback.

I tried hard to use a SWIG native way to convert C++ instances to their Python counterparts from C++, and failed miserably. So what I do now is to store the callback I want to pass in a global variable and then fetch it from Python code called from C++, using a SWIG-binded function so it converts to a Python instance. It's not pretty, but it works.

I created this pull request mainly to discuss the best way to implement callbacks, and to see if you have a better idea. You can see an example of how I use that in C++ here: https://github.com/chaosct/ofxPython/blob/feature_callbacks/example_Callbacks/src/ofApp.cpp

You can also see that I changed tabs for spaces. This is because the generated Python module uses spaces and didn't want to mix them with tabs and break everything.

danomatika commented 9 years ago

I'm not sure I understand. Is this for calling a Python function from C++? Lua has this built in so I didn't need to do anything SWIG specific. Does this pattern fit if we're trying to create a generic SWIG interface for OF or is it something that's better left to your ofxPython implementation?

Also, it might be good to separate out language specific stuff into .i files before the main openFrameworks.i get's too huge.

chaosct commented 9 years ago

Sorry, I didn't make myself clear. This is to pass a C++ function into Python in order to call it inside Python. I was coding this as language agnostic as possible in order to allow other languages to make use of it. I am unaware if Lua has this capacity already embedded.

danomatika commented 9 years ago

If you're calling a C++ function in Python, why not wrap it? Or are you going for a ease-of-use interface so users don't have to use SWIG directly? I'd rather stick with SWIG on that than maintain a custom implementation.

chaosct commented 9 years ago

It's for creating callbacks, if you want to notify custom code. If I download a file using Python and want to be notified in C++ whenever I have all the contents, I would pass a to the downloading function the pointer to the function I expect to be called afterwards.

Or, in my case, I want to run a Python gesture recognition engine where C++ classes can subscribe to the gestures being recognized. I need then a way to pass the pointer to the function I want the recognizer to call whehever a new gesture is available.

danomatika commented 9 years ago

Ah that makes more sense. I think this is a more Python-focused feature then ... or maybe I'm just not envisioning a need for that kind of functionality with many languages beyond Java or Python. :D aka languages with strong libraries that aren't only wrappers for C/C++ libs.

chaosct commented 9 years ago

So if you think that this is not something useful for Lua, I can put it in a separate interface file directly in the ofxPython addon. This way I can mature the things without adding noise in this repository.

If there is a need in the future to put it here we can always do it.

danomatika commented 9 years ago

Yeah, that's what I'm thinking. Actually, it might make sense to separate the language specific stuff into separate files anyway before it gets too huge.

chaosct commented 9 years ago

OK then.