
"Andrew Holden" <aholden@charteroaksystems.com> writes:
I would say that it has worked / is working for me. I began my
study of
metaprogramming with David and Aleksey's book and found that it
David Abrahams wrote: provided
a clear understanding of the principles. Using that book, I was able to write a metaprogram to help manage a dialog box's controls,
That sounds cool; can you tell us more?
Sure. I'm not sure if you were asking about my experiences with the book, or if you wanted to know about that metaprogram I mentioned. I'll try to answer both. When I picked up C++ Template Metaprogramming, I know nothing about metaprogramming or functional programming. It took me a while to get through parts of the book, particularly because of the functional aspects of the template system. I am currently going through Modern C++ Design, and I find myself not viewing it as a source to learn about metaprogramming, but rather as a book about generic programming that often uses metaprogramming techniques as an implementation detail. As for my metaprogram, I was motivated by the need to resize dialog boxes in Microsoft Windows. Windows has no built-in support for moving controls when a dialog box is resized. My solution was to create some classes to implement various relations between the dialog's size and a control's position/size. These controls only worry about positioning one edge of the control. They wound up implementing simple linear equations, with the slope either passed at compile time or built into the class definition, and the constant offset calculated at runtime from the control's initial position. I then created a control class which takes the Windows control ID and four position classes as template parameters. It is responsible for actually determining the control's initial position on dialog creation and moving the control. This allows the user to mix and match different position formulas for each edge of the control. I also created a bunch of typedefs for what I consider common control positions (relative to top-left, relative to bottom-right, stretch horizontally, etc.). Finally, I created a manager class, which takes a type sequence of control classes and ensures they all get updated when the user resizes the dialog. It collects their data in a private member using mpl::inherit_linearly and iterates through them using mpl::for_each. To use these classes, I define an mpl vector of control classes, either using the typedefs I mentioned earlier, or specifying control <id, top, left, bottom, right>. I then instantiate a manager class, passing this vector as a template parameter. After that, it is just a matter of calling the initialize and resize (runtime) functions in the appropriate places. I would be happy to provide the code for this, but I am not sure where I should post it.