Bug Vanquisher

14 April 2007

Runtime Code Generation in C++ 2

Filed under: C++ — Tanveer Badar @ 12:56 AM

Enough with the background, move along and dig into the dirty details. First, CodeObject: Base class for everyone else, contains no data members and all functions are abstract, provides base services contracts copying, assignment and output operations.

Next the six classes in the top row, they do not have much in themselves and do what their name says. A CodeSnippet is a literal code snippet which you will have trouble getting correct through CPlusPlusCodeProvider; a Comment is, well, just a comment; variable declarations are declarations nothing more than that, though, their implementation is full of quirks; a Namespace represents a namespace; a Typedefinition is a typedefinition. VariableDeclaration has two distinct parts, a declarator and a declarator specifier. This distinction is necessary because you can write something like extern static const int** const k; where everything before the pointers is declarator specifier and applies to all the variables which can be specified in this declaration and from pointers onwards is the declarator which applies to k only and can be different for every variable in the list.

Next, consider the two classes PreprocessorDirective and MacroTestDirective. A preprocessor directive represents anything other than #if, meaning things like #define, #undef, #include, #pragma etc. A MacroTestDirective is for everything like #if, #ifdef, #ifndef etc.

Next time, I’ll be discussing Function and its descendants.

8 April 2007

Runtime Code Generation in C++

Filed under: C++ — Tanveer Badar @ 12:33 AM

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.]

4 April 2007

Must Read

Filed under: WPF — Tanveer Badar @ 12:00 PM

<warning> Geek stuff. Read this immediately. </warning>

« Newer Posts

Blog at WordPress.com.