Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

How to Generate C++ Class Template Definitions : Page 3

Use this source-code generator to produce class template header implementation definitions in .hpp and .cpp files.


advertisement
Template Template
If you look closely at the listings presented so far, you can see a pattern (believe me, there is logic behind this class template syntax). In fact, I have been able to produce two template files that can be used to generate the template listings we've seen in this article. The template file for the class definition (typically inside a header file) is defined as follows:

// File: <#class>.hpp // Author: drs. Robert E. Swart> // Date: <#date> // Time: <#time> // Version: 0.01 // Generated by: HeadGen (c) 1995-2001 by Bob Swart
(aka Dr.Bob - www.drbob42.com) // Changes: // #ifndef <#class>_hpp #define <#class>_hpp #include <iostream.h> <#includebase> template <class <#templatechar>> class <#class> <#publicbase> { public: // Constructors & Destructors <#class>(void); <#class>(const <#class><#template>& copy); virtual ~<#class>(void);> // Accessing functions // Modifier functions // Operator overloading <#class><#template>& operator = (const <#class><#template>& other); int operator == (const <#class><#template>& other) const; // Streaming output friend ostream& operator << (ostream& os, const <#class><#template>& other); protected: private: }; #endif

Note the special #-tags. WebBroker developers may recognize these as tags used in the PageProducer components. That's actually the case, since I'm using a TPageProducer component (from the Internet tab) to expand the above template into a true class template definition header—with or without a template base class. The same technique can be applied to the following template listing, that can be used to produce the empty template skeleton implementations:

// File: <#class>.cpp // Author: drs. Robert E. Swart // Date: <#date> // Time: <#time> // Version: 0.01 // Generated by: HeadGen (c) 1995-2001 by Bob Swart
(aka Dr.Bob - www.drbob42.com) // Changes: // #include "<#include>.hpp" // Constructors & Destructors template <class <#templatechar>> <#class><#template>::<#class>(void) <#base> <#body> template <class <#templatechar>> <#class><#template>::<#class>(const <#class><#template>& copy) <#basecopy> <#body> template <class <#templatechar>> <#class><#template>::~<#class>(void) <#body> // Operator overloading template <class <#templatechar>> <#class><#template>& <#class><#template>::operator = (const < #class><#template>& other) <#body> template <class <#templatechar>> int <#class><#template>::operator == (const <#class><#template>& other) const <#body> // Streaming output template <class <#templatechar>> ostream& operator << (ostream& os, const <#class><#template>& other) <#body>

Again, the above listing can be used to produce a stand-alone class template as well as a derived class template. We only need to specify three options: the class name, the (optional) base class name, and the template character.



HeadGen

 
Figure 1Get a close-up view of the HeadGen application.

The utility HeadGen only requires the class name (the template character is T by default), as can be seen in Figure 1.

For the base class, specify Base in the Class Name box leave the Ancestor type box empty, and click on Generate. For the derived class, specify Derived in the Class Name box, Base in the Ancestor Type box and then click on Generate again. In both cases, the T will be added as prefix automatically (files Base.hpp and Base.cpp will contain the definition for TBase).

A very simple Borland C++Builder example program (to test the syntax of the generated files) can be seen below:

//--------------------------------------------------------- #pragma hdrstop #include "Base.cpp" // TBase #include "Derived.cpp"; // TDerived //-------------------------------------------------------- typedef TDerived<int> TintClass; #pragma argsused int main(int argc, char* argv[]) { TintClass* Bob = new TintClass(); TintClass Swart = TintClass(*Bob); if (*Bob == Swart) { *Bob = Swart; } return 0; } //--------------------------------------------------------

Note that I needed to include the .cpp files of the templates, and not only (or just) the .hpp files. That's because the .cpp files are "expanded" (like MACROs) to the compiler, which must find them in order to be able to use them.

External Templates
The two template files are external files HeadGen.h (for the .hpp header) and HeadGen.c (for the .cpp source file). As an additional benefit, you can edit these templates and make sure your own copyright statements appear in them. Make sure to keep all #-tags intact, though, otherwise the template PageProducer won't be able to work correctly anymore.



Bob Swart (aka DrBob - www.drbob42.com.) is an independent trainer and consultant of Borland tools in The Netherlands who is a frequent speaker at Borland conferences. He is a free-lance technical author for The Delphi Magazine, UK-BUG Developer's Magazine, and Delphi Developer and wrote chapters for "The Revolutionary Guide to Delphi 2" (WROX), "Delphi 4 Unleashed," "C++Builder 4 Unleashed," "C++Builder 5 Developer's Guide," "Delphi 6 Developer's Guide" and "Kylix Developer's Guide" (SAMS).
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap