Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Events/Delegates - another try #188

Open
allex4project opened this issue Mar 18, 2025 · 1 comment
Open

Events/Delegates - another try #188

allex4project opened this issue Mar 18, 2025 · 1 comment
Labels
enhancement New feature or request

Comments

@allex4project
Copy link

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?

@pfusik pfusik added the enhancement New feature or request label Mar 18, 2025
@pfusik
Copy link
Collaborator

pfusik commented Mar 22, 2025

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.

In your scenario, you can have:

public abstract class Button
{
    ...
    public abstract void Click(ClickEvent event);
    public abstract void DoubleClick(ClickEvent event);
}

or

public abstract class ClickHandler
{
    public abstract void Click(ClickEvent event);
}

public abstract class DoubleClickHandler
{
    public abstract void DoubleClick(ClickEvent event);
}
...

or

public abstract class ClickHandler
{
    public abstract void Click(ClickEvent event);
    public virtual void DoubleClick(ClickEvent event)
    {
        // default implementation, perhaps empty
    }
    ...
}

or even

public enum ButtonEventType
{
    Click,
    DoubleClick,
    ...
}

public abstract class ClickHandler
{
    public abstract void Click(ButtonEventType type, ClickEvent event);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants