Skip to content

Instantly share code, notes, and snippets.

@chenyukang
Created October 16, 2012 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chenyukang/3900077 to your computer and use it in GitHub Desktop.
Save chenyukang/3900077 to your computer and use it in GitHub Desktop.
Peng's delegate
/*******************************************************************************
*
* @name : DELEGATE_H
*
* @author : BigBird (peng@nextopsoftware.com)
* @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 (peng@nextopsoftware.com)
*
* 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