Enterevents in C # in C ++ (3-functor0)

zhaozj2021-02-08  248

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: functor0.h

#ifndef _functor0_h_

#define _functor0_h_

Class Functor0

{

Class DeleObject

{

}

TYPEDEF VOID (* func_pt) ();

Typedef void (deleObject :: * MEM_FUNC_PT) ();

TYPEDEF VOID (DeleObject :: * CMEM_FUNC_PT) () Const;

Functor_base

FUNC_PT,

MEM_FUNC_PT,

CMEM_FUNC_PT> BASE;

PUBLIC:

Functor0 (): base () {}

Functor0 (FUNC_PT PF): Base (PF) {}

Template

Functor0 (Const T * POBJECT, VOID (T :: * fp) ()):

Base (Const DeleObject *) (POBJECT),

* ((MEM_FUNC_PT *) (& fp))) {}

Template

Functor0 (Const T & POBject, Void (T :: * fp) ()):

Base (Const DeleObject *) (POBJECT),

* ((MEM_FUNC_PT *) (& fp))) {}

Template

Functor0 (Const T * POBJECT, VOID (T :: * fp) () const):

Base (Const DeleObject *) (POBJECT),

* ((CMEM_FUNC_PT *) (& fp))) {}

Template

Functor0 (Const T & POBject, Void (T :: * fp) () const):

Base (Const DeleObject *) (POBJECT),

* ((CMEM_FUNC_PT *) (& fp))) {}

Bool Operator! = (const functor0 & rhs) const {return! (* this == rhs);

Bool Operator == (Const functor0 & rhs) Const

{

Return Base == rhs.base;

}

BOOL Operator == (const void * rhs) Const

{

Return Base == rhs;

}

Functor0 & Operator = (Const functionor0 & rhs)

{

Base = rhs.base;

RETURN * THIS;

}

Void Operator () () const

{

IF (Base.m_PObject == Null) (* base.m_pf) ();

Else

(Base.m_PObject -> * (base.m_pmf)) ();

}

}

Inline functor0 make_functor (void (* fp) ())

{

Return Functor0 (FP);

}

Template

INLINE FUNCTOR0 MAKE_FUNCTOR (const t * tp, void (t :: * fp) ())

{

Return Functor0 (TP, FP);

}

Template

Inline functor0 make_functor (const t * tp, void (t :: * fp) () const)

{

Return Functor0 (TP, FP);

}

#ENDIF / / #1ndef _functor0_h_

// filename: functor0_rt.h

#ifndef _functor0_rt_h_

#define _functor0_rt_h_

Template

Class functor0_rt

{

Class DeleObject

{

}

TYPEDEF R (* func_pt) ();

TYPEDEF R (DELEOBJECT:: * MEM_FUNC_PT) ();

TYPEDEF R (DELEOBJECT:: * CMEM_FUNC_PT) () Const;

Functor_base

FUNC_PT,

MEM_FUNC_PT,

CMEM_FUNC_PT> BASE;

PUBLIC:

Functor0_rt (): base () {}

Functor0_rt (FUNC_PT PF): Base (PF) {}

Template

Functor0_rt (Const T * POBJECT, R (T :: * fp) ()):

Base (Const DeleObject *) (POBJECT),

* ((MEM_FUNC_PT *) (& fp))) {}

Template

Functor0_rt (Const T & Pobject, R (t :: * fp) ()):

Base (Const DeleObject *) (POBJECT),

* ((MEM_FUNC_PT *) (& fp))) {}

Template

Functor0_rt (Const T * POBJECT, R (T :: * fp) () const):

Base (Const DeleObject *) (POBJECT),

* ((CMEM_FUNC_PT *) (& fp))) {}

Template

Functor0_rt (Const T & Pobject, R (t :: * fp) () const):

Base (Const DeleObject *) (POBJECT),

* ((CMEM_FUNC_PT *) (& fp))) {}

BOOL Operator! = (const functor0_rt & r Hs) const {return! (* this == rhs);

BOOL Operator == (const functor0_rt & rhs) const

{

Return Base == rhs.base;

}

BOOL Operator == (const void * rhs) Const

{

Return Base == rhs;

}

Functor0_rt & Operator = (const functor0_rt & rhs) {

Base = rhs.base;

RETURN * THIS;

}

R operator () () const

{

IF (Base.m_PObject == NULL)

Return (* base.m_pf) ();

Else

Return (Base.m_PObject -> * (Base.m_PMF)) ();

}

}

Template

Inline functor0_rt make_functor_rt (r (* fp) ())

{

Return Functor0_rt (fp);

}

Template

Inline functor0_rt make_functor_rt (const t * tp, r (t :: * fp) ())

{

Return Functor0_rt (TP, FP);

}

Template

Inline functor0_rt make_functor_rt (Const T * TP, R (T :: * fp) () const)

{

Return Functor0_rt (TP, FP);

}

#ENDIF / / #ifndef _functor0_rt_h_

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

New Post(0)