Click here to Skip to main content
15,885,141 members
Articles / Programming Languages / C++

MultiMethods Implementation via Deferred Dispatching

Rate me:
Please Sign up or sign in to vote.
3.17/5 (10 votes)
24 Mar 20043 min read 51.7K   530   23   6
MultiMethods Implementation Approach

Introduction

Here, you can find a solution to a well known problem of multimethods. It will be demonstrated using famous "crossing shapes example".

The main merits of the proposed solution are:

  1. no use of type casts of any kind (dynamic, static, reinterpret, const or C-style);
  2. no use of RTTI;
  3. no use of preprocessor;
  4. strong type safety;
  5. constant time of multimethod execution;
  6. no dynamic memory allocation (via new or malloc) is performed during multimethod call;
  7. compiler doing all the work;
  8. use of only standard C++ features.

Background

This approach makes it possible for user to write quite readable code. Suppose we have the following hierarchy of shapes:

   ---- Shape_ ------
   |      |         |
Circle_  Rect_  Triangle_
          |
        Square_

Then, user can write code like this:

C++
Shape_* circle = new Circle_( Point_( 5, 5 ), 3 );
Shape_* rect = new Rect_( Point_( 5, 5 ), Point_( 10, 10 ) );
bool cross1 = circle->Cross( rect );
bool cross2 = rect->Cross( circle );

Multimethod is called in the following form:

(arg0)->Cross( arg1 )

where types of both arg0 and arg1 (TArg0 and TArg1, respectively) are unknown at compile time. At runtime, the TArg0 type is detected via virtual method call (method Cross) and the TArg1 - via use of 'deferred dispatching' pattern (Visitor-like pattern, where nodes can be declared independently of visitor).

User is responsible for providing implementation of crossing detection code for situations where types TArg0 and TArg1 are known. Implementations are provided in the form of template class specialization. For example:

C++
template <>
struct Crossing_< Circle_, Rect_ >
{
//implementation
};

One can provide some (or all) of the following:

  • fully specialized versions of Crossing_ (circles against rectangles, for example),
  • symmetric versions of Crossing_ (rectangles against circles via circles against rectangles crossing, for example),
  • hierarchy depended versions (crossing of two rectangular shapes, for example),
  • fully specialized homogeneous versions (crossing of two circles, for example),
  • etc.

Usage

The details are provided in the form of well-commented code in demo project (in file mmvdd_initial.cpp).

Dependencies:

  • standard compliant C++ compiler
  • boost-1.30.2

Keep in mind that some obvious improvements are missed to keep readability.

Improvements

Some improvements of the initial solution will be provided below.

Deferred Dispatcher Generation

Someone thinks that manual writing of dispatcher is awkward. But we can generate a dispatcher from repository typelist, that contains types of all used concrete shapes.

Improvement Advantages

  • Suppose there are two libraries of shapes and types provided by them which are joined in two typelists: Shapes0 and Shapes1. One can merge these typelists and use these libraries together.
  • Typelists provided by libraries can be easily modified. One can add or remove some shapes.

Improvement Disadvantages

  • Compilation time may grow.
  • Number of shape types is limited by standard (by template recursion limitation).

The details of this improvement are provided in the form of well-commented code in demo project (in file mmvdd_generated.cpp).

References

  1. www.boost.org
  2. Andrei Alexanderscu "Modern C++ Design"
  3. David Vandevoorde, Nicolai M. Josuttis "C++ Templates: The Complete Guide"
  4. Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides "Design Patterns: Elements of Reusable Object-Oriented Software"

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.


Written By
Web Developer
Russian Federation Russian Federation
An experienced software developer.

Now I'm participating in VisualSVN project that is an integration package between Subversion and Visual Studio.

Comments and Discussions

 
GeneralNice idea Pin
Emilio Garavaglia18-Mar-04 23:20
Emilio Garavaglia18-Mar-04 23:20 
GeneralRe: Nice idea Pin
Danil Shopyrin21-Mar-04 20:13
Danil Shopyrin21-Mar-04 20:13 
GeneralGreat start Pin
Igor Okulist18-Mar-04 20:09
Igor Okulist18-Mar-04 20:09 
GeneralRe: Great start Pin
Danil Shopyrin21-Mar-04 20:20
Danil Shopyrin21-Mar-04 20:20 
GeneralRe: Great start Pin
UltraJoe26-Mar-04 3:01
UltraJoe26-Mar-04 3:01 
Maybe if you explained, for the Boost-impaired among us, exactly what you're doing with the Boost classes, it would help those who can't or won't use Boost.

I, too, found it difficult to follow where you were going with all this. I like the overall idea, but I don't see what it gives you. (I confess to having very little idea what "multimethods" is, so this may be my problem. Unsure | :~ )
GeneralRe: Great start Pin
Danil Shopyrin28-Mar-04 19:18
Danil Shopyrin28-Mar-04 19:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.