CSDN博客

img coolcch

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

发表于2004/3/21 17:43:00  462人阅读

前两天看程序员杂志
看到关于 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_
0 0

相关博文

我的热门文章

img
取 消
img