用C++实现C#中的委托/事件(标准C++之升级版)

看到csdn上前辈高人写的文章
http://www.csdn.net/Develop/Read_Article.asp?Id=13147
觉得用 VC6 来写这个确实有点束手束脚
我把他的代码拿来改造了一下 于是就有了这个升级版
这次可真的是完美模拟了 :)
在 GCC 下编译运行通过
VC7 应该也没问题吧
(代码五颜六色的太大了 一篇文档放不下 这次就不弄了
自己粘到C++的编辑器里去看吧!)

2004.3.24 代码修改
内容:修改 template <typename T> class delegate
使之能够用于一般的函数对象 如 std::mem_fun_t, std::mem_fun_ref_t
注:用于函数对象时 只支持 Multi-cast delegates

//filename: funtraits.h
#ifndef _FUNTRAITS_H_
#define _FUNTRAITS_H_

// 函数traits,用来提取函数的返回类型
template<class T>
struct function_traits
{
};
template<class RT>
struct function_traits< RT(*)() >
{
  typedef RT(*function_type)();
  typedef RT result_type;
};
template<class RT,class AT>
struct function_traits< RT(*)(AT) >
{
  typedef RT(*function_type)(AT);
  typedef RT result_type;
  typedef AT argument_type;
};
template<class RT,class AT1,class AT2>
struct function_traits< RT(*)(AT1,AT2) >
{
  typedef RT(*function_type)(AT1,AT2);
  typedef RT result_type;
  typedef AT1 first_argument_type;
  typedef AT2 second_argument_type;
};
template<class RT,class AT1,class AT2,class AT3>
struct function_traits< RT(*)(AT1,AT2,AT3) >
{
  typedef RT(*function_type)(AT1,AT2,AT3);
  typedef RT result_type;
  typedef AT1 first_argument_type;
  typedef AT2 second_argument_type;
  typedef AT3 third_argument_type;
};

// 函数traits,用来提取类成员函数的返回类型
template<class RT, class OT>
struct function_traits< RT (OT::*)() >
{
  typedef RT(*function_type)();
  typedef OT object_type;
  typedef RT result_type;
};
template<class RT, class OT, class AT>
struct function_traits< RT (OT::*)(AT) >
{
  typedef RT(*function_type)(AT);
  typedef OT object_type;
  typedef RT result_type;
  typedef AT argument_type;

  typedef AT first_argument_type;
};
template<class RT,class OT,class AT1,class AT2>
struct function_traits< RT (OT::*)(AT1,AT2) >
{
  typedef RT(*function_type)(AT1,AT2);
  typedef OT object_type;
  typedef RT result_type;
  typedef AT1 first_argument_type;
  typedef AT2 second_argument_type;
};
template<class RT,class OT,class AT1,class AT2,class AT3>
struct function_traits< RT (OT::*)(AT1,AT2,AT3) >
{
  typedef RT(*function_type)(AT1,AT2,AT3);
  typedef OT object_type;
  typedef RT result_type;
  typedef AT1 first_argument_type;
  typedef AT2 second_argument_type;
  typedef AT3 third_argument_type;
};
template<class RT, class OT>
struct function_traits< RT (OT::*)() const >
{
  typedef RT(*function_type)();
  typedef OT object_type;
  typedef RT result_type;
};
template<class RT, class OT, class AT>
struct function_traits< RT (OT::*)(AT) const >
{
  typedef RT(*function_type)(AT);
  typedef OT object_type;
  typedef RT result_type;
  typedef AT argument_type;

  typedef AT first_argument_type;
};
template<class RT,class OT,class AT1,class AT2>
struct function_traits< RT (OT::*)(AT1,AT2) const >
{
  typedef RT(*function_type)(AT1,AT2);
  typedef OT object_type;
  typedef RT result_type;
  typedef AT1 first_argument_type;
  typedef AT2 second_argument_type;
};
template<class RT,class OT,class AT1,class AT2,class AT3>
struct function_traits< RT (OT::*)(AT1,AT2,AT3) const >
{
  typedef RT(*function_type)(AT1,AT2,AT3);
  typedef OT object_type;
  typedef RT result_type;
  typedef AT1 first_argument_type;
  typedef AT2 second_argument_type;
  typedef AT3 third_argument_type;
};

// 把一个普通函数类向转化为类型兼容的指定类的成员函数类型
template <typename OT, typename PFT>
struct to_member_function_pointer
{
};
template <typename OT,typename RT>
struct to_member_function_pointer< OT, RT(*)() >
{
  typedef RT (OT::*type)();
};
template <typename OT, typename RT, typename AT>
struct to_member_function_pointer< OT, RT(*)(AT) >
{
  typedef RT (OT::*type)(AT);
};
template <typename OT, typename RT, typename AT1, typename AT2>
struct to_member_function_pointer< OT, RT(*)(AT1,AT2) >
{
  typedef RT (OT::*type)(AT1,AT2);
};
template <typename OT, typename RT, typename AT1, typename AT2, typename AT3>
struct to_member_function_pointer< OT, RT(*)(AT1,AT2,AT3) >
{
  typedef RT (OT::*type)(AT1,AT2,AT3);
};

