用 C++ 实现 C# 中的 委托/事件 (4-functor1)

类别:编程语言 点击:0 评论:0 推荐:

前两天看程序员杂志
看到关于 C# 中的委托/事件
觉得用起来好像是挺方便的
本人热衷于 C++
想想用 C++ 来模拟似乎也可以
于是就有了下面的代码...
(VC6 不支持偏特化 本人工作环境就是 VC6 痛啊~~~)

没有返回值的函数用 delegate
否则就用 delegate_rt
functor 也一样 functorN/functorN_rt
delegate 的模板参数可以是函数指针(非成员函数)
也可以是 functor
还可以是 delegate
functor 可用 make_functor/make_functor_rt 来生成
要是有偏特化 就可以去掉讨厌的 _rt 了 :(

关于委托 boost里有现成的
不过可能 VC6 里用不了

这些代码旨在个人研究
如果大家发现其中问题 希望能指出

//filename: functor1.h #ifndef _FUNCTOR1_H_ #define _FUNCTOR1_H_ template <typename P1> class functor1 { class deleobject { }; typedef void (*func_pt)(P1); typedef void (deleobject::*mem_func_pt)(P1); typedef void (deleobject::*cmem_func_pt)(P1) const; functor_base<deleobject, func_pt, mem_func_pt, cmem_func_pt> base; public: functor1() : base() {} functor1(func_pt pf) : base(pf) {} template<typename T> functor1(const T *pObject, void (T::*fp)(P1)) : base((const deleobject*)(pObject), *((mem_func_pt*)(&fp))) {} template<typename T> functor1(const T &pObject, void (T::*fp)(P1)) : base((const deleobject*)(pObject), *((mem_func_pt*)(&fp))) {} template<typename T> functor1(const T *pObject, void (T::*fp)(P1) const) : base((const deleobject*)(pObject), *((cmem_func_pt*)(&fp))) {} template<typename T> functor1(const T &pObject, void (T::*fp)(P1) const) : base((const deleobject*)(pObject), *((cmem_func_pt*)(&fp))) {} bool operator !=(const functor1& rhs) const { return !(*this == rhs); } bool operator ==(const functor1& rhs) const { return base == rhs.base; } bool operator ==(const void* rhs) const { return base == rhs; } functor1& operator =(const functor1& rhs) { base = rhs.base; return *this; } void operator()(P1 p1) const { if (base.m_pObject == NULL) (*base.m_pf)(p1); else (base.m_pObject->*(base.m_pmf))(p1); } }; template <typename P1> inline functor1<P1> make_functor(void (*fp)(P1)) { return functor1<P1>(fp); } template <class T, typename P1> inline functor1<P1> make_functor(const T* tp, void (T::*fp)(P1)) { return functor1<P1>(tp, fp); } template <class T, typename P1> inline functor1<P1> make_functor(const T* tp, void (T::*fp)(P1) const) { return functor1<P1>(tp, fp); } #endif // #ifndef _FUNCTOR1_H_

 

//filename: functor1_rt.h #ifndef _FUNCTOR1_RT_H_ #define _FUNCTOR1_RT_H_ template <typename R, typename P1> class functor1_rt { class deleobject { }; typedef R (*func_pt)(P1); typedef R (deleobject::*mem_func_pt)(P1); typedef R (deleobject::*cmem_func_pt)(P1) const; functor_base<deleobject, func_pt, mem_func_pt, cmem_func_pt> base; public: functor1_rt() : base() {} functor1_rt(func_pt pf) : base(pf) {} template<typename T> functor1_rt(const T *pObject, R (T::*fp)(P1)) : base((const deleobject*)(pObject), *((mem_func_pt*)(&fp))) {} template<typename T> functor1_rt(const T &pObject, R (T::*fp)(P1)) : base((const deleobject*)(pObject), *((mem_func_pt*)(&fp))) {} template<typename T> functor1_rt(const T *pObject, R (T::*fp)(P1) const) : base((const deleobject*)(pObject), *((cmem_func_pt*)(&fp))) {} template<typename T> functor1_rt(const T &pObject, R (T::*fp)(P1) const) : base((const deleobject*)(pObject), *((cmem_func_pt*)(&fp))) {} bool operator !=(const functor1_rt& rhs) const { return !(*this == rhs); } bool operator ==(const functor1_rt& rhs) const { return base == rhs.base; } bool operator ==(const void* rhs) const { return base == rhs; } functor1_rt& operator =(const functor1_rt& rhs) { base = rhs.base; return *this; } R operator()(P1 p1) const { if (base.m_pObject == NULL) return (*base.m_pf)(p1); else return (base.m_pObject->*(base.m_pmf))(p1); } }; template <typename R, typename P1> inline functor1_rt<R, P1> make_functor_rt(R (*fp)(P1)) { return functor1_rt<R, P1>(fp); } template <typename R, class T, typename P1> inline functor1_rt<R, P1> make_functor_rt(const T* tp, R (T::*fp)(P1)) { return functor1_rt<R, P1>(tp, fp); } template <typename R, class T, typename P1> inline functor1_rt<R, P1> make_functor_rt(const T* tp, R (T::*fp)(P1) const) { return functor1_rt<R, P1>(tp, fp); } #endif // #ifndef _FUNCTOR1_RT_H_

本文地址:http://com.8s8s.com/it/it28930.htm