Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my embedded program I have a 5 "soft keys" arranged with a LCD screen.

What I would like is to have a pointer for each button that can point to free functions or member functions and can take any signature. This pointer will need to be redirected to other functions at runtime. Obviously I require some sort of delegate function, however all the delegate examples I have seen require knowledge of the function signature at compile time. Is a universal signature delegate possible for c++11?

I am open to suggestions for a better approach.

To demonstrate my ideal..

C++
Delegate *bd1;
Delegate *bd2;
Delegate *bd3;
Delegate *bd4;
Delegate *bd5;

//initial assignment of buttons
bd1 = del_CallMainMenu();
bd2 = SelectedWidget->del_AdjustWidgetUp();
bd3 = SelectedWidget->del_AdjustWidgetDn();
bd4 = nullptr;
bd5 = del_Shutdown();


//Softkey 1 is pressed...
void del_CallMainMenu()
{
 //setup buttons for menu operations
bd1 = Menu->del_GoBack();
bd2 = Menu->del_SelectUp();
bd3 = Menu->del_SelDn();
bd4 = Menu->del_Move();
bd5 = Menu->del_Select(); //<--another example of where delegate may be used to call particular routines

//Do other stuff
}


What I have tried:

After weeks of reading, I think I know the answer, and I think I wont like it. If this is the case I wouldn't mind alternative suggestions.
Posted
Updated 4-Feb-19 23:34pm
v2

There is no easy way around this, since the compiler needs to check the call matches the function signature. You could define each function to take a void* as its only parameter and then cast it internally to the correct structure. Or you could use the varargs option where you pass one fixed parameter (e.g the count of other items) and any number following. In such a case the function declaration needs to be something like:
C++
int myfunc(int p1, ...)
{
}

// and your calls can be
int rc = myfunc(1, foo);
int rc = myfunc(3, foo, 10, "some text");

which is how printf and its variants do it.
 
Share this answer
 
Comments
Timothy Hogan 9-Feb-19 19:11pm    
I feared as much. I will have to go with the varargs signature and maybe use some ugly switch statement. I really wanted to avoid casts Thanks for the reply.
As far as know, without considering 'dirty hacks' (trying to defeat the strongly typed nature of C++), you cannot do that.
I would use just one function signature and then adapt all of my functions to comply with it. Within this approach you might possibly find useful std::function[^].
 
Share this answer
 
Comments
Timothy Hogan 9-Feb-19 19:14pm    
Was trying to also avoid std. I would also like to keep it compliant. I presumed that a signature would be to only option. I was just hoping someone had come up with some amazing work around. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900