Callback_34.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include <typeinfo>
  2. #include <typeindex>
  3. #include <unordered_map>
  4. #include <iostream>
  5. using namespace std;
  6. template <typename R>
  7. class MultiFuncObject {
  8. unordered_map<type_index, void (*)()> m_funcs;
  9. public:
  10. MultiFuncObject<R> operator +=(R(*f)()) {
  11. m_funcs[typeid(R())] = (void (*)()) f;
  12. return *this;
  13. }
  14. template <typename A1>
  15. MultiFuncObject<R> operator +=(R(*f)(A1)) {
  16. m_funcs[typeid(R(A1))] = (void (*)()) f;
  17. return *this;
  18. }
  19. template <typename A1, typename A2>
  20. MultiFuncObject<R> operator +=(R(*f)(A1, A2)) {
  21. m_funcs[typeid(R(A1, A2))] = (void (*)()) f;
  22. return *this;
  23. }
  24. template <typename A1, typename A2, typename A3>
  25. MultiFuncObject<R> operator +=(R(*f)(A1, A2, A3)) {
  26. m_funcs[typeid(R(A1, A2, A3))] = (void (*)()) f;
  27. return *this;
  28. }
  29. R operator()() const
  30. {
  31. unordered_map<type_index, void (*)()>::const_iterator it = m_funcs.find(typeid(R()));
  32. if (it != m_funcs.end()) {
  33. R(*f)() = (R(*)())(it->second);
  34. (*f)();
  35. }
  36. }
  37. template <typename A1>
  38. R operator()(A1 a1) const
  39. {
  40. unordered_map<type_index, void (*)()>::const_iterator it = m_funcs.find(typeid(R(A1)));
  41. if (it != m_funcs.end()) {
  42. R(*f)(A1) = (R(*)(A1))(it->second);
  43. (*f)(a1);
  44. }
  45. }
  46. template <typename A1, typename A2>
  47. R operator()(A1 a1, A2 a2) const
  48. {
  49. unordered_map<type_index, void (*)()>::const_iterator it = m_funcs.find(typeid(R(A1, A2)));
  50. if (it != m_funcs.end()) {
  51. R(*f)(A1, A2) = (R(*)(A1, A2))(it->second);
  52. (*f)(a1, a2);
  53. }
  54. }
  55. template <typename A1, typename A2, typename A3>
  56. R operator()(A1 a1, A2 a2, A3 a3) const
  57. {
  58. unordered_map<type_index, void (*)()>::const_iterator it = m_funcs.find(typeid(R(A1, A2, A3)));
  59. if (it != m_funcs.end()) {
  60. R(*f)(A1, A2, A3) = (R(*)(A1, A2, A3))(it->second);
  61. (*f)(a1, a2, a3);
  62. }
  63. }
  64. };
  65. struct myData {
  66. int myAge = 55;
  67. const char* pchar = "myData struct";
  68. enum en_sex { MALE, FEMALE, DIVERS } sex = MALE;
  69. };
  70. struct myOtherData : myData {
  71. int myOtherAge = 35;
  72. };
  73. void _1() {
  74. cout << "_1" << endl;
  75. }
  76. void _2(myData* a) {
  77. cout << "_2" << " " << a->pchar << endl;
  78. }
  79. void _3(myData* a, myOtherData* b) {
  80. b->myOtherAge = 25;
  81. cout << "_3" << " " << b->myOtherAge << endl;
  82. }
  83. void _4(myData* a, myOtherData* b, int c) {
  84. b->myOtherAge = c;
  85. cout << "_3" << " " << b->myOtherAge << endl;
  86. }
  87. int main() {
  88. myData mydata = { 55, "myData struct", myData::MALE };
  89. myOtherData myotherdata;
  90. MultiFuncObject<void> funcs;
  91. funcs += &_1;
  92. funcs += &_2;
  93. funcs += &_3;
  94. funcs += &_4;
  95. funcs();
  96. funcs(&mydata);
  97. funcs(&mydata, &myotherdata);
  98. funcs(&mydata, &myotherdata, 15);
  99. return 0;
  100. }