sourcehook/generate/FastDelegate.hxx
author Pavol Marko <pavolmarko@gmail.com>
Fri Nov 09 21:14:37 2007 +0000 (2007-11-09)
branchhookman_autogen
changeset 452 2efe13dab607
parent 294 0d87bc8606f8
permissions -rw-r--r--
- Removed unnecessary definitions from sh_pagealloc.h
- Added CallConv_StdCall
- Added CallConv_HasVarArgs and CallConv_HasVafmt
pavolmarko@117
     1
//						FastDelegate.h
pavolmarko@1
     2
//	Efficient delegates in C++ that generate only two lines of asm code!
pavolmarko@1
     3
//  Documentation is found at http://www.codeproject.com/cpp/FastDelegate.asp
pavolmarko@1
     4
//
pavolmarko@1
     5
//						- Don Clugston, Mar 2004.
pavolmarko@1
     6
//		Major contributions were made by Jody Hagins.
pavolmarko@1
     7
// History:
pavolmarko@134
     8
// 24-Apr-04 1.0  * Submitted to CodeProject.
pavolmarko@1
     9
// 28-Apr-04 1.1  * Prevent most unsafe uses of evil static function hack.
pavolmarko@1
    10
//				  * Improved syntax for horrible_cast (thanks Paul Bludov).
pavolmarko@1
    11
//				  * Tested on Metrowerks MWCC and Intel ICL (IA32)
pavolmarko@1
    12
//				  * Compiled, but not run, on Comeau C++ and Intel Itanium ICL.
pavolmarko@1
    13
//	27-Jun-04 1.2 * Now works on Borland C++ Builder 5.5
pavolmarko@1
    14
//				  * Now works on /clr "managed C++" code on VC7, VC7.1
pavolmarko@1
    15
//				  * Comeau C++ now compiles without warnings.
pavolmarko@117
    16
//				  * Prevent the virtual inheritance case from being used on
pavolmarko@1
    17
//					  VC6 and earlier, which generate incorrect code.
pavolmarko@1
    18
//				  * Improved warning and error messages. Non-standard hacks
pavolmarko@1
    19
//					 now have compile-time checks to make them safer.
pavolmarko@1
    20
//				  * implicit_cast used instead of static_cast in many cases.
pavolmarko@1
    21
//				  * If calling a const member function, a const class pointer can be used.
pavolmarko@1
    22
//				  * MakeDelegate() global helper function added to simplify pass-by-value.
pavolmarko@1
    23
//				  * Added fastdelegate.clear()
pavolmarko@1
    24
// 16-Jul-04 1.2.1* Workaround for gcc bug (const member function pointers in templates)
pavolmarko@1
    25
// 30-Oct-04 1.3  * Support for (non-void) return values.
pavolmarko@1
    26
//				  * No more workarounds in client code!
pavolmarko@1
    27
//					 MSVC and Intel now use a clever hack invented by John Dlugosz:
pavolmarko@1
    28
//				     - The FASTDELEGATEDECLARE workaround is no longer necessary.
pavolmarko@1
    29
//					 - No more warning messages for VC6
pavolmarko@1
    30
//				  * Less use of macros. Error messages should be more comprehensible.
pavolmarko@1
    31
//				  * Added include guards
pavolmarko@1
    32
//				  * Added FastDelegate::empty() to test if invocation is safe (Thanks Neville Franks).
pavolmarko@134
    33
//				  * Now tested on VS 2005 Express Beta, PGI C++
pavolmarko@1
    34
// 24-Dec-04 1.4  * Added DelegateMemento, to allow collections of disparate delegates.
pavolmarko@1
    35
//                * <,>,<=,>= comparison operators to allow storage in ordered containers.
pavolmarko@1
    36
//				  * Substantial reduction of code size, especially the 'Closure' class.
pavolmarko@1
    37
//				  * Standardised all the compiler-specific workarounds.
pavolmarko@1
    38
//                * MFP conversion now works for CodePlay (but not yet supported in the full code).
pavolmarko@134
    39
//                * Now compiles without warnings on _any_ supported compiler, including BCC 5.5.1
pavolmarko@117
    40
//				  * New syntax: FastDelegate< int (char *, double) >.
pavolmarko@134
    41
// 14-Feb-05 1.4.1* Now treats =0 as equivalent to .clear(), ==0 as equivalent to .empty(). (Thanks elfric).
pavolmarko@134
    42
//				  * Now tested on Intel ICL for AMD64, VS2005 Beta for AMD64 and Itanium.
pavolmarko@134
    43
// 30-Mar-05 1.5  * Safebool idiom: "if (dg)" is now equivalent to "if (!dg.empty())"
pavolmarko@134
    44
//				  * Fully supported by CodePlay VectorC
pavolmarko@134
    45
//                * Bugfix for Metrowerks: empty() was buggy because a valid MFP can be 0 on MWCC!
pavolmarko@134
    46
//                * More optimal assignment,== and != operators for static function pointers.
pavolmarko@1
    47
pavolmarko@1
    48
#ifndef FASTDELEGATE_H
pavolmarko@1
    49
#define FASTDELEGATE_H
pavolmarko@48
    50
pavolmarko@48
    51
#ifdef _MSC_VER
pavolmarko@48
    52
# if _MSC_VER > 1000
pavolmarko@48
    53
#  pragma once
pavolmarko@48
    54
# endif // _MSC_VER > 1000
pavolmarko@48
    55
#endif // #ifdef _MSC_VER
pavolmarko@1
    56
pavolmarko@1
    57
#include <memory.h> // to allow <,> comparisons
pavolmarko@1
    58
pavolmarko@1
    59
////////////////////////////////////////////////////////////////////////////////
pavolmarko@1
    60
//						Configuration options
pavolmarko@1
    61
//
pavolmarko@1
    62
////////////////////////////////////////////////////////////////////////////////
pavolmarko@1
    63
pavolmarko@1
    64
// Uncomment the following #define for optimally-sized delegates.
pavolmarko@1
    65
// In this case, the generated asm code is almost identical to the code you'd get
pavolmarko@1
    66
// if the compiler had native support for delegates.
pavolmarko@117
    67
// It will not work on systems where sizeof(dataptr) < sizeof(codeptr).
pavolmarko@1
    68
// Thus, it will not work for DOS compilers using the medium model.
pavolmarko@1
    69
// It will also probably fail on some DSP systems.
pavolmarko@1
    70
#define FASTDELEGATE_USESTATICFUNCTIONHACK
pavolmarko@1
    71
pavolmarko@1
    72
// Uncomment the next line to allow function declarator syntax.
pavolmarko@1
    73
// It is automatically enabled for those compilers where it is known to work.
pavolmarko@1
    74
//#define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
pavolmarko@1
    75
pavolmarko@1
    76
////////////////////////////////////////////////////////////////////////////////
pavolmarko@1
    77
//						Compiler identification for workarounds
pavolmarko@1
    78
//
pavolmarko@1
    79
////////////////////////////////////////////////////////////////////////////////
pavolmarko@1
    80
pavolmarko@1
    81
// Compiler identification. It's not easy to identify Visual C++ because
pavolmarko@1
    82
// many vendors fraudulently define Microsoft's identifiers.
pavolmarko@1
    83
#if defined(_MSC_VER) && !defined(__MWERKS__) && !defined(__VECTOR_C) && !defined(__ICL) && !defined(__BORLANDC__)
pavolmarko@1
    84
#define FASTDLGT_ISMSVC
pavolmarko@1
    85
pavolmarko@1
    86
#if (_MSC_VER <1300) // Many workarounds are required for VC6.
pavolmarko@1
    87
#define FASTDLGT_VC6
pavolmarko@1
    88
#pragma warning(disable:4786) // disable this ridiculous warning
pavolmarko@1
    89
#endif
pavolmarko@1
    90
pavolmarko@1
    91
#endif
pavolmarko@1
    92
pavolmarko@1
    93
// Does the compiler uses Microsoft's member function pointer structure?
pavolmarko@1
    94
// If so, it needs special treatment.
pavolmarko@117
    95
// Metrowerks CodeWarrior, Intel, and CodePlay fraudulently define Microsoft's
pavolmarko@1
    96
// identifier, _MSC_VER. We need to filter Metrowerks out.
pavolmarko@1
    97
#if defined(_MSC_VER) && !defined(__MWERKS__)
pavolmarko@1
    98
#define FASTDLGT_MICROSOFT_MFP
pavolmarko@1
    99
pavolmarko@1
   100
#if !defined(__VECTOR_C)
pavolmarko@1
   101
// CodePlay doesn't have the __single/multi/virtual_inheritance keywords
pavolmarko@1
   102
#define FASTDLGT_HASINHERITANCE_KEYWORDS
pavolmarko@1
   103
#endif
pavolmarko@1
   104
#endif
pavolmarko@1
   105
pavolmarko@1
   106
// Does it allow function declarator syntax? The following compilers are known to work:
pavolmarko@1
   107
#if defined(FASTDLGT_ISMSVC) && (_MSC_VER >=1310) // VC 7.1
pavolmarko@1
   108
#define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
pavolmarko@1
   109
#endif
pavolmarko@1
   110
pavolmarko@1
   111
// Gcc(2.95+), and versions of Digital Mars, Intel and Comeau in common use.
pavolmarko@1
   112
#if defined (__DMC__) || defined(__GNUC__) || defined(__ICL) || defined(__COMO__)
pavolmarko@1
   113
#define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
pavolmarko@1
   114
#endif
pavolmarko@1
   115
