On 12/14/09 20:29, Jacob Carlborg wrote:
On 12/13/09 20:44, Eldar Insafutdinov wrote:
Nick Sabalausky Wrote:

"Nick Sabalausky"<a...@a.a> wrote in message
news:hg394f$mr...@digitalmars.com...
"Eldar Insafutdinov"<e.insafutdi...@gmail.com> wrote in message
news:hg2p87$2tt...@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, ...).



Oops, slight typo:

Try string mixins, something like this:

1. Rename Slot to _Slot.

2. Do this:


// Fixed:
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;
}





Thank you for suggestion, but while reducing redundancy it became even
uglier! I'll better stay with the current syntax.

To make it look a little better you can do like this:

template Slot (ReturnType, alias method, ARGS...)
{
// code
}

template Slot (alias method)
{
mixin Slot!(ReturnType!(method), method, ParameterTypeTuple!(method));
}

If the method is overloaded, use the first version, otherwise use the
second. Use them like this:

class Test : QObject
{
mixin Slot!(slot_1);
void slot_1(string str)
{
writefln("slot_1 invoked with ~ " str);
}

mixin Q_OBJECT;
}

ReturnType and ParameterTypeTuple are available in std.traits, in Tango they're in tango.core.Traits under slightly different names.

Reply via email to