A TypeTensor is a K dimensional matrix, that is created in compile type. Its axes are indexed by classes and its elements are accessed be objects in runtime. If its elements are functions, then double, triple (or more) dispatch can be implemented in a simple way.
For example, there are ten classes:
classA0 {}; classA1 : publicA0 {}; classA2 : publicA0 {};
classB0 {}; classB1 : publicB0 {}; classB2 : publicB0 {}; classB3 : publicB0 {};
classC0 {}; classC1 : publicC0 {}; classC2 : publicC0 {};Make typelists from classes:
typedef TypeList<A0, A1, A2> ALIST; typedef TypeList<B0, B1, B2, B3> BLIST; typedef TypeList<C0, C1, C2> CLIST; Make a TypeTensor from type lists, it stores functions, function pointers are created:
using Fun3 = void (*)(A0*, B0*, C0* );
TypeTensor<Fun3, ALIST, BLIST, CLIST> TripleDispatcher; Define dispatch handler functions and they are stored to matrix elements:
TripleDispatcher.at<A1, B1, C1>() = [](A0* a, B0* b, C0* c) { cout << "A1 - B1 - C1 called" << endl; };
TripleDispatcher.at<A2, B3, C1>() = [](A0* a, B0* b, C0* c) { cout << "A2 - B3 - C1 called" << endl; };Define objects:
A0* a = newA2;
B0* b = newB3;
C0* c = newC1;Call via TypeTensor:
TripleDispatcher.Call(a, b, c); // A2 - B3 - C1 calledThe TypeTensorDemo.cpp file has some examples.
If, the matrix has two dimensions, then then it achieves double dispatch, because the selected function to calls depends on two class dynamic type.
To call a stored function the code calls one virtual function per dimension, so double dispatch function calls 3 functions (2 for indexes and one indirection to call stored function).