On 5/8/10 14:01, Justin Johansson wrote:
bearophile wrote:
Justin Johansson:
Whilst QtD looks like a cool project, my question was really about
whether D could achieve the same sorts of things that Qt does with
its meta-object compiler.

In other words, and calling a spade a spade, Qt is a "hack" to make
C++ more pleasant for GUI. Would it be possible for D to achieve
natively the same sorts that Qt does via preprocessing to C++.

I suggest you to explain what such things are, and to give some
examples too.

Bye,
bearophile

Okay, perhaps not everybody here is across Qt.

Programming in Qt is almost C++ bu not exactly C++ as Qt uses a
preprocessor to translate "enhanced C++" into "vanilla C++".

Here is an example that demonstrates that "Qt C++" is not "vanilla C++".

In Qt, one adorns class declarations with things that are not valid C++.

So in a class declaration you can say

class MyWidget
{
public:
// regular C++ stuff, attributes and methods

public slots:
// method declarations for intercepting event signals
};

You will observe that "public slots" is not valid C++. Qt's meta-object
compiler does a preprocessing of the MyWidget class so that methods
declared under the "public slots:" scope are subject to some kind of
reflection or introspection that enables events (signals) to be delegate
to the respective methods.

In doing so, according to my naive understanding, the Qt MOC
(meta-object compiler) compiles the MyWidget class retaining knowledge
of compile time analysis of the MyWidget class methods which are
designated as slots.

So this is some kind of black magic that the MOC does to Qt source code
in order to make event routing easier. The concepts of reflection come
to mind here. In a way the MOC processor must be keeping a lot of the
static analysis of classes around in similar fashion that Java does.

Accordingly it is fair to ask why does Qt do this? Obviously Qt
designers do not feel that vanilla C++ is adequate for cutting GUI apps
which is why they resort to a preprocessor.

Now I was thinking about why is C++ deficient for Qt to go to such
resorts. And if vanilla C++ is deficient for GUI programming, and given
that D is viewed by many as a "better C++", then can D do natively what
Qt has to do with a preprocessor?

I'm not an expert on Qt or its MOC (only 2 weeks into it). Perhaps
others on this NG might cotton on to what I'm trying to express and
chime in.

Does the simple example above help explain what I'm on about?

Cheers

Justin Johanssom

I've read the two links bearophile posted and I think that Qt signals and slots look similar to C# events. This can easily be implemented using delegates. Basically create a new template type (a struct for example) called Event. Event contains a data structure (a linked list for example) of delegates. Then you have a function that adds new delegates to the data structure and finally a function that calls all the delegates in the data structure.

This code is basically the same thing as the Counter example on Qt site.

class Counter
{
        int value_;
        Event!(void, int) valueChanged;

        void value (int value)
        {
                if (value != value_)
                {
                        value_ = value;
                        valueChanged(value);
                }
        }

        int value ()
        {
                return value_;
        }
}

auto a = new Counter;
auto b = new Counter;

a.valueChanged += &b.value; // I'm hoping this will choose the right method based on the signature that is needed

a.value = 12;
b.value = 48;

Am I missing something? This seems pretty simple. If some additional support is needed there is always compile time reflection in the form of __traits: http://www.digitalmars.com/d/2.0/traits.html and runtime reflection in the form of flectioned: http://www.dsource.org/projects/flectioned

/Jacob Carlborg

Reply via email to