Click here to Skip to main content
15,896,557 members
Articles / Programming Languages / C++

Fast C++ Delegate: Boost.Function 'drop-in' replacement and multicast

Rate me:
Please Sign up or sign in to vote.
4.86/5 (51 votes)
1 Jun 200733 min read 293K   1.9K   110  
An article on the implementation of a fast C++ delegate with many advanced features.
// FD.Delegate library examples

// Document/View sample for Boost.Signals
// Copyright Keith MacDonald 2005. Use, modification and
// distribution is subject to the Boost Software License, Version
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// For more information, see http://www.boost.org

// Copyright (C) 2007 JaeWook Choi
// , modified from Boost.Signals doc_view.cpp

#define FD_DELEGATE

#include <iostream>
#include <string>
#ifndef FD_DELEGATE
#include <boost/signal.hpp>
#else
#include <fd/delegate.hpp>
#endif
#include <boost/bind.hpp>

class Document
{
public:

#ifndef FD_DELEGATE
    typedef boost::signal<void (bool)>  signal_t;
    typedef boost::signals::connection  connection_t;
#else
    typedef fd::delegate<void (bool)>  signal_t;
    typedef fd::multicast::token connection_t;
#endif

public:
    Document()
    {}

#ifndef FD_DELEGATE
    connection_t connect(signal_t::slot_function_type subscriber)
    {
      return m_sig.connect(subscriber);
    }

    void disconnect(connection_t subscriber)
    {
      subscriber.disconnect();
    }
#else
    connection_t connect(signal_t subscriber)
    {
      return m_sig.add(subscriber);
    }

    void disconnect(connection_t subscriber)
    {
      subscriber.remove();
    }
#endif

    void append(const char* s)
    {
        m_text += s;
        m_sig(true);
    }

    const std::string& getText() const
    {
        return m_text;
    }

private:
    signal_t    m_sig;
    std::string m_text;
};

class View
{
public:
    View(Document& m)
        : m_document(m), m_connection( m_document.connect(boost::bind(&View::refresh, this, _1)) )
    {
    }

    virtual ~View()
    {
        m_document.disconnect(m_connection);
    }

    virtual void refresh(bool bExtended) const = 0;

protected:
    Document&               m_document;

private:
    Document::connection_t  m_connection;
};

class TextView : public View
{
public:
    TextView(Document& doc)
        : View(doc)
    {}

    virtual void refresh(bool bExtended) const
    {
        std::cout << "TextView: " << m_document.getText() << std::endl;
    }
};

class HexView : public View
{
public:
    HexView(Document& doc)
        : View(doc)
    {}

    virtual void refresh(bool bExtended) const
    {
        const std::string&  s = m_document.getText();

        std::cout << "HexView:";

        for (std::string::const_iterator it = s.begin(); it != s.end(); ++it)
            std::cout << ' ' << std::hex << static_cast<int>(*it);

        std::cout << std::endl;
    }
};

int main(int argc, char* argv[])
{
    Document    doc;
    TextView    v1(doc);
    HexView     v2(doc);

    doc.append(argc == 2 ? argv[1] : "Hello world!");
    return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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
Other
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions