.net中的 delegate的標準C++類比

來源:互聯網
上載者:User

發信人: 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);
}
 
--
要錢沒有,要命也沒有 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.