- 论坛徽章:
- 44
|
本帖最后由 windoze 于 2013-12-11 13:57 编辑
其实吧,你为什么一定要把这些东西塞到一个class里呢?
为什么不干脆写成这样:
- typedef std::map<std::pair<std::string, std::string>, std::function<void()>> action_map;
- action_map the_map({
- {
- {"GET", "/some/path"},
- [](){ std::cout << "GET /some/path" << std::endl; }
- },
- {
- {"POST", "/some/path"},
- [](){ std::cout << "POST /some/path" << std::endl; }
- },
- {
- {"GET", "/some/other/path"},
- [](){ std::cout << "GET /some/other/path" << std::endl; }
- },
- });
复制代码 使用的时候你可以这样:
- action_map::const_iterator i=the_map.find({method, path});
- if(i==the_map.end()) {
- // Send 404 response
- } else {
- i->second();
- }
复制代码 |
|