Familiar with .Net framework’s System.CodeDom namespace?
Aware of any C++ runtime code generation library?
If the answer to first question is yes and to the second is no, then, this post is just for you. If the answer to first question is no, read on to become familiar with the concept. If the answer to second question is yes, then, this is my style of doing thing! . If you answered no to both questions, you have no idea of the power held within this post.
Let us begin with the necessity of runtime code generation. Taking the examples from .Net framework, CodeDom is very useful in a variety of scenarios. If you use ASP.Net, it generates code from the markup at first page request. Ever designed a Windows Forms application, Forms Designer is using CodeDom behind the scenes. Used XmlReader/XmlWriter? They generate reading/writing code at runtime and compile it into an assembly.
The facilities available for C++ are quite limited. You may use Visual Studio’s various wizards. But they are not procedural in nature. They use their own markup and token replacement in template source files to generate code. MFC’s wizards cheat behind the veil to act as if code was generated.
These two reasons were poking around my mind for quite sometime. So, I took the effort to write one such framework myself. It began in 2001, with activity waxing and waning all the time (still two/three [?] major functions are incomplete). Borrowing ideas from System.CodeDom, I decided for a single-root class hierarchy beginning with CodeObject. Here is a quick and dirty class hierarch diagram for the project.
Scared? These are only 60 or so classes from the project. There are about 10 more supporting class which are not part of this hierarchy, makes total count of classes around 80. Classes in red are abstract, you cannot create instances of them. All classes live inside CPlusPlusCodeProvider namespace.
I shall discuss individual classes in coming posts. In the mean time, if you are interested in finding out more by looking at the source code, get it from here.
[1:07 AM, 14th April, 2007: Fixed minor typos.]