Thin Template By Somenath Mukhopadhyay [email_address]
What is a Template <ul><ul><li>Compile time polymorphism </li></ul></ul><ul><ul><li>Use of a single declaration for multip...
Example of Template <ul><ul><li>Declaration: </li></ul></ul><ul><li>Template <class T> T Add (T& a, T& b)‏ </li></ul><ul><...
Disadvantage of standard template <ul><ul><li>For each type it will produce one set of Add function </li></ul></ul><ul><ul...
Thin Template in the rescue <ul><ul><li>It is based upon the assumption that most optimized compilers will generate the co...
Thin Template in the rescue <ul><ul><li>CTest < char> oClass1; </li></ul></ul><ul><ul><li>CTest <int> oClass2; </li></ul><...
Thin Template implementation logic <ul><ul><li>Most modern compilers like Microsoft C++ compiler or GCC have this capabili...
Thin Template Example <ul><li>We will define a template class CList </li></ul><ul><li>CList is derived from a non-template...
Thin Template Example <ul><li>This is shown in the following class diagram </li></ul>
Thin Template Example <ul><li>CListThin Class definition </li></ul><ul><li>class CListThin </li></ul><ul><li>{ </li></ul><...
Thin Template Example <ul><li>CListThin class implementation </li></ul><ul><li>void CListThin::AddThin(SThinItem &aDestDat...
Thin Template Example <ul><li>CList Class Implementation </li></ul><ul><li>template <class T> </li></ul><ul><li>class CLis...
Thin Template and Design Pattern <ul><li>Thin Template  is one of the applications of the Design Pattern called Template M...
Reference Using &quot;Thin Templates&quot; to Reduce Application Size by  Gregor Petrov http://www.codeguru.com/cpp/cpp/cp...
Thank You
Upcoming SlideShare
Loading in …5
×

Thin Template Explained

2,447 views

Published on

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

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,447
On SlideShare
0
From Embeds
0
Number of Embeds
29
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Thin Template Explained

  1. 1. Thin Template By Somenath Mukhopadhyay [email_address]
  2. 2. What is a Template <ul><ul><li>Compile time polymorphism </li></ul></ul><ul><ul><li>Use of a single declaration for multiple types provided the logic is the same for all of the supported types </li></ul></ul>
  3. 3. Example of Template <ul><ul><li>Declaration: </li></ul></ul><ul><li>Template <class T> T Add (T& a, T& b)‏ </li></ul><ul><li>{ </li></ul><ul><li>Return a + b; </li></ul><ul><li>} </li></ul><ul><ul><li>Usage: </li></ul></ul><ul><li>Int x = T<int> Add (1 , 2)‏ </li></ul><ul><li>Float y = T<float> Add (1.1 , 2.2) </li></ul>
  4. 4. Disadvantage of standard template <ul><ul><li>For each type it will produce one set of Add function </li></ul></ul><ul><ul><li>Large code foot print </li></ul></ul><ul><ul><li>For devices having small amount of memory, it is not acceptable </li></ul></ul>
  5. 5. Thin Template in the rescue <ul><ul><li>It is based upon the assumption that most optimized compilers will generate the code only for template functions that actually use different types. </li></ul></ul><ul><ul><li>Example </li></ul></ul><ul><ul><li>Template <class T> class CTest </li></ul></ul><ul><li>{ </li></ul><ul><li>int Test1(void) { return 1;} </li></ul><ul><li>T Test2(void) { return 1;} </li></ul><ul><li>}; </li></ul>
  6. 6. Thin Template in the rescue <ul><ul><li>CTest < char> oClass1; </li></ul></ul><ul><ul><li>CTest <int> oClass2; </li></ul></ul><ul><ul><li>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 </li></ul></ul><ul><ul><li>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 </li></ul></ul>
  7. 7. Thin Template implementation logic <ul><ul><li>Most modern compilers like Microsoft C++ compiler or GCC have this capability. </li></ul></ul><ul><ul><li>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. </li></ul></ul><ul><ul><li>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. </li></ul></ul>
  8. 8. Thin Template Example <ul><li>We will define a template class CList </li></ul><ul><li>CList is derived from a non-template class CListThin </li></ul><ul><li>CList class has a template function called Add, which will add an item to the end of the list </li></ul><ul><li>This Add function will in turn take the help of the CListThin's non-template function AddThin. </li></ul><ul><li>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. </li></ul>
  9. 9. Thin Template Example <ul><li>This is shown in the following class diagram </li></ul>
  10. 10. Thin Template Example <ul><li>CListThin Class definition </li></ul><ul><li>class CListThin </li></ul><ul><li>{ </li></ul><ul><li>protected: </li></ul><ul><li>typedef struct SThinItem </li></ul><ul><li>{ </li></ul><ul><li>SThinItem* prev; </li></ul><ul><li>SThinItem* next; </li></ul><ul><li>}; </li></ul><ul><li>public: </li></ul><ul><li>CListThin():iFirstItem(0),iLastItem(0){}; </li></ul><ul><li>void AddThin(SThinItem& aDestData); </li></ul><ul><li>private: </li></ul><ul><li>SThinItem* iFirstItem; </li></ul><ul><li>SThinItem* iLastItem; </li></ul><ul><li>}; </li></ul>
  11. 11. Thin Template Example <ul><li>CListThin class implementation </li></ul><ul><li>void CListThin::AddThin(SThinItem &aDestData)‏ </li></ul><ul><li>{ </li></ul><ul><li>aDestData.prev = iLastItem ? iLastItem : 0; </li></ul><ul><li>aDestData.next = 0; </li></ul><ul><li>if(iLastItem) iLastItem->next = &aDestData; </li></ul><ul><li>iLastItem = &aDestData; </li></ul><ul><li>if(!iFirstItem) iFirstItem = &aDestData; </li></ul><ul><li>} </li></ul>
  12. 12. Thin Template Example <ul><li>CList Class Implementation </li></ul><ul><li>template <class T> </li></ul><ul><li>class CList : public CListThin </li></ul><ul><li>{ </li></ul><ul><li>public: </li></ul><ul><li>CList(){} </li></ul><ul><li>bool Add(T& aDestData); </li></ul><ul><li>private: </li></ul><ul><li>typedef struct SItem : public CListThin::SThinItem </li></ul><ul><li>{ </li></ul><ul><li>T data; </li></ul><ul><li>}; </li></ul><ul><li>}; </li></ul><ul><li>template<class T> </li></ul><ul><li>bool CList<T> :: Add(T& aDestData)‏ </li></ul><ul><li>{ </li></ul><ul><li>SItem* item = new SItem; </li></ul><ul><li>if(!item) </li></ul><ul><li>return false; </li></ul><ul><li>item->data = aDestData; </li></ul><ul><li>AddThin(*item); </li></ul><ul><li>return true; </li></ul><ul><li>} </li></ul>
  13. 13. Thin Template and Design Pattern <ul><li>Thin Template is one of the applications of the Design Pattern called Template Method (please refer the GoF book for further information)‏ </li></ul>
  14. 14. 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/
  15. 15. Thank You

×