pavolmarko@1
   116
// It works on Metrowerks MWCC 3.2.2. From boost.Config it should work on earlier ones too.
pavolmarko@1
   117
#if defined (__MWERKS__)
pavolmarko@1
   118
#define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
pavolmarko@1
   119
#endif
pavolmarko@1
   120
pavolmarko@117
   121
#ifdef __GNUC__ // Workaround GCC bug #8271
pavolmarko@1
   122
	// At present, GCC doesn't recognize constness of MFPs in templates
pavolmarko@1
   123
#define FASTDELEGATE_GCC_BUG_8271
pavolmarko@1
   124
#endif
pavolmarko@1
   125
pavolmarko@1
   126
pavolmarko@1
   127
pavolmarko@1
   128
////////////////////////////////////////////////////////////////////////////////
pavolmarko@1
   129
//						General tricks used in this code
pavolmarko@1
   130
//
pavolmarko@1
   131
// (a) Error messages are generated by typdefing an array of negative size to
pavolmarko@1
   132
//     generate compile-time errors.
pavolmarko@1
   133
// (b) Warning messages on MSVC are generated by declaring unused variables, and
pavolmarko@1
   134
//	    enabling the "variable XXX is never used" warning.
pavolmarko@1
   135
// (c) Unions are used in a few compiler-specific cases to perform illegal casts.
pavolmarko@1
   136
// (d) For Microsoft and Intel, when adjusting the 'this' pointer, it's cast to
pavolmarko@1
   137
//     (char *) first to ensure that the correct number of *bytes* are added.
pavolmarko@1
   138
//
pavolmarko@1
   139
////////////////////////////////////////////////////////////////////////////////
pavolmarko@1
   140
//						Helper templates
pavolmarko@1
   141
//
pavolmarko@1
   142
////////////////////////////////////////////////////////////////////////////////
pavolmarko@1
   143
pavolmarko@1
   144
pavolmarko@1
   145
namespace fastdelegate {
pavolmarko@1
   146
namespace detail {	// we'll hide the implementation details in a nested namespace.
pavolmarko@1
   147
pavolmarko@1
   148
//		implicit_cast< >
pavolmarko@117
   149
// I believe this was originally going to be in the C++ standard but
pavolmarko@1
   150
// was left out by accident. It's even milder than static_cast.
pavolmarko@1
   151
// I use it instead of static_cast<> to emphasize that I'm not doing
pavolmarko@117
   152
// anything nasty.
pavolmarko@1
   153
// Usage is identical to static_cast<>
pavolmarko@1
   154
template <class OutputClass, class InputClass>
pavolmarko@1
   155
inline OutputClass implicit_cast(InputClass input){
pavolmarko@1
   156
	return input;
pavolmarko@1
   157
}
pavolmarko@1
   158
pavolmarko@1
   159
//		horrible_cast< >
pavolmarko@117
   160
// This is truly evil. It completely subverts C++'s type system, allowing you
pavolmarko@117
   161
// to cast from any class to any other class. Technically, using a union
pavolmarko@1
   162
// to perform the cast is undefined behaviour (even in C). But we can see if
pavolmarko@1
   163
// it is OK by checking that the union is the same size as each of its members.
pavolmarko@117
   164
// horrible_cast<> should only be used for compiler-specific workarounds.
pavolmarko@1
   165
// Usage is identical to reinterpret_cast<>.
pavolmarko@1
   166
pavolmarko@1
   167
// This union is declared outside the horrible_cast because BCC 5.5.1
pavolmarko@1
   168
// can't inline a function with a nested class, and gives a warning.
pavolmarko@1
   169
template <class OutputClass, class InputClass>
pavolmarko@1
   170
union horrible_union{
pavolmarko@1
   171
	OutputClass out;
pavolmarko@1
   172
	InputClass in;
pavolmarko@1
   173
};
pavolmarko@1
   174
pavolmarko@1
   175
template <class OutputClass, class InputClass>
pavolmarko@1
   176
inline OutputClass horrible_cast(const InputClass input){
pavolmarko@1
   177
	horrible_union<OutputClass, InputClass> u;
pavolmarko@1
   178
	// Cause a compile-time error if in, out and u are not the same size.
pavolmarko@1
   179
	// If the compile fails here, it means the compiler has peculiar
pavolmarko@1
   180
	// unions which would prevent the cast from working.
pavolmarko@117
   181
	typedef int ERROR_CantUseHorrible_cast[sizeof(InputClass)==sizeof(u)
pavolmarko@1
   182
		&& sizeof(InputClass)==sizeof(OutputClass) ? 1 : -1];
pavolmarko@1
   183
	u.in = input;
pavolmarko@1
   184
	return u.out;
pavolmarko@1
   185
}
pavolmarko@1
   186
pavolmarko@1
   187
////////////////////////////////////////////////////////////////////////////////
pavolmarko@1
   188
//						Workarounds
pavolmarko@1
   189
//
pavolmarko@1
   190
////////////////////////////////////////////////////////////////////////////////
pavolmarko@1
   191
pavolmarko@1
   192
// Backwards compatibility: This macro used to be necessary in the virtual inheritance
pavolmarko@1
   193
// case for Intel and Microsoft. Now it just forward-declares the class.
pavolmarko@1
   194
#define FASTDELEGATEDECLARE(CLASSNAME)	class CLASSNAME;
pavolmarko@1
   195
pavolmarko@1
   196
// Prevent use of the static function hack with the DOS medium model.
pavolmarko@1
   197
#ifdef __MEDIUM__
pavolmarko@1
   198
#undef FASTDELEGATE_USESTATICFUNCTIONHACK
pavolmarko@1
   199
#endif
pavolmarko@1
   200
pavolmarko@1
   201
//			DefaultVoid - a workaround for 'void' templates in VC6.
pavolmarko@1
   202
//
pavolmarko@1
   203
//  (1) VC6 and earlier do not allow 'void' as a default template argument.
pavolmarko@1
   204
//  (2) They also doesn't allow you to return 'void' from a function.
pavolmarko@1
   205
//
pavolmarko@1
   206
// Workaround for (1): Declare a dummy type 'DefaultVoid' which we use
pavolmarko@1
   207
//   when we'd like to use 'void'. We convert it into 'void' and back
pavolmarko@1
   208
//   using the templates DefaultVoidToVoid<> and VoidToDefaultVoid<>.
pavolmarko@1
   209
// Workaround for (2): On VC6, the code for calling a void function is
pavolmarko@1
   210
//   identical to the code for calling a non-void function in which the
pavolmarko@1
   211
//   return value is never used, provided the return value is returned
pavolmarko@117
   212
//   in the EAX register, rather than on the stack.
pavolmarko@1
   213
//   This is true for most fundamental types such as int, enum, void *.
pavolmarko@117
   214
//   Const void * is the safest option since it doesn't participate
pavolmarko@1
   215
//   in any automatic conversions. But on a 16-bit compiler it might
pavolmarko@1
   216
//   cause extra code to be generated, so we disable it for all compilers
pavolmarko@1
   217
//   except for VC6 (and VC5).
pavolmarko@1
   218
#ifdef FASTDLGT_VC6
pavolmarko@1
   219
// VC6 workaround
pavolmarko@1
   220
typedef const void * DefaultVoid;
pavolmarko@1
   221
#else
pavolmarko@1
   222
// On any other compiler, just use a normal void.
pavolmarko@1
   223
typedef void DefaultVoid;
pavolmarko@1
   224
#endif
pavolmarko@1
   225
pavolmarko@1
   226
// Translate from 'DefaultVoid' to 'void'.
pavolmarko@1
   227
// Everything else is unchanged
pavolmarko@1
   228
template <class T>
pavolmarko@1
   229
struct DefaultVoidToVoid { typedef T type; };
pavolmarko@1
   230
pavolmarko@1
   231
template <>
pavolmarko@1
   232
struct DefaultVoidToVoid<DefaultVoid> {	typedef void type; };
pavolmarko@1
   233
pavolmarko@1
   234
// Translate from 'void' into 'DefaultVoid'
pavolmarko@1
   235
// Everything else is unchanged
pavolmarko@1
   236
template <class T>
pavolmarko@1
   237
struct VoidToDefaultVoid { typedef T type; };
pavolmarko@1
   238
pavolmarko@1
   239
template <>
pavolmarko@1
   240
struct VoidToDefaultVoid<void> { typedef DefaultVoid type; };
pavolmarko@1
   241
pavolmarko@1
   242
pavolmarko@1
   243
pavolmarko@1
   244
////////////////////////////////////////////////////////////////////////////////
pavolmarko@1
   245
//						Fast Delegates, part 1:
pavolmarko@1
   246
//
pavolmarko@1
   247
//		Conversion of member function pointer to a standard form
pavolmarko@1
   248
//
pavolmarko@1
   249
////////////////////////////////////////////////////////////////////////////////
pavolmarko@1
   250
pavolmarko@1
   251
// GenericClass is a fake class, ONLY used to provide a type.
pavolmarko@1
   252
// It is vitally important that it is never defined, so that the compiler doesn't
pavolmarko@1
   253
// think it can optimize the invocation. For example, Borland generates simpler
pavolmarko@1
   254
// code if it knows the class only uses single inheritance.
pavolmarko@1
   255
pavolmarko@1
   256
// Compilers using Microsoft's structure need to be treated as a special case.
pavolmarko@1
   257
#ifdef  FASTDLGT_MICROSOFT_MFP
pavolmarko@1
   258
pavolmarko@1
   259
#ifdef FASTDLGT_HASINHERITANCE_KEYWORDS
pavolmarko@117
   260
	// For Microsoft and Intel, we want to ensure that it's the most efficient type of MFP
pavolmarko@117
   261
	// (4 bytes), even when the /vmg option is used. Declaring an empty class
pavolmarko@1
   262
	// would give 16 byte pointers in this case....
pavolmarko@1
   263
	class __single_inheritance GenericClass;
pavolmarko@1
   264
#endif
pavolmarko@1
   265
	// ...but for Codeplay, an empty class *always* gives 4 byte pointers.
pavolmarko@1
   266
	// If compiled with the /clr option ("managed C++"), the JIT compiler thinks
pavolmarko@1
   267
	// it needs to load GenericClass before it can call any of its functions,
pavolmarko@117
   268
	// (compiles OK but crashes at runtime!), so we need to declare an
pavolmarko@1
   269
	// empty class to make it happy.
pavolmarko@1
   270
	// Codeplay and VC4 can't cope with the unknown_inheritance case either.
pavolmarko@1
   271
	class GenericClass {};
pavolmarko@1
   272
#else
pavolmarko@1
   273
	class GenericClass;
pavolmarko@1
   274
#endif
pavolmarko@1
   275
pavolmarko@1
   276
// The size of a single inheritance member function pointer.
pavolmarko@1
   277
const int SINGLE_MEMFUNCPTR_SIZE = sizeof(void (GenericClass::*)());
pavolmarko@1
   278
pavolmarko@1
   279
//						SimplifyMemFunc< >::Convert()
pavolmarko@1
   280
//
pavolmarko@117
   281
//	A template function that converts an arbitrary member function pointer into the
pavolmarko@1
   282
//	simplest possible form of member function pointer, using a supplied 'this' pointer.
pavolmarko@1
   283
//  According to the standard, this can be done legally with reinterpret_cast<>.
pavolmarko@117
   284
//	For (non-standard) compilers which use member function pointers which vary in size
pavolmarko@117
   285
//  depending on the class, we need to use	knowledge of the internal structure of a
pavolmarko@1
   286
//  member function pointer, as used by the compiler. Template specialization is used
pavolmarko@117
   287
//  to distinguish between the sizes. Because some compilers don't support partial
pavolmarko@1
   288
//	template specialisation, I use full specialisation of a wrapper struct.
pavolmarko@1
   289
pavolmarko@1
   290
// general case -- don't know how to convert it. Force a compile failure
pavolmarko@1
   291
template <int N>
pavolmarko@1
   292
struct SimplifyMemFunc {
pavolmarko@1
   293
	template <class X, class XFuncType, class GenericMemFuncType>
pavolmarko@117
   294
	inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind,
pavolmarko@117
   295
		GenericMemFuncType &bound_func) {
pavolmarko@1
   296
		// Unsupported member function type -- force a compile failure.
pavolmarko@1
   297
	    // (it's illegal to have a array with negative size).
pavolmarko@1
   298
		typedef char ERROR_Unsupported_member_function_pointer_on_this_compiler[N-100];
pavolmarko@117
   299
		return 0;
pavolmarko@1
   300
	}
pavolmarko@1
   301
};
pavolmarko@1
   302
pavolmarko@1
   303
// For compilers where all member func ptrs are the same size, everything goes here.
pavolmarko@1
   304
// For non-standard compilers, only single_inheritance classes go here.
pavolmarko@1
   305
template <>
pavolmarko@117
   306
struct SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE>  {
pavolmarko@1
   307
	template <class X, class XFuncType, class GenericMemFuncType>
pavolmarko@117
   308
	inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind,
pavolmarko@1
   309
			GenericMemFuncType &bound_func) {
pavolmarko@117
   310
#if defined __DMC__
pavolmarko@117
   311
		// Digital Mars doesn't allow you to cast between abitrary PMF's,
pavolmarko@1
   312
		// even though the standard says you can. The 32-bit compiler lets you
pavolmarko@1
   313
		// static_cast through an int, but the DOS compiler doesn't.
pavolmarko@1
   314
		bound_func = horrible_cast<GenericMemFuncType>(function_to_bind);
pavolmarko@117
   315
#else
pavolmarko@1
   316
        bound_func = reinterpret_cast<GenericMemFuncType>(function_to_bind);
pavolmarko@1
   317
#endif
pavolmarko@1
   318
        return reinterpret_cast<GenericClass *>(pthis);
pavolmarko@1
   319
	}
pavolmarko@1
   320
};
pavolmarko@1
   321
pavolmarko@1
   322
////////////////////////////////////////////////////////////////////////////////
pavolmarko@1
   323
//						Fast Delegates, part 1b:
pavolmarko@1
   324
//
pavolmarko@1
   325
//					Workarounds for Microsoft and Intel
pavolmarko@1
   326
//
pavolmarko@1
   327
////////////////////////////////////////////////////////////////////////////////
pavolmarko@1
   328
pavolmarko@1
   329
pavolmarko@1
   330
// Compilers with member function pointers which violate the standard (MSVC, Intel, Codeplay),
pavolmarko@1
   331
// need to be treated as a special case.
pavolmarko@1
   332
#ifdef FASTDLGT_MICROSOFT_MFP
pavolmarko@1
   333
pavolmarko@1
   334
// We use unions to perform horrible_casts. I would like to use #pragma pack(push, 1)
pavolmarko@1
   335
// at the start of each function for extra safety, but VC6 seems to ICE
pavolmarko@1
   336
// intermittently if you do this inside a template.
pavolmarko@1
   337
pavolmarko@1
   338
// __multiple_inheritance classes go here
pavolmarko@1
   339
// Nasty hack for Microsoft and Intel (IA32 and Itanium)
pavolmarko@1
   340
template<>
pavolmarko@1
   341
struct SimplifyMemFunc< SINGLE_MEMFUNCPTR_SIZE + sizeof(int) >  {
pavolmarko@1
   342
	template <class X, class XFuncType, class GenericMemFuncType>
pavolmarko@117
   343
	inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind,
pavolmarko@117
   344
		GenericMemFuncType &bound_func) {
pavolmarko@1
   345
		// We need to use a horrible_cast to do this conversion.
pavolmarko@1
   346
		// In MSVC, a multiple inheritance member pointer is internally defined as:
pavolmarko@1
   347
        union {
pavolmarko@1
   348
			XFuncType func;
pavolmarko@117
   349
			struct {
pavolmarko@1
   350
				GenericMemFuncType funcaddress; // points to the actual member function
pavolmarko@1
   351
				int delta;	     // #BYTES to be added to the 'this' pointer
pavolmarko@1
   352
			}s;
pavolmarko@1
   353
        } u;
pavolmarko@1
   354
		// Check that the horrible_cast will work
pavolmarko@1
   355
		typedef int ERROR_CantUsehorrible_cast[sizeof(function_to_bind)==sizeof(u.s)? 1 : -1];
pavolmarko@1
   356
        u.func = function_to_bind;
pavolmarko@1
   357
		bound_func = u.s.funcaddress;
pavolmarko@117
   358
		return reinterpret_cast<GenericClass *>(reinterpret_cast<char *>(pthis) + u.s.delta);
pavolmarko@1
   359
	}
pavolmarko@1
   360
};
pavolmarko@1
   361
pavolmarko@1
   362
// virtual inheritance is a real nuisance. It's inefficient and complicated.
pavolmarko@1
   363
// On MSVC and Intel, there isn't enough information in the pointer itself to
pavolmarko@1
   364
// enable conversion to a closure pointer. Earlier versions of this code didn't
pavolmarko@1
   365
// work for all cases, and generated a compile-time error instead.
pavolmarko@1
   366
// But a very clever hack invented by John M. Dlugosz solves this problem.
pavolmarko@117
   367
// My code is somewhat different to his: I have no asm code, and I make no
pavolmarko@1
   368
// assumptions about the calling convention that is used.
pavolmarko@1
   369
pavolmarko@117
   370
// In VC++ and ICL, a virtual_inheritance member pointer
pavolmarko@1
   371
// is internally defined as:
pavolmarko@1
   372
struct MicrosoftVirtualMFP {
pavolmarko@1
   373
	void (GenericClass::*codeptr)(); // points to the actual member function
pavolmarko@1
   374
	int delta;		// #bytes to be added to the 'this' pointer
pavolmarko@1
   375
	int vtable_index; // or 0 if no virtual inheritance
pavolmarko@1
   376
};
pavolmarko@1
   377
// The CRUCIAL feature of Microsoft/Intel MFPs which we exploit is that the
pavolmarko@1
   378
// m_codeptr member is *always* called, regardless of the values of the other
pavolmarko@1
   379
// members. (This is *not* true for other compilers, eg GCC, which obtain the
pavolmarko@1
   380
// function address from the vtable if a virtual function is being called).
pavolmarko@1
   381
// Dlugosz's trick is to make the codeptr point to a probe function which
pavolmarko@1
   382
// returns the 'this' pointer that was used.
pavolmarko@1
   383
pavolmarko@1
   384
// Define a generic class that uses virtual inheritance.
pavolmarko@1
   385
// It has a trival member function that returns the value of the 'this' pointer.
pavolmarko@1
   386
struct GenericVirtualClass : virtual public GenericClass
pavolmarko@1
   387
{
pavolmarko@1
   388
	typedef GenericVirtualClass * (GenericVirtualClass::*ProbePtrType)();
pavolmarko@1
   389
	GenericVirtualClass * GetThis() { return this; }
pavolmarko@1
   390
};
pavolmarko@1
   391
