#include <vector> #include <functional> #include <iostream> #include <string> #include <windows.h> using namespace std;
struct Test { int n; Test () : n(3) {} void test0() { cout << "Test::test0: " << n << endl; }
void test1(int i) { cout << "Test::test1: " << i << endl; }
void test2(const string& a, int b) { cout << "Test::test2: " << a << "|" << b << endl; } };
void test0 () { cout << "test0" << endl; }
void test1 (int i) { cout << "test1: " << i << endl; }
void test2(const string& a, int b) { cout << "test2: " << a << "|" << b << endl; }
struct NullType {};
template <typename R, typename A=NullType, typename B=NullType, typename C=NullType, typename D=NullType, typename E=NullType, typename F=NullType, typename G=NullType, typename H=NullType, typename I=NullType, typename J=NullType, typename K=NullType, typename L=NullType, typename M=NullType, typename N=NullType, typename O=NullType> class Functor { };
template <typename R> struct Functor <R> { vector <pair<void*, void*> > _handlers; typedef R(*F)();
void add (F f) { _handlers.push_back (make_pair ((void*)0, (void*)f)); }
template <typename T> void add (const pair<T*, typename R(T::*)()>& f) { typedef R(T::*P)(); struct Pointer {P p;}; Pointer ptr = {f.second}; _handlers.push_back (make_pair ((void*)f.first, *(void**)&ptr)); }
void call () { for (size_t i=0; i<_handlers.size (); i++) { void* p = _handlers[i].first; if (p) // member function { void* mem_fun = _handlers[i].second; __asm{ mov ecx, p call mem_fun } } else { (*(F)_handlers[i].second)(); } } } };
template <typename R, typename A> struct Functor <R, A> { vector <pair<void*, void*> > _handlers; typedef R(*F)(A);
void add (F f) { _handlers.push_back (make_pair ((void*)0, (void*)f)); }
template <typename T> void add (const pair<T*, typename R(T::*)(A)>& f) { typedef R(T::*P)(A); struct Pointer {P p;}; Pointer ptr = {f.second}; _handlers.push_back (make_pair ((void*)f.first, *(void**)&ptr)); }
void call (A a) { for (size_t i=0; i<_handlers.size (); i++) { void* p = _handlers[i].first; if (p) // member function { void* mem_fun = _handlers[i].second; __asm{ push a mov ecx, p call mem_fun } } else { (*(F)_handlers[i].second)(a); } } } };
template <typename R, typename A, typename B> struct Functor < R , A , B > { vector <pair<void*, void*> > _handlers; typedef R(*F)(A, B);
void add (F f) { _handlers.push_back (make_pair ((void*)0, (void*)f)); }
template <typename T> void add (const pair<T*, typename R(T::*)(A, B)>& f) { typedef R(T::*P)(A, B); struct Pointer {P p;}; Pointer ptr = {f.second}; _handlers.push_back (make_pair ((void*)f.first, *(void**)&ptr)); }
void call (A a, B b) { for (size_t i=0; i<_handlers.size (); i++) { void* p = _handlers[i].first; if (p) // member function { void* mem_fun = _handlers[i].second; __asm{ push b push a mov ecx, p call mem_fun }
//T *obj = (T *)_handlers[i].first; //Pointer &ptr = (Pointer&)_handlers[i].second; //P p = ptr.p; //(obj->*p)(a,b); } else { (*(F)_handlers[i].second)(a, b); } } } };
int main(int argc, char* argv[]) { Test t;
Functor <void> f; f.add (make_pair(&t, &Test::test0)); f.add (test0); f.call ();
Functor<void, int> f1; f1.add (make_pair (&t, &Test::test1)); f1.add (test1); f1.call (93);
Functor< void, const string&, int > f2; f2.add (make_pair (&t, &Test::test2)); f2.add (test2); f2.call (string("hello"), 5); getchar(); return 0; }
|