5,692,513 members and growing! (16,923 online)
Email Password   helpLost your password?
General Programming » Programming Tips » General     Intermediate

Functor as event engine

By araud

Functor implementation. Event mechanism creation.
VC7.1, C++Windows, Win2K, WinXPVS.NET2003, VS2005, Visual Studio, Dev

Posted: 8 Mar 2005
Updated: 8 Mar 2005
Views: 35,275
Bookmarked: 10 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
16 votes for this Article.
Popularity: 2.11 Rating: 1.75 out of 5
11 votes, 68.8%
1
2 votes, 12.5%
2
0 votes, 0.0%
3
2 votes, 12.5%
4
1 vote, 6.3%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

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 functor

Advanced:

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

Advanced:

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.

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

License

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

araud


Real christian since 1997 (to be a cristian means to follow Jesus Christ)

Programmer since 1999
main language 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-power-death metall.
Occupation: Web Developer
Location: Russian Federation Russian Federation

Other popular Programming Tips articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 26 (Total in Forum: 26) (Refresh)FirstPrevNext
Generalnew commentsmemberaraud23:19 9 Mar '05  
GeneralBad programmingmemberChristian Graus15:58 8 Mar '05  
GeneralRe: Bad programmingmemberaraud18:45 8 Mar '05  
GeneralRe: Bad programmingmemberChristian Graus18:55 8 Mar '05  
GeneralRe: Bad programmingmemberaraud20:38 8 Mar '05  
GeneralHmmmmmemberKochise22:42 8 Mar '05  
GeneralRe: Hmmmmmemberaraud0:28 9 Mar '05  
GeneralI don't believe in God...memberKochise1:27 9 Mar '05  
GeneralRe: I don't believe in God...memberaraud2:08 9 Mar '05  
GeneralHey :)memberKochise2:53 9 Mar '05  
GeneralRe: HmmmmmemberChristian Graus13:58 9 Mar '05  
GeneralRe: Bad programmingmemberChristian Graus13:56 9 Mar '05  
GeneralRe: Bad programmingmemberaraud21:52 9 Mar '05  
GeneralRe: Bad programmingmemberChristian Graus10:34 10 Mar '05  
GeneralRe: Bad programmingmemberaraud21:56 10 Mar '05  
GeneralRe: Bad programmingmemberChristian Graus12:02 14 Mar '05  
GeneralGood programmingmemberaraud21:16 14 Mar '05  
GeneralRe: Good programmingmemberChristian Graus11:11 15 Mar '05  
GeneralRe: Good programmingmemberaraud19:44 15 Mar '05  
GeneralRe: Good programmingmemberChristian Graus13:21 16 Mar '05  
GeneralCode is very hard to read!memberDon Clugston14:22 8 Mar '05  
GeneralRe: Code is very hard to read!memberaraud18:58 8 Mar '05  
GeneralRe: Code is very hard to read!memberCP Visitor7:45 9 Mar '05  
GeneralRe: Code is very hard to read!memberaraud21:11 9 Mar '05  
Generalgood idea!memberWoR9:04 8 Mar '05  

General General    News News    Question Question    Answer Answer    Joke Joke