On 01/26/2016 10:41 AM, tcak wrote:
> I need/want this class to be able to bind
> a function, a method, or a shared method. From the perspective of class
> design, there shouldn't be any
> difference. Its purpose is to let know about the event, not to care
> about how the event
> handler is designed.

If I understand the problem correctly, an interface can define the interface and a templated class can provide the differences:

import std.stdio;
import std.algorithm;

interface Event {
    void start();
    void stop();
    void itemAdded( size_t itemIndex );
}

class ConcreteEvent(alias onStart, alias onStop, alias onItemAdded) : Event {
    void start() {
        onStart();
    }

    void stop() {
        onStop();
    }

    void itemAdded(size_t itemIndex) {
        itemAdded(itemIndex);
    }
}

void fooStart() {
}

void fooStop() {
}

void fooItemAdded(size_t itemIndex) {
}

void bar(size_t itemIndex) {
}

void main() {
    Event[] events;
    events ~= new ConcreteEvent!(fooStart, fooStop, fooItemAdded);

    struct S {
        void memberFunction() {
        }
    }
    auto s = S();

    auto memberClosure(ref S s) {On 01/26/2016 10:41 AM, tcak wrote:
> I need/want this class to be able to bind
> a function, a method, or a shared method. From the perspective of class
> design, there shouldn't be any
> difference. Its purpose is to let know about the event, not to care
> about how the event
> handler is designed.

If I understand the problem correctly, an interface can define the interface and a templated class can provide differences:

import std.stdio;
import std.algorithm;

interface Event {
    void start();
    void stop();
    void itemAdded( size_t itemIndex );
}

class ConcreteEvent(alias onStart, alias onStop, alias onItemAdded) : Event {
    void start() {
        onStart();
    }

    void stop() {
        onStop();
    }

    void itemAdded(size_t itemIndex) {
        itemAdded(itemIndex);
    }
}

void fooStart() {
}

void fooStop() {
}

void fooItemAdded(size_t itemIndex) {
}

void bar(size_t itemIndex) {
}

void main() {
    Event[] events;
    events ~= new ConcreteEvent!(fooStart, fooStop, fooItemAdded);

    struct S {
        void memberFunction() {
        }
    }
    auto s = S();

    auto memberClosure(ref S s) {
        return () => s.memberFunction();
    }

    events ~= new ConcreteEvent!(() => memberClosure(s),
                                 () => writeln("stop"),
                                 bar);

    events.each!(e => e.stop);
}

Ali

        return () => s.memberFunction();
    }

    events ~= new ConcreteEvent!(() => memberClosure(s),
                                 () => writeln("stop"),
                                 bar);

    events.each!(e => e.stop);
}

Ali

Reply via email to