This is a short series of articles about C++ template meta programming. Template metaprogramming is a programming technique in which templates are used by the C++ compiler to generate temporary source code. The temporary code is merged by the compiler with the rest of the source code and then compiled.
The articles start with an overview of type lists, moving on to MicroStation element type lists. We proceed to show how type lists can enable static polymorphic class inheritance. This provides a tool to add behavioural traits to a class as it inherits from multiple simple classes. Next, we use that polymorphic technique to create a framework of classes for handling MicroStation elements. We show how you can use either dynamic (i.e. a set of pure virtual functions) or static (using the curious recurring template pattern) polymorphism to design a set of classes.
The C++ language continues to evolve. Two evolutionary steps were the introduction of templates and Alexander Stepanov's invention of the Standard Template Library (STL). The STL is incorporated into the C++ language standard.
An unexpected consequence of the introduction of templates was template metaprogramming. Template metaprogramming provides a mechanism to write a compile-time program. In other words, abstract application logic in a way that lets the C++ compiler check its correctness.
Template metaprogramming leads to new areas of program capability. In this article, we'll examine type lists in the context of MicroStation MDL element types. We'll be using concepts and idioms from the C++ Standard Library, the Boost Libraries and in particular the Boost Metaprogramming Library (MPL).
Some examples assume that you are familiar with the C++ Standard Library.
That is, classes such as
std::vector should already be your friends.
The Boost libraries are not part of the C++ standard. They are developed independently of C++ and inform its development. For example, the latest C++ standard includes several libraries that originated at Boost.
If you want to use the Boost libraries, you must …
If you're new to template metaprogramming, and it makes you feel the way you did when you first saw a de-referenced pointer in C, then buy the C++ Template Metaprogramming book by David Abrahams and Aleksey Gurtovoy. It helps you to cut an enlightened path through the Boost documentation, which can seem turbid at times1 .
The following articles discuss C++ type lists, and some uses for them. Type lists are enabled by template C++ metaprogramming techniques. By template metaprogramming techniques, we mean the Boost Meta Programming Language, or MPL. We move on to show how type lists can enable selective inheritance. Selective inheritance means static polymorphism, which enables the compiler to check the design of our polymorphic classes. Next, we show a polymorphic element class for MicroStation developers, which is informed by the content of the preceding articles. We demonstrate two sets of classes that implement dynamic or static polymorphism. Finally, we demonstrate a simple factory implementation, informed by Boost.
|Index (the page you are reading): Metaprogramming and the MPL introduction|
|Type Lists and the MPL|
|Conditional Inheritance and the MPL|
|Polymorphic Classes for MicroStation Elements|
|Dynamic Polymorphic Element header file overview|
|Static Polymorphic Element header file overview|
MicroStation® is a computer-aided-design (CAD) tool produced by Bentley Systems. It is used by engineers, architects, and draughting technicians to create 3D models and 2D drawings.
MicroStation can be customised. Bentley Systems themselves have many applications for specialised markets that use MicroStation as a platform. Third-parties likewise may use MicroStation as a platform on which to develop their application software. Bentley Systems provide the MicroStation Software Development Kit (SDK) that software developers use to create their applications.
The SDK has two libraries: the MicroStation Development Library (MDL) and the MicroStationAPI. The MicroStation Development Library contains thousands of functions that provide a C-language interface. The MicroStationAPI provides a rich C++ interface.
We use development tools compatible with MicroStation V8i, which are the C++ compiler and linker provided with Visual Studio 2005. The example code in these articles was built and tested with …
If using a later version of MicroStation, you'll need to check the version of Visual Studio that provides compatible tools. Check this table of Visual Studio versions to match MicroStation and Visual Studio versions.
LA Solutions created the
It creates a unique C++ type for each element type defined in the MDL enumeration
If you're a MicroStation developer, you already have that header file (
mselems.h) delivered with the MicroStation SDK.
You'll find it in the
You can download
We've included another header file that you may find useful (
Some might comment that the Boost documentation is turbid all the time, but that's unfair. Keep in mind that the authors of Boost are …
The book helps, in the case of the MPL, interpret those annotations.