Dynamic binding -- Qt's Signals and Slots vs Objective-C

Fredrik Olsson peylow at gmail.com
Tue Sep 26 01:01:45 PDT 2006


Bill Baxter skrev:
<snip>
> - Could Qt S&S be implemented using Obj-C messaging?
Easier then I thought actually. I did it just for fun, as a category on 
the root class NSObject so all classes get the functionality. And it 
turned out to require 174 SLOC (Source Lines Of Code), and then much is 
convenience code that is not really needed. A minimal implementation 
would easily go under 100 lines of code.

The message passing is already build in, so all I did was using some 
existing classes to emulate the Qt behavior. One for sets to store a set 
of target objects. And then a hash map to store a dictionary (with the 
instance as key) of such sets for each instance with connected signals.

Oh well, I attach the code, and an example using it. Compiles with Xcode 
2.2. Should work with with OSX 10.0 and up, and GNUStep as well.

> - Could Obj-C-like messaging be implemented using Qt S&S?
Stretching the truth one could say the the Qt S&S emitting are Obj-C 
like messaging, but with less optimization, only implemented for one 
special case, whereas Obj-C is made for the more general case. I see it 
as Obj-C messages with a C++ syntax.

> - Qt S&S includes an observer pattern in that emit() will call all 
> connected slots.  Is this built into Obj-C messaging too?
No, but as can be seen in the code I attach it can very easily be 
emulated. Just keep the targets in a collection instance such as NSSet, 
NSArray or NSDictionary, and you can use makeObjectsPerformSelector: to 
"call all".


// Fredrik Olsson
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Signals.zip
Type: application/zip
Size: 7609 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20060926/8b512b9f/attachment.zip>


More information about the Digitalmars-d mailing list