-
Several ways to implement callback mechanism in C + +
(1) Callback mode
The essence of callback is to set a function pointer in and then invoke the method when it is necessary to trigger an event, such as Windows's window message handler function. For example code below, we need to trigger an event outside the notification when the download is complete:
(2) Sink Way
The essence of sink is that you follow each other's requirements to implement a C + + interface, and then set your implementation of the interface to the other side, the other side needs to trigger the event when the interface is called, the connection point in COM is in this way. The requirements for downloading files above, if implemented with sink, the code is as follows:
(3) The essence of Delegate mode delegate is to set member function pointers to each other and then let the other person call when the event needs to be triggered. C # in the way of delegate implementation of the event, so that C + + programmers are very envious, C + + because of the language itself, to achieve delegate is still very troublesome. The above example is implemented in the delegate way as follows:
- Class Cdownloaddelegatebase
- {
- Public
- virtual void Fire (const char* PURL, bool BOK) = 0;
- };
- Template<typename O, TypeName t>
- Class Cdownloaddelegate:public Cdownloaddelegatebase
- {
- typedef void (T::* Fun) (const char*, BOOL);
- Public
- Cdownloaddelegate (o* pObj = null, fun Pfun = null)
- : M_pfun (Pfun), M_pobj (POBJ)
- {
- }
- virtual void Fire (const char* PURL, BOOL BOK)
- {
- if (m_pfun! = NULL
- && m_pobj! = NULL)
- {
- (M_pobj->*m_pfun) (PURL, BOK);
- }
- }
- Private
- Fun M_pfun;
- o* M_pobj;
- };
- Template<typename O, TypeName t>
- cdownloaddelegate<o,t>* makedelegate (o* pobject, void (T::* pfun) (const char* PURL, BOOL))
- {
- return new Cdownloaddelegate<o, t> (Pobject, Pfun);
- }
- Class Cdownloadevent
- {
- Public
- ~cdownloadevent ()
- {
- Vector<cdownloaddelegatebase*>::iterator ITR = M_ardelegates.begin ();
- while (ITR! = M_ardelegates.end ())
- {
- Delete *itr;
- ++itr;
- }
- M_ardelegates.clear ();
- }
- void operator + = (cdownloaddelegatebase* p)
- {
- M_ardelegates.push_back (P);
- }
- void operator-= (cdownloaddelegatebase* p)
- {
- ITR ITR = Remove (M_ardelegates.begin (), M_ardelegates.end (), p);
- ITR itrtemp = ITR;
- while (itrtemp! = M_ardelegates.end ())
- {
- Delete *itr;
- ++itr;
- }
- M_ardelegates.erase (ITR, M_ardelegates.end ());
- }
- void operator () (const char* PURL, BOOL BOK)
- {
- ITR itrtemp = M_ardelegates.begin ();
- while (itrtemp! = M_ardelegates.end ())
- {
- (*itrtemp)->fire (PURL, BOK);
- ++itrtemp;
- }
- }
- Private
- Vector<cdownloaddelegatebase*> m_ardelegates;
- typedef vector<cdownloaddelegatebase*>::iterator ITR;
- };
- Class Cmydownloaderex
- {
- Public
- void DownloadFile (const char* PURL)
- {
- cout << "Downloading:" << pURL << "<< Endl;
- Downloadevent (PURL, true);
- }
- Cdownloadevent downloadevent;
- };
- Class Cmyfileex
- {
- Public
- void Download ()
- {
- Cmydownloaderex Downloader;
- Downloader.downloadevent + = Makedelegate (this, &cmyfileex::ondownloadfinished);
- Downloader. DownloadFile ("www.baidu.com");
- }
- virtual void ondownloadfinished (const char* PURL, BOOL BOK)
- {
- cout << "ondownloadfinished, URL:" << pURL << "status:" << BOK << Endl;
- }
- };
You can see the way the delegate code is much more than the other 2 ways above, and we are above the number of fixed parameters and types of implementation, if you want to implement variable parameters, it is more cumbersome. Variable parameters can be referenced in these 2 implementations:
Yet another C#-style Delegate Class in standard C + + Member Function pointers and the fastest Possible C + + Delegates
We can test our implementation with the following code:
Finally, a simple comparison of the above 3 implementation of the callback method:
The first method of callback is process-oriented, simple and flexible to use, just as the C language itself.
The second method of sink is object-oriented, used more in C + +, can encapsulate a set of callback interfaces in a sink, and is suitable for a series of more fixed callback events.
The third method of delegate is also object-oriented, and sink encapsulates a set of interfaces, delegate's encapsulation is in function units, granularity is smaller and more flexible than sink.
-
Which way do you prefer to implement callbacks?
C + + callback function callback