Click here to Skip to main content
15,896,118 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.9K   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 <fd/delegate.hpp>
#include <boost/shared_ptr.hpp>

using fd::bad_function_call;

struct foobar
{
  int id_;

  void foo(int) { }
  static void bar(int) { }

  void operator ()(int) const { }
};

void hello(int) { }

struct stateless
{
  void operator ()(int) const { }
};

int
test_main(int, char*[])
{
  fd::delegate1<void, int> dg1;

  foobar fb;
  foobar * pfb = &fb;
  boost::shared_ptr<foobar> spfb( new foobar );

  dg1 = &hello;                               // FT01
  dg1( 1 );                                   // hello( 1 );

#if !defined(_MSC_VER) || (_MSC_VER >= 1300)

  dg1 = &foobar::bar;                         // FT01
  dg1( 1 );                                   // foobar::bar( 1 );

#else  // VC6 BUG

  fd::delegate1<void, int> dg1_1 = &foobar::bar;  // FT01
  dg1_1( 1 );                                     // foobar::bar( 1 );

#endif

  fd::delegate2<void, foobar *, int> dg2;
  dg2 = &foobar::foo;                         // FT02
  dg2( pfb, 1 );                              // (pfb->*&foobar::foo)( 1 );

  fd::delegate2<void, foobar &, int> dg3;
  dg3 = &foobar::foo;                         // FT03
  dg3( fb, 1 );                               // (fb.*&foobar::foo)( 1 );

  fd::delegate2<void, foobar, int> dg4;
  dg4 = &foobar::foo;                         // FT04
  dg4( fb, 1 );                               // ((copy of fb).*&foobar::foo)( 1 );


  fd::delegate2<void, boost::shared_ptr<foobar>, int> dg5;

  dg5 = &foobar::foo;                         // FT04
  dg5( spfb, 1 );                             // (get_pointer(spfb)->*&foobar::foo)( 1 );

  dg1.bind( &foobar::foo, pfb );              // FT05
  dg1( 1 );                                   // (pfb->*&foobar::foo)( 1 );

  dg1.bind( &foobar::foo, boost::ref( fb ) ); // FT05
  dg1( 1 );                                   // (fb.*&foobar::foo)( 1 );

  dg1.bind( &foobar::foo, fb );               // FT06
  dg1( 1 );                                   // ((copy of fb).*&foobar::foo)( 1 );

  dg1.bind( &foobar::foo, spfb );             // FT06
  dg1( 1 );                                   // (get_pointer(spfb)->*&foobar::foo)( 1 );

  dg1 = pfb;                                  // FT07
  dg1( 1 );                                   // (*pfb)( 1 );

  dg1 = boost::ref( fb );                     // FT07
  dg1( 1 );                                   // fb( 1 );

  dg1 = fb;                                   // FT08
  dg1( 1 );                                   // (copy of fb)( 1 );

  /*
  dg1 = spfb;                                 // FT08
  dg1( 1 );                                   // (*get_pointer(spfb))( 1 );
  */

  dg1 = stateless();                          // FT09
  dg1( 1 );                                   // stateless()( 1 );

  dg1 = 0;                                    // FT10
  try { dg1( 1 ); }                           // throw bad_function_call();
  catch(bad_function_call) { }

  dg1 += &hello;                              // FT11
  dg1 += fd::delegate1<void, int>( &foobar::foo, spfb );
  dg1 += fb;

  dg1( 1 );                                   // hello( 1 );
                                              // (get_pointer(spfb)->*&foobar::foo)( 1 );
                                              // (copy of fb)( 1 );
  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