Thin Template Explained

50 %
50 %
Information about Thin Template Explained

Published on May 6, 2008

Author: som.mukhopadhyay

Source: slideshare.net

Description

Through these slides I tried to describe the thin template concepts to my juniors

Thin Template By Somenath Mukhopadhyay [email_address]

What is a Template Compile time polymorphism Use of a single declaration for multiple types provided the logic is the same for all of the supported types

Compile time polymorphism

Use of a single declaration for multiple types provided the logic is the same for all of the supported types

Example of Template Declaration: Template <class T> T Add (T& a, T& b)‏ { Return a + b; } Usage: Int x = T<int> Add (1 , 2)‏ Float y = T<float> Add (1.1 , 2.2)

Declaration:

Template <class T> T Add (T& a, T& b)‏

{

Return a + b;

}

Usage:

Int x = T<int> Add (1 , 2)‏

Float y = T<float> Add (1.1 , 2.2)

Disadvantage of standard template For each type it will produce one set of Add function Large code foot print For devices having small amount of memory, it is not acceptable

For each type it will produce one set of Add function

Large code foot print

For devices having small amount of memory, it is not acceptable

Thin Template in the rescue It is based upon the assumption that most optimized compilers will generate the code only for template functions that actually use different types. Example Template <class T> class CTest { int Test1(void) { return 1;} T Test2(void) { return 1;} };

It is based upon the assumption that most optimized compilers will generate the code only for template functions that actually use different types.

Example

Template <class T> class CTest

{

int Test1(void) { return 1;}

T Test2(void) { return 1;}

};

Thin Template in the rescue CTest < char> oClass1; CTest <int> oClass2; Only one code will be generated for function Test1(), because this function doesn’t use template argument T and contains the same code in both versions However for the function Test2(), two versions will be generated for the two different types, one for char and the other for int. This is because this function uses argument T as a return type

CTest < char> oClass1;

CTest <int> oClass2;

Only one code will be generated for function Test1(), because this function doesn’t use template argument T and contains the same code in both versions

However for the function Test2(), two versions will be generated for the two different types, one for char and the other for int. This is because this function uses argument T as a return type

Thin Template implementation logic Most modern compilers like Microsoft C++ compiler or GCC have this capability. The main idea behind “thin template” is to move all code that does not use template arguments into separate functions, so that only one instance will be generated for them. To implement thin template, we usually have a Base Class containing all the template-independent functionalities; and a derived template class from it containing template-dependent structures and code.

Most modern compilers like Microsoft C++ compiler or GCC have this capability.

The main idea behind “thin template” is to move all code that does not use template arguments into separate functions, so that only one instance will be generated for them.

To implement thin template, we usually have a Base Class containing all the template-independent functionalities; and a derived template class from it containing template-dependent structures and code.

Thin Template Example We will define a template class CList CList is derived from a non-template class CListThin CList class has a template function called Add, which will add an item to the end of the list This Add function will in turn take the help of the CListThin's non-template function AddThin. Thus the boilerplate code of moving the pointer to the end of the list has been implemented by the non-template function AddThin and the actual adding of the template data has been implemented by the template function Add.

We will define a template class CList

CList is derived from a non-template class CListThin

CList class has a template function called Add, which will add an item to the end of the list

This Add function will in turn take the help of the CListThin's non-template function AddThin.

Thus the boilerplate code of moving the pointer to the end of the list has been implemented by the non-template function AddThin and the actual adding of the template data has been implemented by the template function Add.

Thin Template Example This is shown in the following class diagram

This is shown in the following class diagram

Thin Template Example CListThin Class definition class CListThin { protected: typedef struct SThinItem { SThinItem* prev; SThinItem* next; }; public: CListThin():iFirstItem(0),iLastItem(0){}; void AddThin(SThinItem& aDestData); private: SThinItem* iFirstItem; SThinItem* iLastItem; };

CListThin Class definition

class CListThin

