Entrustevents in C # in C ++ (1-delegateEmu.cpp)

zhaozj2021-02-08  215

Two days ago, I saw the programmer magazine saw the entrusted / event in C #. I feel that it is very convenient. I am passionate about C think about using C to simulate it seems to have the following code ... (vc6 is not Supporting the offsetting my work environment is VC6 pain ~~~)

No return value of the function otherwise too functorN a delegate with delegate_rtfunctor / functorN_rtdelegate template parameter may be a function pointer (non-member) may be functor may also be delegatefunctor available make_functor / make_functor_rt be generated if there is a partial specialization can be removed Nasty _rt :(

About entrusting Boost has ready-made could not be used in VC6

These codes are intended to be personal research. If you find that problems hope to point out

// filename: delegateemu.cpp

#include

#pragma Warning (Disable: 4786)

#include "delegate.h"

#include "event.h"

#include "functor.h"

#include

Class MyEventArgs: Public Eventargs

{

PUBLIC:

Myeventargs (const char * context = "): context (context) {}

CONST Char * context;

}

Typedef delegate EventHandler1;

Typedef delegate > Eventhandler2;

Class Provider

{

Public: Event okclick1;

Public: Event 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 (

Make_functor (this, & master_base :: process_okclick);

Pro.OKClick2 = EventHandler2 (

Make_functor (& Master_Base :: static_process_okclick);

Pro.OKClick2 = EventHandler2 (

Make_functor (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_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_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 (

Make_functor (this, & master_derived :: process_okclick_myself);

Pro.OKClick2 - = EventHandler2 (

Make_functor ((master_base *) this, & master_base :: process_okclick);

Pro.OKClick2 - = EventHandler2 (

Make_functor (this, & master_derived :: process_okclick_myself1);

Pro.OKClick2 = EventHandler2 (

Make_functor (& master_derived :: static_process_okclick_myself);

Pro.OKClick2 - = EventHandler2 (

Make_functor (global_process_okclick);

}

Protected: Void Process_okClick_Myseelf (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 ("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 ("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 (INT);

TYPEDEF INT (* func_int_int_str) (int, const char *); int main ()

{

Printf ("Event: / N");

Mainclass (). Main ();

Printf ("/ n functor: / n");

Master_base mb;

Master_derived md;

Printf ("/ n func __: / n");

Delegate FFUNC __ (TestFunc__);

Ffunc__ = delegate (testfunc__);

FFUNC__ - = delegate (testfunc__);

FFUNC__ = delegate (testfunc__);

FFUNC__ = FFUNC__ - DELEGATE (TestFunc__);

FFUNC__ = FFUNC__ DELEGATE (TestFunc__);

FFUNC__ = Delegate (TestFunc__) FFUNC__;

FFUNC__ = TestFunc__;

FFUNC__ - = TestFunc__;

FFUNC__ = TestFunc__;

FFUNC__ = FFUNC__ - TestFunc__;

FFUNC__ = FFUNC__ TestFunc__;

FFUNC__ = TestFunc__ ffunc__;

FFUNC __ ();

Printf ("functor.func __: / n");

Delegate ffunc__1 (Make_Functor (TestFunc__);

FFUNC__1 = Make_Functor (& MB, & Master_Base :: MEM_FUNC__);

FFUNC__1 ();

Printf ("/ n func__int: / n");

Delegate FFUNC__INT (TestFunc_Int);

FFUNC__INT (888);

Printf ("functor.func__int: / n");

Delegate > ffunc__int1 (make_functor (testfunc__int));

FFUNC__INT1 = Make_Functor (& MB, & Master_Base :: MEM_FUNC__INT);

FFUNC__INT1 (777);

Printf ("/ n func__int_str: / n");

Delegate FFUNC__INT_STR (TestFunc_Int_STR);

FFUNC__INT_STR (888, "CCC");

Printf ("functor.func__int_str: / n");

Delegate >

FFUNC__INT_STR1 (Make_Functor (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_rt ffunc_int_ (testfunc_int_);

Printf ("FFUNC_INT _ () =% D / N", FFUNC_INT_ ());

Printf ("functor.func_int _: / n");

Delegate_rt >

FFUNC_INT_1 (Make_Functor_RT (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_rt ffunc_int_int (TestFunc_INT_INT);

Printf ("FFUNC_INT_INT () =% D / N", FFUNC_INT_INT (888));

Printf ("functor.func_int_int: / n");

Delegate_rt >

FFUNC_INT_INT1 (Make_Functor_RT (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_rt 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_rt >

FFUNC_INT_INT_STR1 (Make_Functor_RT (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 ftest_virtual;

ftest_virtual = delegate (ffunc__1)

DELEGATE (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 (Make_Functor (& Global_Process_okClick);

TestStatic1 (Object) null, myeventargs ());

MyEventargs E;

Functor2

TestStatic2 (Make_functor (& Global_Process_okClick);

TestStatic2 (Object) NULL, E);

Make_functor (& global_process_okclick) (NULL, E);

Printf ("/ n Test make_functor static member: / n");

EventHandler2 TestStatic3 (Make_Functor (& Master_Base :: static_process_okclick);

TestStatic3 (Object) null, myeventargs ());

Functor2

TestStatic4 (Make_Functor (& Master_Base :: Static_Process_okClick);

TestStatic4 ((Object) NULL, E);

Make_functor (& Master_Base :: Static_Process_okClick) (NULL, E); std :: vector 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_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);

Return 0;

}

转载请注明原文地址:https://www.9cbs.com/read-3020.html

New Post(0)