Switch, case, typelists and type_switch

31 08 2010

Whenever you are building a system that has it’s own type system you will come to a point where you perform type dependent operations. If your types seamlessly map to standard integral types most of the mapping code and extraction can be handled by simple template methods, but from time to time you will find the following code fragments:

case IntegerType: /**/
case DoubleType: /**/

Interestingly the only difference in the above code line is only the requested type. A concrete example is e.g. hashing of a certain value. The type of the value is stored in a variable and depending on the actual type different hash functions have to be called. When you find something like this the first time, you will feel ok, the second time a little more nervous and the third time…

The question is now: How can I rewrite my code so that it will be less explicit and most important easier to extend. The biggest problem with the above solution is that once you extend your type system the whole code will be changed and there must be an easier way to solve this.

One of my first approaches was macro magic to iterate over a sequence and than generate the right code by text expansion. However this will not work out due to the fact that macros in C++ are not recursive and will not be called a second time. Reading in “Modern C++ Design” by Alexandrescu — a must read — I stumbled upon typelists (well I had some support by @bastih01). After one evening screwing my had around them finally I made some progress.

The solution I found is based on static recursive template generation plus dynamic type switching at runtime. The reason for this rather complicated approach is the following: while the general type information is available at compile-time, the explicit instance related type information can only be mapped using an enum at run-time.

Enough words lost, whats the solution?

Consider the following setup: First we define the type list and the enum for storing the type information.

#include <boost/mpl/vector.hpp>

typedef enum
} DataType

typedef boost::mpl::vector<int, float, std::string> basic_types;

Now we need to implement our type_switch operator, basically it is based on TinyTL but used in a Boost environment, because I did not find anything alike in Boost directly.

template <typename L, int N=0, bool Stop=(N==boost::mpl::size<hyrise_basic_types>::value)> struct type_switch;
template <typename L, int N, bool Stop>
struct type_switch
    template<class F>
    typename F::value_type operator()(size_t i, F& f)
        if (i == N)
            return f.operator()<typename boost::mpl::at_c< hyrise_basic_types, N>::type>();
        } else {
            type_switch<L, N+1> next;
            return next(i,f);

template <typename L, int N>
struct type_switch<L, N, true>
    template<class F>
    typename F::value_type operator()(size_t i, F& f)
         throw std::runtime_error("Type does not exist");

If you look at the above code for the first time it is kind of weird to understand what is going on. But once you understand template recursion it’s totally clear. But easy things first: boost::mpl::size defines a template that contains the size of the typelist hyrise_basic_types. The boost::mpl::at_c template defines an random accessor to the typlist based on a constant index.

The template type_switch is a special construct with 3 parameters, the first is the type list, the second is the current position in this list defaulting to 0, and the third is a boolean parameter determining if the recursion should stop. The default implementation of this struct with the operator() method checks if the current value i is equal to N and if this is the case calls the operator() method on the function object submitted as a parameter. If this is not the case it instantiates a new template and increases N by 1. This is possible because all int values for the complete list are known in advance at compile time and so they can be used as template parameters. To avoid infinite recursion a dedicated template specialization with Stop=true provides an implementation that should never be called and does not further invoke any template recursion.

But back to the functor used in this setting. We have one requirement on the functor and this is that we have to specify the value_type of the operator() method directly in the functor. A sample implementation based on boost hash could look like the following.

template<typename T>
struct hash_functor
    typedef T value_type;
    AbstractTable* table;
    size_t f;
    ValueId vid;

     hash_functor(): table(0) {}

     hash_functor(AbstractTable * t, size_t f, ValueId v): table(t), f(f), vid(v) {}
     template<typename R>
     T operator()()
         return boost::hash<R>()(table->getValueForValueId<R>(f, vid));

For this functor, T defines the return type of the functor with an valye_type typedef and R is the type of the actual type used for the type_swtich. Instead of the clustered switch case statement the code for my type depended hash value method looks a lot better.

size_t hash_value(AbstractTable * source, size_t f, ValueId vid)
    hash_functor<size_t> fun(source, f, vid);
    type_switch<hyrise_basic_types> ts;
    return ts(source->typeOfColumn(f), fun);

The last sentence goes to the cost of this access: Each level of hierarchy generates at least one method call plus an evaluation of an if statement. The generic switch/case only generates the comparison, but I think this overhead is neglectable compared to the huge amount of time saved when it comes to extending the usable data types.




One response

29 04 2014
nearby locksmiths

Security is the serious concern for any commercial or residential
building. Today, however, our homes are bursting with expensive conveniences andd toys.
It might be a really tough and hard move tto make, when you have to spednd when you aare in a decline, but it is a necessary move.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: