Click here to Skip to main content
Click here to Skip to main content

Functor as event engine

, 8 Mar 2005
Rate this:
Please Sign up or sign in to vote.
Functor implementation. Event mechanism creation.


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.


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.


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 functor


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 Call


operator +=() calls AddRef on given functor pointer

operator -=() calls Release on given functor pointer

So though GetFunctor allocates memory, CFunctorSet releases it.

Thus, in different cases  you must first AddRef and then after all Release pointer returned by GetFunctor.


            CFunctorSet<void (*)(int)> m_vMyEvent;

            void MyStaticFn(int){}

            class CMyClass{
                void MyMemFn(int){}

            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.


CDeferredFunctor - intended for deferred calls in cross thread communication. Methods:

constructor - binds functor

operator =() - binds functor

Call - stores arguments

DeferredCall - 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 Call

CRemoteFunctor - 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


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Software Developer (Senior)
Russian Federation Russian Federation
Real christian since 1997 (to be a cristian means to follow Jesus Christ)
Programmer since 1999
main language is C++ and I fond of it!
Married to beautiful and clever sister in Christ since 2001
Musician lead guitar - since 1995, vocal since 2004. I play neoclassic-doom-progressive-death metall.

Comments and Discussions

Generalnew comments Pinmemberaraud9-Mar-05 23:19 
GeneralBad programming PinmemberChristian Graus8-Mar-05 15:58 
GeneralRe: Bad programming Pinmemberaraud8-Mar-05 18:45 
GeneralRe: Bad programming PinmemberChristian Graus8-Mar-05 18:55 
GeneralRe: Bad programming Pinmemberaraud8-Mar-05 20:38 
GeneralHmmmm PinmemberKochise8-Mar-05 22:42 
GeneralRe: Hmmmm Pinmemberaraud9-Mar-05 0:28 
GeneralI don't believe in God... PinmemberKochise9-Mar-05 1:27 
GeneralRe: I don't believe in God... Pinmemberaraud9-Mar-05 2:08 
GeneralHey :) PinmemberKochise9-Mar-05 2:53 
GeneralRe: Hmmmm PinmemberChristian Graus9-Mar-05 13:58 
GeneralRe: Bad programming PinmemberChristian Graus9-Mar-05 13:56 
GeneralRe: Bad programming Pinmemberaraud9-Mar-05 21:52 
GeneralRe: Bad programming PinmemberChristian Graus10-Mar-05 10:34 
GeneralRe: Bad programming Pinmemberaraud10-Mar-05 21:56 
GeneralRe: Bad programming PinmemberChristian Graus14-Mar-05 12:02 
GeneralGood programming Pinmemberaraud14-Mar-05 21:16 
GeneralRe: Good programming PinmemberChristian Graus15-Mar-05 11:11 
GeneralRe: Good programming Pinmemberaraud15-Mar-05 19:44 
GeneralRe: Good programming PinmemberChristian Graus16-Mar-05 13:21 
GeneralCode is very hard to read! PinmemberDon Clugston8-Mar-05 14:22 
GeneralRe: Code is very hard to read! Pinmemberaraud8-Mar-05 18:58 
GeneralRe: Code is very hard to read! PinmemberCP Visitor9-Mar-05 7:45 
GeneralRe: Code is very hard to read! Pinmemberaraud9-Mar-05 21:11 
Generalgood idea! PinmemberWoR8-Mar-05 9:04 
GeneralRe: good idea! Pinmemberaraud8-Mar-05 19:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150128.1 | Last Updated 8 Mar 2005
Article Copyright 2005 by araud
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid