-
Notifications
You must be signed in to change notification settings - Fork 60
Description
I have to improve the functionality of a small microcontroller for which exists only a C compiler.
There are almost 90 different types of events all treated in many switch/cases with many state machines in a giant pile of spaghetti code.
For example, from a button:
Click
DoubleClick
MultipleClick
ShortPress (same as Click)
LongPress
VeryLongPress
ExtremelyExtremelyVeryVeryLongPress (just kidding with this one :) )
and with 2 buttons it can create combinations; and then there are sensors and so on.
I've reach Fusion on about 10th page of a google search about C++ to C transpiler.
I've been toying with it for a couple of days. It is impressive it seems almost perfect for what I need - however while it will help me organize the code very well, I think the missing event/delegate will also drive me crazy trying to work around.
I have read the previous topic "Allow delegates / anonymous functions".
A delegate is not only a code reference (function pointer), but also an object reference. In C# you can make delegates to both static and instance methods. How would you map that to C? How about references to abstract/virtual methods?
I think I understand the problem so let's restrict the scope to a Delegate that has one parameter and no return.
The parameter will be a DelegateArgs class which can be extended as needed.
For C Delegate should hold a pointer to the function/method and a pointer to the struct (target); if target is null then the method is static.
The pointer for the method is taken directly from the vTable of the target (since target is already instantiated then there are no pure methods).
Now the implementation in C for Invoke with DelegateArgs is possible.
It is not as fancy as having multiple parameters, but the end functionality will be the same by extending DelegateArgs.
Is it doable?