Dispatch1< ALGORITHM, TYPES, REIFICATION > Class Template Reference

#include <generic_dispatch.h>

Detailed Description

template<typename ALGORITHM, typename TYPES, typename REIFICATION = DefaultReification>
class maxon::Dispatch1< ALGORITHM, TYPES, REIFICATION >

Dispatch1 can be used to dispatch a generic invocation to a non-generic function based on the runtime (reified) type of the first argument. You have to put the non-generic functions in a class (typically you'll only have a single function template which covers all cases, but you may also use several overloaded functions), and they have to be named Do:

class MyAlgorithm
{
public:
template <typename T> static Result<void> Do(const ArrayInterface<T>& array, Int someParam);
};

Then if you have a ArrayInterface<Generic>, you can dispatch to the matching template instantiation by writing

Dispatch1<MyAlgorithm, std::tuple<Int, Float, Vector>>::Do(array, 42) iferr_return;

This would dispatch the types Int, Float and Vector, for all other types an error will be returned.

If you want to dispatch based on the second argument, use Dispatch2. You can also nest Dispatch2 within Dispatch1 for a double-dispatch as in

class MyAlgorithm2
{
public:
template <typename T1, typename T2> static Result<void> Do(const ArrayInterface<T1>& array1, const ArrayInterface<T2>& array2, Int someParam);
};
...
Dispatch1<Dispatch2<MyAlgorithm2, std::tuple<Int, Float>>, std::tuple<Int, Float>>::Do(array1, array2, 42) iferr_return;

This would allow all combinations of Int and Float. Note that for a large number of combinations, the compiler has to create a lot of code.

Template Parameters
ALGORITHMThe algorithm to invoke. The class has to contain a function template and/or overloaded functions named Do. These functions will be invoked based on the reified type of the generic argument.
TYPESList of types which shall be supported of the form std::tuple<TYPE1, TYPE2, ...>. If the runtime type doesn't match one of these types, an error is returned.
REIFICATIONThe reification class is used to obtain the runtime DataType from the generic argument. It has to contain a GetType function for this purpose, and also a function template which converts the generic argument into the reified argument. By default, DefaultReification is used which supports Data, ArrayInterface and WritableArrayInterface.
Int
maxon::Int Int
Definition: ge_sys_math.h:62
iferr_return
#define iferr_return
Definition: resultbase.h:1434