Click here to Skip to main content
Licence 
First Posted 8 Mar 2005
Views 55,024
Downloads 222
Bookmarked 16 times

Functor as event engine

By araud | 8 Mar 2005 | Unedited contribution
Functor implementation. Event mechanism creation.
11 votes, 68.8%
1
2 votes, 12.5%
2

3
2 votes, 12.5%
4
1 vote, 6.3%
5
1.67/5 - 16 votes
1 removed
μ 1.75, σa 2.35 [?]

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

About the Author

araud

Software Developer (Senior)

Russian Federation Russian Federation

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalnew comments Pinmemberaraud23:19 9 Mar '05  
GeneralBad programming PinmemberChristian Graus15:58 8 Mar '05  
GeneralRe: Bad programming Pinmemberaraud18:45 8 Mar '05  
GeneralRe: Bad programming PinmemberChristian Graus18:55 8 Mar '05  
GeneralRe: Bad programming Pinmemberaraud20:38 8 Mar '05  
GeneralHmmmm PinmemberKochise22:42 8 Mar '05  
GeneralRe: Hmmmm Pinmemberaraud0:28 9 Mar '05  
GeneralI don't believe in God... PinmemberKochise1:27 9 Mar '05  
GeneralRe: I don't believe in God... Pinmemberaraud2:08 9 Mar '05  
GeneralHey :) PinmemberKochise2:53 9 Mar '05  
GeneralRe: Hmmmm PinmemberChristian Graus13:58 9 Mar '05  
GeneralRe: Bad programming PinmemberChristian Graus13:56 9 Mar '05  
GeneralRe: Bad programming Pinmemberaraud21:52 9 Mar '05  
Christian Graus wrote:
araud wrote:
But if you understood me, you just flee from strait question. "WHAT CAN YOU SUGGEST INSTEAD?"
 
No, I answered it. Recursion is the method you can use to impliment what you're doing in a single method instead of 10 macros. A recursive function calls itself. Look it up in the MSDN

 
recursion? A new sound in my programming life! Rose | [Rose] Now I've found the way to solve my problem! ARE YOU KIDDING? OMG | :OMG: What recursion can do there? Metamacroses are used to copy overloaded template classes and functions with changing argument count. Rewrite a bit of code in the way you advising!
I really thought you said "recursion" about my phrase "But what can you suggest to implement it in this manner". That why I've been falling in excuses of my english. Roll eyes | :rolleyes:
 
Christian Graus wrote:
araud wrote:
Sorry my code indeed lacks comments.
 
But if you followed the convention the rest of the C/C++ world follows, it would be self documenting.

 
Yes, that's a point I have to work on. Sniff | :^) I will really try.
 
//my lovely topic!
 
Christian Graus wrote:
unless you're Jewish, they were not written for you, and either way, Paul says that no-one was saved by the Law
 
Jesus said "I come not to break the law but to fulfill" and He made the law only tougher - remember "Neither shall you commit adultery" - was said to ancients - "but I tell if you are looking to woman with lust you have already commited this to her" and so on. And Paul said that Law was the child-guider to Christ. That why each cristian must know the Law first and then receive the salvation by bloody and fierce sacrifice of Lord Jesus. Read please book "the hell's best kept secret" and you will understand why you and me should insist on preaching Law first, and ONLY when people say "What shall we do, men and brethren?" we can introduce them our Lord and saviour. In other way Jesus will be useless and devaluated for them.
 
sincerely yours, bro in Christ, Alexander
 
"7 You shall have no other gods before me. 8 You shall not make an images in order to bow down to them or serve them. 11 You shall not take the name of the LORD your God in vain. 12 Observe the sabbath day 16 Honor your father and your mother, that your days may be prolonged. 17 You shall not kill. 18 Neither shall you commit adultery. 19 Neither shall you steal. 20 Neither shall you bear false witness against your neighbor. 21 Neither shall you covet anything that's your neighbor's." Your God
GeneralRe: Bad programming PinmemberChristian Graus10:34 10 Mar '05  
GeneralRe: Bad programming Pinmemberaraud21:56 10 Mar '05  
GeneralRe: Bad programming PinmemberChristian Graus12:02 14 Mar '05  
GeneralGood programming Pinmemberaraud21:16 14 Mar '05  
GeneralRe: Good programming PinmemberChristian Graus11:11 15 Mar '05  
GeneralRe: Good programming Pinmemberaraud19:44 15 Mar '05  
GeneralRe: Good programming PinmemberChristian Graus13:21 16 Mar '05  
GeneralCode is very hard to read! PinmemberDon Clugston14:22 8 Mar '05  
GeneralRe: Code is very hard to read! Pinmemberaraud18:58 8 Mar '05  
GeneralRe: Code is very hard to read! PinmemberCP Visitor7:45 9 Mar '05  
GeneralRe: Code is very hard to read! Pinmemberaraud21:11 9 Mar '05  
Generalgood idea! PinmemberWoR9:04 8 Mar '05  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120210.1 | Last Updated 8 Mar 2005
Article Copyright 2005 by araud
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid