Suggestion: signal/slot mechanism

Kristian kjkilpi at gmail.com
Sun Sep 3 09:17:28 PDT 2006


On Sun, 03 Sep 2006 01:41:55 +0300, Brad Anderson <brad at dsource.org> wrote:
> Kristian wrote:
>> It would be nice if D had a signal/slot mechanism similiar to Qt:
>> http://doc.trolltech.com/4.1/signalsandslots.html
>>
>> It's an elegant way to handle messages sent between objects. It beats
>> event table declarations used by other GUI libraries IMHO.
>>
>> It would make D a lot more appealing language to write GUI applications.
>> Think of wxWidgets written in D... ;)
>>
>>
>> I think it would be quite simple to build a S/S support for a compiler
>> (at first glance, at least). For example:
>>
>> The 'Object' class has a pointer to S/S data (it's null if the object
>> don't currently use signals/slots). S/S data holds a slot list for each
>> signal. It also holds a list of objects that have slot(s) connected to
>> this object's signal(s). This list is used to disconnect necessary slots
>> at a destruction of the object.
>>
>> When the compiler reads a 'emit X' statement, it will do two things.
>> First, it generates an id for the signal which is used to retrieve a
>> correct slot list.
>>
>> Second, the compiler puts the signal's parameters to the stack as it
>> would call a corresponding function. Instead, the
>> 'Object._emit_signal(id)' function (or something) is called, where 'id'
>> is the generated id. (Note that there are no function bodies for
>> signals.) '_emit_signal()' retrieves the correct slot list, and calls
>> all the slots (delegates) in it. Finally the parameters are removed from
>> the stack.
>>
>> Of course, slots should not modify their parameters so that all the
>> slots will receive the same parameter values. Hence slots should not use
>> the 'out type'. There is a market for a 'const type' here... *wink*
>>
>>
>> Maybe there should be no slot keyword at all as there is in Qt. You
>> don't need to declare a function to be a slot; all the (virtual)
>> functions can be used with signals.
>>
>> Because the return values of all the signals are void, the void typeword
>> could be removed from signal declarations.
>>
>> signal clicked();
>>
>> signals:
>> clicked();
>> clicked(int button);
>>
>>
>> BTW, Qt generates ids for signals as follows:
>>
>> signals:
>> void clicked(int button, bool isMoved);
>> -> the id is a string "clicked(int,bool)"
>
> Not terribly active recently, but the SVN repos here may have some stuff  
> for you.
>
> http://svn.dsource.org/projects/dcouple/
>
> BA


Thanks. I like dcouple's syntax. (There seem to be some problems with GC  
and object deletion currently though.) Being a library, you have to create  
signal and slot objects in constructors, which is a downside (when  
compared against S/S mechanism build into D).



More information about the Digitalmars-d mailing list