fork download
  1. #include <vector>
  2. #include <map>
  3. #include <functional>
  4. #include <iostream>
  5. #include <stdexcept>
  6.  
  7. enum class test_type {t0, t1, t2, t3, t4, tn};
  8.  
  9. class tester
  10. {
  11. public:
  12. void run(test_type type, const std::vector<int> &args)
  13. {
  14. func_map[type](args);
  15. };
  16.  
  17. private:
  18. void function_0() { std::cout << "f0\n"; };
  19. void function_1(int p1, int p2) { std::cout << "f1\n"; };
  20. void function_2(int p1, int p2, int p3) { std::cout << "f2\n"; };
  21. void function_3(int p1, int p2) { std::cout << "f3\n"; };
  22. void function_4(int p1) { std::cout << "f4\n"; };
  23. void function_n(int p1) { std::cout << "fn\n"; };
  24.  
  25. static void validate(const std::vector<int> &args, size_t needed) {
  26. if (args.size() != needed)
  27. throw std::invalid_argument("wrong number of arguments");
  28. }
  29.  
  30. using func_type = std::function<void(const std::vector<int> &)>;
  31.  
  32. std::map<test_type, func_type> func_map {
  33. {test_type::t0,
  34. [this](const std::vector<int> &args){
  35. validate(args, 0);
  36. function_0();
  37. }
  38. },
  39. {test_type::t1,
  40. [this](const std::vector<int> &args){
  41. validate(args, 2);
  42. function_1(args[0], args[1]);
  43. }
  44. },
  45. {test_type::t2,
  46. [this](const std::vector<int> &args){
  47. validate(args, 3);
  48. function_2(args[0], args[1], args[2]);
  49. }
  50. },
  51. {test_type::t3,
  52. [this](const std::vector<int> &args){
  53. validate(args, 2);
  54. function_3(args[0], args[1]);
  55. }
  56. },
  57. {test_type::t4,
  58. [this](const std::vector<int> &args){
  59. validate(args, 1);
  60. function_4(args[0]);
  61. }
  62. },
  63. {test_type::tn,
  64. [this](const std::vector<int> &args){
  65. validate(args, 1);
  66. function_n(args[0]);
  67. }
  68. },
  69. };
  70. };
  71.  
  72. int main() {
  73.  
  74. tester test;
  75. test.run(test_type::t0, {});
  76. test.run(test_type::t1, {1, 2});
  77. test.run(test_type::t2, {1, 2, 3});
  78. test.run(test_type::t3, {1, 2});
  79. test.run(test_type::t4, {1});
  80. test.run(test_type::tn, {1});
  81.  
  82. return 0;
  83. }
Success #stdin #stdout 0s 4276KB
stdin
Standard input is empty
stdout
f0
f1
f2
f3
f4
fn