// 转化为const 成员函数
template <typename OT, typename PFT>
struct to_const_member_function_pointer
{
};
template <typename OT, typename RT>
struct to_const_member_function_pointer< OT, RT(*)() >
{
  typedef RT (OT::*type)() const;
};
template <typename OT, typename RT, typename AT>
struct to_const_member_function_pointer< OT, RT(*)(AT) >
{
  typedef RT (OT::*type)(AT) const;
};
template <typename OT, typename RT, typename AT1, typename AT2>
struct to_const_member_function_pointer< OT, RT(*)(AT1,AT2) >
{
  typedef RT (OT::*type)(AT1,AT2) const;
};
template <typename OT, typename RT, typename AT1, typename AT2, typename AT3>
struct to_const_member_function_pointer< OT, RT(*)(AT1,AT2,AT3) >
{
  typedef RT (OT::*type)(AT1,AT2,AT3) const;
};

#endif // #ifndef _FUNTRAITS_H_

//filename: delegate.h
#ifndef _DELEGATE_H_
#define _DELEGATE_H_

#include <vector>
#include <algorithm>
#include <stdexcept>
#include "funtraits.h"

#define DELEGATE_DEFINE / 
protected: / 
  typedef typename base_class::deleobject / 
    deleobject; / 
  typedef typename base_class::object_member_function_pointer / 
    object_member_function_pointer; / 
  typedef typename base_class::const_object_member_function_pointer / 
    const_object_member_function_pointer; / 
  typedef typename base_class::result_type / 
    result_type; / 
  typedef typename base_class::function_union / 
    function_union;

#define DELEGATE_COMMON / 
public: / 
  delegate() {} / 
  delegate(function_pointer pf) : base_class(pf) {} / 
 template<typename O> / 
  delegate(const O *pObject, / 
    typename to_member_function_pointer<O, function_pointer>::type pmf) : / 
    base_class(pObject, pmf) {} / 
 template<typename O> / 
  delegate(const O *pObject, / 
    typename to_const_member_function_pointer<O, function_pointer>::type pmf) : / 
    base_class(pObject, pmf) {} / 
  delegate(const delegate& rhs) : / 
    base_class(rhs) {} / 
  delegate &operator =(function_pointer pf) / 
  { / 
    base_class::operator =(pf); / 
    return *this; / 
  } / 
  delegate &operator =(const base_class& rhs) / 
  { / 
    base_class::operator =(rhs); / 
    return *this; / 
  }
  
#define DELEGATE_OPERATOR_BRACKET / 
public: / 
  void operator()() const / 
  { / 
    typename std::vector<function_union>::const_iterator i = funcs.begin(); / 
    for (; i != funcs.end(); ++i) / 
    { / 
      if (i->m_pObject == NULL) / 
        (*i->m_pf)(); / 
      else / 
        (i->m_pObject->*(i->m_pmf))(); / 
    } / 
  } / 
 template <typename P1> / 
  void operator()(P1 p1) const / 
  { / 
    typename std::vector<function_union>::const_iterator i = funcs.begin(); / 
    for (; i != funcs.end(); ++i) / 
    { / 
      if (i->m_pObject == NULL) / 
        (*i->m_pf)(p1); / 
      else / 
        (i->m_pObject->*(i->m_pmf))(p1); / 
    } / 
  } / 
 template <typename P1, typename P2> / 
  void operator()(P1 p1, P2 p2) const / 
  { / 
    typename std::vector<function_union>::const_iterator i = funcs.begin(); / 
    for (; i != funcs.end(); ++i) / 
    { / 
      if (i->m_pObject == NULL) / 
        (*i->m_pf)(p1, p2); / 
      else / 
        (i->m_pObject->*(i->m_pmf))(p1, p2); / 
    } / 
  } / 
 template <typename P1, typename P2, typename P3> / 
  void operator()(P1 p1, P2 p2, P3 p3) const / 
  { / 
    typename std::vector<function_union>::const_iterator i = funcs.begin(); / 
    for (; i != funcs.end(); ++i) / 
    { / 
      if (i->m_pObject == NULL) / 
        (*i->m_pf)(p1, p2, p3); / 
      else / 
        (i->m_pObject->*(i->m_pmf))(p1, p2, p3); / 
    } / 
  }

