Delphi Clinic C++Builder Gate Training & Consultancy Delphi Notes Weblog Dr.Bob's Webshop
Dr.Bob's Delphi Notes Dr.Bob's Delphi Clinics Dr.Bob's Delphi Courseware Manuals
 Dr.Bob Examines... #24
See Also: other Dr.Bob Examines columns or Delphi articles

HeadGen: C++ Template Generator
In this article I'll explain the usage of HeadGen, my free C++Builder template header/source generator, first introduced in my article in the C++ Zone on DevX.
Before we start, you should make sure you've downloaded the latest version of HeadGen (2001/10/16 - 247,602 bytes). This is still the stand-alone executable, but I'm working on an AddIn Wizard version (for the C++Builder IDE).

The HeadGen application requires two (included) "template" files: HeadGen.h and HeadGen.c. These two files are used as input for a TPageProducer component to produce the C++ template header (based on HeadGen.h) and source (based on HeadGen.h) definitions. More information about these files and their contents will be given in the customisation section of this article.
For now, just start the HeadGen executable to generate C++ template files.

C++ Template Base Class
The first and most basic use of HeadGen will be to generate a canonical template class definition (without ancestor type). The only thing that you need to specify in this case is the name of the template class. In this example I've used Base as class name.
Note that you can also specify the name of the template character, although by default the character T is used.

After you click on the Generate button, two source files will be generated in the current directory. The filename is the same as the class name, with the .hpp (template header) and .cpp (template source) extensions. In the above example, we'll end up with Base.hpp and Base.cpp source files.

C++ Template Derived Class
Sometimes, you want to create a template class derived from another template (base) class. First of all, you need to create the header and source files for the template base class (using the previous example). After this you can generate the header and source for the derived template class.
In my example, I want to create a template class Derived which is derived from template class Base:

After you click on the Generate button, two template new source files will be generated in the current directory, namely Derived.hpp and Derived.cpp.

HeadGen Customisations
If you don't like the way the output is formatted, or you want to add some of your own comment sections, then you are free to modify the HeadGen.h and HeadGen.c template files that are used by the HeadGen application. The HeadGen.h is the input for the .hpp file, and defined as follows:

  //    File: <#class>.hpp
  //  Author: drs. Robert E. Swart
  //    Date: <#date>
  //    Time: <#time>
  // Version: 0.01
  // Generated by: HeadGen 2.0 (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

The HeadGen.c is the input for the .cpp file, and defined as follows:

  //    File: <#class>.cpp
  //  Author: drs. Robert E. Swart
  //    Date: <#date>
  //    Time: <#time>
  // Version: 0.01
  // Generated by: HeadGen 2.0 (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>

Whatever you do, make sure you don't remove the #-tags, because these are needed for the conversion process (and replaced by the TPageProducer component).
The following #-tags are part of the two HeadGen template files:

#-tagReplaced by
#classT + class name
#datecurrent date
#timecurrent date
#includeclass name
#templatechartemplate char
#template< + template char + >
#bodybody comment block
#baseancestor type
#includebasebase class include statement
#publicbasetemplate base class declaration
#basecopybase copy constructor code

Obviously, you can freely change the comments inside the HeadGen.h and HeadGen.c files (shown red in the listings above).

HeadGen Enhancements
I'm currently working on the integration of HeadGen in the C++Builder IDE (as AddIn Wizard). In the meantime, if you have any comments, feedback or questions, don't hesitate to let me know by e-mail. Thanks in advance!


This webpage © 2001-2010 by Bob Swart (aka Dr.Bob - www.drbob42.com). All Rights Reserved.