| Home · All Classes · All Namespaces · Modules · Functions · Files

Callbacks Usage

## Overview

Callbacks are used in Telepathy-Qt by the service side high-level APIs to expose methods that may/should be overriden in implementations.

Ideally we would use virtual methods for this, but as new methods can be added freely (when interfaces change), we would not be able to guarantee a stable API/ABI. Other options, such as virtual padding, virtual_hook and Qt slots also have their own drawbacks.

There are 8 Callback classes, Tp::Callback0 to Tp::Callback7, which define a callback with 0 to 7 arguments respectively. The first template argument is always the return value type and the rest template arguments are the types of the callback arguments in the order that they are passed to the callback.

Callback classes can be constructed from a functor. To make it easy to use function pointers as functors, Telepathy-Qt also provides two helper functions, Tp::memFun and Tp::ptrFun.

Here is an example of their usage:

     // assuming a member function QString MyImpl::myFunc(const QString & s, int i);
     Tp::Callback2<QString, const QString &, int> cb = Tp::memFun(myObj, &MyImpl::myFunc);

     // assuming a non-member or static member function QString myFunc(const QString & s, int i);
     Tp::Callback2<QString, const QString &, int> cb = Tp::ptrFun(&myFunc);

     // assuming Tp::BaseConnectionPtr MyProtocolImpl::createConnection(const QVariantMap &parameters, DBusError *error);
     myBaseProtocol->setCreateConnectionCallback(Tp::memFun(myProtocolImpl, &MyProtocolImpl::createConnection));

You are also free to use any other mechanism for constructing functors, such as boost::bind, C++11's <functional> module or even C++11 lambda functions.