The Wayback Machine - https://web.archive.org/web/20081017115509/http://www.codeguru.com:80/cpp/cpp/cpp_mfc/classes/article.php/c14891/

CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

jobs.internet.com

internet.commerce
Partners & Affiliates
















RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

Home >> Visual C++ / C++ >> C++ >> C++ & MFC >> Classes and Class Use


Templated Visitor Base Class
A templated visitor base class, avoiding the use of typelists and meta-programming.
Rating: none

JohnW@Wessex (view profile)
March 12, 2008

Environment:  C++, Visual Studio 2005

Go to page: 1  2  3  4  Next

Introduction

There are probably many of you out there who, like me, have investigated the possibilities that templates and template meta-programming can bring to your code.


(continued)



After having some success with templated image and image algorithm classes, I turned my attention to seeing whether some of the design patterns I use could be encapsulated in the same way. Investigation on the web revealed that template meta-programming could play a part in achieving this.

An article on Dr Dobbs introduced me to typelists and some of the meta-programming techniques, with the Visitor pattern used as an example.

Unfortunately, I soon found that Visual C++ was not entirely happy with the syntax used and it took some time before I managed to get a version that didn't give the compiler indigestion! That didn't last long, though. Instantiating my first Visitor object caused the compiler to generate the dreaded "Internal compiler error. Try simplifying your code". Well, I gave up at that point, deciding I had better things to do with my life than battling my compiler. So, I decided to try and achieve the similar objective to the Visitor example without meta-programming.

Anyone unfamiliar with the Visitor pattern should read this first.

The objective of the Dr Dobbs example was merely to remove a small amount of boilerplate code and ensure that all handling omissions in all code would be flagged at compile time.

The use of this technique in the Visitor pattern may seem somewhat trivial, but I hope that you may find the methods used useful in more complex situations, despite it not being as 'cool' as using typelists and meta-programming.

Ok, it's time to start.

The Classes

This is the null type. It is merely there to act as a place-holder for default template parameters. It's a template structure that is parameterised with an integer id.

//*****************************************************************
// The null type class.
// For default template parameters.
//*****************************************************************
template <const int id>
struct Null_Type
{
};

This is the class used for actual visitor template parameter types. It's a pure virtual class and requires a derived class to override the Visit function.

//*****************************************************************
// The virtual visit class.
// Declares a pure virtual visit function for the type.
//*****************************************************************
template<typename T>
class Visit_Type
{
public:
    virtual Visit(T &) = 0;
};

This is a specialised class for the default null visit types. It defines a concrete non-virtual protected function. Derived classes do not need to override it.

//*****************************************************************
// Specialised visit class.
// Declares a non-virtual concrete visit function for null
// visitors.
//*****************************************************************
template <const int id>
class Visit_Type<Null_Type<id> >
{
protected:
    void Visit(Null_Type<id> &);
}

About the Author
I've been in the electronics business for 28 years, gradually moving over the years from pure hardware design to mostly software with hardware interface elements in a real-time environment. Over the years I've programmed in Assembler (6502, 6809, 68000), BASIC, Pascal, C & C++. I've dabbled a little in PHP/MySQL as I run a climbing club website and forum. http://www.southamptonrats.org Projects have included DCT based image compression (pre-jpeg), remote imaging security systems, CCTV cameras, images analysis, real-time conveyor control. I'm currently working on a template library based on the STL for images and image algorithms.

Go to page: 1  2  3  4  Next

Downloads

  • Visitor_Templates.zip - Source files

    Tools:
    Add www.codeguru.com to your favorites
    Add www.codeguru.com to your browser search box
    IE 7 | Firefox 2.0 | Firefox 1.5.x
    Receive news via our XML/RSS feed







  • RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

    (You must be signed in to rank an article. Not a member? Click here to register)

    Latest Comments:
    No Comments Posted.
    Add a Comment:
    Title:
    Comment:
    Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



    (You must be signed in to comment on an article. Not a member? Click here to register)


    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info

    Solutions
    Whitepapers and eBooks
    Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
    Microsoft Article: BitLocker Encryption on Windows Server 2008
    Go Parallel Article: Intel Thread Checker, Meet 20 Million LOC
    IBM Whitepaper: Innovative Collaboration to Advance Your Business
    Internet.com eBook: Real Life Rails
    Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
    Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
    Internet.com eBook: The Pros and Cons of Outsourcing
    Internet.com eBook: Best Practices for Developing a Web Site
    IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
    Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
    Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
    IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
    Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
    Go Parallel Article: Getting Started with TBB on Windows
    HP eBook: Storage Networking , Part 1
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
    HP Video: Is Your Data Center Ready for a Real World Disaster?
    Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
    HP On Demand Webcast: Virtualization in Action
    Go Parallel Video: Performance and Threading Tools for Game Developers
    Rackspace Hosting Center: Customer Videos
    Intel vPro Developer Virtual Bootcamp
    HP Disaster-Proof Solutions eSeminar
    HP On Demand Webcast: Discover the Benefits of Virtualization
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Microsoft Download: Silverlight 2 Software Development Kit Beta 2
    30-Day Trial: SPAMfighter Exchange Module
    Red Gate Download: SQL Toolbelt
    Iron Speed Designer Application Generator
    Microsoft Download: Silverlight 2 Beta 2 Runtime
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
    Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES