openframeworks / openFrameworks

openFrameworks is a community-developed cross platform toolkit for creative coding in C++.
http://openframeworks.cc
Other
9.98k stars 2.55k forks source link

Document addListener callback types and add generic callback #4399

Open traumedy opened 9 years ago

traumedy commented 9 years ago

For instance, calling ofxGui addListener method with the incorrect callback function type results in a cryptic error message with VS, complains about pass by reference vs by value parameter in the template for ofEvent, but what it is really complaining about is that the callback function supplied doesn't accept expected parameters (I'm guessing some issue with template abstraction?). It took me a while to figure out that callbacks for a type must accept a reference to a variable of that type, but then I saw this post: http://forum.openframeworks.cc/t/dynamic-buttons/13406 Which implies that you can supply other types of callback functions that also receive context regarding the source of the callback. If this is the case, couldn't it also be possible to provide a callback with no parameters at all so that for instance ANY ofxGui control addListener call could call back to the same function that expects no parameters, like an ofxButton does? Like if you just want to know if ANY value changed regardless of the type or new value? Is this more about ofParameter?

bilderbuchi commented 9 years ago

ping @arturoc

arturoc commented 9 years ago

the events from the gui come from an ofParameter and as with any other ofEvent the method needs to receive a reference. the error (surprisingly for a templated function in c++) sounds correct to me, you were trying to make a method which receives a parameter by value were it was expecting a parameter by reference.

not sure that adding more possible signatures to ofEvents is a good idea but we can discuss about it.

in any case this PR: https://github.com/openframeworks/openFrameworks/pull/4392 will allow to add any std::function which in principle will allow to add any function, no matter it's signature, as a listener.