Implement C + EntrustEvents in C ++ (upgraded version of the standard C ++)

zhaozj2021-02-08  218

See the article written on the 9CBS http://www.9cbs.net/develop/read_article.asp?id=13147 I feel that I use VC6 to write this indefinite harness. I have used his code to change his code. With this upgraded version, this is really perfect. Go see it in the editor!)

2004.3.24 Code Modification Content: Modify Template Class DLEGATE to make it available for general function objects such as std :: MEM_FUN_T, std :: MEM_FUN_REF_T Note: Multi-Cast Delegates only when used for function objects

// filename: Funtraits.h

#ifndef _funtraits_h_

#define _funtraits_h_

// Function Traits, used to extract the return type of the function

Template

Struct function_traits

{

}

Template

Struct function_traits

{

TYPEDEF RT (* function_type) ();

Typedef rt result_type;

}

Template

Struct Function_Traits

{

TypeDef rt (* function_type) (at);

Typedef rt result_type;

Typedef at argument_type;

}

Template

Struct function_traits

{

Typedef rt (* function_type) (AT1, AT2);

Typedef rt result_type;

TYPEDEF AT1 First_Argument_type;

TYPEDEF AT2 SECOND_ARGUMENT_TYPE;

}

Template

Struct Function_Traits

{

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;

}

// Function traits, used to extract the return type of class member function

Template

Struct Function_Traits

{

TYPEDEF RT (* function_type) ();

Typedef Ot Object_Type;

Typedef rt result_type;

}

Template

Struct Function_Traits

{

TypeDef rt (* function_type) (at);

Typedef Ot Object_Type;

Typedef rt result_type;

Typedef at argument_type;

TYPEDEF AT First_Argument_Type;

}

Template

Struct Function_Traits

