.net中的 delegate的标准C++模拟

发信人: RoachCock (穷鬼), 信区: Programming
标  题: .net中的 delegate的标准C++模拟
发信站: BBS 水木清华站 (Mon Mar 18 21:11:30 2002) 

用模板的偏特化和成员模板,重载函数调用运算符成功的实现了delegate,既可以绑定普通函数,也可以绑定对象及其成员函数
在cygnuwin下编译通过,
还不支持一个delegate包含多个函数的用法,不过相信很简单,从std::list派生一个类
就可以了
 
我用的cygun有些毛病,
my_delegate d2=my_delegate(t,&Test::f);
                           ^如果写成&t,就会导致编译器内部错误,没办法了
 
我本来写程序是加空行的,贴到BBS上就没了,忍受一下吧
Win32下的各种调用约定很讨厌,没有考虑,不过实现起来不费什么脑筋,就是麻烦,
不管了
 
// Test.cpp : Defines the entry point for the console application.
//
#include <stddef.h>
template<class T>

//函数traits,用来提取函数的返回类型

struct function_traits
{
};
template<class RT>
struct function_traits< RT(*)() >
{
 typedef RT result_type;
};
template<class RT,class AT>
struct function_traits< RT(*)(AT) >
{
 typedef RT result_type;
 typedef AT argument_type;
};
template<class RT,class AT1,class AT2>
struct function_traits< RT(*)(AT1,AT2) >
{
 typedef RT result_type;
 typedef AT1 first_argument_type;
 typedef AT2 second_argument_type;
};

// 函数traits,用来提取类成员函数的返回类型


template<class RT, class OT>
struct function_traits< RT (OT::*)() >
{
 typedef OT object_type;
 typedef RT result_type;
};
template<class RT, class OT, class AT>
struct function_traits< RT (OT::*)(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 OT object_type;
 typedef RT result_type;
 typedef AT1 first_argument_type;
 typedef AT2 second_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;
};

// delegate的实现

template <typename PFT>
class delegate
{
 class object
 {
 }*m_pObject; // 对象指针,是一个代理对象
 typedef typename to_member_function_pointer<object, PFT>::type object_member_fuunction_pointer;
 union
 {
  PFT m_pf;
  object_member_function_pointer m_pmf;
 }; // 函数指针和成员函数指针的联合体
 public:
  typedef typename function_traits<PFT>::result_type result_type;
 
  delegate()
  {
   m_pObject=NULL;
   m_pf=NULL;
  }
 
  delegate(PFT pf)
  {
   operator=(pf);
  }
 
  template<typename OT>
   delegate(
   OT *pObject,
   typename to_member_function_pointer<OT, PFT>::type pmf
   )
  {
   m_pObject=reinterpret_cast<object*>(pObject);
   m_pmf=*(reinterpret_cast<object_member_function_pointer*>(&pmf));
  }
 
  template<typename OT>
   delegate(
   OT &pObject,
   typename to_member_function_pointer<OT, PFT>::type pmf
   )
  {
   m_pObject=reinterpret_cast<object*>(&pObject);
   m_pmf=*(reinterpret_cast<object_member_function_pointer*>(&pmf));
  }
 
  template<typename OT>
   delegate(
   const OT *pObject,
   typename to_const_member_function_pointer<OT, PFT>::type pmf
   )
  {
   m_pObject=const_cast<object*>(reinterpret_cast<object*>(pObject));
   m_pmf=*(reinterpret_cast<object_member_function_pointer*>(&pmf));
  }
 
  template<typename OT>
   delegate(
   const OT &pObject,
   typename to_const_member_function_pointer<OT, PFT>::type pmf
   )
  {
   m_pObject=const_cast<object*>(reinterpret_cast<object*>(&pObject));
   m_pmf=*(reinterpret_cast<object_member_function_pointer*>(&pmf));
  }
 
  delegate & operator=(PFT pf)
  {
   m_pf=pf;
   m_pObject=0;
   return *this;
  }
  template<int>
:gcc的函数模板不许无参数,加了个占位的"int"才能通过
   result_type operator()()
  {
   if(m_pObject)
    return (m_pObject->*m_pmf)();
   else
    return m_pf();
  }
  template<typename AT>
   result_type operator()(
   AT a1
   )
  {
   if(m_pObject)
    return (m_pObject->*m_pmf)(a1);
   else
    return m_pf(a1);
  }
  template<typename AT1, typename AT2>
   result_type operator()(
   AT1 a1,
   AT2 a2
   )
  {
   if(m_pObject)
    return (m_pObject->*m_pmf)(a1,a2);
   else
    return m_pf(a1,a2);
  }
  template<typename AT1, typename AT2, typename AT3>
   result_type operator()(
   AT1 a1,
   AT2 a2,
   AT3 a3
   )
  {
   if(m_pObject)
    return (m_pObject->*m_pmf)(a1,a2,a3);
   else
    return m_pf(a1,a2,a3);
  }
};
int gf(int)
{
 return 0;
}
class Test
{
public:
 int f(int){return 0;}
};
typedef delegate < int (*)(int) > my_delegate;
int main()
{
 Test t;

 my_delegate d1=&gf; // 普通函数
 my_delegate d2=my_delegate(t,&Test::f); //对象和类成员函数
 d1(0); //调用
 d2(2);
}
 
--
要钱没有,要命也没有 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++的委托(delegate)是一种函数指针的高级形式,它可以将函数作为参数传递给其他函数或存储在数据结构。委托提供了一种灵活的方式来实现回调机制和事件处理。 在C++,可以使用函数指针、函数对象和Lambda表达式来实现委托。下面是几种常见的委托用法: 1. 函数指针委托: 可以使用函数指针作为委托类型,将一个函数指针赋值给委托变量,然后通过委托变量调用相应的函数。 示例代码: ```cpp void Function1() { // 函数1的实现 } void Function2() { // 函数2的实现 } typedef void (*DelegateType)(); // 定义委托类型 int main() { DelegateType delegate = nullptr; delegate = &Function1; // 将函数1赋值给委托变量 delegate(); // 调用委托,实际上调用了函数1 delegate = &Function2; // 将函数2赋值给委托变量 delegate(); // 调用委托,实际上调用了函数2 return 0; } ``` 2. 函数对象委托: 可以使用函数对象(重载了函数调用运算符operator()的类对象)作为委托类型,将一个函数对象赋值给委托变量,然后通过委托变量调用相应的函数。 示例代码: ```cpp class FunctionObject { public: void operator()() { // 函数对象的实现 } }; int main() { FunctionObject functionObject; typedef void (FunctionObject::*DelegateType)(); // 定义委托类型 DelegateType delegate = nullptr; delegate = &FunctionObject::operator(); // 将函数对象的函数调用运算符赋值给委托变量 (functionObject.*delegate)(); // 调用委托,实际上调用了函数对象的函数调用运算符 return 0; } ``` 3. Lambda表达式委托: 可以使用Lambda表达式作为委托类型,直接将Lambda表达式赋值给委托变量,然后通过委托变量调用相应的函数。 示例代码: ```cpp int main() { auto lambda = []() { // Lambda表达式的实现 }; typedef decltype(lambda) DelegateType; // 定义委托类型 DelegateType delegate = lambda; // 将Lambda表达式赋值给委托变量 delegate(); // 调用委托,实际上调用了Lambda表达式 return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值