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