Simulate the. NET proxy syntax using the C ++ template

Source: Internet
Author: User

It took some time. net proxy mode. in addition, the original C ++ code contains a large number of items that require proxy. Since there are no similar items, they are implemented by interfaces and seem awkward. I wonder if I can do such a thing too.

The boost function is not very familiar, but the basic implementation is still clear. directly Using boost: function is a bit cool, because I only need a very small agent syntax. moreover, after so many years, writing C ++ code is getting less and less passionate, just like this opportunity to give yourself some passion.

Not much nonsense, read the code (do not look at the comments, do not comment, do not understand go to see boost: function)

 

Vfxdelegate. h

# Pragma once </P> <p> namespace vfx <br/> {<br/> template <class _ FN> <br/> struct DeleGate <br/> {<br /> ~ Delegate () <br/>{< br/> Delete _ m_imp; <br/>}</P> <p> delegate (); <br/> delegate (_ FN); <br/> template <class _ cty> <br/> delegate (_ cty & OBJ ); <br/> template <class _ cty> <br/> delegate (_ cty & OBJ, _ FN ); </P> <p> delegate & operator = (_ FN ); <br/> template <class _ cty> <br/> delegate & operator = (_ cty OBJ); </P> <p> delegate (const delegate & RH ); <br/> delegate & operator = (const delegate & RH); </P> <p> bool o Perator = (const delegate & RH) const; <br/> bool Operator! = (Const delegate & RH) const; </P> <p> operator bool () const; <br/> void swap (delegate & RH); <br/> }; </P> <p> template <class _ ty> <br/> struct multdelegate <br/> {<br/> typedef DeleGate <_ ty> delegate_type; <br/> typedef STL: vector <delegate_type> delegate_vecloud; </P> <p> multdelegate & operator + = (const delegate_type & DT ); <br/> multdelegate & operator-= (const delegate_type & DT); </P> <p> void clear (); <br/> void swap (multdelegate & RH ); <br/> PRIVATE: <br/> delegate_vectorym_events; <br/> }; </P> <p> namespace detail <br/> {<br/> template <class _ ty> <br/> struct _ delegategettype _ <br/> {<br/> typedef _ ty type; <br/> }; <br/> template <class _ ty> <br/> struct _ delegategettype _ <_ ty *> <br/>{< br/> typedef _ ty type; <br/> }; <br/> template <class _ ty> <br/> struct _ delegategettype _ <_ ty &> <br/>{< br/> typedef _ ty type; <br/>}; <br/>}</P> <p> # pragma push_macro ("tmpargs ") <br/> # pragma push_macro ("funargs") <br/> # pragma push_macro ("funparams") <br/> # pragma push_macro ("paramslist ") </P> <p> # UNDEF tmpargs <br/> # UNDEF funargs <br/> # UNDEF funparams <br/> # UNDEF paramslist <br/> # define tmpargs <br /># define funargs void <br/> # define funparams void <br/> # define paramslist <br/> # include "vfxdelegate. INL "</P> <p> # UNDEF tmpargs <br/> # UNDEF funargs <br/> # UNDEF funparams <br/> # UNDEF paramslist <br/> # define tmpargs, class t0 <br/> # define funargs t0 <br/> # define funparams t0 <br/> # define paramslist t0 <br/> # include "vfxdelegate. INL "</P> <p> # UNDEF tmpargs <br/> # UNDEF funargs <br/> # UNDEF funparams <br/> # UNDEF paramslist <br/> # define tmpargs, class T0, class t1 <br/> # define funargs T0, t1 <br/> # define funparams T0, T1 t1 <br/> # define paramslist T0, t1 <br/> # include "vfxdelegate. INL "</P> <p> # UNDEF tmpargs <br/> # UNDEF funargs <br/> # UNDEF funparams <br/> # UNDEF paramslist <br/> # define tmpargs, class T0, class T1, class t2 <br/> # define funargs T0, T1, T2 <br/> # define funparams t0 T0, T1 T1, t2 t2 <br/> # define paramslist T0, T1, T2 <br/> # include "vfxdelegate. INL "</P> <p> # UNDEF tmpargs <br/> # UNDEF funargs <br/> # UNDEF funparams <br/> # UNDEF paramslist <br/> # define tmpargs, class T0, class T1, class T2, class T3 <br/> # define funargs T0, T1, T2, T3 <br/> # define funparams t0 T0, T1 T1, t2 T2, T3 T3 <br/> # define paramslist T0, T1, T2, T3 <br/> # include "vfxdelegate. INL "</P> <p> # UNDEF tmpargs <br/> # UNDEF funargs <br/> # UNDEF funparams <br/> # UNDEF paramslist <br/> # define tmpargs, class T0, class T1, class T2, class T3, class t4 <br/> # define funargs T0, T1, T2, T3, T4 <br/> # define funparams T0, t1 T1, T2 T2, T3 T3, T4 t4 <br/> # define paramslist T0, T1, T2, T3, T4 <br/> # include "vfxdelegate. INL "</P> <p> # UNDEF tmpargs <br/> # UNDEF funargs <br/> # UNDEF funparams <br/> # UNDEF paramslist <br/> # define tmpargs, class T0, class T1, class T2, class T3, class T4, class T5 <br/> # define funargs T0, T1, T2, T3, T4, t5 <br/> # define funparams t0 T0, T1 T1, T2 T2, T3 T3, T4 T4, T5 T5 <br/> # define paramslist T0, T1, T2, T3, t4, T5 <br/> # include "vfxdelegate. INL "</P> <p> # UNDEF tmpargs <br/> # UNDEF funargs <br/> # UNDEF funparams <br/> # UNDEF paramslist <br/> # define tmpargs, class T0, class T1, class T2, class T3, class T4, class T5, class T6 <br/> # define funargs T0, T1, T2, T3, T4, T5, t6 <br/> # define funparams t0 T0, T1 T1, T2 T2, T3 T3, T4 T4, T5 T5, T6 T6 <br/> # define paramslist T0, T1, t2, T3, T4, T5, T6 <br/> # include "vfxdelegate. INL "</P> <p> # UNDEF tmpargs <br/> # UNDEF funargs <br/> # UNDEF funparams <br/> # UNDEF paramslist <br/> # define tmpargs, class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7 <br/> # define funargs T0, T1, T2, T3, T4, t5, T6, T7 <br/> # define funparams t0 T0, T1 T1, T2 T2, T3 T3, T4 T4, T5 T5, T6 T6, t7 T7 <br/> # define paramslist T0, T1, T2, T3, T4, T5, T6, T7 <br/> # include "vfxdelegate. INL "</P> <p> # UNDEF tmpargs <br/> # UNDEF funargs <br/> # UNDEF funparams <br/> # UNDEF paramslist <br/> # define tmpargs, class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8 <br/> # define funargs T0, T1, T2, t3, T4, T5, T6, T7, T8 <br/> # define funparams t0 T0, T1 T1, T2 T2, T3 T3, T4 T4, T5 T5, T6 T6, t7 T7, T8 T8 <br/> # define paramslist T0, T1, T2, T3, T4, T5, T6, T7, T8 <br/> # include "vfxdelegate. INL "</P> <p> # UNDEF tmpargs <br/> # UNDEF funargs <br/> # UNDEF funparams <br/> # UNDEF paramslist <br/> # define tmpargs, class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9 <br/> # define funargs T0, T1, t2, T3, T4, T5, T6, T7, T8, T9 <br/> # define funparams t0 T0, T1 T1, T2 T2, T3 T3, T4 T4, T5 T5, t6 T6, T7 T7, T8 T8, T9 T9 <br/> # define paramslist T0, T1, T2, T3, T4, T5, T6, T7, T8, t9 <br/> # include "vfxdelegate. INL "</P> <p> # UNDEF tmpargs <br/> # UNDEF funargs <br/> # UNDEF funparams <br/> # UNDEF paramslist <br/> # define tmpargs, class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10 <br/> # define funargs T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10 <br/> # define funparams T0, t1 T1, T2 T2, T3 T3, T4 T4, T5 T5, T6 T6, T7 T7, T8 T8, T9 T9, T10 T10 <br/> # define paramslist T0, T1, t2, T3, T4, T5, T6, T7, T8, T9, T10 <br/> # include "vfxdelegate. INL "</P> <p> # UNDEF tmpargs <br/> # UNDEF funargs <br/> # UNDEF funparams <br/> # UNDEF paramslist <br/> # define tmpargs, class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11 <br/> # define funargs T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, t11 <br/> # define funparams t0 T0, T1 T1, T2 T2, T3 T3, T4 T4, T5 T5, T6 T6, T7 T7, T8 T8, T9 T9, t10 T10, T11 T11 <br/> # define paramslist T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, t11 <br/> # include "vfxdelegate. INL "</P> <p> # pragma pop_macro (" tmpargs ") <br/> # pragma pop_macro (" funargs ") <br/> # pragma pop_macro ("funparams") <br/> # pragma pop_macro ("paramslist") <br/>

 

