[Qt-interest] Objective-C like message-passing

Peter Kümmel syntheticpp at gmx.net
Tue Sep 15 01:14:06 CEST 2009


One main feature of Objective-C/Smalltalk is their message-passing.
Using this mechanism all OOP is done on a dynamic typing and late-binding basis:
http://en.wikipedia.org/wiki/Objective-C
Very different here, is C++ with its compile time polymorphism.

Having Qt it isn't very hard to add Objective-C like message-passing to C++/Qt.

I've pushed such a extension to gitorious:
http://qt.gitorious.org/~syntheticpp/qt/syntheticpp/commits/message-passing


What is the best way to do such message-passing using C++?
First I had the idea to also use [] and to overload operator[] on QObject, but
this could break existing code where already operator[] is overloaded.
After several other ideas I ended here:

- passing a message to a QObject which doesn't takes arguments and returns nothing:

    obj->take("slotname")(); or

    qmsg("slotname").to(obj)(); or

    qmsg(obj, "slotname")();


- passing a message to a QObject which takes a argument and returns nothing:

    obj->take("slotname")(111); or

    qmsg("slotname").to(obj)(111); or

    qmsg(obj, "slotname")(111);


- passing a message to a QObject which takes a argument and returns a value:

    Type t = obj->take("slotname").reply<Type>(1111); or

    Type t = qmsg("slotname").to(obj).reply<Type>(1111); or

    Type t = qmsg(obj, "slotname").reply<Type>(1111);


I wonder if there are better ways to do these calls, any ideas?

Example code could be seen here:
http://qt.gitorious.org/~syntheticpp/qt/syntheticpp/blobs/message-passing/examples/messagepassing/simple/main.cpp

And typedefing QMetaMessage to qmsg allows it to use unpatched Qts by simply including
http://qt.gitorious.org/~syntheticpp/qt/syntheticpp/blobs/message-passing/src/corelib/kernel/qmetamessage.h



It is very interesting to have such non-C++ way of invoking methods on
objects where at runtime only is known that they are a QObjects (Qt's NSObject).
Using message-passing makes it possible 'think different' about architectures.
it's at the other end of compile-time type-checking.

It would also be interesting to implement more Objective-C like behavior with Qt.


Cheers,
Peter




More information about the Qt-interest-old mailing list