Click here to Skip to main content
15,895,142 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.7K   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/delegate0.hpp>
#include <fd/delegate/delegate1.hpp>
#include <boost/last_value.hpp>

int inc_1(int & n) { std::cout<< "inc_1(" << n << ")" << std::endl; return ++n; };
int inc_2(int & n) { std::cout<< "inc_2(" << n << ")" << std::endl; return ++n; };
int inc_3(int & n) { std::cout<< "inc_3(" << n << ")" << std::endl; return ++n; };
int inc_4(int & n) { std::cout<< "inc_4(" << n << ")" << std::endl; return ++n; };
int inc_5(int & n) { std::cout<< "inc_5(" << n << ")" << std::endl; return ++n; };

void inc_v1() { std::cout<< "inc_v1()" << std::endl; };
void inc_v2() { std::cout<< "inc_v2()" << std::endl; };
void inc_v3() { std::cout<< "inc_v3()" << std::endl; };
void inc_v4() { std::cout<< "inc_v4()" << std::endl; };
void inc_v5() { std::cout<< "inc_v5()" << std::endl; };

struct abort_if_greater_than_3
{
  typedef bool result_type;

  template<typename InputIterator>
  bool operator ()(InputIterator first, InputIterator last) const
  {
    if(first == last)
      return false;

    for( ; first != last; ++first )
    {
      if( *first > 3 )
        return true;
    }
    return false;
  }
};

struct abort_after_3_call
{
  typedef void result_type;

  int num_;

  abort_after_3_call() : num_( 0 ) { }

  template<typename InputIterator>
  void operator ()(InputIterator first, InputIterator last)
  {
    for( ; first != last && num_ < 3; ++first, ++num_)
    {
      *first; // Dereferencing the iterator causes the delegate invocation at the specific position.
    }
  }
};

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

  dg1 += &inc_1;
  dg1 += &inc_2;
  dg1 += &inc_3;
  dg1 += &inc_4;
  dg1 += &inc_5;

  int n = 0;

  std::cout << "dg1( n );" << std::endl;
  dg1( n );
  BOOST_CHECK( n == 5 );

  n = 0;
  std::cout << "dg1( n, abort_if_greater_than_3() );" << std::endl;
  dg1( n, abort_if_greater_than_3() );
  BOOST_CHECK( n == 4 );

  int n1 = 0, n2 = 0;
  std::cout << "dg1( n1 ) == dg1( n2, boost::last_value<int>() )" << std::endl;
  BOOST_CHECK( dg1( n1 ) == dg1( n2, boost::last_value<int>() ) );

  fd::delegate0<void> dg2;

  dg2 += &inc_v1;
  dg2 += &inc_v2;
  dg2 += &inc_v3;
  dg2 += &inc_v4;
  dg2 += &inc_v5;

  std::cout << "dg2();" << std::endl;
  dg2();

  std::cout << "dg2( boost::last_value<void>() );" << std::endl;
  dg2( boost::last_value<void>() );

  std::cout << "dg2( abort_after_3_call() );" << std::endl;
  dg2( abort_after_3_call() );

  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