Vfxdelegate. INL

Namespace vfx <br/>{< br/> template <class _ result tmpargs> <br/> struct DeleGate <_ result (funargs)> <br/>{< br/> PRIVATE: <br/> struct impl <br/>{< br/> virtual ~ Impl () {}< br/> virtual _ result call (funparams) = 0; <br/> virtual impl * clone () const = 0; <br/> virtual char type () const = 0; <br/> virtual bool equip (const impl * Rh) const = 0; <br/> }; <br/> struct fnimpl: impl <br/> {<br/> typedef _ result (* fun_type) (funargs); <br/> fun_type _ m_fn; <br/> fnimpl (fun_type FN): _ m_fn (FN) {}< br/> virtual _ result call (funparams) <br/>{< br/> return _ m_fn (paramslist); <br/>}< Br/> virtual impl * clone () const <br/>{< br/> return New fnimpl (_ m_fn ); <br/>}< br/> virtual char type () const <br/>{< br/> return 1; <br/>}< br/> virtual bool equip (const impl * Rh) const <br/> {<br/> return _ m_fn = (fnimpl *) RH) -> _ m_fn; <br/>}< br/>}; <br/> template <class _ cty> <br/> struct memfnimpl: impl <br/> {<br/> typedef _ result (_ cty: * fun_type) (funargs); <br/> fun_type _ m_fn; <br/> _ cty * _ m_ob J; <br/> memfnimpl (_ cty * OBJ, fun_type FN): _ m_fn (FN), _ m_obj (OBJ) {}< br/> memfnimpl (_ cty & OBJ, fun_type FN): _ m_fn (FN), _ m_obj (& OBJ) {}< br/> virtual _ result call (funparams) <br/>{< br/> return (_ m_obj-> * _ m_fn) (paramslist ); <br/>}< br/> virtual impl * clone () const <br/>{< br/> return New memfnimpl (_ m_obj, _ m_fn ); <br/>}< br/> virtual char type () const <br/>{< br/> return 2; <br/>}< br/> virtual bool equip (cons T impl * Rh) const <br/> {<br/> return _ m_fn = (memfnimpl *) RH) -> _ m_fn & _ m_obj = (memfnimpl *) RH)-> _ m_obj; <br/>}< br/> }; <br/> template <class _ cty> <br/> struct objfnimpl: impl <br/>{< br/> _ cty * _ m_obj; <br/> objfnimpl (_ cty * OBJ): _ m_obj (OBJ) {}< br/> objfnimpl (_ cty & OBJ): _ m_obj (& OBJ) {}< br/> virtual _ result call (funparams) <br/>{< br/> return _ m_obj-> operator () (paramslist ); <br/>}< br/> virtual Impl * clone () const <br/>{< br/> return New objfnimpl (_ m_obj); <br/>}< br/> virtual char type () const <br/>{< br/> return 3; <br/>}< br/> virtual bool equip (const impl * Rh) const <br/>{< br/> return _ m_obj = (memfnimpl *) RH)-> _ m_obj; <br/>}< br/> }; </P> <p> impl * _ m_imp; </P> <p> Public: <br/> ~ Delegate () <br/>{< br/> Delete _ m_imp; <br/>}</P> <p> delegate (): _ m_imp (0) {}< br/> delegate (_ result (* fN) (funargs) <br/>: _ m_imp (New fnimpl (FN )) {}</P> <p> template <class _ cty> <br/> delegate (_ cty & OBJ) <br/>: _ m_imp (New objfnimpl <vfx :: detail: _ delegategettype _ <_ cty >:: type> (OBJ )) {}</P> <p> template <class _ cty> <br/> delegate (_ cty & OBJ, _ result (_ cty: * fN) (funargs )) <br/>: _ m_imp (New memfnimpl <vfx: detail: _ Deleg Ategettype _ <_ cty >:: type> (OBJ, FN) {}</P> <p> delegate & operator = (_ result (* fN) (funargs )) <br/>{< br/> Delete _ m_imp; <br/> _ m_imp = new fnimpl; <br/>}< br/> template <class _ cty> <br/> delegate & operator = (_ cty OBJ) <br/>{< br/> Delete _ m_imp; <br/> _ m_imp = new objfnimpl <vfx: detail: _ delegategettype _ <_ cty> :: type> (OBJ); <br/>}</P> <p> delegate (const delegate & RH) <br/>{< br/> If (RH. _ m_imp! = 0) <br/> _ m_imp = RH. _ m_imp-> clone (); <br/> else <br/> _ m_imp = 0; <br/>}< br/> delegate & operator = (const delegate & RH) <br/>{< br/> If (this! = & RH) <br/>{< br/> Delete _ m_imp; <br/> If (Rh. _ m_imp! = 0) <br/> _ m_imp = RH. _ m_imp-> clone (); <br/> else <br/> _ m_imp = 0; <br/>}< br/> return * this; <br/>}</P> <p> bool operator = (const delegate & RH) const <br/>{< br/> If (_ m_imp = NULL | RH. _ m_imp = NULL) <br/> return _ m_imp = RH. _ m_imp; <br/> return _ m_imp-> type () = RH. _ m_imp-> type () & <br/> _ m_imp-> equip (RH. _ m_imp); <br/>}</P> <p> bool Operator! = (Const delegate & RH) const <br/>{< br/> return! (Operator = (RH); <br/>}</P> <p> _ result operator () (funparams) const <br/>{< br/> If (_ m_imp = NULL) <br/> throw STD: runtime_error ("Call null delegate "); <br/> return _ m_imp-> call (paramslist); <br/>}</P> <p> operator bool () const <br/>{< br/> return _ m_imp! = 0; <br/>}</P> <p> void swap (delegate & RH) <br/>{< br/> impl * temp = _ m_imp; <br/> _ m_imp = RH. _ m_imp; <br/> RH. _ m_imp = temp; <br/>}< br/> }; </P> <p> template <class _ result tmpargs> <br/> struct multdelegate <_ result (funargs)> <br/>{ <br/> typedef DeleGate <_ result (funargs)> delegate_type; <br/> typedef STL: vector <delegate_type> delegate_vectory; </P> <p> void clear () <br/>{< br/> m_events.clear (); <br/>}< br/> Multdelegate & operator + = (const delegate_type & dt) <br/>{< br/> for (delegate_vectings: iterator I = m_events.begin (); I! = M_events.end (); ++ I) <br/>{< br/> If (* I = DT) <br/> return * this; <br/>}< br/> m_events.push_back (DT); <br/> return * this; <br/>}< br/> multdelegate & operator-= (const delegate_type & dt) <br/>{< br/> for (delegate_vectings :: iterator I = m_events.begin (); I! = M_events.end (); ++ I) <br/>{< br/> If (* I = DT) <br/>{< br/> m_events.erase (I ); <br/> break; <br/>}< br/> return * This; <br/>}< br/> void operator () (funparams) const <br/>{< br/> for (delegate_vectings: const_iterator I = m_events.begin (); I! = M_events.end (); ++ I) <br/>{< br/> If (* I) (paramslist ); <br/>}</P> <p> void swap (multdelegate & RH) <br/>{< br/> m_events.swap (RH. m_events); <br/>}< br/> PRIVATE: <br/> delegate_vectorym_events; <br/>}; <br/>}< br/>

 

Use Case:

Typedef vfx: DeleGate <void (vipropertyeditable *)> propertygridevents; </P> <p> class pgrid_api events: Public cxtppropertygrid <br/>{< br/> declare_dynamic (events) <br/> vipropertyeditable * m_pobject; </P> <p> vfx: multdelegate <void (vipropertyeditable *)> valuechanged; <br/> void policylistener_valuechanged () <br/>{< br/> This-> valuechanged (m_pobject); <br/>}< br/>}; </P> <p> class cm Apeditordoc: Public cdocument <br/>{< br/> void ongridvaluechanged (vipropertyeditable * pobject); <br/>{< br/> If (pobject! = NULL) <br/> setmodifiedflag (true); <br/>}< br/>}; </P> <p> // cpropertygrideditorm_wndpropertygrid; <br/> void cmainframe: initdocument () <br/>{< br/> cmapeditordoc * pdoc = (cmapeditordoc *) getactivedocument (); <br/> m_wndpropertygrid.valuechanged + = propertygridevents (* pdoc, & cmapeditordoc: ongridvaluechanged); <br/>}</P> <p>

 

Note: STL: vector <> is the typedef of STD: vector <, vfx: alloctor>. Therefore, you only need to replace STL with STD.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.