pavolmarko@1
   392
// __virtual_inheritance classes go here
pavolmarko@1
   393
template <>
pavolmarko@1
   394
struct SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE + 2*sizeof(int) >
pavolmarko@1
   395
{
pavolmarko@1
   396
pavolmarko@1
   397
	template <class X, class XFuncType, class GenericMemFuncType>
pavolmarko@117
   398
	inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind,
pavolmarko@1
   399
		GenericMemFuncType &bound_func) {
pavolmarko@1
   400
		union {
pavolmarko@1
   401
			XFuncType func;
pavolmarko@1
   402
			GenericClass* (X::*ProbeFunc)();
pavolmarko@1
   403
			MicrosoftVirtualMFP s;
pavolmarko@1
   404
		} u;
pavolmarko@1
   405
		u.func = function_to_bind;
pavolmarko@1
   406
		bound_func = reinterpret_cast<GenericMemFuncType>(u.s.codeptr);
pavolmarko@1
   407
		union {
pavolmarko@1
   408
			GenericVirtualClass::ProbePtrType virtfunc;
pavolmarko@1
   409
			MicrosoftVirtualMFP s;
pavolmarko@1
   410
		} u2;
pavolmarko@1
   411
		// Check that the horrible_cast<>s will work
pavolmarko@1
   412
		typedef int ERROR_CantUsehorrible_cast[sizeof(function_to_bind)==sizeof(u.s)
pavolmarko@1
   413
			&& sizeof(function_to_bind)==sizeof(u.ProbeFunc)
pavolmarko@1
   414
			&& sizeof(u2.virtfunc)==sizeof(u2.s) ? 1 : -1];
pavolmarko@117
   415
   // Unfortunately, taking the address of a MF prevents it from being inlined, so
pavolmarko@1
   416
   // this next line can't be completely optimised away by the compiler.
pavolmarko@1
   417
		u2.virtfunc = &GenericVirtualClass::GetThis;
pavolmarko@1
   418
		u.s.codeptr = u2.s.codeptr;
pavolmarko@1
   419
		return (pthis->*u.ProbeFunc)();
pavolmarko@1
   420
	}
pavolmarko@1
   421
};
pavolmarko@1
   422
pavolmarko@1
   423
#if (_MSC_VER <1300)
pavolmarko@1
   424
pavolmarko@1
   425
// Nasty hack for Microsoft Visual C++ 6.0
pavolmarko@1
   426
// unknown_inheritance classes go here
pavolmarko@1
   427
// There is a compiler bug in MSVC6 which generates incorrect code in this case!!
pavolmarko@1
   428
template <>
pavolmarko@1
   429
struct SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE + 3*sizeof(int) >
pavolmarko@1
   430
{
pavolmarko@1
   431
	template <class X, class XFuncType, class GenericMemFuncType>
pavolmarko@117
   432
	inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind,
pavolmarko@1
   433
		GenericMemFuncType &bound_func) {
pavolmarko@1
   434
		// There is an apalling but obscure compiler bug in MSVC6 and earlier:
pavolmarko@117
   435
		// vtable_index and 'vtordisp' are always set to 0 in the
pavolmarko@1
   436
		// unknown_inheritance case!
pavolmarko@1
   437
		// This means that an incorrect function could be called!!!
pavolmarko@1
   438
		// Compiling with the /vmg option leads to potentially incorrect code.
pavolmarko@1
   439
		// This is probably the reason that the IDE has a user interface for specifying
pavolmarko@117
   440
		// the /vmg option, but it is disabled -  you can only specify /vmg on
pavolmarko@1
   441
		// the command line. In VC1.5 and earlier, the compiler would ICE if it ever
pavolmarko@1
   442
		// encountered this situation.
pavolmarko@1
   443
		// It is OK to use the /vmg option if /vmm or /vms is specified.
pavolmarko@1
   444
pavolmarko@1
   445
		// Fortunately, the wrong function is only called in very obscure cases.
pavolmarko@117
   446
		// It only occurs when a derived class overrides a virtual function declared
pavolmarko@117
   447
		// in a virtual base class, and the member function
pavolmarko@1
   448
		// points to the *Derived* version of that function. The problem can be
pavolmarko@117
   449
		// completely averted in 100% of cases by using the *Base class* for the
pavolmarko@1
   450
		// member fpointer. Ie, if you use the base class as an interface, you'll
pavolmarko@1
   451
		// stay out of trouble.
pavolmarko@1
   452
		// Occasionally, you might want to point directly to a derived class function
pavolmarko@117
   453
		// that isn't an override of a base class. In this case, both vtable_index
pavolmarko@1
   454
		// and 'vtordisp' are zero, but a virtual_inheritance pointer will be generated.
pavolmarko@1
   455
		// We can generate correct code in this case. To prevent an incorrect call from
pavolmarko@117
   456
		// ever being made, on MSVC6 we generate a warning, and call a function to
pavolmarko@117
   457
		// make the program crash instantly.
pavolmarko@1
   458
		typedef char ERROR_VC6CompilerBug[-100];
pavolmarko@117
   459
		return 0;
pavolmarko@1
   460
	}
pavolmarko@1
   461
};
pavolmarko@1
   462
pavolmarko@1
   463
pavolmarko@117
   464
#else
pavolmarko@1
   465
pavolmarko@1
   466
// Nasty hack for Microsoft and Intel (IA32 and Itanium)
pavolmarko@117
   467
// unknown_inheritance classes go here
pavolmarko@1
   468
// This is probably the ugliest bit of code I've ever written. Look at the casts!
pavolmarko@1
   469
// There is a compiler bug in MSVC6 which prevents it from using this code.
pavolmarko@1
   470
template <>
pavolmarko@1
   471
struct SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE + 3*sizeof(int) >
pavolmarko@1
   472
{
pavolmarko@1
   473
	template <class X, class XFuncType, class GenericMemFuncType>
pavolmarko@117
   474
	inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind,
pavolmarko@1
   475
			GenericMemFuncType &bound_func) {
pavolmarko@1
   476
		// The member function pointer is 16 bytes long. We can't use a normal cast, but
pavolmarko@1
   477
		// we can use a union to do the conversion.
pavolmarko@1
   478
		union {
pavolmarko@1
   479
			XFuncType func;
pavolmarko@117
   480
			// In VC++ and ICL, an unknown_inheritance member pointer
pavolmarko@1
   481
			// is internally defined as:
pavolmarko@1
   482
			struct {
pavolmarko@1
   483
				GenericMemFuncType m_funcaddress; // points to the actual member function
pavolmarko@1
   484
				int delta;		// #bytes to be added to the 'this' pointer
pavolmarko@1
   485
				int vtordisp;		// #bytes to add to 'this' to find the vtable
pavolmarko@1
   486
				int vtable_index; // or 0 if no virtual inheritance
pavolmarko@1
   487
			} s;
pavolmarko@1
   488
		} u;
pavolmarko@1
   489
		// Check that the horrible_cast will work
pavolmarko@1
   490
		typedef int ERROR_CantUsehorrible_cast[sizeof(XFuncType)==sizeof(u.s)? 1 : -1];
pavolmarko@1
   491
		u.func = function_to_bind;
pavolmarko@1
   492
		bound_func = u.s.funcaddress;
pavolmarko@1
   493
		int virtual_delta = 0;
pavolmarko@1
   494
		if (u.s.vtable_index) { // Virtual inheritance is used
pavolmarko@117
   495
			// First, get to the vtable.
pavolmarko@1
   496
			// It is 'vtordisp' bytes from the start of the class.
pavolmarko@1
   497
			const int * vtable = *reinterpret_cast<const int *const*>(
pavolmarko@1
   498
				reinterpret_cast<const char *>(pthis) + u.s.vtordisp );
pavolmarko@1
   499
pavolmarko@1
   500
			// 'vtable_index' tells us where in the table we should be looking.
pavolmarko@117
   501
			virtual_delta = u.s.vtordisp + *reinterpret_cast<const int *>(
pavolmarko@1
   502
				reinterpret_cast<const char *>(vtable) + u.s.vtable_index);
pavolmarko@1
   503
		}
pavolmarko@1
   504
		// The int at 'virtual_delta' gives us the amount to add to 'this'.
pavolmarko@1
   505
        // Finally we can add the three components together. Phew!
pavolmarko@1
   506
        return reinterpret_cast<GenericClass *>(
pavolmarko@1
   507
			reinterpret_cast<char *>(pthis) + u.s.delta + virtual_delta);
pavolmarko@1
   508
	};
pavolmarko@1
   509
};
pavolmarko@1
   510
#endif // MSVC 7 and greater
pavolmarko@1
   511
pavolmarko@1
   512
#endif // MS/Intel hacks
pavolmarko@1
   513
pavolmarko@1
   514
}  // namespace detail
pavolmarko@1
   515
pavolmarko@1
   516
////////////////////////////////////////////////////////////////////////////////
pavolmarko@1
   517
//						Fast Delegates, part 2:
pavolmarko@1
   518
//
pavolmarko@1
   519
//	Define the delegate storage, and cope with static functions
pavolmarko@1
   520
//
pavolmarko@1
   521
////////////////////////////////////////////////////////////////////////////////
pavolmarko@1
   522
pavolmarko@1
   523
// DelegateMemento -- an opaque structure which can hold an arbitary delegate.
pavolmarko@1
   524
// It knows nothing about the calling convention or number of arguments used by
pavolmarko@1
   525
// the function pointed to.
pavolmarko@1
   526
// It supplies comparison operators so that it can be stored in STL collections.
pavolmarko@117
   527
// It cannot be set to anything other than null, nor invoked directly:
pavolmarko@1
   528
//   it must be converted to a specific delegate.
pavolmarko@1
   529
pavolmarko@1
   530
// Implementation:
pavolmarko@1
   531
// There are two possible implementations: the Safe method and the Evil method.
pavolmarko@1
   532
//				DelegateMemento - Safe version
pavolmarko@1
   533
//
pavolmarko@1
   534
// This implementation is standard-compliant, but a bit tricky.
pavolmarko@117
   535
// A static function pointer is stored inside the class.
pavolmarko@1
   536
// Here are the valid values:
pavolmarko@1
   537
// +-- Static pointer --+--pThis --+-- pMemFunc-+-- Meaning------+
pavolmarko@1
   538
// |   0				|  0       |   0        | Empty          |
pavolmarko@1
   539
// |   !=0              |(dontcare)|  Invoker   | Static function|
pavolmarko@134
   540
// |   0                |  !=0     |  !=0*      | Method call    |
pavolmarko@1
   541
// +--------------------+----------+------------+----------------+
pavolmarko@134
   542
//  * For Metrowerks, this can be 0. (first virtual function in a 
pavolmarko@134
   543
//       single_inheritance class).
pavolmarko@1
   544
// When stored stored inside a specific delegate, the 'dontcare' entries are replaced
pavolmarko@1
   545
// with a reference to the delegate itself. This complicates the = and == operators
pavolmarko@1
   546
// for the delegate class.
pavolmarko@1
   547
pavolmarko@1
   548
//				DelegateMemento - Evil version
pavolmarko@1
   549
//
pavolmarko@117
   550
// For compilers where data pointers are at least as big as code pointers, it is
pavolmarko@117
   551
// possible to store the function pointer in the this pointer, using another
pavolmarko@1
   552
// horrible_cast. In this case the DelegateMemento implementation is simple:
pavolmarko@1
   553
// +--pThis --+-- pMemFunc-+-- Meaning---------------------+
pavolmarko@1
   554
// |    0     |  0         | Empty                         |
pavolmarko@134
   555
// |  !=0     |  !=0*      | Static function or method call|
pavolmarko@1
   556
// +----------+------------+-------------------------------+
pavolmarko@134
   557
//  * For Metrowerks, this can be 0. (first virtual function in a 
pavolmarko@134
   558
//       single_inheritance class).
pavolmarko@134
   559
// Note that the Sun C++ and MSVC documentation explicitly state that they 
pavolmarko@1
   560
// support static_cast between void * and function pointers.
pavolmarko@1
   561
pavolmarko@1
   562
class DelegateMemento {
pavolmarko@117
   563
protected:
pavolmarko@1
   564
	// the data is protected, not private, because many
pavolmarko@1
   565
	// compilers have problems with template friends.
pavolmarko@1
   566
	typedef void (detail::GenericClass::*GenericMemFuncType)(); // arbitrary MFP.
pavolmarko@1
   567
	detail::GenericClass *m_pthis;
pavolmarko@1
   568
	GenericMemFuncType m_pFunction;
pavolmarko@1
   569
pavolmarko@1
   570
#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
pavolmarko@1
   571
	typedef void (*GenericFuncPtr)(); // arbitrary code pointer
pavolmarko@1
   572
	GenericFuncPtr m_pStaticFunction;
pavolmarko@1
   573
#endif
pavolmarko@1
   574
pavolmarko@1
   575
public:
pavolmarko@1
   576
#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
pavolmarko@1
   577
	DelegateMemento() : m_pthis(0), m_pFunction(0), m_pStaticFunction(0) {};
pavolmarko@1
   578
	void clear() {
pavolmarko@1
   579
		m_pthis=0; m_pFunction=0; m_pStaticFunction=0;
pavolmarko@1
   580
	}
pavolmarko@1
   581
#else
pavolmarko@1
   582
	DelegateMemento() : m_pthis(0), m_pFunction(0) {};
pavolmarko@134
   583
	void clear() {	m_pthis=0; m_pFunction=0;	}
pavolmarko@1
   584
#endif
pavolmarko@1
   585
public:
pavolmarko@1
   586
#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
pavolmarko@1
   587
	inline bool IsEqual (const DelegateMemento &x) const{
pavolmarko@1
   588
	    // We have to cope with the static function pointers as a special case
pavolmarko@1
   589
		if (m_pFunction!=x.m_pFunction) return false;
pavolmarko@1
   590
		// the static function ptrs must either both be equal, or both be 0.
pavolmarko@1
   591
		if (m_pStaticFunction!=x.m_pStaticFunction) return false;
pavolmarko@1
   592
		if (m_pStaticFunction!=0) return m_pthis==x.m_pthis;
pavolmarko@1
   593
		else return true;
pavolmarko@1
   594
	}
pavolmarko@1
   595
#else // Evil Method
pavolmarko@1
   596
	inline bool IsEqual (const DelegateMemento &x) const{
pavolmarko@1
   597
		return m_pthis==x.m_pthis && m_pFunction==x.m_pFunction;
pavolmarko@1
   598
	}
pavolmarko@1
   599
#endif
pavolmarko@1
   600
	// Provide a strict weak ordering for DelegateMementos.
pavolmarko@1
   601
	inline bool IsLess(const DelegateMemento &right) const {
pavolmarko@1
   602
		// deal with static function pointers first
pavolmarko@1
   603
#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
pavolmarko@117
   604
		if (m_pStaticFunction !=0 || right.m_pStaticFunction!=0)
pavolmarko@1
   605
				return m_pStaticFunction < right.m_pStaticFunction;
pavolmarko@1
   606
#endif
pavolmarko@1
   607
		if (m_pthis !=right.m_pthis) return m_pthis < right.m_pthis;
pavolmarko@117
   608
	// There are no ordering operators for member function pointers,
pavolmarko@1
   609
	// but we can fake one by comparing each byte. The resulting ordering is
pavolmarko@1
   610
	// arbitrary (and compiler-dependent), but it permits storage in ordered STL containers.
pavolmarko@1
   611
		return memcmp(&m_pFunction, &right.m_pFunction, sizeof(m_pFunction)) < 0;
pavolmarko@1
   612
pavolmarko@1
   613
	}
pavolmarko@134
   614
	// BUGFIX (Mar 2005):
pavolmarko@134
   615
	// We can't just compare m_pFunction because on Metrowerks,
pavolmarko@134
   616
	// m_pFunction can be zero even if the delegate is not empty!
pavolmarko@1
   617
	inline bool operator ! () const		// Is it bound to anything?
pavolmarko@134
   618
	{ return m_pthis==0 && m_pFunction==0; }
pavolmarko@134
   619
	inline bool empty() const		// Is it bound to anything?
pavolmarko@134
   620
	{ return m_pthis==0 && m_pFunction==0; }
pavolmarko@1
   621
public:
pavolmarko@1
   622
	DelegateMemento & operator = (const DelegateMemento &right)  {
pavolmarko@117
   623
		SetMementoFrom(right);
pavolmarko@1
   624
		return *this;
pavolmarko@1
   625
	}
pavolmarko@1
   626
	inline bool operator <(const DelegateMemento &right) {
pavolmarko@1
   627
		return IsLess(right);
pavolmarko@1
   628
	}
pavolmarko@1
   629
	inline bool operator >(const DelegateMemento &right) {
pavolmarko@1
   630
		return right.IsLess(*this);
pavolmarko@1
   631
	}
pavolmarko@117
   632
	DelegateMemento (const DelegateMemento &right)  :
pavolmarko@117
   633
		m_pthis(right.m_pthis), m_pFunction(right.m_pFunction)
pavolmarko@1
   634
#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
pavolmarko@1
   635
		, m_pStaticFunction (right.m_pStaticFunction)
pavolmarko@1
   636
#endif
pavolmarko@1
   637
		{}
pavolmarko@1
   638
protected:
pavolmarko@1
   639
	void SetMementoFrom(const DelegateMemento &right)  {
pavolmarko@1
   640
		m_pFunction = right.m_pFunction;
pavolmarko@1
   641
		m_pthis = right.m_pthis;
pavolmarko@1
   642
#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
pavolmarko@1
   643
		m_pStaticFunction = right.m_pStaticFunction;
pavolmarko@1
   644
#endif
pavolmarko@1
   645
	}
pavolmarko@1
   646
};
pavolmarko@1
   647
pavolmarko@1
   648
pavolmarko@1
   649
//						ClosurePtr<>
pavolmarko@1
   650
//
pavolmarko@1
   651
// A private wrapper class that adds function signatures to DelegateMemento.
pavolmarko@1
   652
// It's the class that does most of the actual work.
pavolmarko@1
   653
// The signatures are specified by:
pavolmarko@117
   654
// GenericMemFunc: must be a type of GenericClass member function pointer.
pavolmarko@117
   655
// StaticFuncPtr:  must be a type of function pointer with the same signature
pavolmarko@1
   656
//                 as GenericMemFunc.
pavolmarko@1
   657
// UnvoidStaticFuncPtr: is the same as StaticFuncPtr, except on VC6
pavolmarko@1
   658
//                 where it never returns void (returns DefaultVoid instead).
pavolmarko@1
   659
pavolmarko@1
   660
// An outer class, FastDelegateN<>, handles the invoking and creates the
pavolmarko@1
   661
// necessary typedefs.
pavolmarko@1
   662
