Boost logo

Boost :

From: Ares Lagae (ares.lagae_at_[hidden])
Date: 2008-03-05 08:37:47


* Problem

- I have the following types "uint8, uint16, uint32, int8, int16, int32,
float32, float64" called "scalar types".
- The types "uint8, uint16, uint32" are called "size types".
- "List types" are defined as every possible combination of size types and
scalar types (24 in this case).
- For every scalar type and every size type I need to store a callback.
- The function templates
template <typename ScalarType> void scalar_type_callback();
template <typename SizeType, typename ScalarType> void list_type_callback();
are function templates and not functions, and cannot be stored in a function
object.

* Solution 1

I wrote code using boost::mpl (a time ago, with some help of the boost
groups) that generates a class containing a callback for each scalar type
and a class containing a callback for each list type.

* Solution 2

The code from solution 1 worked, but was slow to compile, and I did not
fully understand it. Therefore I decided to read the book "C++ Template
Metaprogramming" and reimplement the code from scratch, without boost::mpl.

The attached code consists of
- A number number of simple mpl constructs similar to the ones in boost::mpl
(bool_, if_, eval_if, pair, inherit, identity, void_).
- A single sequence list with operations push_front and reverse. It does not
include an abstraction layer over sequences.
- A number of simple mpl constructs needed to implement the solution to the
problem above (for_each, joint_view, transform, product_view,
inherit_linearly). Since sequences are not abstracted, these are
implemented in terms of the list sequence and (only) the two operations
push_front and reverse, using fold-like constructs.

The only construct not in boost::mpl is product_view, which constructs the
outer product of two sequences (it might be interesting to include
something like this in boost::mpl).

There is no support for lambda expressions, which causes some complications
(e.g. the Operation argument for transform is a class template in my
design), but the mpl code is pretty clear and usable.

I am attaching this code in the hope it is usefull to anyone (e.g.
didactical purposes, small mpl systems without using boost::mpl). I am also
open to suggestions for improvements, simplifications, ...

-- 
Ares Lagae
Computer Graphics Research Group, Katholieke Universiteit Leuven
http://www.cs.kuleuven.be/~ares/



Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk