前两天看程序员杂志
看到关于 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: functor0.h
#ifndef _FUNCTOR0_H_
#define _FUNCTOR0_H_
class functor0
{
class deleobject
{
};
typedef void (*func_pt)();
typedef void (deleobject::*mem_func_pt)();
typedef void (deleobject::*cmem_func_pt)() const;
functor_base<deleobject,
func_pt,
mem_func_pt,
cmem_func_pt> base;
public:
functor0() : base() {}
functor0(func_pt pf) : base(pf) {}
template<typename T>
functor0(const T *pObject, void (T::*fp)()) :
base((const deleobject*)(pObject),
*((mem_func_pt*)(&fp))) {}
template<typename T>
functor0(const T &pObject, void (T::*fp)()) :
base((const deleobject*)(pObject),
*((mem_func_pt*)(&fp))) {}
template<typename T>
functor0(const T *pObject, void (T::*fp)() const) :
base((const deleobject*)(pObject),
*((cmem_func_pt*)(&fp))) {}
template<typename T>
functor0(const T &pObject, void (T::*fp)() const) :
base((const deleobject*)(pObject),
*((cmem_func_pt*)(&fp))) {}
bool operator !=(const functor0& rhs) const { return !(*this == rhs); }
bool operator ==(const functor0& rhs) const
{
return base == rhs.base;
}
bool operator ==(const void* rhs) const
{
return base == rhs;
}
functor0& operator =(const functor0& rhs)
{
base = rhs.base;
return *this;
}
void operator()() const
{
if (base.m_pObject == NULL)
(*base.m_pf)();
else
(base.m_pObject->*(base.m_pmf))();
}
};
inline functor0 make_functor(void (*fp)())
{
return functor0(fp);
}
template <class T>
inline functor0 make_functor(const T* tp, void (T::*fp)())
{
return functor0(tp, fp);
}
template <class T>
inline functor0 make_functor(const T* tp, void (T::*fp)() const)
{
return functor0(tp, fp);
}
#endif // #ifndef _FUNCTOR0_H_
//filename: functor0_rt.h
#ifndef _FUNCTOR0_RT_H_
#define _FUNCTOR0_RT_H_
template <typename R>
class functor0_rt
{
class deleobject
{
};
typedef R (*func_pt)();
typedef R (deleobject::*mem_func_pt)();
typedef R (deleobject::*cmem_func_pt)() const;
functor_base<deleobject,
func_pt,
mem_func_pt,
cmem_func_pt> base;
public:
functor0_rt() : base() {}
functor0_rt(func_pt pf) : base(pf) {}
template<typename T>
functor0_rt(const T *pObject, R (T::*fp)()) :
base((const deleobject*)(pObject),
*((mem_func_pt*)(&fp))) {}
template<typename T>
functor0_rt(const T &pObject, R (T::*fp)()) :
base((const deleobject*)(pObject),
*((mem_func_pt*)(&fp))) {}
template<typename T>
functor0_rt(const T *pObject, R (T::*fp)() const) :
base((const deleobject*)(pObject),
*((cmem_func_pt*)(&fp))) {}
template<typename T>
functor0_rt(const T &pObject, R (T::*fp)() const) :
base((const deleobject*)(pObject),
*((cmem_func_pt*)(&fp))) {}
bool operator !=(const functor0_rt& rhs) const { return !(*this == rhs); }
bool operator ==(const functor0_rt& rhs) const
{
return base == rhs.base;
}
bool operator ==(const void* rhs) const
{
return base == rhs;
}
functor0_rt& operator =(const functor0_rt& rhs)
{
base = rhs.base;
return *this;
}
R operator()() const
{
if (base.m_pObject == NULL)
return (*base.m_pf)();
else
return (base.m_pObject->*(base.m_pmf))();
}
};
template <typename R>
inline functor0_rt<R> make_functor_rt(R (*fp)())
{
return functor0_rt<R>(fp);
}
template <typename R, class T>
inline functor0_rt<R> make_functor_rt(const T* tp, R (T::*fp)())
{
return functor0_rt<R>(tp, fp);
}
template <typename R, class T>
inline functor0_rt<R> make_functor_rt(const T* tp, R (T::*fp)() const)
{
return functor0_rt<R>(tp, fp);
}
#endif // #ifndef _FUNCTOR0_RT_H_