| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- #pragma once
- #include <typeinfo>
- #include <typeindex>
- #include <unordered_map>
- #include <iostream>
- using namespace std;
- template <typename R>
- class MultiFuncObject {
- unordered_map<type_index, void (*)()> m_funcs;
- public:
- MultiFuncObject<R> operator +=(R(*f)()) {
- // auto _r0_ = make_pair(1, typeid(R()));
- m_funcs[typeid(R())] = (void (*)()) f;
- return *this;
- }
- template <typename A1>
- MultiFuncObject<R> operator +=(R(*f)(A1)) {
- m_funcs[typeid(R(A1))] = (void (*)()) f;
- return *this;
- }
- template <typename A1, typename A2>
- MultiFuncObject<R> operator +=(R(*f)(A1, A2)) {
- m_funcs[typeid(R(A1, A2))] = (void (*)()) f;
- return *this;
- }
- template <typename A1, typename A2, typename A3>
- MultiFuncObject<R> operator +=(R(*f)(A1, A2, A3)) {
- m_funcs[typeid(R(A1, A2, A3))] = (void (*)()) f;
- return *this;
- }
- R operator()() const
- {
- unordered_map<type_index, void (*)()>::const_iterator it = m_funcs.find(typeid(R()));
- if (it != m_funcs.end()) {
- R(*f)() = (R(*)())(it->second);
- (*f)();
- }
- }
- template <typename A1>
- R operator()(A1 a1) const
- {
- unordered_map<type_index, void (*)()>::const_iterator it = m_funcs.find(typeid(R(A1)));
- if (it != m_funcs.end()) {
- R(*f)(A1) = (R(*)(A1))(it->second);
- (*f)(a1);
- }
- }
- template <typename A1, typename A2>
- R operator()(A1 a1, A2 a2) const
- {
- unordered_map<type_index, void (*)()>::const_iterator it = m_funcs.find(typeid(R(A1, A2)));
- if (it != m_funcs.end()) {
- R(*f)(A1, A2) = (R(*)(A1, A2))(it->second);
- (*f)(a1, a2);
- }
- }
- template <typename A1, typename A2, typename A3>
- R operator()(A1 a1, A2 a2, A3 a3) const
- {
- unordered_map<type_index, void (*)()>::const_iterator it = m_funcs.find(typeid(R(A1, A2, A3)));
- if (it != m_funcs.end()) {
- R(*f)(A1, A2, A3) = (R(*)(A1, A2, A3))(it->second);
- (*f)(a1, a2, a3);
- }
- }
- };
- //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- #if 0
- template <typename R>
- class NewMultiFuncObject {
- unordered_map<std::pair<int, type_index>, void (*)()> m_funcs; //
- public:
- NewMultiFuncObject<R> operator +=(R(*f)()) {
- // m_funcs[make_pair(1, typeid(R()))] = (void (*)()) f;
- return *this;
- }
- template <typename A1>
- NewMultiFuncObject<R> operator +=(R(*f)(A1)) {
- // m_funcs[make_pair(1, typeid(R(A1)))] = (void (*)()) f;
- return *this;
- }
- template <typename A1, typename A2>
- NewMultiFuncObject<R> operator +=(R(*f)(A1, A2)) {
- // m_funcs[make_pair(1, typeid(R(A1, A2)))] = (void (*)()) f;
- return *this;
- }
- template <typename A1, typename A2, typename A3>
- NewMultiFuncObject<R> operator +=(R(*f)(A1, A2, A3)) {
- // m_funcs[make_pair(1, typeid(R(A1, A2, A3)))] = (void (*)()) f;
- return *this;
- }
- R operator()() const
- {
- unordered_map<std::pair<int, type_index>, void (*)()>::const_iterator it = m_funcs.find(make_pair(1, typeid(R())));
- if (it != m_funcs.end()) {
- // R(*f)() = (R(*)())(it->second);
- // (*f)();
- }
- }
- template <typename A1>
- R operator()(A1 a1) const
- {
- unordered_map<std::pair<int, type_index>, void (*)()>::const_iterator it = m_funcs.find(make_pair(1, typeid(R(A1))));
- if (it != m_funcs.end()) {
- // R(*f)(A1) = (R(*)(A1))(it->second);
- // (*f)(a1);
- }
- }
- template <typename A1, typename A2>
- R operator()(A1 a1, A2 a2) const
- {
- unordered_map<std::pair<int, type_index>, void (*)()>::const_iterator it = m_funcs.find(make_pair(1, typeid(R(A1, A2))));
- if (it != m_funcs.end()) {
- // R(*f)(A1, A2) = (R(*)(A1, A2))(it->second);
- // (*f)(a1, a2);
- }
- }
- template <typename A1, typename A2, typename A3>
- R operator()(A1 a1, A2 a2, A3 a3) const
- {
- unordered_map<std::pair<int, type_index>, void (*)()>::const_iterator it = m_funcs.find(make_pair(1, typeid(R(A1, A2, A3))));
- if (it != m_funcs.end()) {
- // R(*f)(A1, A2, A3) = (R(*)(A1, A2, A3))(it->second);
- // (*f)(a1, a2, a3);
- }
- }
- };
- #endif
- #if 1
- template <typename R>
- class NewMultiFuncObject {
- unordered_map<type_index, void (*)()> m_funcs;
- unordered_map<string, unordered_map<std::pair<std::int32_t, type_index>, void (*)()>> m_funcs2;
- public:
- NewMultiFuncObject<R> operator +=(R(*f)()) {
- m_funcs2["test"][typeid(R())] = (void (*)()) f;
- return *this;
- }
- R operator()() const
- {
- unordered_map<type_index , void (*)()>::const_iterator it = m_funcs.find(typeid(R()));
- if (it != m_funcs.end()) {
- R(*f)() = (R(*)())(it->second);
- (*f)();
- }
- }
- };
- #endif
|