Thursday, April 21, 2011

Pluggable/Hookable C++ class

disclaimer
C++ gurus please don't shoot me if you find this one offensive.

I was trying to get a pluggable/hookable class that could let access to it's private members to other class which can hook into it. Found a nice enough way to do so using a template method at stack overflow.

http://stackoverflow.com/questions/424104/can-i-access-private-members-from-outside-the-class-without-using-friends

My requirements:
I'm working with the TTF file parser. The basic operations that I know of right now are:
1. read the file.
2. pack the read files into a valid TTF file.

The classes that represent the TTF table should be able to provide a way to other classes to hook into it and change it's state. This is because the font can require many unpredictable transformations. For example transform TTF font to other formats such as SVG, WOFF etc.

The way we would like to transform the font is quite unpredictable.

If anyone knows a better solution, you are always welcome.

Improvements
One area that I think we can improve the implementation is to have a constraint on the template method. So, that only a class inheriting some specific class can hook into the method.

Further note:
It also shows that you should be careful while designing your classes. You may unwittingly open a back door to the class, that may cause a havoc.

I'll try to post example codes in future.