I’m currently massively refactoring (well, rewriting is probably closer to the truth) my unmanaged metadata API. The rewrite is largely due to focusing too much on micro-level issues in my previous metadata API and not spending enough (any?) attention to macro-level issues.
Most of the code that I have written focuses on low-level grungy details: manipulating the CIL stream, cracking method signatures, creating local variable signatures etc. My new code base does some really nice things with resolving TypeRef tokens, creating TypeRef tokens and other macro-level issues that I had previously hard-coded into my runtime aspect weaver to work around deficiencies in my metadata API.
In the new code I’m writing, I’m trying to mirror the managed System.Reflection API’s wherever possible. However, one area that I don’t want to mirror is the use of object arrays in System.Reflection. For example, when you call Type.GetMethods(), it returns a MethodInfo[]. Instead, since my library is a C++ library, I want to build custom iterators to enable access to a metadata element’s children.
The first thing that I did was look in Nicolai Josuttis’ The Standard C++ Library, a Tutorial and Reference, the standard text on STL. Unfortunately, there was little coverage on building custom iterators.
Next, I did some Google searches and came up with a couple of articles:
- Samir Bajaj’s Taking Advantage of STL Algorithms by Implementing a Custom Iterator from the April 2001 issue of MSDN Magazine.
- Christopher Baus and Thomas Becker’s Custom Iterators for the STL article on the oonumerics.org web site.
The first article provides some code to munch on, the second article provides the insights. Concrete to abstract. That’s the best way to learn.
A brief diversion: at the bottom of the Baus and Becker paper is a reference to an interview with A. Stepanov of STL fame. This is a truly great interview which reveals how much insight Stepanov brings to computer science and generic programming in particular. I learned that STL was the result of a bacterial infection in this interview! Well worth the read.