Thin Templates

Symbian platform C++ uses C++ templates in an efficient way called the thin template idiom.

Templates are particularly useful for collection classes, such as arrays. The class declaration:

class CArrayFixFlat<T> ...

specifies a family of array classes, which may contain any type. The template may be instantiated, for instance, as

CArrayFixFlat<CCoeControl*>* iControls;

which declares an array of CCoeControl* pointers. Other collection classes use the same idea, for instance TPckgBuf<T>, TSglQue<T> etc.

Templates may also use other parameters, such as an integer:

class TBufC<TInt S> ...;

This type of template class may be instantiated also:

TBufC<20> name; 

The functionality provided by templates is powerful, and highly desirable. Without templates, collection classes usually use void* pointers: as a result, they are not typesafe.

Templates can be difficult to manage. A template really defines a whole family of classes. Each member of that family that is instantiated requires its own object code. Avoidance of object code duplication is a difficult issue in a C++ implementation. For the Symbian platform, object code duplication must be avoided at all costs. The solution used is the thin template idiom.

The thin template idiom begins with a base class, code in terms of TAny* parameters:

class CArrayFixBase ... {
    IMPORT_C const TAny* At(TInt aIndex) const;

This base class has the real code, just once. The code resides in a single DLL and is exported from that DLL. The base class may be fat: it may contain an arbitrary amount of code.

Then, a derived class is used as follows:

class CArrayFix<T> : public CArrayFixBase {
    inline const T& At(TInt aIndex) const
    {return(*((const T *)CArrayFixBase::At(anIndex)));}

Because this class uses only inline functions, it generates no extra code. But because the casting is encapsulated in the inline function, the class is typesafe to its users. The templated class is thin: it generates no code at all.