{

protected:

typedef struct SThinItem

{

SThinItem* prev;

SThinItem* next;

};

public:

CListThin():iFirstItem(0),iLastItem(0){};

void AddThin(SThinItem& aDestData);

private:

SThinItem* iFirstItem;

SThinItem* iLastItem;

};

Thin Template Example CListThin class implementation void CListThin::AddThin(SThinItem &aDestData)‏ { aDestData.prev = iLastItem ? iLastItem : 0; aDestData.next = 0; if(iLastItem) iLastItem->next = &aDestData; iLastItem = &aDestData; if(!iFirstItem) iFirstItem = &aDestData; }

CListThin class implementation

void CListThin::AddThin(SThinItem &aDestData)‏

{

aDestData.prev = iLastItem ? iLastItem : 0;

aDestData.next = 0;

if(iLastItem) iLastItem->next = &aDestData;

iLastItem = &aDestData;

if(!iFirstItem) iFirstItem = &aDestData;

}

Thin Template Example CList Class Implementation template <class T> class CList : public CListThin { public: CList(){} bool Add(T& aDestData); private: typedef struct SItem : public CListThin::SThinItem { T data; }; }; template<class T> bool CList<T> :: Add(T& aDestData)‏ { SItem* item = new SItem; if(!item) return false; item->data = aDestData; AddThin(*item); return true; }

CList Class Implementation

template <class T>

class CList : public CListThin

{

public:

CList(){}

bool Add(T& aDestData);

private:

typedef struct SItem : public CListThin::SThinItem

{

T data;

};

};

template<class T>

bool CList<T> :: Add(T& aDestData)‏

{

SItem* item = new SItem;

if(!item)

return false;

item->data = aDestData;

AddThin(*item);

return true;

}

Thin Template and Design Pattern Thin Template is one of the applications of the Design Pattern called Template Method (please refer the GoF book for further information)‏

Thin Template is one of the applications of the Design Pattern called Template Method (please refer the GoF book for further information)‏

Reference Using &quot;Thin Templates&quot; to Reduce Application Size by Gregor Petrov http://www.codeguru.com/cpp/cpp/cpp_mfc/tutorials/article.php/c7927/

Thank You

Add a comment

Related presentations

Related pages

Thin on Thin? Where should you do Thin Provisioning ...

With the new awesome thin provisioning GUI and more flexible virtual disk behavior (hallelujah – no more "clone/template=eagerzeroedthick”!) in vSphere ...
Read more

VMware KB: Using thin provisioned disks with virtual machines

The VMware Knowledge Base provides ... Thin provisioned disks can grow to the full ... the use of thin provisioning should be limited to templates.
Read more

VMware vStorage Thin Provisioning

VMware vStorage Thin Provisioning gives you higher utilization by letting you dedicate more storage capacity than the actual purchased capacity.
Read more

XenDesktop policies explained - citrix24.com

XenDesktop policies explained. Share this ... Templates consist of pre-configured settings that ... or with other devices such as thin clients that may ...
Read more

Microsoft RDS Policies explained (Part 2)

Microsoft RDS Policies explained ... the policy Computer ConfigurationAdministrative TemplatesSystemDevice InstallationDevice ... Thin Clients; Reviews ...
Read more

Thin client - Wikipedia, the free encyclopedia

A thin client is a lightweight computer that is purpose ... (Learn how and when to remove this template ... Thin clients have their roots in ...
Read more

Move VMware ESXi VM to new datastore - preserve thin ...

Move VMware ESXi VM to new datastore - preserve thin-provisioning. up vote 12 down vote favorite. 14. ... You can also use File -> Export -> Export OVF ...
Read more

Business finance explained - GOV.UK

Business finance explained 1. Overview; 2. Investment finance; 3. Crowdfunding ; 4. Loans; 5. Grants; 6. Overdrafts; 7. Invoice financing; 8. Leasing and ...
Read more

CSS Outline Properties - W3Schools

CSS Outline. The CSS outline properties specify the style, color, and width of an outline. An outline is a line that is drawn around elements (outside the ...
Read more