Functor as event engine






2.25/5 (17 votes)
Mar 8, 2005
2 min read

80643

272
Functor implementation. Event mechanism creation.
Introduction
Functor implementation
Functor is a wrapper around function. It's useful to unify static and member function calls. Also it's profitable for event mechanism creation.
Syntax represented below works only on MSVC++ 7.1, this file have defines for 6.0 also but you won't be happy using them.
Using:
IFunctor - common interface for all functors. It has few functions:
Call()
- calls bounded function,
operator ()
- calls Call
IsEqual(IFunctor *)
- returns true if both functors are bounded with the same function.Advanced:
IFunctor is inherited from IRefCounted
IRefCounted has these methods:
AddRef()
- increments reference counter
Release()
- decrements reference counter and deletes object when reference count is zero.
DecreaseRef()
- decrements reference counter without deletion
GetFunctor
() - overloaded function that creates functor, returns IFunctor
GetFunctor(StaticFunction)
- creates static functor
GetFunctor(&obj/*CMyObj */, CMyObj::Function)
- creates member functor
GetFunctor(pFunctor/*IFunctor* */)
- creates member functorAdvanced:
GetFunctor returns pointer to IFunctor with zero reference counter. So AddRef and then Release will free allocated memory.
CFunctorSet
- container for IFunctor*. Also derived from IFunctor, has methods:
operator +=(IFunctor*)
- appends functor
operator -=(IFunctor*)
- removes functor (and not only if pointers are equal: it uses IsEqual on functors)
Call
- calls all functors
operator ()
-calls CallAdvanced:
operator +=()
calls AddRef on given functor pointer
operator -=()
calls Release on given functor pointerSo though GetFunctor allocates memory, CFunctorSet releases it.
Thus, in different cases you must first AddRef and then after all Release pointer returned by GetFunctor.
Example:
CFunctorSet<void (*)(int)> m_vMyEvent;
void MyStaticFn(int){}
class CMyClass{ public: void MyMemFn(int){} };
m_vMyEvent+=GetFunctor(MyStaticFn); CMyClass obj; m_vMyEvent+=GetFunctor(&obj, CMyClass::MyMemFn); m_vMyEvent(1); //calls both functions m_vMyEvent-=GetFunctor(MyStaticFn); //it works 'cause operator -=() uses IsEqual m_vMyEvent(2); //calls only member function of CMyClass obj.
Advanced:
CDeferredFunctor
- intended for deferred calls in cross thread communication. Methods:
constructor
- binds functoroperator =() - binds functor
Call
- stores argumentsDeferredCall - calls bounded functor with stored arguments
IsCalled()
- returns true if Call is already made but DeferredCall is still not
DeferredCallSaveResult()
- saves result of calling
GetResult()
- returns result saved by DeferredCallSaveResult
SetOnCall()
- allows to set functor that will be called during CallCRemoteFunctor - same as CDeferredFunctor but stores arguments in IStream. Methods:
constructor
- binds functor
Call
- stores arguments
RemoteCall
- calls bounded functor with stored arguments
GetData(IStream * pStream)
- copies internal stream to given
SetData(IStream * pStream, bool bRewind=true)
- copies given stream to the internal, sets given stream to start optionally before copying
SetOnCall
- allows to set functor that will be called during Call