Policy Based Design Using C++ Typemaps
Typemaps and Policies
As we have created the typemap meta-class, the policy design becomes simple and straightforward - just pass the typemap as template parameter. The usual programming library may have some default policies, suitable for most of the applications (Listing 8). The programmer then picks the default container, and modifies it to fit the specific needs.
typedef typemap::insert<KError, MyErr, typemap::insert<KMemory, MyMem, DefaultPolicy >::X >::X MyPolicy ;
Then any class that accepts policy as typemap can be used like this.
ImageFile<MyPolicy> image_file ; Socket<MyPolicy> sock ; Database<MyPolicy> db ; Widget<MyPolicy> w ;
The class accepts the policy as template parameter and then searches the used policies by their keys (Listing 9). The class only chooses the needed policies, ignoring others. If the library designer decided to add some other policies, then it is just enough to add it to DefaultPolicy container.
We can go further to submit policies that are containers themselves. For ImageFile this is the container of all supported image formats.
typedef typemap::insert<KPgm, PgmFmt, typemap::insert<KJpeg, JpegFmt, typemap::insert<KPng, PngFmt >::X >::X >::X SupportedFormats ;
More likely the library would have some set of supported formats, and programmer can just simply adjust the default list.
typedef typemap::erase< KGif,DefaultFormats >::X MyFormats ;
Then the new policy containing the list supported formats would look like this.
typedef typemap::insert< KImageFmts, MyFormats >::X FmtsPolicy ;
Then the class, that accepts the KImageFmts component has to iterate through the container to perform the necessary action (Listing 9).
Copyright (C) 2005,2006,2013 Andre Mirzoyan