11.7 FnDispatcher and Symmetry
Because of FnDispatcher's dynamism, adding support for symmetry is much easier than it was with the static StaticDispatcher.
All we have to do to support symmetry is to register two trampolines: one that calls the executor in normal order, and one that swaps the parameters before calling. We add a new template parameter to Add, as shown.
template <class BaseLhs, class BaseRhs = BaseLhs,
typename ResultType = void>
class FnDispatcher
{
...
template <class ConcreteLhs, class ConcreteRhs,
ResultType (*callback)(ConcreteLhs&, ConcreteRhs&),
bool symmetric>
bool Add()
{
struct Local
{
... Trampoline as before ...
static void TrampolineR(BaseRhs& rhs, BaseLhs& lhs)
{
return Trampoline(lhs, rhs);
}
};
Add<ConcreteLhs, ConcreteRhs>(&Local::Trampoline);
if (symmetric)
{
Add<ConcreteRhs, ConcreteLhs>(&Local::TrampolineR);
}
}
};
Symmetry with FnDispatcher has function-level granularity—for each function you register, you can decide whether or not you want symmetric dispatching.
|