5,401,186 members and growing! (16,349 online)
Email Password   helpLost your password?
Languages » C / C++ Language » Templates     Intermediate

Event Driven Programming using Template Specialization

By Christopher Diggins

Template specializations can be used as call-backs instead of functors or function pointers to create an event-driven framework.
C++/CLI, VC7.1, C++, Windows, .NET, Visual Studio, Dev

Posted: 18 Dec 2004
Updated: 18 Dec 2004
Views: 24,145
Bookmarked: 16 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
17 votes for this Article.
Popularity: 4.29 Rating: 3.48 out of 5
4 votes, 23.5%
1
3 votes, 17.6%
2
2 votes, 11.8%
3
3 votes, 17.6%
4
5 votes, 29.4%
5

Introduction

Template specializations provide a mechanism by which we can create an event-driven framework, instead of using function pointers.

Template specializations are alternative definitions of a template for specific parameter values. They are sometimes known as user-defined specializations. For more information, I recommend reading The C++ Programming Language, 3rd Edition by Bjarne Stroustrup.

There are two advantages of this approach, first no function pointer registration step is needed like in typical event-driven frameworks, and secondly, the compiler can do a far better job of optimizing the call.

Event-Driven Programming

Event-driven code is useful when writing a library and we want the library to provide default behavior in response to certain events, but to allow library users to provide custom behavior to one or more events. This occurs frequently in the implementation of GUI libraries. A message loop has to dispatch events to user-defined functions. This is typically done either through virtual-functions or through function-pointers. Using template specializations is an easy to use and efficient alternative.

The Code

Here is a sample program, which demonstrates the usage of template specializations for callbacks:

  const int RED_PILL = 0;
  const int BLUE_PILL = 1; 

  template<int T>
  struct EventHandler {
    static void Event() { };
  }; 

  template<typename Dummy_T = void>
  struct Dispatcher {
    static bool EventDispatch(char ch) {
      switch (ch) {
        case 'a': {
          EventHandler<RED_PILL>::Event();
          return true;
        }
        case 'b': {
          EventHandler<BLUE_PILL>::Event();
          return true;
        }
        default : {
          return false;
        }
      }
    }
  }; 

  template<>
  struct EventHandler<BLUE_PILL> {
    static void Event() { puts("Welcome to the matrix!"); };
  }; 

  int main() {
    puts("press a for the red-pill, b for the blue-pill");
    char ch = getchar();
    Dispatcher<>::EventDispatch(ch);
    return 0;
  }

Explanation

This code represents how an event-driven library could be created. The basic EventHandler and Dispatcher classes represent what would be found in the library. The specializations and the main() function represent what would be defined by the user of the library.

The first EventHandler class is a template which contains empty function definitions. This is the class which is specialized. The other EventHandler classes are the template specializations defined by the user of a library. The programmer simply needs to provide implementations of the Event() function and can do whatever else they want inside of it.

The Dispatcher class houses a Dispatch() function which triggers the appropriate user-defined event. The template parameter is ignored, but is provided to make sure that the class is constructed by the compiler after the specializations are defined by the programmer.

Notice that there is no explicit specialization EventHandler<RED_PILL>. The default handler is called in this case, which does nothing. The call should be entirely removed by the optimizer.

Why is EventDispatch inside of a template?

This is a drawback of the technique due to compilation order dependencies. Typically, in an event-driven framework, we will want to define the specializations in code after the code where the specialization is used. As soon as the compiler sees a usage of the template, it is too late for us to define a specialization. What I do then is define the usage within a template. This allows me to put off compilation of the usage until after the definition of the specializations.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Christopher Diggins


  I've been programming personal computers since my I got my first Atari 400 in 1980. I am currently self-employed as a consultant and author.

  I've worked as a professional programmer for over 12 years. I have written for Doctor Dobbs Journal, and C++ Users Journal. I also contributed to the C++ Cookbook released by O'Reilly.

  My area of research is the design and implementation of programming languages. My current project is the Cat programming language which is a cross between Forth and Haskell.
Occupation: Web Developer
Location: Canada Canada

Other popular C / C++ Language articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
Subject  Author Date 
Generalis multi-thread safe?memberf28:21 5 Jul '06  
Generalhard to detect errmemberLvYou16:58 23 Dec '04  
GeneralAdvantage?memberChristof Schardt12:37 18 Dec '04  
GeneralRe: Advantage?sussJohn_ Boy_13:30 18 Dec '04  
GeneralRe: Advantage?memberTweety19:12 18 Dec '04  
GeneralRe: Advantage?memberjmoritz16:29 25 Dec '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 18 Dec 2004
Editor: Smitha Vijayan
Copyright 2004 by Christopher Diggins
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project