#define DELEGATE_OPERATOR_BRACKET_RT / 
public: / 
  result_type operator()() const / 
  { / 
    if (funcs.size() != 1) / 
      throw std::runtime_error("non-multicast delegate: method error!"); / 
    if (funcs.front().m_pObject == NULL) / 
      return (*funcs.front().m_pf)(); / 
    return (funcs.front().m_pObject->*(funcs.front().m_pmf))(); / 
  } / 
 template <typename P1> / 
  result_type operator()(P1 p1) const / 
  { / 
    if (funcs.size() != 1) / 
      throw std::runtime_error("non-multicast delegate: method error!"); / 
    if (funcs.front().m_pObject == NULL) / 
      return (*funcs.front().m_pf)(p1); / 
    return (funcs.front().m_pObject->*(funcs.front().m_pmf))(p1); / 
  } / 
 template <typename P1, typename P2> / 
  result_type operator()(P1 p1, P2 p2) const / 
  { / 
    if (funcs.size() != 1) / 
      throw std::runtime_error("non-multicast delegate: method error!"); / 
    if (funcs.front().m_pObject == NULL) / 
      return (*funcs.front().m_pf)(p1, p2); / 
    return (funcs.front().m_pObject->*(funcs.front().m_pmf))(p1, p2); / 
  } / 
 template <typename P1, typename P2, typename P3> / 
  result_type operator()(P1 p1, P2 p2, P3 p3) const / 
  { / 
    if (funcs.size() != 1) / 
      throw std::runtime_error("non-multicast delegate: method error!"); / 
    if (funcs.front().m_pObject == NULL) / 
      return (*funcs.front().m_pf)(p1, p2, p3); / 
    return (funcs.front().m_pObject->*(funcs.front().m_pmf))(p1, p2, p3); / 
  }


template <typename T>
class delegate
{
protected:
  typedef T functor;
  std::vector<functor> funcs;
public:
  delegate() {}
  delegate(const functor& Func)
  {
//    if (!(Func == NULL)) 
      funcs.push_back(Func);
  }
  delegate(const delegate& rhs) : funcs(rhs.funcs) {}

  void operator ()() const
  {
    typename std::vector<functor>::const_iterator i = funcs.begin();
    for (; i != funcs.end(); ++i)
    {
      (*i)();
    }
  }
 template <typename P1>
  void operator ()(P1 p1) const
  {
    typename std::vector<functor>::const_iterator i = funcs.begin();
    for (; i != funcs.end(); ++i)
    {
      (*i)(p1);
    }
  }
 template <typename P1, typename P2>
  void operator ()(P1 p1, P2 p2) const
  {
    typename std::vector<functor>::const_iterator i = funcs.begin();
    for (; i != funcs.end(); ++i)
    {
      (*i)(p1, p2);
    }
  }
 template <typename P1, typename P2, typename P3>
  void operator ()(P1 p1, P2 p2, P3 p3) const
  {
    typename std::vector<functor>::const_iterator i = funcs.begin();
    for (; i != funcs.end(); ++i)
    {
      (*i)(p1, p2, p3);
    }
  }
  bool operator ==(const delegate& rhs) const
  {
    return funcs == rhs.funcs;
  }
/*  bool operator ==(const void* rhs) const
  {
    if (funcs.size())
      return &funcs == rhs;
    return NULL == rhs;
  }*/

  delegate& operator =(const delegate& rhs)
  {
    funcs = rhs.funcs;
    return *this;
  }
  delegate& operator +=(const delegate& rhs)
  {
    if (this == &rhs)
      return *this;
    typename std::vector<functor>::const_iterator j, i = rhs.funcs.begin();
    for (; i != rhs.funcs.end(); ++i)
    {
      j = std::find(funcs.begin(), funcs.end(), *i);
      if (j == funcs.end())
        funcs.push_back(*i);
    }
    return *this;
  }
  delegate& operator -=(const delegate& rhs)
  {
    if (this == &rhs)
    {
      funcs.clear();
      return *this;
    }
    typename std::vector<functor>::iterator j;
    typename std::vector<functor>::const_iterator i = rhs.funcs.begin();
    for (; i != rhs.funcs.end(); ++i)
    {
      j = std::find(funcs.begin(), funcs.end(), *i);
      if (j != funcs.end())
        funcs.erase(j);
    }
    return *this;
  }
  delegate operator +(const delegate& rhs) const
  {
    return delegate(*this) += rhs;
  }
  delegate operator -(const delegate& rhs) const
  {
    return delegate(*this) -= rhs;
  }

  delegate& operator =(const functor& rhs)
  {
    funcs.clear();
//    if (!(rhs == NULL))
      funcs.push_back(rhs);
    return *this;
  }
  delegate& operator +=(const functor& rhs)
  {
//    if (rhs == NULL)
//      return *this;
    typename std::vector<functor>::const_iterator j =
      std::find(funcs.begin(), funcs.end(), rhs);
    if (j == funcs.end())
      funcs.push_back(rhs);
    return *this;
  }
  delegate& operator -=(const functor& rhs)
  {
//    if (rhs == NULL)
//      return *this;
    typename std::vector<functor>::iterator j =
      std::find(funcs.begin(), funcs.end(), rhs);
    if (j != funcs.end())
      funcs.erase(j);
    return *this;
  }
  delegate operator +(const functor& rhs) const
  {
    return delegate(*this) += rhs;
  }
  delegate operator -(const functor& rhs) const
  {
    return delegate(*this) -= rhs;
  }
  friend delegate operator +(const functor& lhs, const delegate& rhs)
  {
    return rhs + lhs;
  }
};

