3.3. Runtime dispatch

Now let us make modifications to the above by stipulating that the function operates on an array of type T and size size, and backends have restrictions both on the type as well as the size of the array: Backend A only accepts ints, backend B accepts any type, but only buffers whose size is a power of 2, while backend C accepts any input:

namespace vsip_csl
{
namespace dispatcher
{
template <>
struct Evaluator<Operation, A, void(int*, size_t)>
{
  static bool const ct_valid = true;
  static bool rt_valid(int*, size_t) { return true;}
  static void exec(int *input, size_t size) { A_impl::process(input, size);}
};

template <typename T>
struct Evaluator<Operation, B, void(T*, size_t)>
{
  static bool const ct_valid = has_feature<T>::value;
  static bool rt_valid(T*, size_t size) { return size^(size-1);}
  static void exec(T *input, size_t size) { B_impl<T>::process(input, size);}
};

template <typename T>
struct Evaluator<Operation, C, void(T*, size_t)>
{
  static bool const ct_valid = true;
  static bool rt_valid(T*, size_t) { return true;}
  static void exec(T *input, size_t size) { C_impl<T>::process(input, size);}
};


template <> 
struct List<Operation>
{
  typedef Make_type_list<A, B, C>::type type;
};

}
}

The operation wrapper for this now simply becomes:

template <typename T>
void process(T *input, size_t size)
{
  vsip_csl::dispatch<Operation, void(T*, size_t)>(input, size);
}

3.3.1. Implementation details

The runtime-dispatch works conceptually similar to the compile-time dispatch. However, in this case the dispatch is actually a two-phase process. The first phase is the same as in the compile-time dispatch. It reduces the type-list of Evaluators to those elements that match the given input type(s).

The second phase, then, is carried out at runtime, when this reduced type-list is traversed to evaluate all rt_valid() member functions until a match is found, based on runtime characteristics. In the case presented here this is a size parameter, though it could be anything else, as the signature of the operation to be carried out is a template parameter, too.