Function pointers as an alternative to broadcasters & listeners


#1

Can anybody give opinions & advice in regards to using function pointers in a button subclass, then assign void functions to the buttons function pointer attribute, so when the button is clicked, it can call a method or friend function via the pointer, from within the buttons clicked method. I’m trying to use an alternative solution to JUCE broadcasters & listeners, as I’m more experienced with function pointers


#2

I often use C++11 std::function (and usually store C++11 lambdas into those) these days which is a better alternative to function pointers. However, like function pointers, that works well only for synchronous callbacks on the same thread. For example Juce’s ChangeBroadcaster can do asynchronous callbacks, also from other threads than the GUI/message thread. The Juce broadcaster/listener system also supports multiple listeners, which is not IMHO often needed, but still handy to have.


#3

JUCE’s mechanisms are implemented with function pointers “under the hood”.


#4

Thanks for your help sir, pleasure. By the way, I recommend Linux Ubuntu
16.04, and here’s a few interesting websites



Khronos (Vulkan API)


#5

Is it possible to assign friend functions or class methods to a listener,
e.g button::Listener, slider::listener etc. I’m not sure how the
broadcaster listener mechanism actually works. I mean, is it reminiscent to
signals & slots in Qt Creator ? Show some example code please

Thanks


#6

Is there some problem you are trying to solve? Why don’t you just use the Juce classes as they are intended to be used? You can do your custom stuff later, if needed.


#7

I think i just need to familiarize myself with the listener broadcaster
system


#8

It seems as though the JUCE mechanism typically uses conditional if
statements in the buttonClicked method of the component assigned as a
listener/observer. I’ve been made to understand this is necessary if GUI’s
contain several buttons/sliders etc. I’m trying to avoid having to use
conditional if branches, for efficiency, particularly if a GUI needs
several buttons (or other components). Function pointer arrays seems to be
a better more efficient solution, then use some kind of button component
array index (assuming it exists), or add such an index in button subclass.
I must avoid evaluating if statements just to discover which button has
been clicked, to invoke the function associated with a particular button.
Do you understand what i mean ?


#9

The user is never going to be able to press the buttons so fast your CPU couldn’t handle doing the conditionals. (You might obviously have some other reasons for avoiding the multiple conditionals in the buttonClicked handler, but efficiency is not going to be a good reason for that.)


#10

Cheers, but read this article, you might find it interesting, it
demonstrates exactly what i want to do, although rather complex and
implementation, reminiscent to Qt creators signal slot mechanism. The
article even suggests that JUCE should have event handling as standard.
This current broadcaster listener implementation isn’t flexible enough,
widgets to widgets is 1 thing, but professional designs require widgets to
desired object methods