Header1.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #pragma once
  2. #include <typeinfo>
  3. #include <typeindex>
  4. #include <unordered_map>
  5. #include <iostream>
  6. using namespace std;
  7. template <typename R>
  8. class MultiFuncObject {
  9. unordered_map<type_index, void (*)()> m_funcs;
  10. public:
  11. MultiFuncObject<R> operator +=(R(*f)()) {
  12. // auto _r0_ = make_pair(1, typeid(R()));
  13. m_funcs[typeid(R())] = (void (*)()) f;
  14. return *this;
  15. }
  16. template <typename A1>
  17. MultiFuncObject<R> operator +=(R(*f)(A1)) {
  18. m_funcs[typeid(R(A1))] = (void (*)()) f;
  19. return *this;
  20. }
  21. template <typename A1, typename A2>
  22. MultiFuncObject<R> operator +=(R(*f)(A1, A2)) {
  23. m_funcs[typeid(R(A1, A2))] = (void (*)()) f;
  24. return *this;
  25. }
  26. template <typename A1, typename A2, typename A3>
  27. MultiFuncObject<R> operator +=(R(*f)(A1, A2, A3)) {
  28. m_funcs[typeid(R(A1, A2, A3))] = (void (*)()) f;
  29. return *this;
  30. }
  31. R operator()() const
  32. {
  33. unordered_map<type_index, void (*)()>::const_iterator it = m_funcs.find(typeid(R()));
  34. if (it != m_funcs.end()) {
  35. R(*f)() = (R(*)())(it->second);
  36. (*f)();
  37. }
  38. }
  39. template <typename A1>
  40. R operator()(A1 a1) const
  41. {
  42. unordered_map<type_index, void (*)()>::const_iterator it = m_funcs.find(typeid(R(A1)));
  43. if (it != m_funcs.end()) {
  44. R(*f)(A1) = (R(*)(A1))(it->second);
  45. (*f)(a1);
  46. }
  47. }
  48. template <typename A1, typename A2>
  49. R operator()(A1 a1, A2 a2) const
  50. {
  51. unordered_map<type_index, void (*)()>::const_iterator it = m_funcs.find(typeid(R(A1, A2)));
  52. if (it != m_funcs.end()) {
  53. R(*f)(A1, A2) = (R(*)(A1, A2))(it->second);
  54. (*f)(a1, a2);
  55. }
  56. }
  57. template <typename A1, typename A2, typename A3>
  58. R operator()(A1 a1, A2 a2, A3 a3) const
  59. {
  60. unordered_map<type_index, void (*)()>::const_iterator it = m_funcs.find(typeid(R(A1, A2, A3)));
  61. if (it != m_funcs.end()) {
  62. R(*f)(A1, A2, A3) = (R(*)(A1, A2, A3))(it->second);
  63. (*f)(a1, a2, a3);
  64. }
  65. }
  66. };
  67. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  68. #if 0
  69. template <typename R>
  70. class NewMultiFuncObject {
  71. unordered_map<std::pair<int, type_index>, void (*)()> m_funcs; //
  72. public:
  73. NewMultiFuncObject<R> operator +=(R(*f)()) {
  74. // m_funcs[make_pair(1, typeid(R()))] = (void (*)()) f;
  75. return *this;
  76. }
  77. template <typename A1>
  78. NewMultiFuncObject<R> operator +=(R(*f)(A1)) {
  79. // m_funcs[make_pair(1, typeid(R(A1)))] = (void (*)()) f;
  80. return *this;
  81. }
  82. template <typename A1, typename A2>
  83. NewMultiFuncObject<R> operator +=(R(*f)(A1, A2)) {
  84. // m_funcs[make_pair(1, typeid(R(A1, A2)))] = (void (*)()) f;
  85. return *this;
  86. }
  87. template <typename A1, typename A2, typename A3>
  88. NewMultiFuncObject<R> operator +=(R(*f)(A1, A2, A3)) {
  89. // m_funcs[make_pair(1, typeid(R(A1, A2, A3)))] = (void (*)()) f;
  90. return *this;
  91. }
  92. R operator()() const
  93. {
  94. unordered_map<std::pair<int, type_index>, void (*)()>::const_iterator it = m_funcs.find(make_pair(1, typeid(R())));
  95. if (it != m_funcs.end()) {
  96. // R(*f)() = (R(*)())(it->second);
  97. // (*f)();
  98. }
  99. }
  100. template <typename A1>
  101. R operator()(A1 a1) const
  102. {
  103. unordered_map<std::pair<int, type_index>, void (*)()>::const_iterator it = m_funcs.find(make_pair(1, typeid(R(A1))));
  104. if (it != m_funcs.end()) {
  105. // R(*f)(A1) = (R(*)(A1))(it->second);
  106. // (*f)(a1);
  107. }
  108. }
  109. template <typename A1, typename A2>
  110. R operator()(A1 a1, A2 a2) const
  111. {
  112. unordered_map<std::pair<int, type_index>, void (*)()>::const_iterator it = m_funcs.find(make_pair(1, typeid(R(A1, A2))));
  113. if (it != m_funcs.end()) {
  114. // R(*f)(A1, A2) = (R(*)(A1, A2))(it->second);
  115. // (*f)(a1, a2);
  116. }
  117. }
  118. template <typename A1, typename A2, typename A3>
  119. R operator()(A1 a1, A2 a2, A3 a3) const
  120. {
  121. unordered_map<std::pair<int, type_index>, void (*)()>::const_iterator it = m_funcs.find(make_pair(1, typeid(R(A1, A2, A3))));
  122. if (it != m_funcs.end()) {
  123. // R(*f)(A1, A2, A3) = (R(*)(A1, A2, A3))(it->second);
  124. // (*f)(a1, a2, a3);
  125. }
  126. }
  127. };
  128. #endif
  129. #if 1
  130. template <typename R>
  131. class NewMultiFuncObject {
  132. unordered_map<type_index, void (*)()> m_funcs;
  133. unordered_map<string, unordered_map<std::pair<std::int32_t, type_index>, void (*)()>> m_funcs2;
  134. public:
  135. NewMultiFuncObject<R> operator +=(R(*f)()) {
  136. m_funcs2["test"][typeid(R())] = (void (*)()) f;
  137. return *this;
  138. }
  139. R operator()() const
  140. {
  141. unordered_map<type_index , void (*)()>::const_iterator it = m_funcs.find(typeid(R()));
  142. if (it != m_funcs.end()) {
  143. R(*f)() = (R(*)())(it->second);
  144. (*f)();
  145. }
  146. }
  147. };
  148. #endif