Click here to Skip to main content
15,915,078 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 79.3K   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

 
GeneralRe: good idea! Pin
araud8-Mar-05 18:30
araud8-Mar-05 18:30 
thank you! Now I'm working on new PRC based on CRemoteFunctor, my new video system already uses this tecnology. Communication between client and server is descripted like this:
REMOTE_INTERFACE(CRPCVideo,
//client
<blockquote>
REG_CNT_FUN(OnCamAppeared, (CString /*camname*/))
REG_CNT_FUN(OnCamDisappeared, (CString /*camname*/))
REG_CNT_FUN(OnFormatChanged, (CString /*camname*/, SPBITMAPINFO /*header*/))
REG_CNT_FUN(OnFrameReady, (CString /*camname*/, SPBYTE /*dib*/))
,
//server
REG_SVR_FUN(	GetFrame, (CString /*camname*/))
REG_SVR_FUN(	Subscribe, (CString /*camname*/, byte /*fps*/))
REG_SVR_FUN(	Unsubscribe, (CString /*camname*/))
</blockquote>
)


both sides inherit CRPCVideo(macroses give different code for client ad server) and implement pure virual functions of this class, like this:
void CVideoCamClient::GetFrame(){
...
}


Caller has remote functors of it's opponent and calls them simply like this:
...
$OnFrameReady(camname, dib); //don't be afraid of $ - this is not documented 
but allowed identifier symbol. I use it to mark functors.
...

When all bugs will be corrected I will public those headers.
P.S. assuredly class created in REMOTE_INTERFACE needs remote provider - class that allows to transfer data between connection points.
...
SetProvider(pProvider);
...



"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

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.