Click here to Skip to main content
15,892,298 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 292.4K   1.9K   110  
An article on the implementation of a fast C++ delegate with many advanced features.
// FD.Delegate library

//  Copyright (c) 2007 JaeWook Choi
//
// Distributed under 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)

#include <boost/test/minimal.hpp>
#include <cassert>
#include <iostream>
#include <typeinfo>
#include <fd/delegate.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/ref.hpp>

struct foobar
{
  int foo(long, int) { return 0; }
  int bar(long, int) const { return 0; }
  static double hello(char *, unsigned) { return 0.0; }

  long operator ()(int, int) { return 0; }
  float operator ()(void *) const { return 0.0f; }
};

void hello_foobar(int, long) { }

int
test_main(int, char*[])
{
  std::cout << "01.function #1" << std::endl;
  fd::delegate2<void, long, int> dg1( &hello_foobar );
  std::cout << dg1.type().name() << std::endl;
  std::cout << typeid( &hello_foobar ).name() << std::endl;
  BOOST_CHECK(dg1.type() == typeid( &hello_foobar ));

  std::cout << "02.function #2" << std::endl;
  fd::delegate2<void, int, long> dg2( &hello_foobar );
  std::cout << dg2.type().name() << std::endl;
  std::cout << typeid( &hello_foobar ).name() << std::endl;
  BOOST_CHECK(dg2.type() == typeid( &hello_foobar ));

  std::cout << "03.static member function" << std::endl;
  fd::delegate2<double, char *, unsigned> dg3( &foobar::hello );
  std::cout << dg3.type().name() << std::endl;
  std::cout << typeid( &foobar::hello ).name() << std::endl;
  BOOST_CHECK(dg3.type() == typeid( &foobar::hello ));

  std::cout << "04.member function #1" << std::endl;
  fd::delegate3<int, foobar *, long, int> dg11(&foobar::foo);
  std::cout << dg11.type().name() << std::endl;
  std::cout << typeid( &foobar::foo ).name() << std::endl;
  BOOST_CHECK(dg11.type() == typeid( &foobar::foo ));

  std::cout << "05.member function #2" << std::endl;
  fd::delegate3<int, foobar &, long, int> dg12(&foobar::foo);
  std::cout << dg12.type().name() << std::endl;
  std::cout << typeid( &foobar::foo ).name() << std::endl;
  BOOST_CHECK(dg12.type() == typeid( &foobar::foo ));

  std::cout << "06.member function #3" << std::endl;
  fd::delegate3<int, foobar, long, int> dg13(&foobar::foo);
  std::cout << dg13.type().name() << std::endl;
  std::cout << typeid( &foobar::foo ).name() << std::endl;
  BOOST_CHECK(dg13.type() == typeid( &foobar::foo ));

  std::cout << "07.member function #4" << std::endl;
  fd::delegate3<int, boost::shared_ptr<foobar>, long, int> dg14(&foobar::foo);
  std::cout << dg14.type().name() << std::endl;
  std::cout << typeid( &foobar::foo ).name() << std::endl;
  BOOST_CHECK(dg14.type() == typeid( &foobar::foo ));

  std::cout << "08.member function #5" << std::endl;
  fd::delegate3<int, boost::shared_ptr<foobar>, long, int> dg15(&foobar::foo);
  std::cout << dg15.type().name() << std::endl;
  std::cout << typeid( &foobar::foo ).name() << std::endl;
  BOOST_CHECK(dg15.type() == typeid( &foobar::foo ));

  std::cout << "09.const member function #1" << std::endl;
  fd::delegate3<int, foobar *, long, int> dg21(&foobar::bar);
  std::cout << dg21.type().name() << std::endl;
  std::cout << typeid( &foobar::bar ).name() << std::endl;
  BOOST_CHECK(dg21.type() == typeid( &foobar::bar ));

  std::cout << "10.const member function #2" << std::endl;
  fd::delegate3<int, foobar const *, long, int> dg22(&foobar::bar);
  std::cout << dg22.type().name() << std::endl;
  std::cout << typeid( &foobar::bar ).name() << std::endl;
  BOOST_CHECK(dg22.type() == typeid( &foobar::bar ));

  std::cout << "11.const member function #3" << std::endl;
  fd::delegate3<int, foobar &, long, int> dg23(&foobar::bar);
  std::cout << dg23.type().name() << std::endl;
  std::cout << typeid( &foobar::bar ).name() << std::endl;
  BOOST_CHECK(dg23.type() == typeid( &foobar::bar ));

  std::cout << "12.const member function #4" << std::endl;
  fd::delegate3<int, foobar const &, long, int> dg24(&foobar::bar);
  std::cout << dg24.type().name() << std::endl;
  std::cout << typeid( &foobar::bar ).name() << std::endl;
  BOOST_CHECK(dg24.type() == typeid( &foobar::bar ));

  std::cout << "13.const member function #5" << std::endl;
  fd::delegate3<int, foobar, long, int> dg25(&foobar::bar);
  std::cout << dg25.type().name() << std::endl;
  std::cout << typeid( &foobar::bar ).name() << std::endl;
  BOOST_CHECK(dg25.type() == typeid( &foobar::bar ));

  std::cout << "14.const member function #6" << std::endl;
  fd::delegate3<int, foobar const, long, int> dg26(&foobar::bar);
  std::cout << dg26.type().name() << std::endl;
  std::cout << typeid( &foobar::bar ).name() << std::endl;
  BOOST_CHECK(dg26.type() == typeid( &foobar::bar ));

  std::cout << "15.const member function #7" << std::endl;
  fd::delegate3<int, boost::shared_ptr<foobar>, long, int> dg27(&foobar::bar);
  std::cout << dg27.type().name() << std::endl;
  std::cout << typeid( &foobar::bar ).name() << std::endl;
  BOOST_CHECK(dg27.type() == typeid( &foobar::bar ));

  foobar fb;
  foobar const cfb = fb;
  boost::shared_ptr<foobar> spfb(new foobar);

  std::cout << "16.member function bind #1" << std::endl;
  fd::delegate2<int, long, int> dg31(&foobar::foo, &fb);
  std::cout << dg31.type().name() << std::endl;
  std::cout << typeid( &foobar::foo ).name() << std::endl;
  BOOST_CHECK(dg31.type() == typeid( &foobar::foo));

  std::cout << "17.member function bind #2" << std::endl;
  fd::delegate2<int, long, int> dg32(&foobar::foo, boost::ref(fb));
  std::cout << dg32.type().name() << std::endl;
  std::cout << typeid( &foobar::foo ).name() << std::endl;
  BOOST_CHECK(dg32.type() == typeid( &foobar::foo));

  std::cout << "18.member function bind #3" << std::endl;
  fd::delegate2<int, long, int> dg33(&foobar::foo, fb);
  std::cout << dg33.type().name() << std::endl;
  std::cout << typeid( &foobar::foo ).name() << std::endl;
  BOOST_CHECK(dg33.type() == typeid( &foobar::foo));

  std::cout << "19.member function bind #4" << std::endl;
  fd::delegate2<int, long, int> dg34(&foobar::foo, cfb);
  std::cout << dg34.type().name() << std::endl;
  std::cout << typeid( &foobar::foo ).name() << std::endl;
  BOOST_CHECK(dg34.type() == typeid( &foobar::foo));

  std::cout << "20.member function bind #5" << std::endl;
  fd::delegate2<int, long, int> dg35(&foobar::foo, spfb);
  std::cout << dg35.type().name() << std::endl;
  std::cout << typeid( &foobar::foo ).name() << std::endl;
  BOOST_CHECK(dg35.type() == typeid( &foobar::foo));

  std::cout << "21.const member function bind #1" << std::endl;
  fd::delegate2<int, long, int> dg41(&foobar::bar, &fb);
  std::cout << dg41.type().name() << std::endl;
  std::cout << typeid( &foobar::bar ).name() << std::endl;
  BOOST_CHECK(dg41.type() == typeid( &foobar::bar));

  std::cout << "22.const member function bind #2" << std::endl;
  fd::delegate2<int, long, int> dg42(&foobar::bar, &cfb);
  std::cout << dg42.type().name() << std::endl;
  std::cout << typeid( &foobar::bar ).name() << std::endl;
  BOOST_CHECK(dg42.type() == typeid( &foobar::bar));

  std::cout << "23.const member function bind #3" << std::endl;
  fd::delegate2<int, long, int> dg43(&foobar::bar, boost::ref(fb));
  std::cout << dg43.type().name() << std::endl;
  std::cout << typeid( &foobar::bar ).name() << std::endl;
  BOOST_CHECK(dg43.type() == typeid( &foobar::bar));

  std::cout << "24.const member function bind #4" << std::endl;
  fd::delegate2<int, long, int> dg44(&foobar::bar, boost::cref(cfb));
  std::cout << dg44.type().name() << std::endl;
  std::cout << typeid( &foobar::bar ).name() << std::endl;
  BOOST_CHECK(dg44.type() == typeid( &foobar::bar));

  std::cout << "25.const member function bind #5" << std::endl;
  fd::delegate2<int, long, int> dg45(&foobar::bar, fb);
  std::cout << dg45.type().name() << std::endl;
  std::cout << typeid( &foobar::bar ).name() << std::endl;
  BOOST_CHECK(dg45.type() == typeid( &foobar::bar));

  std::cout << "26.const member function bind #6" << std::endl;
  fd::delegate2<int, long, int> dg46(&foobar::bar, cfb);
  std::cout << dg46.type().name() << std::endl;
  std::cout << typeid( &foobar::bar ).name() << std::endl;
  BOOST_CHECK(dg46.type() == typeid( &foobar::bar));

  std::cout << "27.const member function bind #7" << std::endl;
  fd::delegate2<int, long, int> dg47(&foobar::bar, spfb);
  std::cout << dg47.type().name() << std::endl;
  std::cout << typeid( &foobar::bar ).name() << std::endl;
  BOOST_CHECK(dg47.type() == typeid( &foobar::bar));

  std::cout << "28.function object #1" << std::endl;
  fd::delegate2<long, int, int> dg51(&fb);
  std::cout << dg51.type().name() << std::endl;
  std::cout << typeid( foobar ).name() << std::endl;
  BOOST_CHECK(dg51.type() == typeid( foobar ));

  std::cout << "29.function object #2" << std::endl;
  fd::delegate2<long, int, int> dg52(boost::ref(fb));
  std::cout << dg52.type().name() << std::endl;
  std::cout << typeid( foobar ).name() << std::endl;
  BOOST_CHECK(dg52.type() == typeid( foobar ));

  std::cout << "30.function object #3" << std::endl;
  fd::delegate2<long, int, int> dg53(fb);
  std::cout << dg53.type().name() << std::endl;
  std::cout << typeid( foobar ).name() << std::endl;
  BOOST_CHECK(dg53.type() == typeid( foobar ));

  std::cout << "31.function object #4" << std::endl;
  fd::delegate2<long, int, int> dg54(cfb);
  std::cout << dg54.type().name() << std::endl;
  std::cout << typeid( foobar ).name() << std::endl;
  BOOST_CHECK(dg54.type() == typeid( foobar ));

  /*
  std::cout << "32.function object #5" << std::endl;
  fd::delegate2<long, int, int> dg55(spfb);
  std::cout << dg55.type().name() << std::endl;
  std::cout << typeid( foobar ).name() << std::endl;
  BOOST_CHECK(dg55.type() == typeid( foobar ));
  */

  std::cout << "33.const function object #1" << std::endl;
  fd::delegate1<float, void *> dg61(&fb);
  std::cout << dg61.type().name() << std::endl;
  std::cout << typeid( foobar ).name() << std::endl;
  BOOST_CHECK(dg61.type() == typeid( foobar ));

  std::cout << "34.const function object #2" << std::endl;
  fd::delegate1<float, void *> dg62(&cfb);
  std::cout << dg62.type().name() << std::endl;
  std::cout << typeid( foobar ).name() << std::endl;
  BOOST_CHECK(dg62.type() == typeid( foobar ));

  std::cout << "35.const function object #3" << std::endl;
  fd::delegate1<float, void *> dg63(boost::ref(fb));
  std::cout << dg63.type().name() << std::endl;
  std::cout << typeid( foobar ).name() << std::endl;
  BOOST_CHECK(dg63.type() == typeid( foobar ));

  std::cout << "36.const function object #4" << std::endl;
  fd::delegate1<float, void *> dg64(boost::cref(cfb));
  std::cout << dg64.type().name() << std::endl;
  std::cout << typeid( foobar ).name() << std::endl;
  BOOST_CHECK(dg64.type() == typeid( foobar ));

  std::cout << "37.const function object #5" << std::endl;
  fd::delegate1<float, void *> dg65(fb);
  std::cout << dg65.type().name() << std::endl;
  std::cout << typeid( foobar ).name() << std::endl;
  BOOST_CHECK(dg65.type() == typeid( foobar ));

  std::cout << "38.const function object #6" << std::endl;
  fd::delegate1<float, void *> dg66(cfb);
  std::cout << dg66.type().name() << std::endl;
  std::cout << typeid( foobar ).name() << std::endl;
  BOOST_CHECK(dg66.type() == typeid( foobar ));

  /*
  std::cout << "39.const function object #7" << std::endl;
  fd::delegate1<float, void *> dg67(spfb);
  std::cout << dg67.type().name() << std::endl;
  std::cout << typeid( foobar ).name() << std::endl;
  BOOST_CHECK(dg67.type() == typeid( foobar ));
  */

  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