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

前两天看程序员杂志
看到关于 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: functor2.h
#ifndef _FUNCTOR2_H_
#define _FUNCTOR2_H_

template <typename P1, typename P2>
class functor2
{
  class deleobject
  {
  };
  typedef void (*func_pt)(P1, P2);
  typedef void (deleobject::*mem_func_pt)(P1, P2);
  typedef void (deleobject::*cmem_func_pt)(P1, P2) const;
  functor_base<deleobject,
    func_pt, 
    mem_func_pt, 
    cmem_func_pt> base;
  
  public: 
    functor2() : base() {}
    functor2(func_pt pf) : base(pf) {}
    template<typename T> 
      functor2(const T *pObject, void (T::*fp)(P1, P2)) :
        base((const deleobject*)(pObject), 
          *((mem_func_pt*)(&fp))) {}
    template<typename T> 
      functor2(const T &pObject, void (T::*fp)(P1, P2)) :
        base((const deleobject*)(pObject), 
          *((mem_func_pt*)(&fp))) {}
    template<typename T> 
      functor2(const T *pObject, void (T::*fp)(P1, P2) const) :
        base((const deleobject*)(pObject), 
          *((cmem_func_pt*)(&fp))) {}
    template<typename T> 
      functor2(const T &pObject, void (T::*fp)(P1, P2) const) :
        base((const deleobject*)(pObject), 
          *((cmem_func_pt*)(&fp))) {}
  bool operator !=(const functor2& rhs) const { return !(*this == rhs); }
  bool operator ==(const functor2& rhs) const
  {
    return base == rhs.base;
  }
  bool operator ==(const void* rhs) const
  {
    return base == rhs;
  }
  functor2& operator =(const functor2& rhs)
  {
    base = rhs.base;
    return *this;
  }
  void operator()(P1 p1, P2 p2) const
  {
    if (base.m_pObject == NULL)
      (*base.m_pf)(p1, p2);
    else
      (base.m_pObject->*(base.m_pmf))(p1, p2);
  }
};

template <typename P1, typename P2>
inline functor2<P1, P2> make_functor(void (*fp)(P1, P2))
{
  return functor2<P1, P2>(fp);
}

template <class T, typename P1, typename P2>
inline functor2<P1, P2> make_functor(const T* tp, void (T::*fp)(P1, P2))
{
  return functor2<P1, P2>(tp, fp);
}

template <class T, typename P1, typename P2>
inline functor2<P1, P2> make_functor(const T* tp, void (T::*fp)(P1, P2) const)
{
  return functor2<P1, P2>(tp, fp);
}

#endif // #ifndef _FUNCTOR2_H_
 
//filename: functor2_rt.h
#ifndef _FUNCTOR2_RT_H_
#define _FUNCTOR2_RT_H_

template <typename R, typename P1, typename P2>
class functor2_rt
{
  class deleobject
  {
  };
  typedef R (*func_pt)(P1, P2);
  typedef R (deleobject::*mem_func_pt)(P1, P2);
  typedef R (deleobject::*cmem_func_pt)(P1, P2) const;
  functor_base<deleobject,
    func_pt, 
    mem_func_pt, 
    cmem_func_pt> base;
  
  public: 
    functor2_rt() : base() {}
    functor2_rt(func_pt pf) : base(pf) {}
    template<typename T> 
      functor2_rt(const T *pObject, R (T::*fp)(P1, P2)) :
        base((const deleobject*)(pObject), 
          *((mem_func_pt*)(&fp))) {}
    template<typename T> 
      functor2_rt(const T &pObject, R (T::*fp)(P1, P2)) :
        base((const deleobject*)(pObject), 
          *((mem_func_pt*)(&fp))) {}
    template<typename T> 
      functor2_rt(const T *pObject, R (T::*fp)(P1, P2) const) :
        base((const deleobject*)(pObject), 
          *((cmem_func_pt*)(&fp))) {}
    template<typename T> 
      functor2_rt(const T &pObject, R (T::*fp)(P1, P2) const) :
        base((const deleobject*)(pObject), 
          *((cmem_func_pt*)(&fp))) {}
  bool operator !=(const functor2_rt& rhs) const { return !(*this == rhs); }
  bool operator ==(const functor2_rt& rhs) const
  {
    return base == rhs.base;
  }
  bool operator ==(const void* rhs) const
  {
    return base == rhs;
  }
  functor2_rt& operator =(const functor2_rt& rhs)
  {
    base = rhs.base;
    return *this;
  }
  R operator()(P1 p1, P2 p2) const
  {
    if (base.m_pObject == NULL)
      return (*base.m_pf)(p1, p2);
    else
      return (base.m_pObject->*(base.m_pmf))(p1, p2);
  }
};

template <typename R, typename P1, typename P2>
inline functor2_rt<R, P1, P2> make_functor_rt(R (*fp)(P1, P2))
{
  return functor2_rt<R, P1, P2>(fp);
}

template <typename R, class T, typename P1, typename P2>
inline functor2_rt<R, P1, P2> make_functor_rt(const T* tp, R (T::*fp)(P1, P2))
{
  return functor2_rt<R, P1, P2>(tp, fp);
}

template <typename R, class T, typename P1, typename P2>
inline functor2_rt<R, P1, P2> make_functor_rt(const T* tp, R (T::*fp)(P1, P2) const)
{
  return functor2_rt<R, P1, P2>(tp, fp);
}

#endif // #ifndef _FUNCTOR2_RT_H_
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值