template <typename T>
class delegate_base
{
protected:
  class deleobject {};
  typedef T function_pointer;
  typedef typename to_member_function_pointer<deleobject, function_pointer>::type
    object_member_function_pointer;
  typedef typename to_const_member_function_pointer<deleobject, function_pointer>::type
    const_object_member_function_pointer;
  typedef typename function_traits<function_pointer>::result_type
    result_type;
  struct function_union
  {
    union
    {
      function_pointer m_pf;
      object_member_function_pointer m_pmf;
      const_object_member_function_pointer m_pcmf;
    };
    union
    {
      deleobject *m_pObject;
      const deleobject *m_pcObject;
    };
    function_union() : m_pf(NULL), m_pObject(NULL) {}
    function_union(function_pointer pf) : m_pf(pf), m_pObject(NULL) {}
   template <class O>
    function_union(const O* pObject,
      typename to_member_function_pointer<O, function_pointer>::type pmf) :
      m_pmf(*reinterpret_cast<object_member_function_pointer*>(&pmf)),
      m_pcObject(reinterpret_cast<const deleobject*>(pObject)) {}
   template <class O>
    function_union(const O* pObject,
      typename to_const_member_function_pointer<O, function_pointer>::type pmf) :
      m_pcmf(*reinterpret_cast<const_object_member_function_pointer*>(&pmf)),
      m_pcObject(reinterpret_cast<const deleobject*>(pObject)) {}
    bool operator ==(const function_union& rhs) const
    {
      if (m_pObject == NULL)
        return rhs.m_pObject == NULL && m_pf == rhs.m_pf;
      return m_pObject == rhs.m_pObject && m_pmf == rhs.m_pmf;
    }
    bool operator ==(function_pointer rhs) const
    {
      return m_pObject == NULL && m_pf == rhs;
    }
    bool operator ==(const void* rhs) const
    {
      return m_pObject == NULL && m_pf == rhs;
    }
  };
  std::vector<function_union> funcs;
public:
  delegate_base() {}
  delegate_base(function_pointer pf) { if (!(pf == NULL)) funcs.push_back(pf); }
 template<typename O>
  delegate_base(const O *pObject,
    typename to_member_function_pointer<O, function_pointer>::type pmf)
  {
    if (pObject && pmf)
      funcs.push_back(function_union(pObject, pmf));
  }
 template<typename O>
  delegate_base(const O *pObject,
    typename to_const_member_function_pointer<O, function_pointer>::type pmf)
  {
    if (pObject && pmf)
      funcs.push_back(function_union(pObject, pmf));
  }
  delegate_base(const delegate_base& rhs) : funcs(rhs.funcs) {}

  bool operator ==(const void* rhs) const
  {
    if (funcs.size())
      return &funcs == rhs;
    return NULL == rhs;
  }
  bool operator ==(const delegate_base& rhs) const
  {
    return funcs == rhs.funcs;
  }
  bool operator !=(const delegate_base& rhs) const
  {
    return funcs != rhs.funcs;
  }
  delegate_base &operator =(const delegate_base& rhs)
  {
    funcs = rhs.funcs;
    return *this;
  }
  delegate_base &operator +=(const delegate_base& rhs)
  {
    if (this == &rhs)
      return *this;
    typename std::vector<function_union>::const_iterator j,
      i = rhs.funcs.begin();
    for (; i != rhs.funcs.end(); ++i)
    {
      j = std::find(funcs.begin(), funcs.end(), *i);
      if (j == funcs.end())
        funcs.push_back(*i);
    }
    return *this;
  }
  delegate_base& operator -=(const delegate_base& rhs)
  {
    if (this == &rhs)
    {
      funcs.clear();
      return *this;
    }
    typename std::vector<function_union>::iterator j;
    typename std::vector<function_union>::const_iterator i = rhs.funcs.begin();
    for (; i != rhs.funcs.end(); ++i)
    {
      j = std::find(funcs.begin(), funcs.end(), *i);
      if (j != funcs.end())
        funcs.erase(j);
    }
    return *this;
  }
  delegate_base operator +(const delegate_base& rhs) const
  {
    return delegate_base(*this) += rhs;
  }
  delegate_base operator -(const delegate_base& rhs) const
  {
    return delegate_base(*this) -= rhs;
  }

  delegate_base &operator =(function_pointer pf)
  {
    funcs.clear();
    if (!(pf == NULL)) funcs.push_back(pf);
    return *this;
  }
  delegate_base& operator +=(function_pointer rhs)
  {
    if (rhs == NULL)
      return *this;
    typename std::vector<function_union>::const_iterator j =
      std::find(funcs.begin(), funcs.end(), rhs);
    if (j == funcs.end())
      funcs.push_back(rhs);
    return *this;
  }
  delegate_base& operator -=(function_pointer rhs)
  {
    if (rhs == NULL)
      return *this;
    typename std::vector<function_union>::iterator j =
      std::find(funcs.begin(), funcs.end(), rhs);
    if (j != funcs.end())
      funcs.erase(j);
    return *this;
  }
  delegate_base operator +(function_pointer rhs) const
  {
    return delegate_base(*this) += rhs;
  }
  delegate_base operator -(function_pointer rhs) const
  {
    return delegate_base(*this) -= rhs;
  }
  friend delegate_base operator +(function_pointer lhs, const delegate_base& rhs)
  {
    return rhs + lhs;
  }
};

template <typename R>
class delegate<R (*)()> : public delegate_base<R (*)()>
{
protected:
  typedef delegate_base<R (*)()> base_class;
  typedef R (*function_pointer)();
DELEGATE_DEFINE
DELEGATE_COMMON
DELEGATE_OPERATOR_BRACKET_RT
};

template <typename R, typename V1>
class delegate<R (*)(V1)> : public delegate_base<R (*)(V1)>
{
protected:
  typedef delegate_base<R (*)(V1)> base_class;
  typedef R (*function_pointer)(V1);
DELEGATE_DEFINE
DELEGATE_COMMON
DELEGATE_OPERATOR_BRACKET_RT
};

template <typename R, typename V1, typename V2>
class delegate<R (*)(V1, V2)> : public delegate_base<R (*)(V1, V2)>
{
protected:
  typedef delegate_base<R (*)(V1, V2)> base_class;
  typedef R (*function_pointer)(V1, V2);
DELEGATE_DEFINE
DELEGATE_COMMON
DELEGATE_OPERATOR_BRACKET_RT
};

template <>
class delegate<void (*)()> : public delegate_base<void (*)()>
{
protected:
  typedef delegate_base<void (*)()> base_class;
  typedef void (*function_pointer)();
protected:
  typedef base_class::deleobject
    deleobject;
  typedef base_class::object_member_function_pointer
    object_member_function_pointer;
  typedef base_class::const_object_member_function_pointer
    const_object_member_function_pointer;
  typedef base_class::result_type
    result_type;
  typedef base_class::function_union
    function_union;
DELEGATE_COMMON
public:
  void operator()() const
  {
    std::vector<function_union>::const_iterator i = funcs.begin();
    for (; i != funcs.end(); ++i)
    {
      if (i->m_pObject == NULL)
        (*i->m_pf)();
      else
        (i->m_pObject->*(i->m_pmf))();
    }
  }
};

template <typename V1>
class delegate<void (*)(V1)> : public delegate_base<void (*)(V1)>
{
protected:
  typedef delegate_base<void (*)(V1)> base_class;
  typedef void (*function_pointer)(V1);
DELEGATE_DEFINE
DELEGATE_COMMON
DELEGATE_OPERATOR_BRACKET
};

template <typename V1, typename V2>
class delegate<void (*)(V1, V2)> : public delegate_base<void (*)(V1, V2)>
{
protected:
  typedef delegate_base<void (*)(V1, V2)> base_class;
  typedef void (*function_pointer)(V1, V2);
DELEGATE_DEFINE
DELEGATE_COMMON
DELEGATE_OPERATOR_BRACKET
};

template <typename V1, typename V2, typename V3>
class delegate<void (*)(V1, V2, V3)> : public delegate_base<void (*)(V1, V2, V3)>
{
protected:
  typedef delegate_base<void (*)(V1, V2, V3)> base_class;
  typedef void (*function_pointer)(V1, V2, V3);
DELEGATE_DEFINE
DELEGATE_COMMON
DELEGATE_OPERATOR_BRACKET
};


template <typename F>
inline delegate<F> make_delegate(F& fp)
{
  return delegate<F>(fp);
}

template <typename F>
inline delegate<F*> make_delegate(F* fp)
{
  return delegate<F*>(fp);
}

template <typename F>
inline delegate<F> make_delegate(const F& fp)
{
  return delegate<F>(fp);
}

template <typename F>
inline delegate<F*> make_delegate(const F* fp)
{
  return delegate<F*>(fp);
}

template <class T, typename F>
inline delegate<typename function_traits<F>::function_type>
  make_delegate(const T* tp, F fp)
{
  return delegate<typename function_traits<F>::function_type>(tp, fp);
}

#endif // #ifndef _DELEGATE_H_

//filename: event.h
#ifndef _EVENT_H_
#define _EVENT_H_

typedef void* object;

class EventArgs
{
public:
  bool cancel;
  EventArgs() : cancel(false) {}
};

template <typename T>
class event
{
  T handlers;
public:
  void operator +=(const T& rhs)
  {
    handlers += rhs;
  }
  void operator -=(const T& rhs)
  {
    handlers -= rhs;
  }
  void operator()() const
  {
    handlers(NULL, EventArgs());
  }
   template <typename P1>
  void operator()(P1 p1) const
  {
    handlers(p1, EventArgs());
  }
   template <typename P1, typename P2>
  void operator()(P1 p1, P2 p2) const
  {
    handlers(p1, p2);
  }
};

#endif // #ifndef _EVENT_H_
//filename: main.cpp
#include <stdlib.h>

#include "delegate.h"
#include "event.h"
#include <functional>

#define make_functor make_delegate
#define make_functor_rt make_delegate

class MyEventArgs : public EventArgs
{
public:
  MyEventArgs(const char* Context = "") : context(Context) {}
  const char* context;
};

typedef delegate<void (*)(object, MyEventArgs&)> EventHandler1;
typedef EventHandler1 EventHandler2;

class Provider
{
  public:  event<EventHandler1> OkClick1;
  public:  event<EventHandler2> OkClick2;
};

static void global_Process_OkClick(object source, MyEventArgs& e)
{
  printf("global_Process_OkClick, /t%s/n", e.context);
}

class Master_base
{
public:
  public: Provider pro;
  public: Master_base()
  {
    pro.OkClick1 += EventHandler1(static_Process_OkClick);

    pro.OkClick2 += EventHandler2(this, &Master_base::Process_OkClick);
    pro.OkClick2 += EventHandler2(&Master_base::static_Process_OkClick);

    pro.OkClick2 += EventHandler2(global_Process_OkClick);
  }
  protected: void Process_OkClick(object source, MyEventArgs& e)
  {
    printf("       Process_OkClick, /t%s/n", e.context);
  }
  public: static void static_Process_OkClick(object source, MyEventArgs& e)
  {
    printf("static_Process_OkClick, /t%s/n", e.context);
  }
  public: virtual void test_virtual() const
  {
    printf("Master_base::test_virtual const/n");
  }
  public: void mem_func__()
  {
    printf("Master_base::mem_func__/n");
  }
  public: void mem_func__int(int x)
  {
    printf("Master_base::mem_func__int %d/n", x);
  }
  public: void mem_func__int_str(int x, const char* str)
  {
    printf("Master_base::mem_func__int %d %s/n", x, str);
  }
  public: int mem_func_int_()
  {
    printf("Master_base::mem_func_int_/n");
    return 123;
  }
  public: int mem_func_int_int(int x)
  {
    printf("Master_base::mem_func_int_int %d/n", x);
    return x;
  }
  public: int mem_func_int_int_str(int x, const char* str)
  {
    printf("Master_base::mem_func_int_int %d %s/n", x, str);
    return x;
  }
};

class Master_derived: public Master_base
{
  public: Master_derived()
  {
    pro.OkClick1 += EventHandler1(static_Process_OkClick_Myself);

    pro.OkClick2 += EventHandler2(this, &Master_derived::Process_OkClick_Myself);
    pro.OkClick2 -= EventHandler2((Master_base*)this,
      (void (Master_base::*)(object, MyEventArgs&))&Master_derived::Process_OkClick);
    pro.OkClick2 -= EventHandler2(this, &Master_derived::Process_OkClick_Myself1);
    pro.OkClick2 += EventHandler2(&Master_derived::static_Process_OkClick_Myself);

    pro.OkClick2 -= EventHandler2(global_Process_OkClick);
  }
  protected: void Process_OkClick_Myself(object source, MyEventArgs& e)
  {
    printf("       Process_OkClick_Myself, /t%s/n", e.context);
  }
  private: void Process_OkClick_Myself1(object source, MyEventArgs& e)
  {
    printf("       Process_OkClick_Myself1, /t%s/n", e.context);
  }
  static void static_Process_OkClick_Myself(object source, MyEventArgs& e)
  {
    printf("static_Process_OkClick_Myself, /t%s/n", e.context);
  }
  public: virtual void test_virtual() const
  {
    printf("Master_derived::test_virtual const/n");
  }
};

class MainClass
{
public:
  void Main()
  {
    Master_base example1;
    Master_derived example2;
    printf("  example1.pro.OkClick1:/n");
    example1.pro.OkClick1(this, MyEventArgs("example1.pro.OkClick1"));
    printf("  example2.pro.OkClick1:/n");
    example2.pro.OkClick1(this, MyEventArgs("example2.pro.OkClick1"));
    printf("/n");
    printf("  example1.pro.OkClick2:/n");
    example1.pro.OkClick2(this, MyEventArgs("example1.pro.OkClick2"));
    printf("  example2.pro.OkClick2:/n");
    example2.pro.OkClick2(this, MyEventArgs("example2.pro.OkClick2"));
  }
};

void testfunc__()
{
  printf("testfunc__/n");
}

