Implement C # (5-functor2) with C ++

zhaozj2021-02-08  222

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

#ifndef _functor2_h_

#define _functor2_h_

Template

Class functor2

{

Class DeleObject

{

}

TypeDef void (* func_pt) (P1, P2);

TypeDef void (deleObject :: * MEM_FUNC_PT) (P1, P2);

TYPEDEF VOID (DELEOBJECT:: * CMEM_FUNC_PT) (P1, P2) Const;

Functor_base

FUNC_PT,

MEM_FUNC_PT,

CMEM_FUNC_PT> BASE;

PUBLIC:

Functor2 (): base () {}

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

Template

Functor2 (Const T * POBJECT, VOID (T :: * fp) (P1, P2)):

Base (Const DeleObject *) (POBJECT),

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

Template

Functor2 (Const T & Pobject, Void (T :: * fp) (P1, P2)):

Base (Const DeleObject *) (POBJECT),

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

Template

Functor2 (Const T * POBJECT, VOID (T :: * FP) (P1, P2) Const):

Base (Const DeleObject *) (POBJECT),

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

Template

Functor2 (Const T & POBJECT, VOID (T :: * fp) (P1, P2) Const):

Base (Const DeleObject *) (POBJECT),

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

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

Bool Operator == (Const functor2 & rhs) Const

{

Return Base == rhs.base;

}

BOOL Operator == (const void * rhs) Const

{

Return Base == rhs;

}

Functor2 & operator = (const functor2 & rhs) {

Base = rhs.base;

RETURN * THIS;

}

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

{

IF (Base.m_PObject == NULL)

(* Base.m_pf) (P1, P2);

Else

(Base.m_PObject -> * (base.m_pmf)) (P1, P2);

}

}

Template

Inline functor2 make_functor (void (* fp) (P1, P2))

{

Return Functor2 (FP);

}

Template

Inline functor2 make_functor (const t * tp, void (t :: * fp) (p1, p2))

{

Return Functor2 (TP, FP);

}

Template

Inline functor2 make_functor (const t * tp, void (t :: * fp) (p1, p2) const)

{

Return Functor2 (TP, FP);

}

#ENDIF / / #1ndef _functor2_h_

// filename: functor2_rt.h

#ifndef _functor2_rt_h_

#define _functor2_rt_h_

Template

Class functor2_rt

{

Class DeleObject

{

}

TYPEDEF R (* FUNC_PT) (P1, P2);

TYPEDEF R (DELEOBJECT:: * MEM_FUNC_PT) (P1, P2);

TypeDef R (DeleObject :: * CMEM_FUNC_PT) (P1, P2) Const;

Functor_base

FUNC_PT,

MEM_FUNC_PT,

CMEM_FUNC_PT> BASE;

PUBLIC:

Functor2_rt (): Base () {}

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

Template

Functor2_rt (Const T * POBJECT, R (T :: * fp) (P1, P2)):

Base (Const DeleObject *) (POBJECT),

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

Template

Functor2_rt (Const T & Pobject, R (t :: * fp) (P1, P2)):

Base (Const DeleObject *) (POBJECT),

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

Template

Functor2_rt (Const T * POBJECT, R (T :: * fp) (P1, P2) Const):

Base (Const DeleObject *) (POBJECT),

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

Template

Functor2_rt (Const T & Pobject, R (t :: * fp) (p1, p2) const): Base (Const DeleObject *) (POBJECT),

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

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

Bool Operator == (const functor2_rt & rhs) Const

{

Return Base == rhs.base;

}

BOOL Operator == (const void * rhs) Const

{

Return Base == rhs;

}

Functor2_rt & operator = (const functor2_rt & rhs)

{

Base = rhs.base;

RETURN * THIS;

}

R Operator () (P1 P1, P2 P2) Const

{

IF (Base.m_PObject == NULL)

Return (* base.m_pf) (P1, P2);

Else

Return (base.m_pobject -> * (base.m_pmf)) (p1, p2);

}

}

Template

Inline functor2_rt make_functor_rt (r (* fp) (p1, p2))

{

Return Functor2_rt (fp);

}

Template

Inline functor2_rt make_functor_rt (Const T * TP, R (T :: * fp) (P1, P2))

{

Return Functor2_RT (tp, fp);

}

Template

Inline functor2_rt make_functor_rt (Const T * TP, R (T :: * fp) (P1, P2) const)

{

Return Functor2_RT (tp, fp);

}

#ENDIF / / #1ndef _functor2_rt_h_

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

New Post(0)