Skip to main content

QtWS15- Qt Value Class Design, Marc Mutz, KDAB

Qt World Summit 2015, OCTOBER 5-7, BERLIN, GERMANY Qt Value Class Design: Qt is well-known for its excellent higher-level APIs, from QtQuick/QML over QtWidgets to the very central QObject class hierarchy. Consequently, this is what training material has rightfully been focused on. But holding together the fabric of JavaScript and C++ higher-level APIs is a plethora of value classes, that is, classes that are are designed to behave like built-in types such as int. You use these classes every day in your Qt work: From QString, QByteArray, QVariant over QSize, QPoint, QRect and their -F variants, to the collection classes such as QVector, QStringList and QHash, and finally drawing classes such as QColor, QPen, QBrush, QPolygon and QPainterPath. And that's just scratching the surface of QtCore and QtGui. Unlike polymorphic classes, which are designed for inheritance and gently guide your subclass design by the knobs they expose (protected and virtual members, say), value classes have to be designed from scratch each time one is needed. At the same time, value classes tend to affect the performance of your programs in more profound ways than polymorphic classes ever could. Who hasn't started out passing QStrings around by value instead of reference-to-const, and then wondered why QAtomicInt functions top the list of call counts, or even time spent in performance profile data? Likewise, failure to introduce a value class over a tuple of values (say, two ints instead of a Point class) makes for hard-to use (for humans) and hard-to-optimize (for compilers) APIs. In this presentation, we'll take a deep dive into value class design, in particular, but not exclusively, from a performance point of view. You will learn how your compiler sees your code and how you can level the playing field again. But we'll also cover API design and binary compatibility issues. We'll start by classifying the different approaches used to write value classes and then present specific guidelines for each of these. You will learn how to make your value classes compact and fast, easy to use, and extensible. The presentation targets an audience with intermediate to expert Qt knowledge. Some familiarity with features new in C++11/14 is highly beneficial, too. In particular, we will not explain the Qt classes that we use, and will only very briefly describe new C++11/14 feature we may be using in the code. Beginners are welcome, too, but may want to read up on the above-mentioned topics to get the maximum out of this presentation.