// This class does everything else.
pavolmarko@1
   663
pavolmarko@1
   664
namespace detail {
pavolmarko@1
   665
pavolmarko@1
   666
template < class GenericMemFunc, class StaticFuncPtr, class UnvoidStaticFuncPtr>
pavolmarko@1
   667
class ClosurePtr : public DelegateMemento {
pavolmarko@1
   668
public:
pavolmarko@1
   669
	// These functions are for setting the delegate to a member function.
pavolmarko@1
   670
pavolmarko@117
   671
	// Here's the clever bit: we convert an arbitrary member function into a
pavolmarko@117
   672
	// standard form. XMemFunc should be a member function of class X, but I can't
pavolmarko@1
   673
	// enforce that here. It needs to be enforced by the wrapper class.
pavolmarko@1
   674
	template < class X, class XMemFunc >
pavolmarko@1
   675
	inline void bindmemfunc(X *pthis, XMemFunc function_to_bind ) {
pavolmarko@1
   676
		m_pthis = SimplifyMemFunc< sizeof(function_to_bind) >
pavolmarko@1
   677
			::Convert(pthis, function_to_bind, m_pFunction);
pavolmarko@1
   678
#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
pavolmarko@1
   679
		m_pStaticFunction = 0;
pavolmarko@1
   680
#endif
pavolmarko@1
   681
	}
pavolmarko@1
   682
	// For const member functions, we only need a const class pointer.
pavolmarko@117
   683
	// Since we know that the member function is const, it's safe to
pavolmarko@1
   684
	// remove the const qualifier from the 'this' pointer with a const_cast.
pavolmarko@1
   685
	// VC6 has problems if we just overload 'bindmemfunc', so we give it a different name.
pavolmarko@1
   686
	template < class X, class XMemFunc>
pavolmarko@1
   687
	inline void bindconstmemfunc(const X *pthis, XMemFunc function_to_bind) {
pavolmarko@1
   688
		m_pthis= SimplifyMemFunc< sizeof(function_to_bind) >
pavolmarko@1
   689
			::Convert(const_cast<X*>(pthis), function_to_bind, m_pFunction);
pavolmarko@1
   690
#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
pavolmarko@1
   691
		m_pStaticFunction = 0;
pavolmarko@1
   692
#endif
pavolmarko@1
   693
	}
pavolmarko@1
   694
#ifdef FASTDELEGATE_GCC_BUG_8271	// At present, GCC doesn't recognize constness of MFPs in templates
pavolmarko@1
   695
	template < class X, class XMemFunc>
pavolmarko@1
   696
	inline void bindmemfunc(const X *pthis, XMemFunc function_to_bind) {
pavolmarko@1
   697
		bindconstmemfunc(pthis, function_to_bind);
pavolmarko@1
   698
#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
pavolmarko@1
   699
		m_pStaticFunction = 0;
pavolmarko@1
   700
#endif
pavolmarko@1
   701
	}
pavolmarko@1
   702
#endif
pavolmarko@1
   703
	// These functions are required for invoking the stored function
pavolmarko@1
   704
	inline GenericClass *GetClosureThis() const { return m_pthis; }
pavolmarko@1
   705
	inline GenericMemFunc GetClosureMemPtr() const { return reinterpret_cast<GenericMemFunc>(m_pFunction); }
pavolmarko@1
   706
pavolmarko@1
   707
// There are a few ways of dealing with static function pointers.
pavolmarko@1
   708
// There's a standard-compliant, but tricky method.
pavolmarko@1
   709
// There's also a straightforward hack, that won't work on DOS compilers using the
pavolmarko@1
   710
// medium memory model. It's so evil that I can't recommend it, but I've
pavolmarko@1
   711
// implemented it anyway because it produces very nice asm code.
pavolmarko@1
   712
pavolmarko@1
   713
#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
pavolmarko@1
   714
pavolmarko@1
   715
//				ClosurePtr<> - Safe version
pavolmarko@1
   716
//
pavolmarko@1
   717
// This implementation is standard-compliant, but a bit tricky.
pavolmarko@1
   718
// I store the function pointer inside the class, and the delegate then
pavolmarko@1
   719
// points to itself. Whenever the delegate is copied, these self-references
pavolmarko@1
   720
// must be transformed, and this complicates the = and == operators.
pavolmarko@1
   721
public:
pavolmarko@1
   722
	// The next two functions are for operator ==, =, and the copy constructor.
pavolmarko@1
   723
	// We may need to convert the m_pthis pointers, so that
pavolmarko@1
   724
	// they remain as self-references.
pavolmarko@1
   725
	template< class DerivedClass >
pavolmarko@1
   726
	inline void CopyFrom (DerivedClass *pParent, const DelegateMemento &x) {
pavolmarko@1
   727
		SetMementoFrom(x);
pavolmarko@1
   728
		if (m_pStaticFunction!=0) {
pavolmarko@1
   729
			// transform self references...
pavolmarko@1
   730
			m_pthis=reinterpret_cast<GenericClass *>(pParent);
pavolmarko@1
   731
		}
pavolmarko@1
   732
	}
pavolmarko@117
   733
	// For static functions, the 'static_function_invoker' class in the parent
pavolmarko@117
   734
	// will be called. The parent then needs to call GetStaticFunction() to find out
pavolmarko@1
   735
	// the actual function to invoke.
pavolmarko@1
   736
	template < class DerivedClass, class ParentInvokerSig >
pavolmarko@117
   737
	inline void bindstaticfunc(DerivedClass *pParent, ParentInvokerSig static_function_invoker,
pavolmarko@1
   738
				StaticFuncPtr function_to_bind ) {
pavolmarko@134
   739
		if (function_to_bind==0) { // cope with assignment to 0
pavolmarko@134
   740
			m_pFunction=0;
pavolmarko@134
   741
		} else {
pavolmarko@134
   742
			bindmemfunc(pParent, static_function_invoker);
pavolmarko@134
   743
        }
pavolmarko@1
   744
		m_pStaticFunction=reinterpret_cast<GenericFuncPtr>(function_to_bind);
pavolmarko@1
   745
	}
pavolmarko@117
   746
	inline UnvoidStaticFuncPtr GetStaticFunction() const {
pavolmarko@117
   747
		return reinterpret_cast<UnvoidStaticFuncPtr>(m_pStaticFunction);
pavolmarko@1
   748
	}
pavolmarko@1
   749
#else
pavolmarko@1
   750
pavolmarko@1
   751
//				ClosurePtr<> - Evil version
pavolmarko@1
   752
//
pavolmarko@117
   753
// For compilers where data pointers are at least as big as code pointers, it is
pavolmarko@117
   754
// possible to store the function pointer in the this pointer, using another
pavolmarko@1
   755
// horrible_cast. Invocation isn't any faster, but it saves 4 bytes, and
pavolmarko@1
   756
// speeds up comparison and assignment. If C++ provided direct language support
pavolmarko@1
   757
// for delegates, they would produce asm code that was almost identical to this.
pavolmarko@117
   758
// Note that the Sun C++ and MSVC documentation explicitly state that they
pavolmarko@1
   759
// support static_cast between void * and function pointers.
pavolmarko@1
   760
pavolmarko@1
   761
	template< class DerivedClass >
pavolmarko@1
   762
	inline void CopyFrom (DerivedClass *pParent, const DelegateMemento &right) {
pavolmarko@1
   763
		SetMementoFrom(right);
pavolmarko@1
   764
	}
pavolmarko@117
   765
	// For static functions, the 'static_function_invoker' class in the parent
pavolmarko@117
   766
	// will be called. The parent then needs to call GetStaticFunction() to find out
pavolmarko@1
   767
	// the actual function to invoke.
pavolmarko@1
   768
	// ******** EVIL, EVIL CODE! *******
pavolmarko@1
   769
	template < 	class DerivedClass, class ParentInvokerSig>
pavolmarko@117
   770
	inline void bindstaticfunc(DerivedClass *pParent, ParentInvokerSig static_function_invoker,
pavolmarko@1
   771
				StaticFuncPtr function_to_bind) {
pavolmarko@134
   772
		if (function_to_bind==0) { // cope with assignment to 0
pavolmarko@134
   773
			m_pFunction=0;
pavolmarko@134
   774
		} else { 
pavolmarko@134
   775
		   // We'll be ignoring the 'this' pointer, but we need to make sure we pass
pavolmarko@134
   776
		   // a valid value to bindmemfunc().
pavolmarko@134
   777
			bindmemfunc(pParent, static_function_invoker);
pavolmarko@134
   778
        }
pavolmarko@1
   779
pavolmarko@1
   780
		// WARNING! Evil hack. We store the function in the 'this' pointer!
pavolmarko@117
   781
		// Ensure that there's a compilation failure if function pointers
pavolmarko@1
   782
		// and data pointers have different sizes.
pavolmarko@1
   783
		// If you get this error, you need to #undef FASTDELEGATE_USESTATICFUNCTIONHACK.
pavolmarko@1
   784
		typedef int ERROR_CantUseEvilMethod[sizeof(GenericClass *)==sizeof(function_to_bind) ? 1 : -1];
pavolmarko@1
   785
		m_pthis = horrible_cast<GenericClass *>(function_to_bind);
pavolmarko@1
   786
		// MSVC, SunC++ and DMC accept the following (non-standard) code:
pavolmarko@1
   787
//		m_pthis = static_cast<GenericClass *>(static_cast<void *>(function_to_bind));
pavolmarko@1
   788
		// BCC32, Comeau and DMC accept this method. MSVC7.1 needs __int64 instead of long
pavolmarko@1
   789
//		m_pthis = reinterpret_cast<GenericClass *>(reinterpret_cast<long>(function_to_bind));
pavolmarko@1
   790
	}
pavolmarko@1
   791
	// ******** EVIL, EVIL CODE! *******
pavolmarko@1
   792
	// This function will be called with an invalid 'this' pointer!!
pavolmarko@1
   793
	// We're just returning the 'this' pointer, converted into
pavolmarko@1
   794
	// a function pointer!
pavolmarko@1
   795
	inline UnvoidStaticFuncPtr GetStaticFunction() const {
pavolmarko@117
   796
		// Ensure that there's a compilation failure if function pointers
pavolmarko@1
   797
		// and data pointers have different sizes.
pavolmarko@1
   798
		// If you get this error, you need to #undef FASTDELEGATE_USESTATICFUNCTIONHACK.
pavolmarko@1
   799
		typedef int ERROR_CantUseEvilMethod[sizeof(UnvoidStaticFuncPtr)==sizeof(this) ? 1 : -1];
pavolmarko@1
   800
		return horrible_cast<UnvoidStaticFuncPtr>(this);
pavolmarko@1
   801
	}
pavolmarko@1
   802
#endif // !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
pavolmarko@1
   803
pavolmarko@134
   804
	// Does the closure contain this static function?
pavolmarko@134
   805
	inline bool IsEqualToStaticFuncPtr(StaticFuncPtr funcptr){
pavolmarko@134
   806
		if (funcptr==0) return empty();
pavolmarko@134
   807
	// For the Evil method, if it doesn't actually contain a static function, this will return an arbitrary
pavolmarko@134
   808
	// value that is not equal to any valid function pointer.
pavolmarko@134
   809
		else return funcptr==reinterpret_cast<StaticFuncPtr>(GetStaticFunction());
pavolmarko@134
   810
	}
pavolmarko@1
   811
};
pavolmarko@1
   812
pavolmarko@1
   813
pavolmarko@1
   814
} // namespace detail
pavolmarko@1
   815
pavolmarko@1
   816
////////////////////////////////////////////////////////////////////////////////
pavolmarko@1
   817
//						Fast Delegates, part 3:
pavolmarko@1
   818
//
pavolmarko@1
   819
//				Wrapper classes to ensure type safety
pavolmarko@1
   820
//
pavolmarko@1
   821
////////////////////////////////////////////////////////////////////////////////
pavolmarko@1
   822
pavolmarko@1
   823
pavolmarko@1
   824
// Once we have the member function conversion templates, it's easy to make the
pavolmarko@117
   825
// wrapper classes. So that they will work with as many compilers as possible,
pavolmarko@1
   826
// the classes are of the form
pavolmarko@1
   827
//   FastDelegate3<int, char *, double>
pavolmarko@1
   828
// They can cope with any combination of parameters. The max number of parameters
pavolmarko@1
   829
// allowed is 8, but it is trivial to increase this limit.
pavolmarko@1
   830
// Note that we need to treat const member functions seperately.
pavolmarko@1
   831
// All this class does is to enforce type safety, and invoke the delegate with
pavolmarko@1
   832
// the correct list of parameters.
pavolmarko@1
   833
pavolmarko@1
   834
// Because of the weird rule about the class of derived member function pointers,
pavolmarko@1
   835
// you sometimes need to apply a downcast to the 'this' pointer.
pavolmarko@117
   836
// This is the reason for the use of "implicit_cast<X*>(pthis)" in the code below.
pavolmarko@1
   837
// If CDerivedClass is derived from CBaseClass, but doesn't override SimpleVirtualFunction,
pavolmarko@1
   838
// without this trick you'd need to write:
pavolmarko@1
   839
//		MyDelegate(static_cast<CBaseClass *>(&d), &CDerivedClass::SimpleVirtualFunction);
pavolmarko@1
   840
// but with the trick you can write
pavolmarko@1
   841
//		MyDelegate(&d, &CDerivedClass::SimpleVirtualFunction);
pavolmarko@1
   842
pavolmarko@1
   843
// RetType is the type the compiler uses in compiling the template. For VC6,
pavolmarko@1
   844
// it cannot be void. DesiredRetType is the real type which is returned from
pavolmarko@1
   845
// all of the functions. It can be void.
pavolmarko@1
   846
pavolmarko@134
   847
// Implicit conversion to "bool" is achieved using the safe_bool idiom,
pavolmarko@134
   848
// using member data pointers (MDP). This allows "if (dg)..." syntax
pavolmarko@134
   849
// Because some compilers (eg codeplay) don't have a unique value for a zero
pavolmarko@134
   850
// MDP, an extra padding member is added to the SafeBool struct.
pavolmarko@134
   851
// Some compilers (eg VC6) won't implicitly convert from 0 to an MDP, so
pavolmarko@134
   852
// in that case the static function constructor is not made explicit; this
pavolmarko@134
   853
// allows "if (dg==0) ..." to compile.
pavolmarko@134
   854
pavolmarko@1
   855
@VARARGS
pavolmarko@1
   856
template<@CLASSARGS, class RetType=detail::DefaultVoid>
pavolmarko@1
   857
class FastDelegate@NUM {
pavolmarko@1
   858
private:
pavolmarko@1
   859
	typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
pavolmarko@1
   860
	typedef DesiredRetType (*StaticFunctionPtr)(@FUNCARGS);
pavolmarko@1
   861
	typedef RetType (*UnvoidStaticFunctionPtr)(@FUNCARGS);
pavolmarko@1
   862
	typedef RetType (detail::GenericClass::*GenericMemFn)(@FUNCARGS);
pavolmarko@134
   863
	typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
pavolmarko@134
   864
	ClosureType m_Closure;
pavolmarko@1
   865
public:
pavolmarko@1
   866
	// Typedefs to aid generic programming
pavolmarko@1
   867
	typedef FastDelegate@NUM type;
pavolmarko@1
   868
pavolmarko@1
   869
	// Construction and comparison functions
pavolmarko@1
   870
	FastDelegate@NUM() { clear(); }
pavolmarko@1
   871
	FastDelegate@NUM(const FastDelegate@NUM &x) {
pavolmarko@1
   872
		m_Closure.CopyFrom(this, x.m_Closure); }
pavolmarko@1
   873
	void operator = (const FastDelegate@NUM &x)  {
pavolmarko@1
   874
		m_Closure.CopyFrom(this, x.m_Closure); }
pavolmarko@1
   875
	bool operator ==(const FastDelegate@NUM &x) const {
pavolmarko@1
   876
		return m_Closure.IsEqual(x.m_Closure);	}
pavolmarko@1
   877
	bool operator !=(const FastDelegate@NUM &x) const {
pavolmarko@1
   878
		return !m_Closure.IsEqual(x.m_Closure); }
pavolmarko@1
   879
	bool operator <(const FastDelegate@NUM &x) const {
pavolmarko@1
   880
		return m_Closure.IsLess(x.m_Closure);	}
pavolmarko@1
   881
	bool operator >(const FastDelegate@NUM &x) const {
pavolmarko@1
   882
		return x.m_Closure.IsLess(m_Closure);	}
pavolmarko@1
   883
	// Binding to non-const member functions
pavolmarko@1
   884
	template < class X, class Y >
pavolmarko@1
   885
	FastDelegate@NUM(Y *pthis, DesiredRetType (X::* function_to_bind)(@FUNCARGS) ) {
pavolmarko@1
   886
		m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
pavolmarko@1
   887
	template < class X, class Y >
pavolmarko@1
   888
	inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(@FUNCARGS)) {
pavolmarko@1
   889
		m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind);	}
pavolmarko@1
   890
	// Binding to const member functions.
pavolmarko@1
   891
	template < class X, class Y >
pavolmarko@1
   892
	FastDelegate@NUM(const Y *pthis, DesiredRetType (X::* function_to_bind)(@FUNCARGS) const) {
pavolmarko@1
   893
		m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind);	}
pavolmarko@1
   894
	template < class X, class Y >
pavolmarko@1
   895
	inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(@FUNCARGS) const) {
pavolmarko@1
   896
		m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind);	}
pavolmarko@1
   897
	// Static functions. We convert them into a member function call.
pavolmarko@134
   898
	// This constructor also provides implicit conversion
pavolmarko@1
   899
	FastDelegate@NUM(DesiredRetType (*function_to_bind)(@FUNCARGS) ) {
pavolmarko@1
   900
		bind(function_to_bind);	}
pavolmarko@134
   901
	// for efficiency, prevent creation of a temporary
pavolmarko@134
   902
	void operator = (DesiredRetType (*function_to_bind)(@FUNCARGS) ) {
pavolmarko@134
   903
		bind(function_to_bind);	}
pavolmarko@1
   904
	inline void bind(DesiredRetType (*function_to_bind)(@FUNCARGS)) {
pavolmarko@117
   905
		m_Closure.bindstaticfunc(this, &FastDelegate@NUM::InvokeStaticFunction,
pavolmarko@1
   906
			function_to_bind); }
pavolmarko@134
   907
	// Invoke the delegate
pavolmarko@134
   908
	RetType operator() (@FUNCARGS) const {
pavolmarko@1
   909
	return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(@INVOKEARGS); }
pavolmarko@134
   910
	// Implicit conversion to "bool" using the safe_bool idiom
pavolmarko@134
   911
