One Unified Global Perspective
Communications with a Global Perspective
Home
Intro
Contact Us
Voice over IP
PBX Solutions
Services
Support
Glossary
Open Source
Blog
Forum

WebMail





2007 Sep 09 - Sun

VC++ MultiCast Delegate Template

In follow up to my article on Fast Delegates back in June, I used the C++ STL (Standard Template Library) to create a simple, one parameter Multicast Delegate for C++. The code provided here provides a mechanism for creating a Multicast Delegate by incorporating the FastDelegate library. I used the 'vector' template from the STL to implement dynamically adding and removing object method calls to the Delegate.

#pragma once

#include <vector>
#include "FastDelegate.h"
// http://www.codeproject.com/cpp/FastDelegate.asp

template<class RO> class Delegate {
  // RO: Return Object in call

public:
  typedef FastDelegate1<RO> OnMessageHandler;
  void Add( OnMessageHandler function );
  void Remove( OnMessageHandler function );
  bool IsEmpty();
  void operator()( RO );
protected:
private:
  std::vector<OnMessageHandler> rOnFD;
};

template<class RO> void Delegate<RO>::Add( 
OnMessageHandler function ) {
  rOnFD.push_back( function );
}

template<class RO> void Delegate<RO>::Remove( 
OnMessageHandler function ) {

  std::vector<OnMessageHandler>::iterator rOnFD_Iter;

  rOnFD_Iter = rOnFD.begin();
  while ( rOnFD.end() != rOnFD_Iter ) {
    if ( function == *rOnFD_Iter ) {
      rOnFD_Iter.erase( rOnFD_Iter );
      break;
    }
    rOnFD_Iter++;
  }
}

template<class RO> bool Delegate<RO>::IsEmpty() {
  return rOnFD.empty();
}

template<class RO> void Delegate<RO>::operator()( RO ro ) {

  std::vector<OnMessageHandler>::iterator rOnFD_Iter;

  rOnFD_Iter = rOnFD.begin();
  while ( rOnFD.end() != rOnFD_Iter ) {
    (*rOnFD_Iter)( ro );
    rOnFD_Iter++;
  }
}

// =======

// Here is a declaration of three events, with each event receiving a 
// pointer to a object with class of CIQFSymbol:

Delegate<CIQFSymbol*> OnFundamentalMessage, OnUpdateMessage, OnSummaryMessage;

// Here is how to add a delegate to the MultiCast Delegate:

pSym->OnFundamentalMessage.Add( 
  MakeDelegate( this, &CGTScalpDlg::EmitDataFundamental ) );

// EmitDataFundamental is declared with:

void EmitDataFundamental( CIQFSymbol *pSym );

// The mulicast delegate can then be invoked with:

OnFundamentalMessage( this );

// =======

Caveats: I havn't proven that Delegate's Remove method works as intended yet, but theoretically it should, as coded. Proper initialization, deconstruction cleanup, and thread safety are left as exercises for the reader.

[/Personal/SoftwareDevelopment] permanent link



Blog Content ©2008
Ray Burkholder
All Rights Reserved
ray@oneunified.net
(441) 505 7293
Available for Contract Work
Resume

RSS: Click to see the XML version of this web page.

View Ray 
Burkholder's profile on LinkedIn
technorati
Add to Technorati Favorites



September
Su Mo Tu We Th Fr Sa
           
9
           


Main Links:
Monitoring Server
SSH Tools
QuantDeveloper Code

Special Links:
Frink

Blog Links:
Sergey Solyanik
Marc Andreessen
HotGigs
Micro Persuasion
... Reasonable ...
Chris Donnan
BeyondVC
lifehacker
Trader Mike
Ticker Sense
HeadRush
TraderFeed
Stock Bandit
The Daily WTF
Guy Kawaski
J. Brant Arseneau
Steve Pavlina
Matt Cutts
Kevin Scaldeferri
Joel On Software
Quant Recruiter
Blosxom User Group
Wesner Moise
Julian Dunn
Steve Yegge

2007
Months
Sep




Mason HQ

Disclaimer: This site may include market analysis. All ideas, opinions, and/or forecasts, expressed or implied herein, are for informational purposes only and should not be construed as a recommendation to invest, trade, and/or speculate in the markets. Any investments, trades, and/or speculations made in light of the ideas, opinions, and/or forecasts, expressed or implied herein, are committed at your own risk, financial or otherwise.