Style Case Studies: Generic Callbacks - Correcting Mechanical Errors and Limitations
(Page 4 of 5 )
b) Mechanical limitations that restrict the usefulness of the facility.
7. Consider making the callback function a normal parameter, not a template parameter. Non-type template parameters are rare in part because there’s rarely much benefit in so strictly fixing a type at compile time. That is, we could instead have:
template < class T >
class callback {
public:
typedef void (T::*Func)();
callback(T& t, Func func) : object(t), f(func) {} // bind actual object
void operator()() const { (object.*f)(); } // launch callback function
private:
T& object;
Func f;
};
Now the function to be used can vary at run-time, and it would be simple to add a member function that allowed the user to change the function that an existing callback object was bound to, something not possible in previous versions of the code.
Guideline: It’s usually a good idea to prefer making non-type parameters into normal function parameters, unless they really need to be template parameters.
8. Enable containerization. If a program wants to keep one callback object for later use, it’s likely to want to keep more of them. What if it wants to put the callback objects into a container, such as a vector or a list? Currently that’s not possible, because callback objects aren’t assignable—they don’t support operator=. Why not? Because they contain a reference, and once that reference is bound during construction, it can never be rebound to something else.
Pointers, however, have no such compunction and are quite happy to point at whatever you’d ask them to. In this case it’s perfectly safe for callback instead to store a pointer, not a reference, to the object it’s to be called on and then to use the default compiler-generated copy constructor and copy assignment operator:
template < class T >
class callback {
public:
typedef void (T::*Func)();
callback(T& t, Func func) : object(&t), f(func) {} // bind actual object
void operator()() const { (object->*f)(); } // launch callback function
private:
T* object;
Func f;
};
Now it’s possible to have, for example, a list< callback< Widget, &Widget::SomeFunc > >.
Guideline: Prefer to make your objects compatible with containers. In particular, to be put into a standard container, an object must be assignable.
“But wait,” you might wonder at this point, “if I could have that kind of a list, why couldn’t I have a list of arbitrary kinds of callbacks of various types, so that I can remember them all and go execute them all when I want to?” Indeed, you can, if you add a base class:
9. Enable polymorphism: Provide a common base class for callback types. If we want to let users have a list<callbackbase*> (or, better, a list< shared_ptr<callbackbase> >) we can do it by providing just such a base class, which by default happens to do nothing in its operator():
class callbackbase {
public:
virtual void operator()() const { };
virtual ~callbackbase() = 0;
};
callbackbase::~callbackbase() { }
template < class T >
class callback : public callbackbase {
public:
typedef void (T::*Func)();
callback(T& t, Func func) : object(&t), f(func) {} // bind actual object
void operator()() const { (object->*f)(); } // launch callback function
private:
T* object;
Func f;
};
Now anyone who wants to can keep a list<callbackbase*> and polymorphically invoke operator() on its elements. Of course, a list< shared_ptr<callback> > would be even better; see [Sutter02b].
Note that adding a base class is a tradeoff, but only a small one: We’ve added the overhead of a second indirection, namely a virtual function call, when the callback is triggered through the base interface. But that overhead actually manifests only when you use the base interface. Code that doesn’t need the base interface doesn’t pay for it.
This chapter is from Exceptional C++ Style, by Herb Sutter (ISBN 0201760428, copyright 2005. All rights reserved. It is reprinted with permission from Addison-Wesley Professional). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Provide a Helper >>
More Code Examples Articles
More By Addison-Wesley/Prentice Hall PTR