Click here to Skip to main content
15,886,795 members
Articles / Programming Languages / C

Dynamic Message Passing for C++

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
23 Mar 2009CPOL2 min read 55.2K   400   14   3
Dynamic message passing for C++.

Introduction

Programming languages like Smalltalk and Objective-C are considered more flexible than C++ because they support dynamic message passing. C++ does not support dynamic message passing; it only supports static message passing: when a method of an object is invoked, the target object must have the invoked method; otherwise, the compiler outputs an error.

Although the way C++ does message passing is much faster than the way Smalltalk or Objective-C does it, sometimes the flexibility of Smalltalk or Objective-C is required. This little article shows how it is possible to achieve dynamic message passing in C++ with minimum code.

Background

C++ achieves message passing with the help of vtables.

Smalltalk and Objective C use a method map to achieve the same result.

Example

The supplied header contains an implementation of dynamic message passing. In order to add dynamic message passing capabilities to an object, the following things must be done:

  1. Declare a global function which acts as a signature of the message.
  2. Inherit from dmp::object.
  3. In the class constructor, add one or more messages to the object using the method add_message.
  4. Send a message to an object using the function invoke(signature, ...parameters).

Here is an example:

//the dynamic-message-passing header
#include "dmp.hpp" 

//the message signature
std::string my_message(int a, double b) { return ""; }

//a class that accepts messages dynamically
class test : public dmp::object {
public:
    test() {
        add_message(&::my_message, &test::my_message);
    }

    std::string my_message(int a, double b);
}; 

int main() {
    test t;
    std::string s = t.invoke(&::my_message, 10, 3.14);
}

How it Works

Each object contains a shared ptr to a map. The map's key is the pointer to the signature of the message. The map's value is a pointer to an internal message structure which holds a pointer to the method to invoke.

When a method is invoked, the appropriate message structure is retrieved from the map, and the method of the object stored in the message structure is invoked, using this as the target object.

The map used internally is unordered, for efficiency reasons.

The message map of objects is shared between objects for efficiency reasons as well. When a message map is modified, it is duplicated if it is not unique, i.e., the copy-on-write pattern is applied.

The code is thread safe only when the message maps are not modified while used for look-up by different threads. If a message map is modified, then no thread must send messages to an object at the same time.

The code uses the boost library because of:

  1. unordered maps; the class boost::unordered_map is used.
  2. the pre-processor; used for automatically constructing code for different number of parameters.
  3. shared ptrs; used for internal memory management.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Greece Greece
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhow can I download the demo code? Pin
jamsbone28-Mar-16 7:01
jamsbone28-Mar-16 7:01 
QuestionWell Article is ok Pin
BrainlessLabs.com2-Oct-11 22:06
BrainlessLabs.com2-Oct-11 22:06 
AnswerRe: Well Article is ok Pin
Achilleas Margaritis4-Oct-11 0:20
Achilleas Margaritis4-Oct-11 0:20 

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.