sourcehook/generate/FastDelegate.hxx
changeset 1 586787053c73
child 45 04770941f5a9
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/sourcehook/generate/FastDelegate.hxx	Thu Apr 14 12:54:13 2005 +0000
     1.3 @@ -0,0 +1,992 @@
     1.4 +//						FastDelegate.h 
     1.5 +//	Efficient delegates in C++ that generate only two lines of asm code!
     1.6 +//  Documentation is found at http://www.codeproject.com/cpp/FastDelegate.asp
     1.7 +//
     1.8 +//						- Don Clugston, Mar 2004.
     1.9 +//		Major contributions were made by Jody Hagins.
    1.10 +// History:
    1.11 +// 24-Apr-04 1.0  Submitted to CodeProject. 
    1.12 +// 28-Apr-04 1.1  * Prevent most unsafe uses of evil static function hack.
    1.13 +//				  * Improved syntax for horrible_cast (thanks Paul Bludov).
    1.14 +//				  * Tested on Metrowerks MWCC and Intel ICL (IA32)
    1.15 +//				  * Compiled, but not run, on Comeau C++ and Intel Itanium ICL.
    1.16 +//	27-Jun-04 1.2 * Now works on Borland C++ Builder 5.5
    1.17 +//				  * Now works on /clr "managed C++" code on VC7, VC7.1
    1.18 +//				  * Comeau C++ now compiles without warnings.
    1.19 +//				  * Prevent the virtual inheritance case from being used on 
    1.20 +//					  VC6 and earlier, which generate incorrect code.
    1.21 +//				  * Improved warning and error messages. Non-standard hacks
    1.22 +//					 now have compile-time checks to make them safer.
    1.23 +//				  * implicit_cast used instead of static_cast in many cases.
    1.24 +//				  * If calling a const member function, a const class pointer can be used.
    1.25 +//				  * MakeDelegate() global helper function added to simplify pass-by-value.
    1.26 +//				  * Added fastdelegate.clear()
    1.27 +// 16-Jul-04 1.2.1* Workaround for gcc bug (const member function pointers in templates)
    1.28 +// 30-Oct-04 1.3  * Support for (non-void) return values.
    1.29 +//				  * No more workarounds in client code!
    1.30 +//					 MSVC and Intel now use a clever hack invented by John Dlugosz:
    1.31 +//				     - The FASTDELEGATEDECLARE workaround is no longer necessary.
    1.32 +//					 - No more warning messages for VC6
    1.33 +//				  * Less use of macros. Error messages should be more comprehensible.
    1.34 +//				  * Added include guards
    1.35 +//				  * Added FastDelegate::empty() to test if invocation is safe (Thanks Neville Franks).
    1.36 +//				  * Now tested on VS 2005 Beta, PGI C++
    1.37 +// 24-Dec-04 1.4  * Added DelegateMemento, to allow collections of disparate delegates.
    1.38 +//                * <,>,<=,>= comparison operators to allow storage in ordered containers.
    1.39 +//				  * Substantial reduction of code size, especially the 'Closure' class.
    1.40 +//				  * Standardised all the compiler-specific workarounds.
    1.41 +//                * MFP conversion now works for CodePlay (but not yet supported in the full code).
    1.42 +//                * Now compiles without warnings on _any_ compiler, including BCC 5.5.1
    1.43 +//				  * New syntax: FastDelegate< int (char *, double) >. 
    1.44 +
    1.45 +#ifndef FASTDELEGATE_H
    1.46 +#define FASTDELEGATE_H
    1.47 +#if _MSC_VER > 1000
    1.48 +#pragma once
    1.49 +#endif // _MSC_VER > 1000
    1.50 +
    1.51 +#include <memory.h> // to allow <,> comparisons
    1.52 +
    1.53 +////////////////////////////////////////////////////////////////////////////////
    1.54 +//						Configuration options
    1.55 +//
    1.56 +////////////////////////////////////////////////////////////////////////////////
    1.57 +
    1.58 +// Uncomment the following #define for optimally-sized delegates.
    1.59 +// In this case, the generated asm code is almost identical to the code you'd get
    1.60 +// if the compiler had native support for delegates.
    1.61 +// It will not work on systems where sizeof(dataptr) < sizeof(codeptr). 
    1.62 +// Thus, it will not work for DOS compilers using the medium model.
    1.63 +// It will also probably fail on some DSP systems.
    1.64 +#define FASTDELEGATE_USESTATICFUNCTIONHACK
    1.65 +
    1.66 +// Uncomment the next line to allow function declarator syntax.
    1.67 +// It is automatically enabled for those compilers where it is known to work.
    1.68 +//#define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
    1.69 +
    1.70 +////////////////////////////////////////////////////////////////////////////////
    1.71 +//						Compiler identification for workarounds
    1.72 +//
    1.73 +////////////////////////////////////////////////////////////////////////////////
    1.74 +
    1.75 +// Compiler identification. It's not easy to identify Visual C++ because
    1.76 +// many vendors fraudulently define Microsoft's identifiers.
    1.77 +#if defined(_MSC_VER) && !defined(__MWERKS__) && !defined(__VECTOR_C) && !defined(__ICL) && !defined(__BORLANDC__)
    1.78 +#define FASTDLGT_ISMSVC
    1.79 +
    1.80 +#if (_MSC_VER <1300) // Many workarounds are required for VC6.
    1.81 +#define FASTDLGT_VC6
    1.82 +#pragma warning(disable:4786) // disable this ridiculous warning
    1.83 +#endif
    1.84 +
    1.85 +#endif
    1.86 +
    1.87 +// Does the compiler uses Microsoft's member function pointer structure?
    1.88 +// If so, it needs special treatment.
    1.89 +// Metrowerks CodeWarrior, Intel, and CodePlay fraudulently define Microsoft's 
    1.90 +// identifier, _MSC_VER. We need to filter Metrowerks out.
    1.91 +#if defined(_MSC_VER) && !defined(__MWERKS__)
    1.92 +#define FASTDLGT_MICROSOFT_MFP
    1.93 +
    1.94 +#if !defined(__VECTOR_C)
    1.95 +// CodePlay doesn't have the __single/multi/virtual_inheritance keywords
    1.96 +#define FASTDLGT_HASINHERITANCE_KEYWORDS
    1.97 +#endif
    1.98 +#endif
    1.99 +
   1.100 +// Does it allow function declarator syntax? The following compilers are known to work:
   1.101 +#if defined(FASTDLGT_ISMSVC) && (_MSC_VER >=1310) // VC 7.1
   1.102 +#define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
   1.103 +#endif
   1.104 +
   1.105 +// Gcc(2.95+), and versions of Digital Mars, Intel and Comeau in common use.
   1.106 +#if defined (__DMC__) || defined(__GNUC__) || defined(__ICL) || defined(__COMO__)
   1.107 +#define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
   1.108 +#endif
   1.109 +
   1.110 +// It works on Metrowerks MWCC 3.2.2. From boost.Config it should work on earlier ones too.
   1.111 +#if defined (__MWERKS__)
   1.112 +#define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
   1.113 +#endif
   1.114 +
   1.115 +#ifdef __GNUC__ // Workaround GCC bug #8271 
   1.116 +	// At present, GCC doesn't recognize constness of MFPs in templates
   1.117 +#define FASTDELEGATE_GCC_BUG_8271
   1.118 +#endif
   1.119 +
   1.120 +
   1.121 +
   1.122 +////////////////////////////////////////////////////////////////////////////////
   1.123 +//						General tricks used in this code
   1.124 +//
   1.125 +// (a) Error messages are generated by typdefing an array of negative size to
   1.126 +//     generate compile-time errors.
   1.127 +// (b) Warning messages on MSVC are generated by declaring unused variables, and
   1.128 +//	    enabling the "variable XXX is never used" warning.
   1.129 +// (c) Unions are used in a few compiler-specific cases to perform illegal casts.
   1.130 +// (d) For Microsoft and Intel, when adjusting the 'this' pointer, it's cast to
   1.131 +//     (char *) first to ensure that the correct number of *bytes* are added.
   1.132 +//
   1.133 +////////////////////////////////////////////////////////////////////////////////
   1.134 +//						Helper templates
   1.135 +//
   1.136 +////////////////////////////////////////////////////////////////////////////////
   1.137 +
   1.138 +
   1.139 +namespace fastdelegate {
   1.140 +namespace detail {	// we'll hide the implementation details in a nested namespace.
   1.141 +
   1.142 +//		implicit_cast< >
   1.143 +// I believe this was originally going to be in the C++ standard but 
   1.144 +// was left out by accident. It's even milder than static_cast.
   1.145 +// I use it instead of static_cast<> to emphasize that I'm not doing
   1.146 +// anything nasty. 
   1.147 +// Usage is identical to static_cast<>
   1.148 +template <class OutputClass, class InputClass>
   1.149 +inline OutputClass implicit_cast(InputClass input){
   1.150 +	return input;
   1.151 +}
   1.152 +
   1.153 +//		horrible_cast< >
   1.154 +// This is truly evil. It completely subverts C++'s type system, allowing you 
   1.155 +// to cast from any class to any other class. Technically, using a union 
   1.156 +// to perform the cast is undefined behaviour (even in C). But we can see if
   1.157 +// it is OK by checking that the union is the same size as each of its members.
   1.158 +// horrible_cast<> should only be used for compiler-specific workarounds. 
   1.159 +// Usage is identical to reinterpret_cast<>.
   1.160 +
   1.161 +// This union is declared outside the horrible_cast because BCC 5.5.1
   1.162 +// can't inline a function with a nested class, and gives a warning.
   1.163 +template <class OutputClass, class InputClass>
   1.164 +union horrible_union{
   1.165 +	OutputClass out;
   1.166 +	InputClass in;
   1.167 +};
   1.168 +
   1.169 +template <class OutputClass, class InputClass>
   1.170 +inline OutputClass horrible_cast(const InputClass input){
   1.171 +	horrible_union<OutputClass, InputClass> u;
   1.172 +	// Cause a compile-time error if in, out and u are not the same size.
   1.173 +	// If the compile fails here, it means the compiler has peculiar
   1.174 +	// unions which would prevent the cast from working.
   1.175 +	typedef int ERROR_CantUseHorrible_cast[sizeof(InputClass)==sizeof(u) 
   1.176 +		&& sizeof(InputClass)==sizeof(OutputClass) ? 1 : -1];
   1.177 +	u.in = input;
   1.178 +	return u.out;
   1.179 +}
   1.180 +
   1.181 +////////////////////////////////////////////////////////////////////////////////
   1.182 +//						Workarounds
   1.183 +//
   1.184 +////////////////////////////////////////////////////////////////////////////////
   1.185 +
   1.186 +// Backwards compatibility: This macro used to be necessary in the virtual inheritance
   1.187 +// case for Intel and Microsoft. Now it just forward-declares the class.
   1.188 +#define FASTDELEGATEDECLARE(CLASSNAME)	class CLASSNAME;
   1.189 +
   1.190 +// Prevent use of the static function hack with the DOS medium model.
   1.191 +#ifdef __MEDIUM__
   1.192 +#undef FASTDELEGATE_USESTATICFUNCTIONHACK
   1.193 +#endif
   1.194 +
   1.195 +//			DefaultVoid - a workaround for 'void' templates in VC6.
   1.196 +//
   1.197 +//  (1) VC6 and earlier do not allow 'void' as a default template argument.
   1.198 +//  (2) They also doesn't allow you to return 'void' from a function.
   1.199 +//
   1.200 +// Workaround for (1): Declare a dummy type 'DefaultVoid' which we use
   1.201 +//   when we'd like to use 'void'. We convert it into 'void' and back
   1.202 +//   using the templates DefaultVoidToVoid<> and VoidToDefaultVoid<>.
   1.203 +// Workaround for (2): On VC6, the code for calling a void function is
   1.204 +//   identical to the code for calling a non-void function in which the
   1.205 +//   return value is never used, provided the return value is returned
   1.206 +//   in the EAX register, rather than on the stack. 
   1.207 +//   This is true for most fundamental types such as int, enum, void *.
   1.208 +//   Const void * is the safest option since it doesn't participate 
   1.209 +//   in any automatic conversions. But on a 16-bit compiler it might
   1.210 +//   cause extra code to be generated, so we disable it for all compilers
   1.211 +//   except for VC6 (and VC5).
   1.212 +#ifdef FASTDLGT_VC6
   1.213 +// VC6 workaround
   1.214 +typedef const void * DefaultVoid;
   1.215 +#else
   1.216 +// On any other compiler, just use a normal void.
   1.217 +typedef void DefaultVoid;
   1.218 +#endif
   1.219 +
   1.220 +// Translate from 'DefaultVoid' to 'void'.
   1.221 +// Everything else is unchanged
   1.222 +template <class T>
   1.223 +struct DefaultVoidToVoid { typedef T type; };
   1.224 +
   1.225 +template <>
   1.226 +struct DefaultVoidToVoid<DefaultVoid> {	typedef void type; };
   1.227 +
   1.228 +// Translate from 'void' into 'DefaultVoid'
   1.229 +// Everything else is unchanged
   1.230 +template <class T>
   1.231 +struct VoidToDefaultVoid { typedef T type; };
   1.232 +
   1.233 +template <>
   1.234 +struct VoidToDefaultVoid<void> { typedef DefaultVoid type; };
   1.235 +
   1.236 +
   1.237 +
   1.238 +////////////////////////////////////////////////////////////////////////////////
   1.239 +//						Fast Delegates, part 1:
   1.240 +//
   1.241 +//		Conversion of member function pointer to a standard form
   1.242 +//
   1.243 +////////////////////////////////////////////////////////////////////////////////
   1.244 +
   1.245 +// GenericClass is a fake class, ONLY used to provide a type.
   1.246 +// It is vitally important that it is never defined, so that the compiler doesn't
   1.247 +// think it can optimize the invocation. For example, Borland generates simpler
   1.248 +// code if it knows the class only uses single inheritance.
   1.249 +
   1.250 +// Compilers using Microsoft's structure need to be treated as a special case.
   1.251 +#ifdef  FASTDLGT_MICROSOFT_MFP
   1.252 +
   1.253 +#ifdef FASTDLGT_HASINHERITANCE_KEYWORDS
   1.254 +	// For Microsoft and Intel, we want to ensure that it's the most efficient type of MFP 
   1.255 +	// (4 bytes), even when the /vmg option is used. Declaring an empty class 
   1.256 +	// would give 16 byte pointers in this case....
   1.257 +	class __single_inheritance GenericClass;
   1.258 +#endif
   1.259 +	// ...but for Codeplay, an empty class *always* gives 4 byte pointers.
   1.260 +	// If compiled with the /clr option ("managed C++"), the JIT compiler thinks
   1.261 +	// it needs to load GenericClass before it can call any of its functions,
   1.262 +	// (compiles OK but crashes at runtime!), so we need to declare an 
   1.263 +	// empty class to make it happy.
   1.264 +	// Codeplay and VC4 can't cope with the unknown_inheritance case either.
   1.265 +	class GenericClass {};
   1.266 +#else
   1.267 +	class GenericClass;
   1.268 +#endif
   1.269 +
   1.270 +// The size of a single inheritance member function pointer.
   1.271 +const int SINGLE_MEMFUNCPTR_SIZE = sizeof(void (GenericClass::*)());
   1.272 +
   1.273 +//						SimplifyMemFunc< >::Convert()
   1.274 +//
   1.275 +//	A template function that converts an arbitrary member function pointer into the 
   1.276 +//	simplest possible form of member function pointer, using a supplied 'this' pointer.
   1.277 +//  According to the standard, this can be done legally with reinterpret_cast<>.
   1.278 +//	For (non-standard) compilers which use member function pointers which vary in size 
   1.279 +//  depending on the class, we need to use	knowledge of the internal structure of a 
   1.280 +//  member function pointer, as used by the compiler. Template specialization is used
   1.281 +//  to distinguish between the sizes. Because some compilers don't support partial 
   1.282 +//	template specialisation, I use full specialisation of a wrapper struct.
   1.283 +
   1.284 +// general case -- don't know how to convert it. Force a compile failure
   1.285 +template <int N>
   1.286 +struct SimplifyMemFunc {
   1.287 +	template <class X, class XFuncType, class GenericMemFuncType>
   1.288 +	inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind, 
   1.289 +		GenericMemFuncType &bound_func) { 
   1.290 +		// Unsupported member function type -- force a compile failure.
   1.291 +	    // (it's illegal to have a array with negative size).
   1.292 +		typedef char ERROR_Unsupported_member_function_pointer_on_this_compiler[N-100];
   1.293 +		return 0; 
   1.294 +	}
   1.295 +};
   1.296 +
   1.297 +// For compilers where all member func ptrs are the same size, everything goes here.
   1.298 +// For non-standard compilers, only single_inheritance classes go here.
   1.299 +template <>
   1.300 +struct SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE>  {	
   1.301 +	template <class X, class XFuncType, class GenericMemFuncType>
   1.302 +	inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind, 
   1.303 +			GenericMemFuncType &bound_func) {
   1.304 +#if defined __DMC__  
   1.305 +		// Digital Mars doesn't allow you to cast between abitrary PMF's, 
   1.306 +		// even though the standard says you can. The 32-bit compiler lets you
   1.307 +		// static_cast through an int, but the DOS compiler doesn't.
   1.308 +		bound_func = horrible_cast<GenericMemFuncType>(function_to_bind);
   1.309 +#else 
   1.310 +        bound_func = reinterpret_cast<GenericMemFuncType>(function_to_bind);
   1.311 +#endif
   1.312 +        return reinterpret_cast<GenericClass *>(pthis);
   1.313 +	}
   1.314 +};
   1.315 +
   1.316 +////////////////////////////////////////////////////////////////////////////////
   1.317 +//						Fast Delegates, part 1b:
   1.318 +//
   1.319 +//					Workarounds for Microsoft and Intel
   1.320 +//
   1.321 +////////////////////////////////////////////////////////////////////////////////
   1.322 +
   1.323 +
   1.324 +// Compilers with member function pointers which violate the standard (MSVC, Intel, Codeplay),
   1.325 +// need to be treated as a special case.
   1.326 +#ifdef FASTDLGT_MICROSOFT_MFP
   1.327 +
   1.328 +// We use unions to perform horrible_casts. I would like to use #pragma pack(push, 1)
   1.329 +// at the start of each function for extra safety, but VC6 seems to ICE
   1.330 +// intermittently if you do this inside a template.
   1.331 +
   1.332 +// __multiple_inheritance classes go here
   1.333 +// Nasty hack for Microsoft and Intel (IA32 and Itanium)
   1.334 +template<>
   1.335 +struct SimplifyMemFunc< SINGLE_MEMFUNCPTR_SIZE + sizeof(int) >  {
   1.336 +	template <class X, class XFuncType, class GenericMemFuncType>
   1.337 +	inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind, 
   1.338 +		GenericMemFuncType &bound_func) { 
   1.339 +		// We need to use a horrible_cast to do this conversion.
   1.340 +		// In MSVC, a multiple inheritance member pointer is internally defined as:
   1.341 +        union {
   1.342 +			XFuncType func;
   1.343 +			struct {	 
   1.344 +				GenericMemFuncType funcaddress; // points to the actual member function
   1.345 +				int delta;	     // #BYTES to be added to the 'this' pointer
   1.346 +			}s;
   1.347 +        } u;
   1.348 +		// Check that the horrible_cast will work
   1.349 +		typedef int ERROR_CantUsehorrible_cast[sizeof(function_to_bind)==sizeof(u.s)? 1 : -1];
   1.350 +        u.func = function_to_bind;
   1.351 +		bound_func = u.s.funcaddress;
   1.352 +		return reinterpret_cast<GenericClass *>(reinterpret_cast<char *>(pthis) + u.s.delta); 
   1.353 +	}
   1.354 +};
   1.355 +
   1.356 +// virtual inheritance is a real nuisance. It's inefficient and complicated.
   1.357 +// On MSVC and Intel, there isn't enough information in the pointer itself to
   1.358 +// enable conversion to a closure pointer. Earlier versions of this code didn't
   1.359 +// work for all cases, and generated a compile-time error instead.
   1.360 +// But a very clever hack invented by John M. Dlugosz solves this problem.
   1.361 +// My code is somewhat different to his: I have no asm code, and I make no 
   1.362 +// assumptions about the calling convention that is used.
   1.363 +
   1.364 +// In VC++ and ICL, a virtual_inheritance member pointer 
   1.365 +// is internally defined as:
   1.366 +struct MicrosoftVirtualMFP {
   1.367 +	void (GenericClass::*codeptr)(); // points to the actual member function
   1.368 +	int delta;		// #bytes to be added to the 'this' pointer
   1.369 +	int vtable_index; // or 0 if no virtual inheritance
   1.370 +};
   1.371 +// The CRUCIAL feature of Microsoft/Intel MFPs which we exploit is that the
   1.372 +// m_codeptr member is *always* called, regardless of the values of the other
   1.373 +// members. (This is *not* true for other compilers, eg GCC, which obtain the
   1.374 +// function address from the vtable if a virtual function is being called).
   1.375 +// Dlugosz's trick is to make the codeptr point to a probe function which
   1.376 +// returns the 'this' pointer that was used.
   1.377 +
   1.378 +// Define a generic class that uses virtual inheritance.
   1.379 +// It has a trival member function that returns the value of the 'this' pointer.
   1.380 +struct GenericVirtualClass : virtual public GenericClass
   1.381 +{
   1.382 +	typedef GenericVirtualClass * (GenericVirtualClass::*ProbePtrType)();
   1.383 +	GenericVirtualClass * GetThis() { return this; }
   1.384 +};
   1.385 +
   1.386 +// __virtual_inheritance classes go here
   1.387 +template <>
   1.388 +struct SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE + 2*sizeof(int) >
   1.389 +{
   1.390 +
   1.391 +	template <class X, class XFuncType, class GenericMemFuncType>
   1.392 +	inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind, 
   1.393 +		GenericMemFuncType &bound_func) {
   1.394 +		union {
   1.395 +			XFuncType func;
   1.396 +			GenericClass* (X::*ProbeFunc)();
   1.397 +			MicrosoftVirtualMFP s;
   1.398 +		} u;
   1.399 +		u.func = function_to_bind;
   1.400 +		bound_func = reinterpret_cast<GenericMemFuncType>(u.s.codeptr);
   1.401 +		union {
   1.402 +			GenericVirtualClass::ProbePtrType virtfunc;
   1.403 +			MicrosoftVirtualMFP s;
   1.404 +		} u2;
   1.405 +		// Check that the horrible_cast<>s will work
   1.406 +		typedef int ERROR_CantUsehorrible_cast[sizeof(function_to_bind)==sizeof(u.s)
   1.407 +			&& sizeof(function_to_bind)==sizeof(u.ProbeFunc)
   1.408 +			&& sizeof(u2.virtfunc)==sizeof(u2.s) ? 1 : -1];
   1.409 +   // Unfortunately, taking the address of a MF prevents it from being inlined, so 
   1.410 +   // this next line can't be completely optimised away by the compiler.
   1.411 +		u2.virtfunc = &GenericVirtualClass::GetThis;
   1.412 +		u.s.codeptr = u2.s.codeptr;
   1.413 +		return (pthis->*u.ProbeFunc)();
   1.414 +	}
   1.415 +};
   1.416 +
   1.417 +#if (_MSC_VER <1300)
   1.418 +
   1.419 +// Nasty hack for Microsoft Visual C++ 6.0
   1.420 +// unknown_inheritance classes go here
   1.421 +// There is a compiler bug in MSVC6 which generates incorrect code in this case!!
   1.422 +template <>
   1.423 +struct SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE + 3*sizeof(int) >
   1.424 +{
   1.425 +	template <class X, class XFuncType, class GenericMemFuncType>
   1.426 +	inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind, 
   1.427 +		GenericMemFuncType &bound_func) {
   1.428 +		// There is an apalling but obscure compiler bug in MSVC6 and earlier:
   1.429 +		// vtable_index and 'vtordisp' are always set to 0 in the 
   1.430 +		// unknown_inheritance case!
   1.431 +		// This means that an incorrect function could be called!!!
   1.432 +		// Compiling with the /vmg option leads to potentially incorrect code.
   1.433 +		// This is probably the reason that the IDE has a user interface for specifying
   1.434 +		// the /vmg option, but it is disabled -  you can only specify /vmg on 
   1.435 +		// the command line. In VC1.5 and earlier, the compiler would ICE if it ever
   1.436 +		// encountered this situation.
   1.437 +		// It is OK to use the /vmg option if /vmm or /vms is specified.
   1.438 +
   1.439 +		// Fortunately, the wrong function is only called in very obscure cases.
   1.440 +		// It only occurs when a derived class overrides a virtual function declared 
   1.441 +		// in a virtual base class, and the member function 
   1.442 +		// points to the *Derived* version of that function. The problem can be
   1.443 +		// completely averted in 100% of cases by using the *Base class* for the 
   1.444 +		// member fpointer. Ie, if you use the base class as an interface, you'll
   1.445 +		// stay out of trouble.
   1.446 +		// Occasionally, you might want to point directly to a derived class function
   1.447 +		// that isn't an override of a base class. In this case, both vtable_index 
   1.448 +		// and 'vtordisp' are zero, but a virtual_inheritance pointer will be generated.
   1.449 +		// We can generate correct code in this case. To prevent an incorrect call from
   1.450 +		// ever being made, on MSVC6 we generate a warning, and call a function to 
   1.451 +		// make the program crash instantly. 
   1.452 +		typedef char ERROR_VC6CompilerBug[-100];
   1.453 +		return 0; 
   1.454 +	}
   1.455 +};
   1.456 +
   1.457 +
   1.458 +#else 
   1.459 +
   1.460 +// Nasty hack for Microsoft and Intel (IA32 and Itanium)
   1.461 +// unknown_inheritance classes go here 
   1.462 +// This is probably the ugliest bit of code I've ever written. Look at the casts!
   1.463 +// There is a compiler bug in MSVC6 which prevents it from using this code.
   1.464 +template <>
   1.465 +struct SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE + 3*sizeof(int) >
   1.466 +{
   1.467 +	template <class X, class XFuncType, class GenericMemFuncType>
   1.468 +	inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind, 
   1.469 +			GenericMemFuncType &bound_func) {
   1.470 +		// The member function pointer is 16 bytes long. We can't use a normal cast, but
   1.471 +		// we can use a union to do the conversion.
   1.472 +		union {
   1.473 +			XFuncType func;
   1.474 +			// In VC++ and ICL, an unknown_inheritance member pointer 
   1.475 +			// is internally defined as:
   1.476 +			struct {
   1.477 +				GenericMemFuncType m_funcaddress; // points to the actual member function
   1.478 +				int delta;		// #bytes to be added to the 'this' pointer
   1.479 +				int vtordisp;		// #bytes to add to 'this' to find the vtable
   1.480 +				int vtable_index; // or 0 if no virtual inheritance
   1.481 +			} s;
   1.482 +		} u;
   1.483 +		// Check that the horrible_cast will work
   1.484 +		typedef int ERROR_CantUsehorrible_cast[sizeof(XFuncType)==sizeof(u.s)? 1 : -1];
   1.485 +		u.func = function_to_bind;
   1.486 +		bound_func = u.s.funcaddress;
   1.487 +		int virtual_delta = 0;
   1.488 +		if (u.s.vtable_index) { // Virtual inheritance is used
   1.489 +			// First, get to the vtable. 
   1.490 +			// It is 'vtordisp' bytes from the start of the class.
   1.491 +			const int * vtable = *reinterpret_cast<const int *const*>(
   1.492 +				reinterpret_cast<const char *>(pthis) + u.s.vtordisp );
   1.493 +
   1.494 +			// 'vtable_index' tells us where in the table we should be looking.
   1.495 +			virtual_delta = u.s.vtordisp + *reinterpret_cast<const int *>( 
   1.496 +				reinterpret_cast<const char *>(vtable) + u.s.vtable_index);
   1.497 +		}
   1.498 +		// The int at 'virtual_delta' gives us the amount to add to 'this'.
   1.499 +        // Finally we can add the three components together. Phew!
   1.500 +        return reinterpret_cast<GenericClass *>(
   1.501 +			reinterpret_cast<char *>(pthis) + u.s.delta + virtual_delta);
   1.502 +	};
   1.503 +};
   1.504 +#endif // MSVC 7 and greater
   1.505 +
   1.506 +#endif // MS/Intel hacks
   1.507 +
   1.508 +}  // namespace detail
   1.509 +
   1.510 +////////////////////////////////////////////////////////////////////////////////
   1.511 +//						Fast Delegates, part 2:
   1.512 +//
   1.513 +//	Define the delegate storage, and cope with static functions
   1.514 +//
   1.515 +////////////////////////////////////////////////////////////////////////////////
   1.516 +
   1.517 +// DelegateMemento -- an opaque structure which can hold an arbitary delegate.
   1.518 +// It knows nothing about the calling convention or number of arguments used by
   1.519 +// the function pointed to.
   1.520 +// It supplies comparison operators so that it can be stored in STL collections.
   1.521 +// It cannot be set to anything other than null, nor invoked directly: 
   1.522 +//   it must be converted to a specific delegate.
   1.523 +
   1.524 +// Implementation:
   1.525 +// There are two possible implementations: the Safe method and the Evil method.
   1.526 +//				DelegateMemento - Safe version
   1.527 +//
   1.528 +// This implementation is standard-compliant, but a bit tricky.
   1.529 +// A static function pointer is stored inside the class. 
   1.530 +// Here are the valid values:
   1.531 +// +-- Static pointer --+--pThis --+-- pMemFunc-+-- Meaning------+
   1.532 +// |   0				|  0       |   0        | Empty          |
   1.533 +// |   !=0              |(dontcare)|  Invoker   | Static function|
   1.534 +// |   0                |  !=0     |  !=0       | Method call    |
   1.535 +// +--------------------+----------+------------+----------------+
   1.536 +// When stored stored inside a specific delegate, the 'dontcare' entries are replaced
   1.537 +// with a reference to the delegate itself. This complicates the = and == operators
   1.538 +// for the delegate class.
   1.539 +
   1.540 +//				DelegateMemento - Evil version
   1.541 +//
   1.542 +// For compilers where data pointers are at least as big as code pointers, it is 
   1.543 +// possible to store the function pointer in the this pointer, using another 
   1.544 +// horrible_cast. In this case the DelegateMemento implementation is simple:
   1.545 +// +--pThis --+-- pMemFunc-+-- Meaning---------------------+
   1.546 +// |    0     |  0         | Empty                         |
   1.547 +// |  !=0     |  !=0       | Static function or method call|
   1.548 +// +----------+------------+-------------------------------+
   1.549 +// Note that the Sun C++ and MSVC documentation explicitly state that they 
   1.550 +// support static_cast between void * and function pointers.
   1.551 +
   1.552 +class DelegateMemento {
   1.553 +protected: 
   1.554 +	// the data is protected, not private, because many
   1.555 +	// compilers have problems with template friends.
   1.556 +	typedef void (detail::GenericClass::*GenericMemFuncType)(); // arbitrary MFP.
   1.557 +	detail::GenericClass *m_pthis;
   1.558 +	GenericMemFuncType m_pFunction;
   1.559 +
   1.560 +#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
   1.561 +	typedef void (*GenericFuncPtr)(); // arbitrary code pointer
   1.562 +	GenericFuncPtr m_pStaticFunction;
   1.563 +#endif
   1.564 +
   1.565 +public:
   1.566 +#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
   1.567 +	DelegateMemento() : m_pthis(0), m_pFunction(0), m_pStaticFunction(0) {};
   1.568 +	void clear() {
   1.569 +		m_pthis=0; m_pFunction=0; m_pStaticFunction=0;
   1.570 +	}
   1.571 +#else
   1.572 +	DelegateMemento() : m_pthis(0), m_pFunction(0) {};
   1.573 +	void clear() {
   1.574 +		m_pthis=0; m_pFunction=0;
   1.575 +	}
   1.576 +#endif
   1.577 +public:
   1.578 +#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
   1.579 +	inline bool IsEqual (const DelegateMemento &x) const{
   1.580 +	    // We have to cope with the static function pointers as a special case
   1.581 +		if (m_pFunction!=x.m_pFunction) return false;
   1.582 +		// the static function ptrs must either both be equal, or both be 0.
   1.583 +		if (m_pStaticFunction!=x.m_pStaticFunction) return false;
   1.584 +		if (m_pStaticFunction!=0) return m_pthis==x.m_pthis;
   1.585 +		else return true;
   1.586 +	}
   1.587 +#else // Evil Method
   1.588 +	inline bool IsEqual (const DelegateMemento &x) const{
   1.589 +		return m_pthis==x.m_pthis && m_pFunction==x.m_pFunction;
   1.590 +	}
   1.591 +#endif
   1.592 +	// Provide a strict weak ordering for DelegateMementos.
   1.593 +	inline bool IsLess(const DelegateMemento &right) const {
   1.594 +		// deal with static function pointers first
   1.595 +#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
   1.596 +		if (m_pStaticFunction !=0 || right.m_pStaticFunction!=0) 
   1.597 +				return m_pStaticFunction < right.m_pStaticFunction;
   1.598 +#endif
   1.599 +		if (m_pthis !=right.m_pthis) return m_pthis < right.m_pthis;
   1.600 +	// There are no ordering operators for member function pointers, 
   1.601 +	// but we can fake one by comparing each byte. The resulting ordering is
   1.602 +	// arbitrary (and compiler-dependent), but it permits storage in ordered STL containers.
   1.603 +		return memcmp(&m_pFunction, &right.m_pFunction, sizeof(m_pFunction)) < 0;
   1.604 +
   1.605 +	}
   1.606 +	inline bool operator ! () const		// Is it bound to anything?
   1.607 +	{ return m_pFunction==0; }
   1.608 +public:
   1.609 +	DelegateMemento & operator = (const DelegateMemento &right)  {
   1.610 +		SetMementoFrom(right); 
   1.611 +		return *this;
   1.612 +	}
   1.613 +	inline bool operator <(const DelegateMemento &right) {
   1.614 +		return IsLess(right);
   1.615 +	}
   1.616 +	inline bool operator >(const DelegateMemento &right) {
   1.617 +		return right.IsLess(*this);
   1.618 +	}
   1.619 +	DelegateMemento (const DelegateMemento &right)  : 
   1.620 +		m_pFunction(right.m_pFunction), m_pthis(right.m_pthis)
   1.621 +#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
   1.622 +		, m_pStaticFunction (right.m_pStaticFunction)
   1.623 +#endif
   1.624 +		{}
   1.625 +protected:
   1.626 +	void SetMementoFrom(const DelegateMemento &right)  {
   1.627 +		m_pFunction = right.m_pFunction;
   1.628 +		m_pthis = right.m_pthis;
   1.629 +#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
   1.630 +		m_pStaticFunction = right.m_pStaticFunction;
   1.631 +#endif
   1.632 +	}
   1.633 +};
   1.634 +
   1.635 +
   1.636 +//						ClosurePtr<>
   1.637 +//
   1.638 +// A private wrapper class that adds function signatures to DelegateMemento.
   1.639 +// It's the class that does most of the actual work.
   1.640 +// The signatures are specified by:
   1.641 +// GenericMemFunc: must be a type of GenericClass member function pointer. 
   1.642 +// StaticFuncPtr:  must be a type of function pointer with the same signature 
   1.643 +//                 as GenericMemFunc.
   1.644 +// UnvoidStaticFuncPtr: is the same as StaticFuncPtr, except on VC6
   1.645 +//                 where it never returns void (returns DefaultVoid instead).
   1.646 +
   1.647 +// An outer class, FastDelegateN<>, handles the invoking and creates the
   1.648 +// necessary typedefs.
   1.649 +// This class does everything else.
   1.650 +
   1.651 +namespace detail {
   1.652 +
   1.653 +template < class GenericMemFunc, class StaticFuncPtr, class UnvoidStaticFuncPtr>
   1.654 +class ClosurePtr : public DelegateMemento {
   1.655 +public:
   1.656 +	// These functions are for setting the delegate to a member function.
   1.657 +
   1.658 +	// Here's the clever bit: we convert an arbitrary member function into a 
   1.659 +	// standard form. XMemFunc should be a member function of class X, but I can't 
   1.660 +	// enforce that here. It needs to be enforced by the wrapper class.
   1.661 +	template < class X, class XMemFunc >
   1.662 +	inline void bindmemfunc(X *pthis, XMemFunc function_to_bind ) {
   1.663 +		m_pthis = SimplifyMemFunc< sizeof(function_to_bind) >
   1.664 +			::Convert(pthis, function_to_bind, m_pFunction);
   1.665 +#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
   1.666 +		m_pStaticFunction = 0;
   1.667 +#endif
   1.668 +	}
   1.669 +	// For const member functions, we only need a const class pointer.
   1.670 +	// Since we know that the member function is const, it's safe to 
   1.671 +	// remove the const qualifier from the 'this' pointer with a const_cast.
   1.672 +	// VC6 has problems if we just overload 'bindmemfunc', so we give it a different name.
   1.673 +	template < class X, class XMemFunc>
   1.674 +	inline void bindconstmemfunc(const X *pthis, XMemFunc function_to_bind) {
   1.675 +		m_pthis= SimplifyMemFunc< sizeof(function_to_bind) >
   1.676 +			::Convert(const_cast<X*>(pthis), function_to_bind, m_pFunction);
   1.677 +#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
   1.678 +		m_pStaticFunction = 0;
   1.679 +#endif
   1.680 +	}
   1.681 +#ifdef FASTDELEGATE_GCC_BUG_8271	// At present, GCC doesn't recognize constness of MFPs in templates
   1.682 +	template < class X, class XMemFunc>
   1.683 +	inline void bindmemfunc(const X *pthis, XMemFunc function_to_bind) {
   1.684 +		bindconstmemfunc(pthis, function_to_bind);
   1.685 +#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
   1.686 +		m_pStaticFunction = 0;
   1.687 +#endif
   1.688 +	}
   1.689 +#endif
   1.690 +	// These functions are required for invoking the stored function
   1.691 +	inline GenericClass *GetClosureThis() const { return m_pthis; }
   1.692 +	inline GenericMemFunc GetClosureMemPtr() const { return reinterpret_cast<GenericMemFunc>(m_pFunction); }
   1.693 +
   1.694 +// There are a few ways of dealing with static function pointers.
   1.695 +// There's a standard-compliant, but tricky method.
   1.696 +// There's also a straightforward hack, that won't work on DOS compilers using the
   1.697 +// medium memory model. It's so evil that I can't recommend it, but I've
   1.698 +// implemented it anyway because it produces very nice asm code.
   1.699 +
   1.700 +#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
   1.701 +
   1.702 +//				ClosurePtr<> - Safe version
   1.703 +//
   1.704 +// This implementation is standard-compliant, but a bit tricky.
   1.705 +// I store the function pointer inside the class, and the delegate then
   1.706 +// points to itself. Whenever the delegate is copied, these self-references
   1.707 +// must be transformed, and this complicates the = and == operators.
   1.708 +public:
   1.709 +	// The next two functions are for operator ==, =, and the copy constructor.
   1.710 +	// We may need to convert the m_pthis pointers, so that
   1.711 +	// they remain as self-references.
   1.712 +	template< class DerivedClass >
   1.713 +	inline void CopyFrom (DerivedClass *pParent, const DelegateMemento &x) {
   1.714 +		SetMementoFrom(x);
   1.715 +		if (m_pStaticFunction!=0) {
   1.716 +			// transform self references...
   1.717 +			m_pthis=reinterpret_cast<GenericClass *>(pParent);
   1.718 +		}
   1.719 +	}
   1.720 +	// For static functions, the 'static_function_invoker' class in the parent 
   1.721 +	// will be called. The parent then needs to call GetStaticFunction() to find out 
   1.722 +	// the actual function to invoke.
   1.723 +	template < class DerivedClass, class ParentInvokerSig >
   1.724 +	inline void bindstaticfunc(DerivedClass *pParent, ParentInvokerSig static_function_invoker, 
   1.725 +				StaticFuncPtr function_to_bind ) {
   1.726 +		bindmemfunc(pParent, static_function_invoker);
   1.727 +		m_pStaticFunction=reinterpret_cast<GenericFuncPtr>(function_to_bind);
   1.728 +	}
   1.729 +	inline UnvoidStaticFuncPtr GetStaticFunction() const { 
   1.730 +		return reinterpret_cast<UnvoidStaticFuncPtr>(m_pStaticFunction); 
   1.731 +	}
   1.732 +#else
   1.733 +
   1.734 +//				ClosurePtr<> - Evil version
   1.735 +//
   1.736 +// For compilers where data pointers are at least as big as code pointers, it is 
   1.737 +// possible to store the function pointer in the this pointer, using another 
   1.738 +// horrible_cast. Invocation isn't any faster, but it saves 4 bytes, and
   1.739 +// speeds up comparison and assignment. If C++ provided direct language support
   1.740 +// for delegates, they would produce asm code that was almost identical to this.
   1.741 +// Note that the Sun C++ and MSVC documentation explicitly state that they 
   1.742 +// support static_cast between void * and function pointers.
   1.743 +
   1.744 +	template< class DerivedClass >
   1.745 +	inline void CopyFrom (DerivedClass *pParent, const DelegateMemento &right) {
   1.746 +		SetMementoFrom(right);
   1.747 +	}
   1.748 +	// For static functions, the 'static_function_invoker' class in the parent 
   1.749 +	// will be called. The parent then needs to call GetStaticFunction() to find out 
   1.750 +	// the actual function to invoke.
   1.751 +	// ******** EVIL, EVIL CODE! *******
   1.752 +	template < 	class DerivedClass, class ParentInvokerSig>
   1.753 +	inline void bindstaticfunc(DerivedClass *pParent, ParentInvokerSig static_function_invoker, 
   1.754 +				StaticFuncPtr function_to_bind) {
   1.755 +		// We'll be ignoring the 'this' pointer, but we need to make sure we pass
   1.756 +		// a valid value to bindmemfunc().
   1.757 +		bindmemfunc(pParent, static_function_invoker);
   1.758 +
   1.759 +		// WARNING! Evil hack. We store the function in the 'this' pointer!
   1.760 +		// Ensure that there's a compilation failure if function pointers 
   1.761 +		// and data pointers have different sizes.
   1.762 +		// If you get this error, you need to #undef FASTDELEGATE_USESTATICFUNCTIONHACK.
   1.763 +		typedef int ERROR_CantUseEvilMethod[sizeof(GenericClass *)==sizeof(function_to_bind) ? 1 : -1];
   1.764 +		m_pthis = horrible_cast<GenericClass *>(function_to_bind);
   1.765 +		// MSVC, SunC++ and DMC accept the following (non-standard) code:
   1.766 +//		m_pthis = static_cast<GenericClass *>(static_cast<void *>(function_to_bind));
   1.767 +		// BCC32, Comeau and DMC accept this method. MSVC7.1 needs __int64 instead of long
   1.768 +//		m_pthis = reinterpret_cast<GenericClass *>(reinterpret_cast<long>(function_to_bind));
   1.769 +	}
   1.770 +	// ******** EVIL, EVIL CODE! *******
   1.771 +	// This function will be called with an invalid 'this' pointer!!
   1.772 +	// We're just returning the 'this' pointer, converted into
   1.773 +	// a function pointer!
   1.774 +	inline UnvoidStaticFuncPtr GetStaticFunction() const {
   1.775 +		// Ensure that there's a compilation failure if function pointers 
   1.776 +		// and data pointers have different sizes.
   1.777 +		// If you get this error, you need to #undef FASTDELEGATE_USESTATICFUNCTIONHACK.
   1.778 +		typedef int ERROR_CantUseEvilMethod[sizeof(UnvoidStaticFuncPtr)==sizeof(this) ? 1 : -1];
   1.779 +		return horrible_cast<UnvoidStaticFuncPtr>(this);
   1.780 +	}
   1.781 +#endif // !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
   1.782 +
   1.783 +};
   1.784 +
   1.785 +
   1.786 +} // namespace detail
   1.787 +
   1.788 +////////////////////////////////////////////////////////////////////////////////
   1.789 +//						Fast Delegates, part 3:
   1.790 +//
   1.791 +//				Wrapper classes to ensure type safety
   1.792 +//
   1.793 +////////////////////////////////////////////////////////////////////////////////
   1.794 +
   1.795 +
   1.796 +// Once we have the member function conversion templates, it's easy to make the
   1.797 +// wrapper classes. So that they will work with as many compilers as possible, 
   1.798 +// the classes are of the form
   1.799 +//   FastDelegate3<int, char *, double>
   1.800 +// They can cope with any combination of parameters. The max number of parameters
   1.801 +// allowed is 8, but it is trivial to increase this limit.
   1.802 +// Note that we need to treat const member functions seperately.
   1.803 +// All this class does is to enforce type safety, and invoke the delegate with
   1.804 +// the correct list of parameters.
   1.805 +
   1.806 +// Because of the weird rule about the class of derived member function pointers,
   1.807 +// you sometimes need to apply a downcast to the 'this' pointer.
   1.808 +// This is the reason for the use of "implicit_cast<X*>(pthis)" in the code below. 
   1.809 +// If CDerivedClass is derived from CBaseClass, but doesn't override SimpleVirtualFunction,
   1.810 +// without this trick you'd need to write:
   1.811 +//		MyDelegate(static_cast<CBaseClass *>(&d), &CDerivedClass::SimpleVirtualFunction);
   1.812 +// but with the trick you can write
   1.813 +//		MyDelegate(&d, &CDerivedClass::SimpleVirtualFunction);
   1.814 +
   1.815 +// RetType is the type the compiler uses in compiling the template. For VC6,
   1.816 +// it cannot be void. DesiredRetType is the real type which is returned from
   1.817 +// all of the functions. It can be void.
   1.818 +
   1.819 +@VARARGS
   1.820 +template<@CLASSARGS, class RetType=detail::DefaultVoid>
   1.821 +class FastDelegate@NUM {
   1.822 +private:
   1.823 +	typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
   1.824 +	typedef DesiredRetType (*StaticFunctionPtr)(@FUNCARGS);
   1.825 +	typedef RetType (*UnvoidStaticFunctionPtr)(@FUNCARGS);
   1.826 +	typedef RetType (detail::GenericClass::*GenericMemFn)(@FUNCARGS);
   1.827 +	detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> m_Closure;
   1.828 +public:
   1.829 +	// Typedefs to aid generic programming
   1.830 +	typedef FastDelegate@NUM type;
   1.831 +
   1.832 +	// Construction and comparison functions
   1.833 +	FastDelegate@NUM() { clear(); }
   1.834 +	FastDelegate@NUM(const FastDelegate@NUM &x) {
   1.835 +		m_Closure.CopyFrom(this, x.m_Closure); }
   1.836 +	void operator = (const FastDelegate@NUM &x)  {
   1.837 +		m_Closure.CopyFrom(this, x.m_Closure); }
   1.838 +	bool operator ==(const FastDelegate@NUM &x) const {
   1.839 +		return m_Closure.IsEqual(x.m_Closure);	}
   1.840 +	bool operator !=(const FastDelegate@NUM &x) const {
   1.841 +		return !m_Closure.IsEqual(x.m_Closure); }
   1.842 +	bool operator <(const FastDelegate@NUM &x) const {
   1.843 +		return m_Closure.IsLess(x.m_Closure);	}
   1.844 +	bool operator >(const FastDelegate@NUM &x) const {
   1.845 +		return x.m_Closure.IsLess(m_Closure);	}
   1.846 +	// Binding to non-const member functions
   1.847 +	template < class X, class Y >
   1.848 +	FastDelegate@NUM(Y *pthis, DesiredRetType (X::* function_to_bind)(@FUNCARGS) ) {
   1.849 +		m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
   1.850 +	template < class X, class Y >
   1.851 +	inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(@FUNCARGS)) {
   1.852 +		m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind);	}
   1.853 +	// Binding to const member functions.
   1.854 +	template < class X, class Y >
   1.855 +	FastDelegate@NUM(const Y *pthis, DesiredRetType (X::* function_to_bind)(@FUNCARGS) const) {
   1.856 +		m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind);	}
   1.857 +	template < class X, class Y >
   1.858 +	inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(@FUNCARGS) const) {
   1.859 +		m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind);	}
   1.860 +	// Static functions. We convert them into a member function call.
   1.861 +	// Note that this also provides a conversion from static functions.
   1.862 +	FastDelegate@NUM(DesiredRetType (*function_to_bind)(@FUNCARGS) ) {
   1.863 +		bind(function_to_bind);	}
   1.864 +	inline void bind(DesiredRetType (*function_to_bind)(@FUNCARGS)) {
   1.865 +		m_Closure.bindstaticfunc(this, &FastDelegate@NUM::InvokeStaticFunction, 
   1.866 +			function_to_bind); }
   1.867 +	RetType operator() (@FUNCARGS) const { // Invoke the delegate
   1.868 +		// this next line is the only one that violates the standard
   1.869 +	return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(@INVOKEARGS); }
   1.870 +	inline bool operator ! () const	{	// Is it bound to anything?
   1.871 +			return !m_Closure; }
   1.872 +	inline bool empty() const	{
   1.873 +			return !m_Closure; }
   1.874 +	void clear() { m_Closure.clear();}
   1.875 +	// Conversion to and from the DelegateMemento storage class
   1.876 +	const DelegateMemento & GetMemento() { return m_Closure; }
   1.877 +	void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); }
   1.878 +
   1.879 +private:	// Invoker for static functions
   1.880 +	RetType InvokeStaticFunction(@FUNCARGS) const {
   1.881 +	return (*(m_Closure.GetStaticFunction()))(@INVOKEARGS); }
   1.882 +};
   1.883 +
   1.884 +@ENDVAR
   1.885 +
   1.886 +////////////////////////////////////////////////////////////////////////////////
   1.887 +//						Fast Delegates, part 4:
   1.888 +// 
   1.889 +//				FastDelegate<> class (Original author: Jody Hagins)
   1.890 +//	Allows boost::function style syntax like:
   1.891 +//			FastDelegate< double (int, long) >
   1.892 +// instead of:
   1.893 +//			FastDelegate2< int, long, double >
   1.894 +//
   1.895 +////////////////////////////////////////////////////////////////////////////////
   1.896 +
   1.897 +#ifdef FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
   1.898 +
   1.899 +// Declare FastDelegate as a class template.  It will be specialized
   1.900 +// later for all number of arguments.
   1.901 +template <typename Signature>
   1.902 +class FastDelegate;
   1.903 +
   1.904 +@VARARGS
   1.905 +// Specialization to allow use of
   1.906 +// FastDelegate< R ( @SELARGS ) >
   1.907 +// instead of
   1.908 +// FastDelegate@NUM < @SELARGS, R >
   1.909 +template<typename R, @CLASSARGS>
   1.910 +class FastDelegate< R ( @SELARGS ) >
   1.911 +  // Inherit from FastDelegate@NUM so that it can be treated just
   1.912 +  // like a FastDelegate@NUM
   1.913 +  : public FastDelegate@NUM < @SELARGS, R >
   1.914 +{
   1.915 +public:
   1.916 +  // Make using the base type a bit easier via typedef.
   1.917 +  typedef FastDelegate@NUM < @SELARGS, R > BaseType;
   1.918 +
   1.919 +  // Allow users access to the specific type of this delegate.
   1.920 +  typedef FastDelegate SelfType;
   1.921 +
   1.922 +  // Mimic the base class constructors.
   1.923 +  FastDelegate() : BaseType() { }
   1.924 +
   1.925 +  template < class X, class Y >
   1.926 +  FastDelegate(Y * pthis, 
   1.927 +    R (X::* function_to_bind)( @FUNCARGS ))
   1.928 +    : BaseType(pthis, function_to_bind)  { }
   1.929 +
   1.930 +  template < class X, class Y >
   1.931 +  FastDelegate(const Y *pthis,
   1.932 +      R (X::* function_to_bind)( @FUNCARGS ) const)
   1.933 +    : BaseType(pthis, function_to_bind)
   1.934 +  {  }
   1.935 +
   1.936 +  FastDelegate(R (*function_to_bind)( @FUNCARGS ))
   1.937 +    : BaseType(function_to_bind)  { }
   1.938 +  void operator = (const BaseType &x)  {	  
   1.939 +		*static_cast<BaseType*>(this) = x; }
   1.940 +};
   1.941 +
   1.942 +@ENDVAR
   1.943 +
   1.944 +#endif //FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
   1.945 +
   1.946 +////////////////////////////////////////////////////////////////////////////////
   1.947 +//						Fast Delegates, part 5:
   1.948 +//
   1.949 +//				MakeDelegate() helper function
   1.950 +//
   1.951 +//			MakeDelegate(&x, &X::func) returns a fastdelegate of the type
   1.952 +//			necessary for calling x.func() with the correct number of arguments.
   1.953 +//			This makes it possible to eliminate many typedefs from user code.
   1.954 +//
   1.955 +////////////////////////////////////////////////////////////////////////////////
   1.956 +
   1.957 +// Also declare overloads of a MakeDelegate() global function to 
   1.958 +// reduce the need for typedefs.
   1.959 +// We need seperate overloads for const and non-const member functions.
   1.960 +// Also, because of the weird rule about the class of derived member function pointers,
   1.961 +// implicit downcasts may need to be applied later to the 'this' pointer.
   1.962 +// That's why two classes (X and Y) appear in the definitions. Y must be implicitly
   1.963 +// castable to X.
   1.964 +
   1.965 +// Workaround for VC6. VC6 needs void return types converted into DefaultVoid.
   1.966 +// GCC 3.2 and later won't compile this unless it's preceded by 'typename',
   1.967 +// but VC6 doesn't allow 'typename' in this context.
   1.968 +// So, I have to use a macro.
   1.969 +
   1.970 +#ifdef FASTDLGT_VC6
   1.971 +#define FASTDLGT_RETTYPE detail::VoidToDefaultVoid<RetType>::type
   1.972 +#else 
   1.973 +#define FASTDLGT_RETTYPE RetType
   1.974 +#endif
   1.975 +
   1.976 +@VARARGS
   1.977 +template <class X, class Y, @CLASSARGS, class RetType>
   1.978 +FastDelegate@NUM<@SELARGS, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(@FUNCARGS)) { 
   1.979 +	return FastDelegate@NUM<@SELARGS, FASTDLGT_RETTYPE>(x, func);
   1.980 +}
   1.981 +
   1.982 +template <class X, class Y, @CLASSARGS, class RetType>
   1.983 +FastDelegate@NUM<@SELARGS, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(@FUNCARGS) const) { 
   1.984 +	return FastDelegate@NUM<@SELARGS, FASTDLGT_RETTYPE>(x, func);
   1.985 +}
   1.986 +
   1.987 +@ENDVAR
   1.988 +
   1.989 + // clean up after ourselves...
   1.990 +#undef FASTDLGT_RETTYPE
   1.991 +
   1.992 +} // namespace fastdelegate
   1.993 +
   1.994 +#endif // !defined(FASTDELEGATE_H)
   1.995 +