Designing a Generic Callback Dispatcher

In many applications, you need a generic class that invokes callback functions regardless of their class type. For example, an even-driven system that needs to call a member function of a GUI component without knowing its actual type. You can create a generic callback class template to streamline this task. Such a template takes the class whose member function is to be called as its first parameter. The second template parameter is a pointer to that class’s member function. The trick is that you base the second parameter on the first one, as follows:

 template < class T, void (T::*F)() > class callback {/**/};

The implementation of the template is relatively simple: it has a reference to T, which is the class whose member function is to be called, a constructor and a member function called execute(), which invokes the callback member function:

 template < class T, void (T::*F)() >class callback{public: callback(T& t) : object(t) {} // assign actual object to T void execute() {(object.*F)();}// launch callback functionprivate: T& object;};

Remember that in order to call a member function through a pointer to member, you must have a reference or pointer to the actual object. This is why the template has a T& as a data member. Now suppose we want to use the callback template to execute a callback member function of class A:

 class A{public: void f();};

Here’s how you instantiate the template: a template argument must be a constant expression. Therefore, you can’t use a variable as the address of the member function. Instead, use the & operator to take the function’s address. Finally, pass the object whose member function you want to be called as the argument for the template object:

 int main(){ A a; // first, create an object callback < A, &A::f > c(a); // instantiate template  c.execute(); // call callback member function}

You can use the callback class template with any class type, as long as the member function called has the same signature.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

Top 5 B2B SaaS Marketing Agencies for 2023

In recent years, the software-as-a-service (SaaS) sector has experienced exponential growth as more and more companies choose cloud-based solutions. Any SaaS company hoping to stay ahead of the curve in this quickly changing industry needs to invest in effective marketing. So selecting the best marketing agency can mean the difference

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.