Created
October 16, 2012 15:46
-
-
Save chenyukang/3900077 to your computer and use it in GitHub Desktop.
Peng's delegate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************* | |
* | |
* @name : DELEGATE_H | |
* | |
* @author : BigBird ([email protected]) | |
* @date : 2012-10-16 21:58:04 | |
* | |
* @brief : | |
* | |
*******************************************************************************/ | |
#ifndef dpDelegate_h | |
#define dpDelegate_h | |
#include <vector> | |
template< class T > | |
struct DelegateFuncBase { | |
T* obj; | |
DelegateFuncBase() : obj (NULL) {} | |
virtual void evaluate() = 0; | |
virtual ~DelegateFuncBase() {} | |
}; | |
template< class T, typename P0 = void, | |
typename P1 = void > | |
class DelegateFunc; | |
template< class T, typename P0, typename P1 > | |
class DelegateFunc | |
: public DelegateFuncBase<T> { | |
typedef DelegateFuncBase<T> Base; | |
typedef void (T::*Function) (P0,P1); | |
Function func; | |
P0 p0; P1 p1; | |
public: | |
DelegateFunc(Function f, P0 s0, P1 s1) | |
:func(f), | |
p0(s0), p1(s1), | |
Base() {} | |
void evaluate() { (Base::obj->*func)(p0, p1); } | |
}; | |
template< class T, typename P0 > | |
class DelegateFunc< T, P0 > | |
: public DelegateFuncBase<T> { | |
typedef DelegateFuncBase<T> Base; | |
typedef void (T::*Function) (P0); | |
Function func; | |
P0 p0; | |
public: | |
DelegateFunc(Function f, P0 s0) | |
:func(f), p0(s0), | |
Base() {} | |
void evaluate() { (Base::obj->*func)(p0); } | |
}; | |
template< class T > | |
class DelegateFunc< T > | |
: public DelegateFuncBase<T> { | |
typedef DelegateFuncBase<T> Base; | |
typedef void (T::*Function) (); | |
Function func; | |
public: | |
DelegateFunc(Function f) | |
:func(f), Base() {} | |
void evaluate() { (Base::obj->*func)(); } | |
}; | |
template< class T > | |
class Delegate { | |
typedef DelegateFuncBase<T> DelegateFuncBaseType; | |
std::vector< DelegateFuncBaseType* > delegates; | |
public: | |
void operator += (DelegateFuncBaseType* o) { | |
delegates.push_back(o); | |
} | |
void evaluation(T* obj) { | |
for(int i = 0; i < delegates.size(); ++i) { | |
DelegateFuncBaseType* df = delegates[i]; | |
df->obj = obj; | |
df->evaluate(); | |
} | |
} | |
~Delegate() { | |
for(int i = 0; i < delegates.size(); ++i) { | |
delete delegates[i]; | |
} | |
} | |
}; | |
template< typename T > | |
DelegateFuncBase<T>* delegate(void (T::*func)()) { | |
return new DelegateFunc<T>(func); | |
} | |
template< typename T, typename P0 > | |
DelegateFuncBase<T>* delegate(void (T::*func)(P0), P0 p0) { | |
return new DelegateFunc<T,P0>(func,p0); | |
} | |
template< typename T, typename P0, typename P1 > | |
DelegateFuncBase<T>* delegate(void (T::*func)(P0,P1), P0 p0, P1 p1) { | |
return new DelegateFunc<T,P0,P1>(func,p0,p1); | |
} | |
#endif /*dpDelegate_h*/ | |
/******************************************************************************* | |
* | |
* main.cpp | |
* | |
* @brief | |
* | |
* @author BigBird ([email protected]) | |
* | |
* COPYRIGHT (C) 2011, Nextop INC., ALL RIGHTS RESERVED. | |
* | |
*******************************************************************************/ | |
// main.cc | |
#include <iostream> | |
#include "delegate.h" | |
using namespace std; | |
class B { | |
int v; | |
public: | |
B() : v(0) {} | |
void print() { | |
cout << "Hello World!" << endl; | |
} | |
void assign(int d) { | |
v = d; | |
} | |
void print_value() { | |
cout << v << endl; | |
} | |
}; | |
int main () { | |
Delegate<B> db; | |
db += (delegate(&B::assign, 2)); | |
db += (delegate(&B::print_value)); | |
db += (delegate(&B::print)); | |
B b; | |
db.evaluation(&b); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment