CSDN博客

img coolcch

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

发表于2004/3/25 17:12:00  814人阅读

看到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 0

相关博文

我的热门文章

img
取 消
img