{

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

Struct Function_Traits

{

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

Struct Function_Traits

{

TYPEDEF RT (* function_type) ();

Typedef Ot Object_Type;

Typedef rt result_type;

}

Template

Struct Function_Traits

{

TypeDef rt (* function_type) (at);

Typedef Ot Object_Type;

Typedef rt result_type;

Typedef at argument_type;

TYPEDEF AT First_Argument_Type;

}

Template

Struct Function_Traits

{

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

Struct Function_Traits

{

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;

}

// Put a common function class to a member function type of a specified class that is compatible into a type

Template

Struct to_member_function_point {

}

Template

Struct to_member_function_pointer

{

Typedef rt (ot :: * type) ();

}

Template

Struct to_member_function_point

{

Typedef RT (OT :: * Type) (AT);

}

Template

Struct to_member_function_pointer

{

Typedef RT (OT :: * Type) (AT1, AT2);

}

Template

Struct to_member_function_pointer

{

Typedef RT (OT :: * Type) (AT1, AT2, AT3);

}

// Translate into a Const member function

Template

Struct to_const_member_function_point

{

}

Template

Struct to_const_member_function_point

{

TYPEDEF RT (ot :: * type) () const;

}

Template

Struct to_const_member_function_point

{

Typedef RT (OT :: * Type) (at) const;

}

Template

Struct to_const_member_function_point

{

Typedef RT (OT :: * Type) (AT1, AT2) Const;

}

Template

Struct to_const_member_function_point

{

TypedEf RT (OT :: * Type) (AT1, AT2, AT3) Const;

}

#ENDIF / / #1ndef _funtraits_h_

// filename: delegate.h

#ifndef _dlegate_h_

#define _dlegate_h_

#include

#include

#include

#include "funtraits.h"

#define delegate_define /

protected: / typedef typename base_class :: DeleObject /

DELEOBJECT; /

TypedEf TypeName Base_class :: object_member_function_point /

Object_member_function_point; /

TYPEDEF TYPENAME BASE_CLASS :: Const_Object_member_function_pointer /

const_object_member_function_point; /

TypedEf Typename Base_class :: results_type /

Result_type; /

TypedEf Typename Base_class :: function_union /

Function_union;

#define delegate_common /

PUBLIC: /

Delegate () {} /

Delegate (Function_Pointer PF): Base_Class (PF) {} /

Template /

Delegate (Const o * POBJECT, /

TypenAme to_member_function_pointer :: Type PMF): /

Base_class (POBJECT, PMF) {} /

Template /

Delegate (Const o * POBJECT, /

TypeName TO_CONST_MEMBER_FUNCTION_POINTER :: Type PMF): /

Base_class (POBJECT, PMF) {} /

Delegate (const delegate & rhs): /

BASE_CLASS (RHS) {} /

Delegate & Operator = (Function_Point 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 :: 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 /

Void Operator () (P1 P1) Const /

{/

TypeName Std :: Vector :: 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 /

Void Operator () (P1 P1, P2 P2) Const /

{/

TypeName Std :: Vector :: 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 /

Void Operator () (P1 P1, P2 P2, P3 P3) Const /

{/

TypeName Std :: Vector :: 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 /

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 /

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 /

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

Class Delegate

{

protected:

Typedef t functor;

Std :: Vector 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 :: const_iterator i = funcs.begin ();

For (; i! = funcs.end (); i)

{

(* i) ();

}

}

Template

Void Operator () (p1 p1) const

{

TypeName Std :: Vector :: const_iterator i = funcs.begin ();

For (; i! = funcs.end (); i)

{

(* i) (p1);

}

}

Template

Void Operator () (P1 P1, P2 P2) Const

{

TypeName Std :: Vector :: const_iterator i = funcs.begin ();

For (; i! = funcs.end (); i)

{

(* i) (p1, p2);

}

}

Template

Void Operator () (P1 P1, P2 P2, P3 P3) Const

{

TypeName Std :: Vector :: const_iterator i = funcs.begin ();

For (; i! = funcs.end (); i)

{

(* i) (P1, P2, P3);

}

}

Bool Operator == (const delegate & r Hs) 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 :: 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 :: item j;

TypeName Std :: Vector :: 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.rase (j);

}

RETURN * THIS;

}

DELEGATE OPERATOR (Const Delegate & r Hs) 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 :: 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 :: item j =

Std :: Find (funcs.begin (), funcs.end (), rhs;

IF (j! = funcs.end ())

Funcs.rase (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 Dector & lh, Const Delegate & rhs)

{

RETURN RHS LHS;

}

}

Template

Class delegate_base

{

protected:

Class deleObject {};

TypeDef t function_point;

TypedEf Typename to_member_function_point :: Type

Object_member_function_point;

TypedEf TypeName to_Const_member_function_point :: Type

Const_Object_member_function_point;

TypedEf TypeName Function_Traits :: 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

Function_union (Const O * POBJECT,

TypenAme to_member_function_pointer :: Type PMF):

M_PMF (* ReinterPret_cast (& PMF)),

m_pcobject (reinterpret_cast (pObject)) {}

Template

Function_union (Const O * POBJECT,

TypenAme to_const_member_function_point :: Type PMF):

M_PCMF (* Reinterpret_cast (& PMF)),

m_pcobject (reinterpret_cast (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 funcs;

PUBLIC:

DELEGATE_BASE () {}

DELEGATE_BASE (Function_Pointer PF) {if (! (pf == null) funcs.push_back (pf);

Template

Delegate_base (Const O * POBject,

Typename to_member_function_point :: Type PMF)

{

IF (POBJECT && PMF)

Funcs.push_back (function_union (pobject, pmf));

}

Template

Delegate_base (Const O * POBject,

TypeName TO_CONST_MEMBER_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 :: 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 :: item j;

TypeName Std :: Vector :: 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.rase (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_Point RHS)

{

IF (rhs == null)

RETURN * THIS;

TypeName Std :: Vector :: 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 :: item j = std :: find (funcs.begin (), funcs.end (), rhs);

IF (j! = funcs.end ())

Funcs.rase (j);

RETURN * THIS;

}

DELEGATE_BASE OPERATOR (Function_Point RHS) Const

{

RETURN DELEGATE_BASE (* this) = rhs;

}

DELEGATE_BASE OPERATOR - (Function_Point RHS) Const

{

RETURN DELEGATE_BASE (* this) - = rhs;

}

Friend Delegate_base Operator (Function_Pointer LHS, Const Delegate_Base & RHS)

{

RETURN RHS LHS;

}

}

Template

Class delegate : public delegate_base

{

protected:

TYPEDEF DELEGATE_BASE base_class;

TYPEDEF R (* function_pointer) ();

Delegate_Define

Delegate_Common

Delegate_operator_bracket_rt

}

Template

Class delegate : public delegate_base

{

protected:

TYPEDEF DELEGATE_BASE Base_class;

TYPEDEF R (* function_pointer) (V1);

Delegate_Define

Delegate_Common

Delegate_operator_bracket_rt

}

Template

Class Delegate : Public Delegate_Base

{

protected:

Typedef delegate_base Base_class;

TYPEDEF R (* function_pointer) (V1, V2);

Delegate_Define

Delegate_Common

Delegate_operator_bracket_rt

}

Template <>

Class delegate : public delegate_base

{

protected:

TYPEDEF DELEGATE_BASE base_class;

TYPEDEF VOID (* function_pointer) ();

protected:

Typedef Base_Class :: DeleObject

DeleObject;

TYPEDEF BASE_CLASS :: Object_member_function_pointer

Object_member_function_point;

TYPEDEF BASE_CLASS :: const_Object_member_function_pointerconst_object_member_function_point;

TypedEf Base_Class :: Result_Type

Result_Type;

TypedEf Base_class :: function_union

Function_union;

Delegate_Common

PUBLIC:

Void Operator () () const

{

Std :: vector :: 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

Class delegate : public delegate_base

{

protected:

TYPEDEF DELEGATE_BASE base_class;

TYPEDEF VOID (* function_point) (V1);

Delegate_Define

Delegate_Common

Delegate_operator_bracket

}

Template

Class Delegate : Public Delegate_Base

{

protected:

Typedef delegate_base Base_class;

Typedef void (* function_pointer) (V1, V2);

Delegate_Define

Delegate_Common

Delegate_operator_bracket

}

Template

Class Delegate : Public Delegate_base

{

protected:

TYPEDEF DELEGATE_BASE Base_class;

Typedef void (* function_pointer) (V1, V2, V3);

Delegate_Define

Delegate_Common

Delegate_operator_bracket

}

Template

Inline delegate make_dlegate (f & fp)

{

Return delegate (fp);

}

Template

Inline Delegate make_dlegate (f * fp)

{

Return delegate (fp);

}

Template

Inline delegate make_dlegate (const f & fp)

{

Return delegate (fp);

Template

Inline delegate make_delegate (const f * fp)

{

Return delegate (fp);

}

Template

INLINE DELEGATE :: function_type>

Make_Delegate (Const T * TP, FF)

{

Return Delegate :: function_type> (tp, fp);

}

#ndif // #ifndef _dlegate_h_

// filename: Event.h

#ifndef _event_h_

#define _event_h_

TypedEf void * Object;

Class Eventargs

{

PUBLIC:

Bool Cancel;

Eventargs (): Cancel (False) {}

}

Template

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

Void Operator () (p1 p1) const

{

Handlers (p1, evenetargs ());

}

Template

Void Operator () (P1 P1, P2 P2) Const

{

Handlers (P1, P2);

}

}

#ndif // #ifndef _Event_H_

// filename: main.cpp

#include

#include "delegate.h"

#include "event.h"

#include

#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 EventHandler1;

TypeDef EventHandler1 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 (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_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 (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_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 (int Argc, char * argv [])

{

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 (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 (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 (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 FFUNC_INT_ (TestFunc_INT_);

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

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

Delegate

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 FFUNC_INT_INT (TESTFUNC_INT_INT);

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

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

Delegate

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 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

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 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 (& global_process_okclick);

TestStatic1 (Object) null, myeventargs ());

MyEventargs E;

Delegate

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

TestStatic4 (& Master_Base :: static_process_okclick);

TestStatic4 ((Object) NULL, E);

Make_functor (& Master_Base :: Static_Process_okClick) ((Object) 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);

System ("pause");

Return 0;

}

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

New Post(0)