Code Examples
  Home arrow Code Examples arrow Page 4 - Style Case Studies: Generic Callbacks
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
CODE EXAMPLES

Style Case Studies: Generic Callbacks
By: Addison-Wesley/Prentice Hall PTR
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2004-09-29

    Table of Contents:
  • Style Case Studies: Generic Callbacks
  • Dissecting Generic Callbacks and Improving Style
  • More Style Improvements
  • Correcting Mechanical Errors and Limitations
  • Provide a Helper

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More Code Examples Articles
    More By Addison-Wesley/Prentice Hall PTR


     

    CODE EXAMPLES ARTICLES

    - Handling Animations and Bitmaps Using GDI+ f...
    - Download a Web Page using the WebClient
    - Creating a Chart using Data from a Database ...
    - The Basics of Charting with the MS Chart Con...
    - Searching Body Text with textRange: Enter th...
    - Searching Body Text with textRange: Building...
    - Searching Body Text with textRange, part 1: ...
    - First Steps in Programming
    - Programming in C
    - Quick Introduction to ASF,ASX, and Networkin...
    - SatView: Pointer Perfect, Part 2: Constructi...
    - SatView: Pointer Perfect, Part 1
    - Style Case Studies: Construction Unions
    - Creating an Engine for Games for Windows
    - Style Case Studies: Generic Callbacks





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway