Click here to Skip to main content
Licence 
First Posted 5 Mar 2002
Views 72,025
Bookmarked 38 times

Yet another approach to Delegates in unmanaged C++

By | 5 Mar 2002 | Article
Asynchronous delegates in unamanaged C++ using the C# syntax

Sample Image - cppDelegate.gif

Introduction

Ben Chan introduced a way of simulating delegates in unmanaged code. Although his method works I wanted to have a syntax similar found in the C# language. I also wanted to be able to use async delegates.

Using my solution

Defining a Delegate is simple as this:

Delegate OnBtnClick;
AsyncDelegate OnNewMail;

To add a new delegate handler to those defined above you have to choose whether you want to use a static/global function or a member function. To subscribe a delegate you use a syntax similar to C#'s:

class MyClass
{
public:
	void Handler1(LPVOID source, DelegateArgs* args)
	{
	...
	}

	static void StaticHandler(LPVOID source, DelegateArgs* args)
	{
	...
	}
};

void main()
{
	MyClass instance;
	Delegate OnBtnClick;

	OnBtnClick += NewClassDelegateHandler(MyClass, instance, Handler1);
	OnBtnClick += NewDelegateHandler(&MyClass::StaticHandler);


	LPVOID source = 0;
	DelegateArgs* args = 0;

	// you can also remove a delegate handler from the invocation list
	// OnBtnClick -= NewClassDelegateHandler(MyClass, instance, Handler1);

	OnBtnClick.Invoke(source, args);
}

NewClassDelegateHandler and NewDelegateHandler are defines that create the appropriate DelegateHandler class. It can be the StaticDelegateHandler for static functions or ClassDelegateHandler for member functions.

The async delegate only overrides the Delegate::Invoke method by creating a thread to invoke the handlers. You can make your delegate thread safe (AsyncDelegate already is) by defining a Delegate like this Delegate ThreadSafeDelegate(true);

IMPORTANT: To use my solution you have to enable runtime type information in your project.

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

About the Author

xicoloko

Architect
VisionOne AG
Switzerland Switzerland

Member

XicoLoko is a brazilian developer based in Switzerland.


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralStraightforward and useful PinmemberYves Tkaczyk10:50 21 Oct '05  
GeneralExcellent work PinmemberVerifier3:08 19 Nov '03  
GeneralShould not mix C/C++ with .NET PinmemberTW0:04 23 May '03  
Generalthreads.. Pinmembersaltynuts200218:32 25 Feb '03  
Generalget the code to run under VS.NET PinmemberTweety6:54 30 Jan '03  
GeneralRe: get the code to run under VS.NET PinmemberYuetKent23:01 24 Aug '04  
GeneralRe: get the code to run under VS.NET PinmemberTweety3:05 26 Aug '04  
GeneralFairly named "Standard C++" instead biased ads "unmanged C++" ! PinmemberAnonymous13:24 10 Apr '02  
GeneralRe: Fairly named "Standard C++" instead biased ads "unmanged C++" ! PinmemberCachivache17:20 15 May '02  
GeneralRe: Fairly named "Standard C++" instead biased ads "unmanged C++" ! PinmemberM Aamir Maniar17:32 10 Mar '03  
GeneralRe: Fairly named "Standard C++" instead biased ads "unmanged C++" ! PinmemberCholo4:53 15 Mar '03  
GeneralRe: Fairly named "Standard C++" instead biased ads "unmanged C++" ! PinsussCholo Lennon4:53 15 Mar '03  
GeneralWell I don't agree! PinmemberChryler21:29 21 Jul '03  
GeneralRe: Well I don't agree! PinmemberPaul Selormey3:09 4 Sep '03  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 6 Mar 2002
Article Copyright 2002 by xicoloko
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid