Implement C # (4-Functor1) with C ++

zhaozj2021-02-08  271

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

#ifndef _functor1_h_

#define _functor1_h_

Template

Class Functor1

{

Class DeleObject

{

}

TYPEDEF VOID (* FUNC_PT) (P1);

TYPEDEF VOID (DELEOBJECT:: * MEM_FUNC_PT) (P1);

TypeDef void (deleObject :: * cmem_func_pt) (p1) const;

Functor_base

FUNC_PT,

MEM_FUNC_PT,

CMEM_FUNC_PT> BASE;

PUBLIC:

Functor1 (): base () {}

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

Template

Functor1 (Const T * POBJECT, VOID (T :: * fp) (P1)):

Base (Const DeleObject *) (POBJECT),

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

Template

Functor1 (Const T & POBject, Void (T :: * fp) (P1)):

Base (Const DeleObject *) (POBJECT),

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

Template

Functor1 (Const T * POBJECT, VOID (T :: * fp) (P1) const):

Base (Const DeleObject *) (POBJECT),

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

Template

Functor1 (Const T & POBject, Void (T :: * fp) (P1) Const:

Base (Const DeleObject *) (POBJECT),

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

Bool Operator! = (const functor1 & r Hs) const {return! (* this == rhs);

Bool Operator == (Const functionor1 & rhs) Const

{

Return Base == rhs.base;

}

BOOL Operator == (const void * rhs) Const

{

Return Base == rhs;

}

Functor1 & operator = (Const functor1 & rhs)

{

Base = rhs.base;

Return * this;}

Void Operator () (p1 p1) const

{

IF (Base.m_PObject == NULL)

(* base.m_pf) (p1);

Else

(base.m_pobject -> * (base.m_pmf)) (p1);

}

}

Template

Inline functor1 make_functor (void (* fp) (P1))

{

Return Functor1 (FP);

}

Template

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

{

Return Functor1 (TP, FP);

}

Template

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

{

Return Functor1 (TP, FP);

}

#ENDIF / / #1ndef _functor1_h_

// filename: functor1_rt.h

#ifndef _functor1_rt_h_

#define _functor1_rt_h_

Template

Class functor1_rt

{

Class DeleObject

{

}

TYPEDEF R (* FUNC_PT) (P1);

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

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

Functor_base

FUNC_PT,

MEM_FUNC_PT,

CMEM_FUNC_PT> BASE;

PUBLIC:

Functor1_rt (): Base () {}

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

Template

Functor1_rt (Const T * POBJECT, R (T :: * fp) (P1)):

Base (Const DeleObject *) (POBJECT),

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

Template

Functor1_rt (Const T & Pobject, R (T :: * fp) (P1)):

Base (Const DeleObject *) (POBJECT),

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

Template

Functor1_rt (Const T * POBJECT, R (T :: * fp) (P1) const):

Base (Const DeleObject *) (POBJECT),

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

Template

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

Base (Const DeleObject *) (POBJECT),

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

Bool Operator! = (const functor1_rt & rhs) const {return! (* this == rhs);} BOOL Operator == (Const functor1_rt & rhs) Const

{

Return Base == rhs.base;

}

BOOL Operator == (const void * rhs) Const

{

Return Base == rhs;

}

Functor1_rt & Operator = (const functor1_rt & rhs)

{

Base = rhs.base;

RETURN * THIS;

}

R operator () (p1 p1) const

{

IF (Base.m_PObject == NULL)

Return (* base.m_pf) (P1);

Else

Return (base.m_pobject -> * (base.m_pmf)) (P1);

}

}

Template

Inline functor1_rt make_functor_rt (r (* fp) (P1))

{

Return Functor1_rt (fp);

}

Template

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

{

Return Functor1_rt (tp, fp);

}

Template

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

{

Return Functor1_rt (tp, fp);

}

#ENDIF / / #1ndef _functor1_rt_h_

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

New Post(0)