You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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?
The text was updated successfully, but these errors were encountered:
I've reach Fusion on about 10th page of a google search about C++ to C transpiler.
I've never thought about Fusion this way. The goal is being able to write code once and consume it from several other languages.
While Fusion helps produce object-oriented C code, it's just a side-effect.
In 2022, someone forked Fusion (then called Cito), so that it targets C only: see #76. That project seems to be long gone.
I think I understand the problem so let's restrict the scope to a Delegate that has one parameter and no return.
This doesn't buy much. Handling arbitrary function signatures is not a problem.
Is it doable?
Of course it's doable, but it's a major extension of the language.
As I explained in #144, abstract/virtual methods can be already used as a substitute for delegates. Java works this way for three decades.
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".
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?
The text was updated successfully, but these errors were encountered: