본문으로 바로가기

Event Driven Programming using Template Specializations in C++

category 개발언어/c++ 2016. 7. 9. 07:19


c++ template 를 사용하여 이벤트 처리하기


const int RED_PILL = 0;
const int BLUE_PILL = 1;
template<int T>
struct EventHandler {
static void Event() { };
};
template<typename Dummy_T = void>
struct Dispatcher {
static bool EventDispatch(char ch) {
switch (ch) {
case 'a': {
EventHandler<RED_PILL>::Event();
return true;
}
case 'b': {
EventHandler<BLUE_PILL>::Event();
return true;
}
default : {
return false;
}
}
}
};

template<>
struct EventHandler<BLUE_PILL> {
static void Event() { puts("Welcome to the matrix!"); };
};

int main() {
puts("press a for the red-pill, b for the blue-pill");
char ch = getchar();
Dispatcher<>::EventDispatch(ch);
return 0;
}