private:
pavolmarko@134
   912
	typedef struct SafeBoolStruct {
pavolmarko@134
   913
		int a_data_pointer_to_this_is_0_on_buggy_compilers;
pavolmarko@134
   914
		StaticFunctionPtr m_nonzero;
pavolmarko@134
   915
	} UselessTypedef;
pavolmarko@134
   916
    typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type;
pavolmarko@134
   917
public:
pavolmarko@134
   918
	operator unspecified_bool_type() const {
pavolmarko@134
   919
        return empty()? 0: &SafeBoolStruct::m_nonzero;
pavolmarko@134
   920
    }
pavolmarko@134
   921
	// necessary to allow ==0 to work despite the safe_bool idiom
pavolmarko@134
   922
	inline bool operator==(StaticFunctionPtr funcptr) {
pavolmarko@134
   923
		return m_Closure.IsEqualToStaticFuncPtr(funcptr);	}
pavolmarko@134
   924
	inline bool operator!=(StaticFunctionPtr funcptr) { 
pavolmarko@134
   925
		return !m_Closure.IsEqualToStaticFuncPtr(funcptr);    }
pavolmarko@1
   926
	inline bool operator ! () const	{	// Is it bound to anything?
pavolmarko@1
   927
			return !m_Closure; }
pavolmarko@1
   928
	inline bool empty() const	{
pavolmarko@1
   929
			return !m_Closure; }
pavolmarko@1
   930
	void clear() { m_Closure.clear();}
pavolmarko@1
   931
	// Conversion to and from the DelegateMemento storage class
pavolmarko@1
   932
	const DelegateMemento & GetMemento() { return m_Closure; }
pavolmarko@1
   933
	void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); }
pavolmarko@1
   934
pavolmarko@1
   935
private:	// Invoker for static functions
pavolmarko@1
   936
	RetType InvokeStaticFunction(@FUNCARGS) const {
pavolmarko@1
   937
	return (*(m_Closure.GetStaticFunction()))(@INVOKEARGS); }
pavolmarko@1
   938
};
pavolmarko@1
   939
pavolmarko@1
   940
@ENDVAR
pavolmarko@1
   941
pavolmarko@1
   942
////////////////////////////////////////////////////////////////////////////////
pavolmarko@1
   943
//						Fast Delegates, part 4:
pavolmarko@117
   944
//
pavolmarko@1
   945
//				FastDelegate<> class (Original author: Jody Hagins)
pavolmarko@1
   946
//	Allows boost::function style syntax like:
pavolmarko@1
   947
//			FastDelegate< double (int, long) >
pavolmarko@1
   948
// instead of:
pavolmarko@1
   949
//			FastDelegate2< int, long, double >
pavolmarko@1
   950
//
pavolmarko@1
   951
////////////////////////////////////////////////////////////////////////////////
pavolmarko@1
   952
pavolmarko@1
   953
#ifdef FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
pavolmarko@1
   954
pavolmarko@1
   955
// Declare FastDelegate as a class template.  It will be specialized
pavolmarko@1
   956
// later for all number of arguments.
pavolmarko@1
   957
template <typename Signature>
pavolmarko@1
   958
class FastDelegate;
pavolmarko@1
   959
pavolmarko@1
   960
@VARARGS
pavolmarko@1
   961
// Specialization to allow use of
pavolmarko@1
   962
// FastDelegate< R ( @SELARGS ) >
pavolmarko@1
   963
// instead of
pavolmarko@1
   964
// FastDelegate@NUM < @SELARGS, R >
pavolmarko@1
   965
template<typename R, @CLASSARGS>
pavolmarko@1
   966
class FastDelegate< R ( @SELARGS ) >
pavolmarko@134
   967
  // Inherit from FastDelegate@NUM so that it can be treated just like a FastDelegate@NUM
pavolmarko@1
   968
  : public FastDelegate@NUM < @SELARGS, R >
pavolmarko@1
   969
{
pavolmarko@1
   970
public:
pavolmarko@1
   971
  // Make using the base type a bit easier via typedef.
pavolmarko@1
   972
  typedef FastDelegate@NUM < @SELARGS, R > BaseType;
pavolmarko@1
   973
pavolmarko@1
   974
  // Allow users access to the specific type of this delegate.
pavolmarko@1
   975
  typedef FastDelegate SelfType;
pavolmarko@1
   976
pavolmarko@1
   977
  // Mimic the base class constructors.
pavolmarko@1
   978
  FastDelegate() : BaseType() { }
pavolmarko@1
   979
pavolmarko@1
   980
  template < class X, class Y >
pavolmarko@117
   981
  FastDelegate(Y * pthis,
pavolmarko@1
   982
    R (X::* function_to_bind)( @FUNCARGS ))
pavolmarko@1
   983
    : BaseType(pthis, function_to_bind)  { }
pavolmarko@1
   984
pavolmarko@1
   985
  template < class X, class Y >
pavolmarko@1
   986
  FastDelegate(const Y *pthis,
pavolmarko@1
   987
      R (X::* function_to_bind)( @FUNCARGS ) const)
pavolmarko@1
   988
    : BaseType(pthis, function_to_bind)
pavolmarko@1
   989
  {  }
pavolmarko@1
   990
pavolmarko@1
   991
  FastDelegate(R (*function_to_bind)( @FUNCARGS ))
pavolmarko@1
   992
    : BaseType(function_to_bind)  { }
pavolmarko@117
   993
  void operator = (const BaseType &x)  {
pavolmarko@1
   994
		*static_cast<BaseType*>(this) = x; }
pavolmarko@1
   995
};
pavolmarko@1
   996
pavolmarko@1
   997
@ENDVAR
pavolmarko@1
   998
pavolmarko@1
   999
#endif //FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
pavolmarko@1
  1000
pavolmarko@1
  1001
////////////////////////////////////////////////////////////////////////////////
pavolmarko@1
  1002
//						Fast Delegates, part 5:
pavolmarko@1
  1003
//
pavolmarko@1
  1004
//				MakeDelegate() helper function
pavolmarko@1
  1005
//
pavolmarko@1
  1006
//			MakeDelegate(&x, &X::func) returns a fastdelegate of the type
pavolmarko@1
  1007
//			necessary for calling x.func() with the correct number of arguments.
pavolmarko@1
  1008
//			This makes it possible to eliminate many typedefs from user code.
pavolmarko@1
  1009
//
pavolmarko@1
  1010
////////////////////////////////////////////////////////////////////////////////
pavolmarko@1
  1011
pavolmarko@117
  1012
// Also declare overloads of a MakeDelegate() global function to
pavolmarko@1
  1013
// reduce the need for typedefs.
pavolmarko@1
  1014
// We need seperate overloads for const and non-const member functions.
pavolmarko@1
  1015
// Also, because of the weird rule about the class of derived member function pointers,
pavolmarko@1
  1016
// implicit downcasts may need to be applied later to the 'this' pointer.
pavolmarko@1
  1017
// That's why two classes (X and Y) appear in the definitions. Y must be implicitly
pavolmarko@1
  1018
// castable to X.
pavolmarko@1
  1019
pavolmarko@1
  1020
// Workaround for VC6. VC6 needs void return types converted into DefaultVoid.
pavolmarko@1
  1021
// GCC 3.2 and later won't compile this unless it's preceded by 'typename',
pavolmarko@1
  1022
// but VC6 doesn't allow 'typename' in this context.
pavolmarko@1
  1023
// So, I have to use a macro.
pavolmarko@1
  1024
pavolmarko@1
  1025
#ifdef FASTDLGT_VC6
pavolmarko@1
  1026
#define FASTDLGT_RETTYPE detail::VoidToDefaultVoid<RetType>::type
pavolmarko@117
  1027
#else
pavolmarko@1
  1028
#define FASTDLGT_RETTYPE RetType
pavolmarko@1
  1029
#endif
pavolmarko@1
  1030
pavolmarko@1
  1031
@VARARGS
pavolmarko@1
  1032
template <class X, class Y, @CLASSARGS, class RetType>
pavolmarko@117
  1033
FastDelegate@NUM<@SELARGS, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(@FUNCARGS)) {
pavolmarko@1
  1034
	return FastDelegate@NUM<@SELARGS, FASTDLGT_RETTYPE>(x, func);
pavolmarko@1
  1035
}
pavolmarko@1
  1036
pavolmarko@1
  1037
template <class X, class Y, @CLASSARGS, class RetType>
pavolmarko@117
  1038
FastDelegate@NUM<@SELARGS, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(@FUNCARGS) const) {
pavolmarko@1
  1039
	return FastDelegate@NUM<@SELARGS, FASTDLGT_RETTYPE>(x, func);
pavolmarko@1
  1040
}
pavolmarko@1
  1041
pavolmarko@45
  1042
template <@CLASSARGS, class RetType>
pavolmarko@117
  1043
FastDelegate@NUM<@SELARGS, FASTDLGT_RETTYPE> MakeDelegate(RetType (*func)(@FUNCARGS)) {
pavolmarko@45
  1044
	return FastDelegate@NUM<@SELARGS, FASTDLGT_RETTYPE>(func);
pavolmarko@45
  1045
}
pavolmarko@45
  1046
pavolmarko@1
  1047
@ENDVAR
pavolmarko@1
  1048
pavolmarko@1
  1049
 // clean up after ourselves...
pavolmarko@1
  1050
#undef FASTDLGT_RETTYPE
pavolmarko@1
  1051
pavolmarko@1
  1052
} // namespace fastdelegate
pavolmarko@1
  1053
pavolmarko@1
  1054
#endif // !defined(FASTDELEGATE_H)