fork(1) download
  1. #include <cassert>
  2. #include <memory>
  3. #include <sstream>
  4. #include <string>
  5. #include <iostream>
  6.  
  7. class MyClass {};
  8.  
  9. bool Post(std::string /*command*/, std::string /*payload*/) { std::cout << "Post\n"; return false;}
  10. std::string Get(std::string /*command*/) { std::cout << "Get\n"; return ""; }
  11. bool Delete(std::string /*command*/, std::string /*name*/) { std::cout << "Delete\n"; return false;}
  12. int OtherFunc(std::string /*command*/, const MyClass& /*name*/) { std::cout << "OtherFunc\n"; return 0;}
  13.  
  14. enum class CommandType
  15. {
  16. Get, Post, Delete, OtherFunc
  17. };
  18.  
  19. template<typename ... Ts>
  20. typename std::enable_if<sizeof...(Ts) != 1, int>::type
  21. Get (Ts&&...) { assert(false && "should not be called"); return 0; }
  22.  
  23. template<typename T>
  24. typename std::enable_if<!std::is_convertible<T, std::string>::value, int>::type
  25. Get (T&&...) { assert(false && "should not be called"); return 0; }
  26.  
  27. template<typename ... Ts>
  28. typename std::enable_if<sizeof...(Ts) != 2, int>::type
  29. Post (Ts&&...) { assert(false && "should not be called"); return 0; }
  30.  
  31. template<typename T1, typename T2>
  32. typename std::enable_if<!std::is_convertible<T1, std::string>::value
  33. || !std::is_convertible<T2, std::string>::value,
  34. int>::type
  35. Post (T1&&, T2&&) { assert(false && "should not be called"); return 0; }
  36.  
  37. template<typename ... Ts>
  38. typename std::enable_if<sizeof...(Ts) != 2, int>::type
  39. Delete (Ts&&...) { assert(false && "should not be called"); return 0; }
  40.  
  41. template<typename T1, typename T2>
  42. typename std::enable_if<!std::is_convertible<T1, std::string>::value
  43. || !std::is_convertible<T2, std::string>::value,
  44. int>::type
  45. Delete (T1&&, T2&&) { assert(false && "should not be called"); return 0; }
  46.  
  47. template<typename ... Ts>
  48. typename std::enable_if<sizeof...(Ts) != 2, int>::type
  49. OtherFunc (Ts&&...) { assert(false && "should not be called"); return 0; }
  50.  
  51. template<typename T1, typename T2>
  52. typename std::enable_if<!std::is_convertible<T1, std::string>::value
  53. || !std::is_convertible<T2, const MyClass&>::value,
  54. int>::type
  55. OtherFunc (T1&&, T2&&) { assert(false && "should not be called"); return 0; }
  56.  
  57. template<typename... Ts>
  58. std::unique_ptr<std::stringstream>
  59. Execute(CommandType command, Ts&&... parameters) {
  60. auto response = std::make_unique<std::stringstream>();
  61. if(command == CommandType::Get)
  62. *response << Get(std::forward<Ts>(parameters)...);
  63. else if(command == CommandType::Post)
  64. *response << Post(std::forward<Ts>(parameters)...);
  65. else if(command == CommandType::Delete)
  66. *response << Delete(std::forward<Ts>(parameters)...);
  67. else if(command == CommandType::OtherFunc)
  68. *response << OtherFunc(std::forward<Ts>(parameters)...);
  69.  
  70. return response;
  71. }
  72.  
  73.  
  74. int main(){
  75. Execute(CommandType::Get, "hello");
  76. Execute(CommandType::Post, "hello", "world");
  77. Execute(CommandType::Delete, "hello", "world");
  78. Execute(CommandType::OtherFunc, "hello", MyClass{});
  79. }
  80.  
Success #stdin #stdout 0s 3276KB
stdin
Standard input is empty
stdout
Get
Post
Delete
OtherFunc