Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C++
Article

Functor as event engine

Rate me:
Please Sign up or sign in to vote.
2.25/5 (17 votes)
8 Mar 20052 min read 78.7K   270   17   26
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 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:

<blockquote>
    <p>
        CFunctorSet<void (*)(int)> m_vMyEvent;<br/>
        void MyStaticFn(int){}<br/>
        class CMyClass{
        public:
            void MyMemFn(int){}
        };<br/>
        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.
    </p>
</blockquote>

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


Written By
Software Developer (Senior)
Russian Federation Russian Federation
As programmer I understand that every program takes brain to be created. The more complex is the software, the more senior developers it takes. And they say that DNA doesn't have an author? I don't buy it.

Comments and Discussions

 
Generalnew comments Pin
araud9-Mar-05 22:19
araud9-Mar-05 22:19 
GeneralBad programming Pin
Christian Graus8-Mar-05 14:58
protectorChristian Graus8-Mar-05 14:58 
GeneralRe: Bad programming Pin
araud8-Mar-05 17:45
araud8-Mar-05 17:45 
GeneralRe: Bad programming Pin
Christian Graus8-Mar-05 17:55
protectorChristian Graus8-Mar-05 17:55 
GeneralRe: Bad programming Pin
araud8-Mar-05 19:38
araud8-Mar-05 19:38 
GeneralHmmmm Pin
Kochise8-Mar-05 21:42
Kochise8-Mar-05 21:42 
GeneralRe: Hmmmm Pin
araud8-Mar-05 23:28
araud8-Mar-05 23:28 
GeneralI don't believe in God... Pin
Kochise9-Mar-05 0:27
Kochise9-Mar-05 0:27 
...I believe in what humankind can do. Coding is creation, thus *WE* coders *ARE* Gods. Building makers *ARE* Gods, cookers *ARE* God... Faith in something or some other Powerfull, mighty, whatever you can call Him is nothing more than wasting your beliefs !

Believe in the Lottery, you have at least *ONE* chance over some millions something happens without any kind of your interaction. Help people here by providing source code, there is better chance something good happens (e.g. saving the ass of an employee coding against a straight deadline).

Oh, and I do not fears some pseudo commandments. I was born free, and thus free from mental 'slavery'. Follow any rules you want, but here are pragmatic coders whose only Guru is theirself Smile | :) You may love and help people without being told so... You are free to do so Smile | :)

And no needs to link currently unknown and/or non-understood things as being Godly. Once a day, people believed in the mighty creation of God to explain the spontaneous generation of... frogs. Then some crazy scientists spend some time to search why and how, and now everyone knows that the lifecycle of frogs include a tadpole cycle.

Such research were done on DNA, atomic physics and so. Tools becames more and more accurate, allowing scientists to discover more and more things that was hidden to the human perception. In January 2005, Huygens discovered Titan's surface to human sight... I also recognize some days people will perhaps see God. But till this day, I have something else to fill my life with Smile | :)

Some light research are now focusing on animal's form of mindset (perhaps some basic intelligence), equal skills of different skin colored people, women experiences orgasm, such thoughts that were forbiden by your kind of beliefs. And if such research are now allowed, that's because the Church have less power than before... But that's plain politic, forget this :/

And to finish, I fully repect your love for someone 'above', but don't display it so much everywhere, first it won't makes you better, and second it's pretty like pornography for people not sharing your point of view. It's not the right place for such Jesus/God/Commandment/whatever picturing Smile | :)

Kochise

PS : My signature 'In Code we trust !' is a funny joke, I don't spare my time in such faiths, not even coding...

PRONE TO BE EDITED, NOT PURPOSED TO HURT ANYBODY

In Code we trust !
GeneralRe: I don't believe in God... Pin
araud9-Mar-05 1:08
araud9-Mar-05 1:08 
GeneralHey :) Pin
Kochise9-Mar-05 1:53
Kochise9-Mar-05 1:53 
GeneralRe: Hmmmm Pin
Christian Graus9-Mar-05 12:58
protectorChristian Graus9-Mar-05 12:58 
GeneralRe: Bad programming Pin
Christian Graus9-Mar-05 12:56
protectorChristian Graus9-Mar-05 12:56 
GeneralRe: Bad programming Pin
araud9-Mar-05 20:52
araud9-Mar-05 20:52 
GeneralRe: Bad programming Pin
Christian Graus10-Mar-05 9:34
protectorChristian Graus10-Mar-05 9:34 
GeneralRe: Bad programming Pin
araud10-Mar-05 20:56
araud10-Mar-05 20:56 
GeneralRe: Bad programming Pin
Christian Graus14-Mar-05 11:02
protectorChristian Graus14-Mar-05 11:02 
GeneralGood programming Pin
araud14-Mar-05 20:16
araud14-Mar-05 20:16 
GeneralRe: Good programming Pin
Christian Graus15-Mar-05 10:11
protectorChristian Graus15-Mar-05 10:11 
GeneralRe: Good programming Pin
araud15-Mar-05 18:44
araud15-Mar-05 18:44 
GeneralRe: Good programming Pin
Christian Graus16-Mar-05 12:21
protectorChristian Graus16-Mar-05 12:21 
GeneralCode is very hard to read! Pin
Don Clugston8-Mar-05 13:22
Don Clugston8-Mar-05 13:22 
GeneralRe: Code is very hard to read! Pin
araud8-Mar-05 17:58
araud8-Mar-05 17:58 
GeneralRe: Code is very hard to read! Pin
CP Visitor9-Mar-05 6:45
CP Visitor9-Mar-05 6:45 
GeneralRe: Code is very hard to read! Pin
araud9-Mar-05 20:11
araud9-Mar-05 20:11 
Generalgood idea! Pin
WoR8-Mar-05 8:04
WoR8-Mar-05 8:04 

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

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