1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
#include <iostream>
struct A1 { void operator()(){ std::cout << "A1\n"; } };
struct A2 { void operator()(){ std::cout << "A2\n"; } };
struct B1 { void operator()(){ std::cout << "B1\n"; } };
struct B2 { void operator()(){ std::cout << "B2\n"; } };
struct B3 { void operator()(){ std::cout << "B3\n"; } };
template<typename A, typename B>
struct S
{
void operator()(){ A()(); B()(); }
};
template<typename A, typename B, typename Callback>
void dispatch_run(Callback callback)
{
S<A,B>()();
callback();
}
template<typename A, typename Callback>
void dispatch_2(const char * s, Callback callback)
{
if (*s == '1') {
dispatch_run<A, B1>(callback);
}
else if (*s == '2') {
dispatch_run<A, B2>(callback);
}
else {
dispatch_run<A, B3>(callback);
}
}
template<typename Callback>
void dispatch(const char * s, Callback callback)
{
if (*s == '1') {
dispatch_2<A1>(s+1, callback);
}
else {
dispatch_2<A2>(s+1, callback);
}
}
struct Callback {
const char * s;
Callback(const char * str)
: s(str)
{}
void operator()()
{ std::cout << s; }
};
int main()
{
dispatch("11", Callback("fin de 11\n"));
dispatch("13", Callback("fin de 13\n"));
} |
Partager