Slideshare.net (beta)

 
Post: 
Myspace Hi5 Friendster Xanga LiveJournal Facebook Blogger Tagged Typepad Freewebs BlackPlanet gigya icons



All comments

Add a comment on Slide 1

If you have a SlideShare account, login to comment; else you can comment as a guest


Showing 1-50 of 0 (more)

Thin Template Explained

From som.mukhopadhyay, 2 months ago

Through these slides I tried to describe the thin template concept more

258 views  |  0 comments  |  0 favorites
 
 
 

Groups/Events

Not added to any group/event

 
 

Privacy InfoNew!

This slideshow is Public

 
Embed in your blog
Embed (wordpress.com)
custom

Slideshow Statistics
Total Views: 258
on Slideshare: 258
from embeds: 0* * Views from embeds since 21 Aug, 07

Slideshow transcript

Slide 1: Thin Template By Somenath Mukhopadhyay som.mukhopadhyay@som-itsolutions.com

Slide 2: 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

Slide 3: 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)

Slide 4: 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

Slide 5: 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;} };

Slide 6: 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

Slide 7: 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.

Slide 8: 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.

Slide 9: Thin Template Example • This is shown in the following class diagram

Slide 10: 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; •};

Slide 11: 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; •}

Slide 12: 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; •}

Slide 13: 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)

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

Slide 15: Thank You