genCallback

Generate generic callback functions

Public Imports

std.traits
public import std.traits : Parameters, isImplicitlyConvertible;

Members

Functions

connect
void connect(bool delegate(T, _types) dg)
Undocumented in source. Be warned that the author may not have intended to support it.
dispatch
void dispatch(E event)
Undocumented in source. Be warned that the author may not have intended to support it.
emit
void emit()
Undocumented in source. Be warned that the author may not have intended to support it.
emit
void emit(E event)

in case the user wants to declare the event beforehand or if the user wants a callback only with sender and the event but the event doesn't have an empty ctor

Variables

callback
bool delegate(T, _types) callback;
Undocumented in source.

Parameters

T

class which sends the signal

E

event to look for

args

E var types and E var names

Examples

@EventType("ClickEvent")
class ClickEvent : Event
{
    this(in size_t x, in size_t y)
    {
        this.x = x;
        this.y = y;
    }
    mixin basicEventType!ClickEvent;
private :
    const size_t x, y;
}

class Foo
{
    mixin genCallback!(const Foo, ClickEvent, const size_t, "x", const size_t, "y") onClicked;
    // Note: you can leave the parameters blank, if you do, then the event
    //     itself will be passed as the second parameter as const
public :
    void click(in size_t x, in size_t y)
    {
        // click stuff here
        onClicked.emit(x, y);
    }

    void onEvent(Event event)
    {
        // handle your events here
        auto dispacher = scoped!EventDispatcher(event);
        dispacher.dispatch!ClickEvent(&onClicked.dispatch);
    }
}

bool onFooClicked(in Foo sender, in size_t x, in size_t y)
{
    // do stuff here
    assert([x, y] == [4, 5]);

    // true to handle the event
    // false to propagate
    return true;
}

void main()
{
    Foo foo = new Foo();
    foo.onClicked.connect(toDelegate(&onFooClicked));
    foo.click(4,5); // callback!
}

Meta