void testfunc__int(int i)
{
  printf("testfunc__int %d/n", i);
}

void testfunc__int_str(int i, const char* j)
{
  printf("testfunc__int_str %d %s/n", i, j);
}

int testfunc_int_()
{
  printf("testfunc_int_/n");
  return 111;
}

int testfunc_int_int(int i)
{
  printf("testfunc_int_int %d/n", i);
  return i;
}

int testfunc_int_int_str(int i, const char* j)
{
  printf("testfunc_int_int_str %d %s/n", i, j);
  return i;
}

typedef void (*func__)();
typedef void (*func__int)(int);
typedef void (*func__int_str)(int, const char*);

typedef int (*func_int_)();
typedef int (*func_int_int)(int);
typedef int (*func_int_int_str)(int, const char*);

int main(int argc, char *argv[])
{
  printf("event:/n");
  MainClass().Main();



  printf("/n functor:/n");
  Master_base mb;
  Master_derived md;



  printf("/n  func__:/n");
  delegate<func__> ffunc__(testfunc__);
  ffunc__ = delegate<func__>(testfunc__);
  ffunc__ -= delegate<func__>(testfunc__);
  ffunc__ += delegate<func__>(testfunc__);
  ffunc__ = ffunc__ - delegate<func__>(testfunc__);
  ffunc__ = ffunc__ + delegate<func__>(testfunc__);
  ffunc__ = delegate<func__>(testfunc__) + ffunc__;
  ffunc__ = testfunc__;
  ffunc__ -= testfunc__;
  ffunc__ += testfunc__;
  ffunc__ = ffunc__ - testfunc__;
  ffunc__ = ffunc__ + testfunc__;
  ffunc__ = testfunc__ + ffunc__;
  ffunc__();

  printf("  functor.func__:/n");
  delegate<func__> ffunc__1(testfunc__);
  ffunc__1 += make_functor(&mb, &Master_base::mem_func__);
  ffunc__1();


  printf("/n  func__int:/n");
  delegate<func__int> ffunc__int(testfunc__int);
  ffunc__int(888);

  printf("  functor.func__int:/n");
  delegate<func__int> ffunc__int1(testfunc__int);
  ffunc__int1 += make_functor(&mb, &Master_base::mem_func__int);
  ffunc__int1(777);


  printf("/n  func__int_str:/n");
  delegate<func__int_str> ffunc__int_str(testfunc__int_str);
  ffunc__int_str(888, "ccc");

  printf("  functor.func__int_str:/n");
  delegate<func__int_str>
    ffunc__int_str1(testfunc__int_str);
  ffunc__int_str1 += make_functor(&mb, &Master_base::mem_func__int_str);
  ffunc__int_str1(777, "hhh");



  printf("/n  func_int_:/n");
  delegate<func_int_> ffunc_int_(testfunc_int_);
  printf("ffunc_int_()=%d/n", ffunc_int_());

  printf("  functor.func_int_:/n");
  delegate<func_int_>
    ffunc_int_1(testfunc_int_);
  printf("ffunc_int_1()=%d/n", ffunc_int_1());
  ffunc_int_1 -= make_functor_rt(testfunc_int_);
  ffunc_int_1 += make_functor_rt(&mb, &Master_base::mem_func_int_);
  printf("ffunc_int_1()=%d/n", ffunc_int_1());


  printf("/n  func_int_int:/n");
  delegate<func_int_int> ffunc_int_int(testfunc_int_int);
  printf("ffunc_int_int()=%d/n", ffunc_int_int(888));

  printf("  functor.func_int_int:/n");
  delegate<func_int_int>
    ffunc_int_int1(testfunc_int_int);
  printf("ffunc_int_int1()=%d/n", ffunc_int_int1(777));
  ffunc_int_int1 -= make_functor_rt(testfunc_int_int);
  ffunc_int_int1 += make_functor_rt(&mb, &Master_base::mem_func_int_int);
  printf("ffunc_int_int1()=%d/n", ffunc_int_int1(777));


  printf("/n  func_int_int_str:/n");
  delegate<func_int_int_str> ffunc_int_int_str(testfunc_int_int_str);
  printf("ffunc_int_int_str()=%d/n", ffunc_int_int_str(888, "ccc"));

  printf("  functor.func_int_int_str:/n");
  delegate<func_int_int_str>
    ffunc_int_int_str1(testfunc_int_int_str);
  printf("ffunc_int_int_str1()=%d/n", ffunc_int_int_str1(777, "hhh"));
  ffunc_int_int_str1 -= make_functor_rt(testfunc_int_int_str);
  ffunc_int_int_str1 += make_functor_rt(&mb, &Master_base::mem_func_int_int_str);
  printf("ffunc_int_int_str1()=%d/n", ffunc_int_int_str1(777, "hhh"));



  printf("/nstatic function size:/t%d/n",
    sizeof(&global_Process_OkClick));
  printf("member function size:/t%d/n",
    sizeof(&Master_base::mem_func__));
  printf("virtual member function size:/t%d/n",
    sizeof(&Master_base::test_virtual));
  printf("/n");

  delegate<func__> ftest_virtual;
  ftest_virtual = delegate<func__>(ffunc__1)
                + delegate<func__>(make_functor((Master_base*)&md,
                                   &Master_base::test_virtual));
  ftest_virtual();
  printf("  test_virtual2:/n");
  ftest_virtual = ftest_virtual
                - make_functor(&md, &Master_derived::test_virtual);
  ftest_virtual = ftest_virtual
                + make_functor(&mb, &Master_base::test_virtual);
  ftest_virtual = make_functor(&mb, &Master_base::test_virtual)
                + ftest_virtual;
  ftest_virtual();

  printf("/n  Test make_functor global:/n");
  EventHandler2 teststatic1(&global_Process_OkClick);
  teststatic1((object)NULL, MyEventArgs());

  MyEventArgs e;
  delegate<void (*)(object, MyEventArgs&)>
    teststatic2(&global_Process_OkClick);
  teststatic2((object)NULL, e);
  make_functor(&global_Process_OkClick)((object)NULL, e);

  printf("/n  Test make_functor static member:/n");
  EventHandler2 teststatic3(&Master_base::static_Process_OkClick);
  teststatic3((object)NULL, MyEventArgs());

  delegate<void (*)(object, MyEventArgs&)>
    teststatic4(&Master_base::static_Process_OkClick);
  teststatic4((object)NULL, e);
  make_functor(&Master_base::static_Process_OkClick)((object)NULL, e);

  std::vector<int> for_each_test(1, 1);
  for_each_test.push_back(2);
  for_each_test.push_back(3);
  printf("/n  for_each test ffunc__int:/n");
  std::for_each(for_each_test.begin(), for_each_test.end(), ffunc__int);
  printf("/n  for_each test ffunc__int1:/n");
  std::for_each(for_each_test.begin(), for_each_test.end(), ffunc__int1);
  printf("/n  for_each test ffunc_int_int:/n");
  std::for_each(for_each_test.begin(), for_each_test.end(), ffunc_int_int);
  printf("/n  for_each test ffunc_int_int1:/n");
  std::for_each(for_each_test.begin(), for_each_test.end(), ffunc_int_int1);

  delegate<delegate<func__int> > delegate_for_delegate_test(ffunc__int1);
  delegate_for_delegate_test += ffunc__int1;
  printf("/n  delegate_for_delegate_test 1:/n");
  std::for_each(for_each_test.begin(), for_each_test.end(),
    delegate_for_delegate_test);
  ffunc__int1 -= make_functor(testfunc__int);
  delegate_for_delegate_test += ffunc__int1;
  printf("/n  delegate_for_delegate_test 2:/n");
  std::for_each(for_each_test.begin(), for_each_test.end(),
    delegate_for_delegate_test);

  system("PAUSE");
  return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ 并没有原生的委托机制,但可以通过函数指针和对象指针来模拟委托机制。 在 C++ ,函数指针可以作为参数传递和返回值,可以使用函数指针来实现委托的功能。而对象指针可以用于保存对象的状态和数据,也可以用于调用对象的成员函数。 以下是一个简单的 C++ 示例代码,演示了如何通过函数指针和对象指针来实现委托机制: ```cpp #include <iostream> #include <functional> using namespace std; class Delegate { public: void (*func)(void*); // 函数指针 void* obj; // 对象指针 void Invoke() { func(obj); // 调用函数指针 } }; class Test { public: void Print() { cout << "Hello, world!" << endl; } }; void PrintTest(void* obj) { Test* test = (Test*)obj; // 将对象指针转换为 Test 类型指针 test->Print(); // 调用对象的成员函数 } int main() { Test test; Delegate delegate; delegate.func = PrintTest; delegate.obj = &test; delegate.Invoke(); // 调用委托 return 0; } ``` 在这个示例代码,我们定义了一个 `Delegate` 类,其包含了一个函数指针和一个对象指针。`Invoke` 函数用于调用委托。我们还定义了一个 `Test` 类,其包含了一个成员函数 `Print`。在 `main` 函数,我们创建了一个 `Test` 对象 `test`,并创建了一个 `Delegate` 对象 `delegate`,将 `PrintTest` 函数的地址和 `test` 对象的地址分别赋值给 `Delegate` 对象的函数指针和对象指针。最后,我们通过 `delegate.Invoke()` 调用委托,实际上就是调用了 `PrintTest` 函数,并将 `test` 对象传递给了该函数,从而调用了 `Test` 对象的 `Print` 成员函数。 需要注意的是,这只是一个简单的示例,实际的委托机制可能更加复杂,并且需要考虑线程安全等问题。如果需要使用委托机制,建议使用现有的 C++ 第三方库或框架,如 Boost、Qt 等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值