Click here to Skip to main content
15,884,177 members
Articles / Programming Languages / C++

A new way to implement Delegate in C++

Rate me:
Please Sign up or sign in to vote.
4.88/5 (28 votes)
25 May 2007CPOL11 min read 184.4K   918   110  
Solving issues with some current implementations of Delegate in C++
#pragma once

#include <memory.h>
#include <stdexcept>
#include "compile_time_util.h"

#pragma push_macro("new")
#undef new

#if defined(_MSC_VER)
#define SOPHIA_SUPPORT_CALLING_CONVENTION_FOR_METHOD
#endif

#define SOPHIA_DELEGATE_CLASSNAME   COMPILE_TIME_JOIN(delegate,SOPHIA_PARAM_COUNT)

#define SOPHIA_MAKE_PARAMS1_0(t)	
#define SOPHIA_MAKE_PARAMS1_1(t)    t##1
#define SOPHIA_MAKE_PARAMS1_2(t)    t##1, ##t##2
#define SOPHIA_MAKE_PARAMS1_3(t)    t##1, ##t##2, ##t##3
#define SOPHIA_MAKE_PARAMS1_4(t)    t##1, ##t##2, ##t##3, ##t##4
#define SOPHIA_MAKE_PARAMS1_5(t)    t##1, ##t##2, ##t##3, ##t##4, ##t##5
#define SOPHIA_MAKE_PARAMS1_6(t)    t##1, ##t##2, ##t##3, ##t##4, ##t##5, ##t##6
#define SOPHIA_MAKE_PARAMS1_7(t)    t##1, ##t##2, ##t##3, ##t##4, ##t##5, ##t##6, ##t##7
#define SOPHIA_MAKE_PARAMS1_8(t)    t##1, ##t##2, ##t##3, ##t##4, ##t##5, ##t##6, ##t##7, ##t##8
#define SOPHIA_MAKE_PARAMS1_9(t)    t##1, ##t##2, ##t##3, ##t##4, ##t##5, ##t##6, ##t##7, ##t##8, ##t##9
#define SOPHIA_MAKE_PARAMS1_10(t)   t##1, ##t##2, ##t##3, ##t##4, ##t##5, ##t##6, ##t##7, ##t##8, ##t##9, ##t##10

#define SOPHIA_MAKE_PARAMS2_0(t1, t2)	
#define SOPHIA_MAKE_PARAMS2_1(t1, t2)   t1##1 t2##1
#define SOPHIA_MAKE_PARAMS2_2(t1, t2)   t1##1 t2##1, t1##2 t2##2
#define SOPHIA_MAKE_PARAMS2_3(t1, t2)   t1##1 t2##1, t1##2 t2##2, t1##3 t2##3
#define SOPHIA_MAKE_PARAMS2_4(t1, t2)   t1##1 t2##1, t1##2 t2##2, t1##3 t2##3, t1##4 t2##4
#define SOPHIA_MAKE_PARAMS2_5(t1, t2)   t1##1 t2##1, t1##2 t2##2, t1##3 t2##3, t1##4 t2##4, t1##5 t2##5
#define SOPHIA_MAKE_PARAMS2_6(t1, t2)   t1##1 t2##1, t1##2 t2##2, t1##3 t2##3, t1##4 t2##4, t1##5 t2##5, t1##6 t2##6
#define SOPHIA_MAKE_PARAMS2_7(t1, t2)   t1##1 t2##1, t1##2 t2##2, t1##3 t2##3, t1##4 t2##4, t1##5 t2##5, t1##6 t2##6, t1##7 t2##7
#define SOPHIA_MAKE_PARAMS2_8(t1, t2)   t1##1 t2##1, t1##2 t2##2, t1##3 t2##3, t1##4 t2##4, t1##5 t2##5, t1##6 t2##6, t1##7 t2##7, t1##8 t2##8
#define SOPHIA_MAKE_PARAMS2_9(t1, t2)   t1##1 t2##1, t1##2 t2##2, t1##3 t2##3, t1##4 t2##4, t1##5 t2##5, t1##6 t2##6, t1##7 t2##7, t1##8 t2##8, t1##9 t2##9
#define SOPHIA_MAKE_PARAMS2_10(t1, t2)  t1##1 t2##1, t1##2 t2##2, t1##3 t2##3, t1##4 t2##4, t1##5 t2##5, t1##6 t2##6, t1##7 t2##7, t1##8 t2##8, t1##9 t2##9, t1##10 t2##10

#define SOPHIA_MAKE_PARAMS1(n, t)         COMPILE_TIME_JOIN(SOPHIA_MAKE_PARAMS1_, n) (t)
#define SOPHIA_MAKE_PARAMS2(n, t1, t2)    COMPILE_TIME_JOIN(SOPHIA_MAKE_PARAMS2_, n) (t1, t2)

namespace sophia
{

#include "delegate_root.h"

// 0 params
#define SOPHIA_PARAM_COUNT 0
#include "delegate_template.h"
#undef SOPHIA_PARAM_COUNT

// 1 params
#define SOPHIA_PARAM_COUNT 1
#include "delegate_template.h"
#undef SOPHIA_PARAM_COUNT

// 2 params
#define SOPHIA_PARAM_COUNT 2
#include "delegate_template.h"
#undef SOPHIA_PARAM_COUNT

// 3 params
#define SOPHIA_PARAM_COUNT 3
#include "delegate_template.h"
#undef SOPHIA_PARAM_COUNT

// 4 params
#define SOPHIA_PARAM_COUNT 4
#include "delegate_template.h"
#undef SOPHIA_PARAM_COUNT

// 5 params
#define SOPHIA_PARAM_COUNT 5
#include "delegate_template.h"
#undef SOPHIA_PARAM_COUNT

// 6 params
#define SOPHIA_PARAM_COUNT 6
#include "delegate_template.h"
#undef SOPHIA_PARAM_COUNT

// 7 params
#define SOPHIA_PARAM_COUNT 7
#include "delegate_template.h"
#undef SOPHIA_PARAM_COUNT

// 8 params
#define SOPHIA_PARAM_COUNT 8
#include "delegate_template.h"
#undef SOPHIA_PARAM_COUNT

// 9 params
#define SOPHIA_PARAM_COUNT 9
#include "delegate_template.h"
#undef SOPHIA_PARAM_COUNT

// 10 params
#define SOPHIA_PARAM_COUNT 10
#include "delegate_template.h"
#undef SOPHIA_PARAM_COUNT

} // end namespace

#undef SOPHIA_DELEGATE_CLASSNAME
#undef SOPHIA_MAKE_PARAMS1
#undef SOPHIA_MAKE_PARAMS2

#undef SOPHIA_MAKE_PARAMS1_0
#undef SOPHIA_MAKE_PARAMS1_1
#undef SOPHIA_MAKE_PARAMS1_2
#undef SOPHIA_MAKE_PARAMS1_3
#undef SOPHIA_MAKE_PARAMS1_4
#undef SOPHIA_MAKE_PARAMS1_5
#undef SOPHIA_MAKE_PARAMS1_6
#undef SOPHIA_MAKE_PARAMS1_7
#undef SOPHIA_MAKE_PARAMS1_8
#undef SOPHIA_MAKE_PARAMS1_9
#undef SOPHIA_MAKE_PARAMS1_10

#undef SOPHIA_MAKE_PARAMS2_0
#undef SOPHIA_MAKE_PARAMS2_1
#undef SOPHIA_MAKE_PARAMS2_2
#undef SOPHIA_MAKE_PARAMS2_3
#undef SOPHIA_MAKE_PARAMS2_4
#undef SOPHIA_MAKE_PARAMS2_5
#undef SOPHIA_MAKE_PARAMS2_6
#undef SOPHIA_MAKE_PARAMS2_7
#undef SOPHIA_MAKE_PARAMS2_8
#undef SOPHIA_MAKE_PARAMS2_9
#undef SOPHIA_MAKE_PARAMS2_10
#undef SOPHIA_SUPPORT_CALLING_CONVENTION_FOR_METHOD

#pragma pop_macro("new")

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)


Written By
Global Cybersoft (Vietnam)
Vietnam Vietnam
Quynh Nguyen is a Vietnamese who has worked for 7 years in Software Outsourcing area. Currently, he works for Global Cybersoft (Vietnam) Ltd. as a Project Manager in Factory Automation division.

In the first day learning C language in university, he had soon switched to Assembly language because he was not able to understand why people cannot get address of a constant as with a variable. With that stupid starting, he had spent a lot of his time with Assembly language during the time he was in university.

Now he is interesting in Software Development Process, Software Architecture and Design Pattern… He especially indulges in highly concurrent software.

Comments and Discussions