Click here to Skip to main content
13,199,026 members (44,228 online)
Click here to Skip to main content
Add your own
alternative version


29 bookmarked
Posted 14 Jul 2001

Easy to use data dispatcher

, 14 Jul 2001
Rate this:
Please Sign up or sign in to vote.
An easy to use set of classes to dispatch any kind of data.


This is an easy to use set of classes that allows dispatching of every kind of data to objects which may be unknown to the sender. The code needs RTTI and the STL and depends on templates. If you pass pointers, you must ensure that no receiver tries to delete the pointer because there may be more than one receiver. If it's possible, you should only pass objects which are copy-able (e.g. an autoptr for a refcounted object). It also supports dispatching to a different thread if a threadbridge is used (see What about threads for more info about threads).

Here is a simple demo using MFC:

// define your class (MFC or what ever you want)
class CMyView : public CView
protected: // Nur aus Serialisierung erzeugen

    // any function you need

    // and now your data handle (for simple string)
    void HandleNewData(char* data);

    // and now the receiver
    receiver<CMyView, char*> m_MyReceiver;

// a channel object (my come from everywhere)
extern channel MyChannel;

// now the implementation
    // initialize the receiver to work with your class (and function)
    m_MyReceiver.init(this, HandleNewData);

    // now link the receiver to a channel
    // object (let it be global for this example)
    // you may also place the pointer as the third parameter in init
    // you can link a receiver to any number of channels
    // you can destroy the channel or the receiver
    // at anytime (in any order!);

void CMyView::HandleNewData(char* data)
    // do what ever you want

// some where else in your application
// delcare the channel object
channel MyChannel;

// and send some data
void generate_data()
    // send what every you want
    // every compatible receiver that's linked
    // (directly or indirectly) to this channel is called
    // NOTE: string like this are NOT const and will only work
    // with <..., char*> receivers !
    MyChannel << "Hallo Welt";

What about threads

It's also possible to link a channel to another channel. The threadbridge is derived from channel so it can be used where a channel could be used. Note that the threadbridge only works if the threads that have created it has a message pump. threadbridge::message_pump() is a very simple message pump that can be used.

Note that data that is sent through a threadbridge must stay valid. The data may be passed to more than one receiver so you don't know when the data is no longer needed. This may cause a problem if you want to pass data via pointers. You should make your data reference counted and pass it via a simple class like this:

template<class T>
class refptr
    refptr(const T* v) : _V(v) {if(_V)_V->addref();}
    refptr(const refptr<T> &s) : _V(s._V) {if(_V)_V->addref();}
    ~refptr() {if(_V)_V->release();}

    operator T* () {return _V;}

    refptr<T>& operator = (const T* v) {
        _V = v;
        return *this;
    T* _V;

Have fun ...


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

Matthias Mann
Germany Germany
No Biography provided

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171020.1 | Last Updated 15 Jul 2001
Article Copyright 2001 by Matthias Mann
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid