Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Simple C++ member function delegates class using a parameter arbitrator class

, 25 Sep 2007 CPOL
A simple C++ delegates class for the special case of using an arbitrator class for function parameters.
// ArbDelegates.cpp : Defines the entry point for the console application.
//

// Turn off Microsoft's promotional material
#pragma warning ( disable : 4996 )

#include <stdlib.h>
#include <stdio.h>
#include <tchar.h>
#include <string.h>

// _variant_t
#include <comutil.h>
#pragma comment ( lib, "comsuppw.lib" )


// Ensure we have ASSERT and TRACE macros
// !!! Note that the adASSERT macro here converts the expression into
//     a string and displays it as a convenience.  In my own version I 
//     do a search and replace for the '%' character, but didn't have a
//     string class available here.  _CrtDbgReport() expects sprintf
//     like formating, so if you use adASSERT() on an expression with
//     a '%' character in it, KABOOM! ... of course, you were asserting anyway ;)
//     To fix it, replace the #e with zero, or do a search and replace.
#if defined( _DEBUG )
#	include <crtdbg.h>
#   if defined( _UNICODE )
#      define adTRACE0( s )     ( _CrtDbgReportW( _CRT_WARN, 0, 0, NULL, s ), s )
#	   define adASSERT( e )	    ( ( e ) ? 1 : ( ( 1 == _CrtDbgReportW( _CRT_ASSERT, _T( __FILE__ ), __LINE__, 0, _T( #e ) ) ) ? _CrtDbgBreak() : 0, 0 ) )
#   else
#      define adTRACE0( s )     ( _CrtDbgReport( _CRT_WARN, 0, 0, NULL, s ), s )
#	   define adASSERT( e )	    ( ( e ) ? 1 : ( ( 1 == _CrtDbgReport( _CRT_ASSERT, __FILE__, __LINE__, 0, #e ) ) ? _CrtDbgBreak() : 0, 0 ) )
#   endif
#else
#	define adASSERT( e )
#   define adTRACE0( s )        
#endif

// TArbDelegates uses assert macro
#ifndef ASSERT
#   define ASSERT adASSERT
#endif

// Include our delegates class
#include "ArbDelegates.h"

// Define our custom delegates class
typedef adel::TArbDelegate< _variant_t > CDelegate;

// Class containing functions we will call
class CMyClass
{ 
public:    
    int Add2( int a, int b ) { return a + b; } 
    float Add3( float a, float b, float c ) { return a + b + c; } 
    void Trace( _variant_t msg ) { adTRACE0( (_bstr_t )msg ); }
    _variant_t GetString() { return _T( "Some String\n\n" ); }
};

// Test the delegates
void test_delegates()
{
    CMyClass mc;

    // Call CMyClass::Add1()
    CDelegate d;
    d.Register( &mc, &CMyClass::Add2 );
    int x = d( 2, 3 );
    adASSERT( 5 == x );

    // Call CMyClass::Add2()
    CDelegate d2( &mc, &CMyClass::Add3 );
    float y = d2( 1.5, 2.5, 3.5 );
    adASSERT( 7.5f == y );

    // Call CMyClass::Trace
    CDelegate d3( &mc, &CMyClass::Trace );
    d3( _T( "\nHello World!\n\n" ) );

    // Call CMyClass::GetString
    CDelegate d4( &mc, &CMyClass::GetString );
    adTRACE0( (_bstr_t)d4() );

    return;
}

int main(int argc, char* argv[])
{
    // Test the delegates
	test_delegates();

	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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Robert Umbehant
Software Developer (Senior)
United States United States
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150327.1 | Last Updated 25 Sep 2007
Article Copyright 2007 by Robert Umbehant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid