Bug Vanquisher

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

Advertisements

1 Comment »

  1. […] what the hell CPPCodeProvider is, here are some links which explain it in much more details: part 1, part 2, part 3, part 4, part 5 and part […]

    Pingback by Major Changes in CPPCodeProvider « Bug Vanquisher — 19 October 2007 @ 11:40 AM


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: