D2 GUI Libs

Nick Sabalausky a at a.a
Sun Dec 13 09:42:32 PST 2009


"Eldar Insafutdinov" <e.insafutdinov at gmail.com> wrote in message 
news:hg2p87$2ttj$1 at digitalmars.com...
>
> 1) You may know the concept of signals and slots in Qt. Consider the 
> following snippet:
>
> class Test : QObject
> {
>    mixin Signal!("signal_1(string)");
>
>    mixin Slot!("slot_1(string str)");
>    void slot_1(string str)
>    {
>        writefln("slot_1 invoked with ~ " str);
>    }
>
>    mixin Q_OBJECT;
> }
>
> In Qt objects, that inherit from QObject are capable of having signals and 
> slots and participate in connections. In the current scheme mixins of 
> Signal and Slot template mix some static information into the class. The 
> class is then scanned when Q_OBJECT template is mixed in and the proper 
> meta-information is generated to register signals and slots in the Qt type 
> system.
> As you see, this declaration is a bit ugly. In particular defining a slot 
> requires to duplicate its signature in a mixin. What would really be 
> awesome is a mechanism allowing something like:
>
>    @Slot
>    void slot_1(string str)
>    {
>        writefln("slot_1 invoked with " ~ str);
>    }
> I.e we need annotations. But I am not sure how this will work. One of the 
> possible solutions will be that @Slot expands into mixin Slot!(symbol, 
> AnnotationArg1, ...).
>

Try string mixins, something like this:

1. Rename Slot to _Slot.

2. Do this:

template Slot(string decl, string body)
{
    const string Slot = "
        mixin Slot!("~decl.stringof~");
        void slot_1(string str)
        {
            "~body.stringof~"
        }
    ";
}

class Test : QObject
{
    mixin Signal!("signal_1(string)");

    mixin( Slot!("slot_1(string str)", q{
        writefln("slot_1 invoked with ~ " str);
    }) );

   mixin Q_OBJECT;
}





More information about the Digitalmars-d mailing list