SunQuest
 
       Code Examples
  Home arrow Code Examples arrow Style Case Studies: Generic Callbacks
Iron Speed
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 
Actuate Whitepapers 
VeriSign Whitepapers 
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
     
    Iron Speed
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Style Case Studies: Generic Callbacks


    (Page 1 of 5 )

    Part of the allure of generic code is its usability and reusability in as many kinds of situations as reasonably possible. How can the simple facility presented in the cited article be stylistically improved, and how can it be made more useful than it is and really qualify as generic and widely usable code? (This book excerpt is from Exceptional C++ Style by Herb Sutter, ISBN 0-201-76042-8, copyright 2005. All rights reserved. It is reprinted with permission from Addison-Wesley Professional.)

    Editor's Note: This book presents puzzles or problems. A question is posed by JG, a term for new junior-grade military officer, and an answer is presented from a Guru.


    SutterItem 35. Generic Callbacks

    JG Question

    1. What qualities are desirable in designing and writing generic facilities? Explain.

    Guru Question

    2. The following code presents an interesting and genuinely useful idiom for wrapping callback functions. For a more detailed explanation, see the original article. [Kalev01]

    Critique this code and identify:

    a) Stylistic choices that could be improved to make the design better for more idiomatic C++ usage.

    b) Mechanical limitations that restrict the usefulness of the facility.


    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 function
    private:
      T& object;
    };

    Solution

    Generic Quality

    1. What qualities are desirable in designing and writing generic facilities? Explain.

    Generic code should above all be usable. That doesn’t mean it has to include all options up to and including the kitchen sink. What it does mean is that generic code ought to make a reasonable and balanced effort to avoid at least three things:

    1. Avoid undue type restrictions. For example, are you writing a generic container? Then it’s perfectly reasonable to require that the contained type have, say, a copy constructor and a nonthrowing destructor. But what about a default constructor or an assignment operator? Many useful types that users might want to put into our container don’t have a default constructor, and if our container uses it, then we’ve eliminated such a type from being used with our container. That’s not very generic. (For a complete example, see Item 11 of Exceptional C++ [Sutter00].)

    2. Avoid undue functional restrictions. If you’re writing a facility that does X and Y, what if some user wants to do Z, and Z isn’t so much different from Y? Sometimes you’ll want to make your facility flexible enough to support Z; sometimes you won’t. Part of good generic design is choosing the ways and means by which your facility can be customized or extended. That this is important in generic design should hardly be a surprise, though, because the same principle applies to object-oriented class design.

    Policy-based design is one of several important techniques that allow “pluggable” behavior with generic code. For examples of policy-based design, see any of several chapters in [Alexandrescu01]; the SmartPtr and Singleton chapters are a good place to start.

    This leads to a related issue:

    3. Avoid unduly monolithic designs. This important issue doesn’t arise as directly in our style example under consideration below, and it deserves some dedicated consideration in its own right, hence it gets not only its own Item, but its own concluding miniseries of Items: see Items 37 through 40.

    In these three points, you’ll note the recurring word “undue.” That means just what it says: Good judgment is needed when deciding where to draw the line between failing to be sufficiently generic (the “I’m sure nobody would want to use it with anything but char” syndrome) on the one hand and overengineering (the “what if someday someone wants to use this toaster-oven LED display routine to control the booster cutoff on an interplanetary spacecraft?” misguided fantasy) on the other.

    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 1 hosted by Hostway