Implement C + C ++ (2-Delegate Event Functor)

zhaozj2021-02-08  224

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

#ifndef _dlegate_h_

#define _dlegate_h_

#include

#include

#include "delegate_rt.h"

Template

Class Delegate

{

protected:

Std :: Vector funcs;

PUBLIC:

Typedef t functortype;

Delegate () {}

Explicit Delegate (T 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);

}

}

BOOL Operator == (const delegate & rhs) 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 :: Iterator 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 = (t rhs)

{

Funcs.clear ();

IF (! (rhs == null))

Funcs.push_back (rhs);

RETURN * THIS;

}

Delegate & Operator = (T 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 - = (t rhs)

{

IF (rhs == null)

RETURN * THIS;

TypeName Std :: Vector :: Iterator J =

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

IF (j! = funcs.end ())

Funcs.rase (j);

RETURN * THIS;

}

Delegate Operator (T rhs) Const

{

RETURN DELEGATE (* THIS) = RHS;

}

DELEGATE OPERATOR - (t rhs) const

{

RETURN DELEGATE (* this) - = rhs;

}

Friend Delegate Operator (T LHS, Const Delegate & RHS)

{

RETURN RHS LHS;

}

}

#ndif // #ifndef _dlegate_h_

// filename: delegate_rt.h

#ifndef _dlegate_rt_h_

#define _dlegate_RT_H_

#include

#include

#include

Template

Class delegate_rt

{

protected:

Std :: Vector funcs;

PUBLIC:

Typedef t functortype;

Typedef r retturntype;

Delegate_rt () {}

Explicit Delegate_RT (t func)

{

IF (! (func == null)) Funcs.push_back (func);

}

Delegate_rt (const delegate_rt & rhs): funcs (rhs.funcs) {}

Returntype Operator () () Const

{

IF (funcs.size ()! = 1)

Throw std :: runtime_ERROR ("Non-Multicast Delegate: Method Error!");

Return funcs.front ();

}

Template

Returntype Operator () (P1 P1) Const

{

IF (funcs.size ()! = 1)

Throw std :: runtime_ERROR ("Non-Multicast Delegate: Method Error!");

Return Funcs.front () (P1);

}

Template

Returntype Operator () (P1 P1, P2 P2) Const

{

IF (funcs.size ()! = 1)

Throw std :: runtime_ERROR ("Non-Multicast Delegate: Method Error!");

Return Funcs.front () (P1, P2);

}

BOOL Operator == (const delegate_rt & rhs) Const

{

Return funcs == rhs.funcs;

}

BOOL Operator == (const void * rhs) Const

{

IF (funcs.size ())

RETURN & FUNCS == RHS;

Return null == rhs;

}

Delegate_rt & operator = (const delegate_rt & rhs)

{

Funcs = rhs.funcs;

RETURN * THIS;

}

DELEGATE_RT & OPERATOR = (const delegate_rt & 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_rt & Operator - = (const delegate_rt & rhs)

{

IF (this == & rhs)

{

Funcs.clear ();

RETURN * THIS;

}

TypeName Std :: Vector :: Iterator 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_RT OPERATOR (Const Delegate_RT & RHS) Const

{

RETURN DELEGATE_RT (* this) = rhs;

}

DELEGATE_RT OPERATOR - (Const Delegate_RT & RHS) Const

{

RETURN DELEGATE_RT (* this) - = rhs;

}

Delegate_rt & operator = (t rhs)

{

Funcs.clear ();

IF (! (rhs == null))

Funcs.push_back (rhs);

RETURN * THIS;

}

Delegate_rt & Operator = (T 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_rt & Operator - = (t rhs)

{

IF (rhs == null)

RETURN * THIS;

TypeName Std :: Vector :: Iterator J =

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

IF (j! = funcs.end ())

Funcs.rase (j);

RETURN * THIS;

}

DELEGATE_RT OPERATOR (T rhs) Const

{

RETURN DELEGATE_RT (* this) = rhs;

}

DELEGATE_RT OPERATOR - (T rhs) const

{

RETURN DELEGATE_RT (* this) - = rhs;

}

Friend Delegate_rt Operator (T LHS, Const Delegate_RT & RHS) {

RETURN RHS LHS;

}

}

#ndif // #ifndef _Delegate_rt_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: functor.h

#ifndef _functor_h_

#define _functor_h_

Template

Class Functor_Base

{

PUBLIC:

TypeDef t deleObject;

TYPEDEF T1 FUNC_PT;

TYPEDEF T2 MEM_FUNC_PT;

TYPEDEF T3 CMEM_FUNC_PT;

union

{

FUNC_PT M_PF;

MEM_FUNC_PT M_PMF;

CMEM_FUNC_PT M_PCMF;

}

union

{

DELEOBJECT * m_POBJECT;

Const deleObject * m_pcobject;

}

Functor_base (): m_pf (null), m_pobject (null) {}

Functor_base (FUNC_PT PF):

M_PF (PF), M_PObject (null) {}

Functor_base (const deleObject * pobject, mem_func_pt pf):

m_pmf (pf), m_pcobject (pObject) {}

Functor_base (const deleObject * pobject, cmem_func_pt pf):

m_pcmf (pf), m_pcobject (pobject) {}

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

Bool Operator == (Const functor_base & 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 == (const void * rhs) Const

{

Return m_pobject == NULL? m_pf == rhs: * (const void **) & m_pmf == rhs;

}

Functor_base & operator = (const functor_base & rhs)

{

IF (& rhs == this)

Return * this;

M_PObject = rhs.m_pobject;

IF (m_pobject == null)

m_pf = rhs.m_pf;

Else

m_pmf = rhs.m_pmf;

RETURN * THIS;

}

}

#include "functor0.h"

#include "functor1.h"

#include "functor2.h"

#include "functor0_rt.h"

#include "functor1_rt.h"

#include "functor2_rt.h"

#ENDIF / / #1ndef _functor_h_

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

New Post(0)