11.4.  Function Pointers and Functors

[ fromfile: functors.xml id: functors ]

Example 11.12. src/functors/pointers/main.cpp

#include <QtGui>

QString name() {
    return QString("Alan");
}

typedef QString (*funcPtr)();               1
Q_DECLARE_METATYPE(funcPtr);                2

int main() {
    qRegisterMetaType<funcPtr>("funcPtr");  3
    funcPtr ptr = name;                     4

    QString v = (*ptr)();                   5
    qDebug() << v << endl;                  6
}

1

A function that returns QString and takes no args.

2

Declare, so we can use in QVariant.

3

Register, so we can use in queued signal parameters.

4

Function names evaluate to pointers to functions.

5

Invoke a method by dereferencing function ptr.

6

Prints "Alan"


Example 11.13. src/functors/operators/functors.h

[ . . . . ]
class Load : public std::unary_function<QString, QImage> {         1
public:
    QImage operator() (const QString& imageFileName) const {
        return QImage(imageFileName);
    }
};
class Scale {
    public:
    typedef QImage result_type;                                    2
    QImage operator() (QImage image) const {
        for (int i=0; i<10; ++i) {
            QImage copy = image.copy();
            copy.scaled(100, 100, Qt::KeepAspectRatio);
        }
        if (image.width() < image.height()) {
            return image.scaledToHeight(imageSize,
                                        Qt::SmoothTransformation);
        }
        else {
            return image.scaledToWidth(imageSize, 
                                       Qt::SmoothTransformation);
        }
    }
};
class LoadAndScale : public std::unary_function<QString, QImage> { 3
public:
    Scale scale;
    QImage operator() (const QString& imageFileName) const {
        QImage image(imageFileName);
        return scale(image);
    }
};
[ . . . . ]

1

Defines result_type.

2

A trait required for functor objects.

3

Also defines result_type.


Example 11.14. src/functors/operators/imagefunctor.cpp

[ . . . . ]

        connect(m_futureWatcher, SIGNAL(progressValueChanged(int)),
                this, SIGNAL(progressCurrent(int)));
        emit statusMessage("Loading and Transforming in parallel");
        m_futureWatcher->setFuture(QtConcurrent::mapped(files,
                                                  LoadAndScale()));



[55] C++ Technical Report 1 (TR1) is a draft document containing proposed additions to the C++ Standard Library that are likely to be included in the next official standard. See this Wikipedia article for more information.