sourcehook/generate/FastDelegate.h
author Pavol Marko <pavolmarko@gmail.com>
Mon Oct 08 14:20:51 2007 +0000 (2007-10-08)
changeset 387 093c72464b35
parent 382 8b06ecc6e76d
child 390 66690bf8af93
permissions -rw-r--r--
Subtle bug fixed + tests now more resistant against compiler
optimizations
     1 //						FastDelegate.h
     2 //	Efficient delegates in C++ that generate only two lines of asm code!
     3 //  Documentation is found at http://www.codeproject.com/cpp/FastDelegate.asp
     4 //
     5 //						- Don Clugston, Mar 2004.
     6 //		Major contributions were made by Jody Hagins.
     7 // History:
     8 // 24-Apr-04 1.0  * Submitted to CodeProject.
     9 // 28-Apr-04 1.1  * Prevent most unsafe uses of evil static function hack.
    10 //				  * Improved syntax for horrible_cast (thanks Paul Bludov).
    11 //				  * Tested on Metrowerks MWCC and Intel ICL (IA32)
    12 //				  * Compiled, but not run, on Comeau C++ and Intel Itanium ICL.
    13 //	27-Jun-04 1.2 * Now works on Borland C++ Builder 5.5
    14 //				  * Now works on /clr "managed C++" code on VC7, VC7.1
    15 //				  * Comeau C++ now compiles without warnings.
    16 //				  * Prevent the virtual inheritance case from being used on
    17 //					  VC6 and earlier, which generate incorrect code.
    18 //				  * Improved warning and error messages. Non-standard hacks
    19 //					 now have compile-time checks to make them safer.
    20 //				  * implicit_cast used instead of static_cast in many cases.
    21 //				  * If calling a const member function, a const class pointer can be used.
    22 //				  * MakeDelegate() global helper function added to simplify pass-by-value.
    23 //				  * Added fastdelegate.clear()
    24 // 16-Jul-04 1.2.1* Workaround for gcc bug (const member function pointers in templates)
    25 // 30-Oct-04 1.3  * Support for (non-void) return values.
    26 //				  * No more workarounds in client code!
    27 //					 MSVC and Intel now use a clever hack invented by John Dlugosz:
    28 //				     - The FASTDELEGATEDECLARE workaround is no longer necessary.
    29 //					 - No more warning messages for VC6
    30 //				  * Less use of macros. Error messages should be more comprehensible.
    31 //				  * Added include guards
    32 //				  * Added FastDelegate::empty() to test if invocation is safe (Thanks Neville Franks).
    33 //				  * Now tested on VS 2005 Express Beta, PGI C++
    34 // 24-Dec-04 1.4  * Added DelegateMemento, to allow collections of disparate delegates.
    35 //                * <,>,<=,>= comparison operators to allow storage in ordered containers.
    36 //				  * Substantial reduction of code size, especially the 'Closure' class.
    37 //				  * Standardised all the compiler-specific workarounds.
    38 //                * MFP conversion now works for CodePlay (but not yet supported in the full code).
    39 //                * Now compiles without warnings on _any_ supported compiler, including BCC 5.5.1
    40 //				  * New syntax: FastDelegate< int (char *, double) >.
    41 // 14-Feb-05 1.4.1* Now treats =0 as equivalent to .clear(), ==0 as equivalent to .empty(). (Thanks elfric).
    42 //				  * Now tested on Intel ICL for AMD64, VS2005 Beta for AMD64 and Itanium.
    43 // 30-Mar-05 1.5  * Safebool idiom: "if (dg)" is now equivalent to "if (!dg.empty())"
    44 //				  * Fully supported by CodePlay VectorC
    45 //                * Bugfix for Metrowerks: empty() was buggy because a valid MFP can be 0 on MWCC!
    46 //                * More optimal assignment,== and != operators for static function pointers.
    47 
    48 #ifndef FASTDELEGATE_H
    49 #define FASTDELEGATE_H
    50 
    51 #ifdef _MSC_VER
    52 # if _MSC_VER > 1000
    53 #  pragma once
    54 # endif // _MSC_VER > 1000
    55 #endif // #ifdef _MSC_VER
    56 
    57 #include <memory.h> // to allow <,> comparisons
    58 
    59 ////////////////////////////////////////////////////////////////////////////////
    60 //						Configuration options
    61 //
    62 ////////////////////////////////////////////////////////////////////////////////
    63 
    64 // Uncomment the following #define for optimally-sized delegates.
    65 // In this case, the generated asm code is almost identical to the code you'd get
    66 // if the compiler had native support for delegates.
    67 // It will not work on systems where sizeof(dataptr) < sizeof(codeptr).
    68 // Thus, it will not work for DOS compilers using the medium model.
    69 // It will also probably fail on some DSP systems.
    70 #define FASTDELEGATE_USESTATICFUNCTIONHACK
    71 
    72 // Uncomment the next line to allow function declarator syntax.
    73 // It is automatically enabled for those compilers where it is known to work.
    74 //#define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
    75 
    76 ////////////////////////////////////////////////////////////////////////////////
    77 //						Compiler identification for workarounds
    78 //
    79 ////////////////////////////////////////////////////////////////////////////////
    80 
    81 // Compiler identification. It's not easy to identify Visual C++ because
    82 // many vendors fraudulently define Microsoft's identifiers.
    83 #if defined(_MSC_VER) && !defined(__MWERKS__) && !defined(__VECTOR_C) && !defined(__ICL) && !defined(__BORLANDC__)
    84 #define FASTDLGT_ISMSVC
    85 
    86 #if (_MSC_VER <1300) // Many workarounds are required for VC6.
    87 #define FASTDLGT_VC6
    88 #pragma warning(disable:4786) // disable this ridiculous warning
    89 #endif
    90 
    91 #endif
    92 
    93 // Does the compiler uses Microsoft's member function pointer structure?
    94 // If so, it needs special treatment.
    95 // Metrowerks CodeWarrior, Intel, and CodePlay fraudulently define Microsoft's
    96 // identifier, _MSC_VER. We need to filter Metrowerks out.
    97 #if defined(_MSC_VER) && !defined(__MWERKS__)
    98 #define FASTDLGT_MICROSOFT_MFP
    99 
   100 #if !defined(__VECTOR_C)
   101 // CodePlay doesn't have the __single/multi/virtual_inheritance keywords
   102 #define FASTDLGT_HASINHERITANCE_KEYWORDS
   103 #endif
   104 #endif
   105 
   106 // Does it allow function declarator syntax? The following compilers are known to work:
   107 #if defined(FASTDLGT_ISMSVC) && (_MSC_VER >=1310) // VC 7.1
   108 #define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
   109 #endif
   110 
   111 // Gcc(2.95+), and versions of Digital Mars, Intel and Comeau in common use.
   112 #if defined (__DMC__) || defined(__GNUC__) || defined(__ICL) || defined(__COMO__)
   113 #define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
   114 #endif
   115 
   116 // It works on Metrowerks MWCC 3.2.2. From boost.Config it should work on earlier ones too.
   117 #if defined (__MWERKS__)
   118 #define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
   119 #endif
   120 
   121 #ifdef __GNUC__ // Workaround GCC bug #8271
   122 	// At present, GCC doesn't recognize constness of MFPs in templates
   123 #define FASTDELEGATE_GCC_BUG_8271
   124 #endif
   125 
   126 
   127 
   128 ////////////////////////////////////////////////////////////////////////////////
   129 //						General tricks used in this code
   130 //
   131 // (a) Error messages are generated by typdefing an array of negative size to
   132 //     generate compile-time errors.
   133 // (b) Warning messages on MSVC are generated by declaring unused variables, and
   134 //	    enabling the "variable XXX is never used" warning.
   135 // (c) Unions are used in a few compiler-specific cases to perform illegal casts.
   136 // (d) For Microsoft and Intel, when adjusting the 'this' pointer, it's cast to
   137 //     (char *) first to ensure that the correct number of *bytes* are added.
   138 //
   139 ////////////////////////////////////////////////////////////////////////////////
   140 //						Helper templates
   141 //
   142 ////////////////////////////////////////////////////////////////////////////////
   143 
   144 
   145 namespace fastdelegate {
   146 namespace detail {	// we'll hide the implementation details in a nested namespace.
   147 
   148 //		implicit_cast< >
   149 // I believe this was originally going to be in the C++ standard but
   150 // was left out by accident. It's even milder than static_cast.
   151 // I use it instead of static_cast<> to emphasize that I'm not doing
   152 // anything nasty.
   153 // Usage is identical to static_cast<>
   154 template <class OutputClass, class InputClass>
   155 inline OutputClass implicit_cast(InputClass input){
   156 	return input;
   157 }
   158 
   159 //		horrible_cast< >
   160 // This is truly evil. It completely subverts C++'s type system, allowing you
   161 // to cast from any class to any other class. Technically, using a union
   162 // to perform the cast is undefined behaviour (even in C). But we can see if
   163 // it is OK by checking that the union is the same size as each of its members.
   164 // horrible_cast<> should only be used for compiler-specific workarounds.
   165 // Usage is identical to reinterpret_cast<>.
   166 
   167 // This union is declared outside the horrible_cast because BCC 5.5.1
   168 // can't inline a function with a nested class, and gives a warning.
   169 template <class OutputClass, class InputClass>
   170 union horrible_union{
   171 	OutputClass out;
   172 	InputClass in;
   173 };
   174 
   175 template <class OutputClass, class InputClass>
   176 inline OutputClass horrible_cast(const InputClass input){
   177 	horrible_union<OutputClass, InputClass> u;
   178 	// Cause a compile-time error if in, out and u are not the same size.
   179 	// If the compile fails here, it means the compiler has peculiar
   180 	// unions which would prevent the cast from working.
   181 	typedef int ERROR_CantUseHorrible_cast[sizeof(InputClass)==sizeof(u)
   182 		&& sizeof(InputClass)==sizeof(OutputClass) ? 1 : -1];
   183 	u.in = input;
   184 	return u.out;
   185 }
   186 
   187 ////////////////////////////////////////////////////////////////////////////////
   188 //						Workarounds
   189 //
   190 ////////////////////////////////////////////////////////////////////////////////
   191 
   192 // Backwards compatibility: This macro used to be necessary in the virtual inheritance
   193 // case for Intel and Microsoft. Now it just forward-declares the class.
   194 #define FASTDELEGATEDECLARE(CLASSNAME)	class CLASSNAME;
   195 
   196 // Prevent use of the static function hack with the DOS medium model.
   197 #ifdef __MEDIUM__
   198 #undef FASTDELEGATE_USESTATICFUNCTIONHACK
   199 #endif
   200 
   201 //			DefaultVoid - a workaround for 'void' templates in VC6.
   202 //
   203 //  (1) VC6 and earlier do not allow 'void' as a default template argument.
   204 //  (2) They also doesn't allow you to return 'void' from a function.
   205 //
   206 // Workaround for (1): Declare a dummy type 'DefaultVoid' which we use
   207 //   when we'd like to use 'void'. We convert it into 'void' and back
   208 //   using the templates DefaultVoidToVoid<> and VoidToDefaultVoid<>.
   209 // Workaround for (2): On VC6, the code for calling a void function is
   210 //   identical to the code for calling a non-void function in which the
   211 //   return value is never used, provided the return value is returned
   212 //   in the EAX register, rather than on the stack.
   213 //   This is true for most fundamental types such as int, enum, void *.
   214 //   Const void * is the safest option since it doesn't participate
   215 //   in any automatic conversions. But on a 16-bit compiler it might
   216 //   cause extra code to be generated, so we disable it for all compilers
   217 //   except for VC6 (and VC5).
   218 #ifdef FASTDLGT_VC6
   219 // VC6 workaround
   220 typedef const void * DefaultVoid;
   221 #else
   222 // On any other compiler, just use a normal void.
   223 typedef void DefaultVoid;
   224 #endif
   225 
   226 // Translate from 'DefaultVoid' to 'void'.
   227 // Everything else is unchanged
   228 template <class T>
   229 struct DefaultVoidToVoid { typedef T type; };
   230 
   231 template <>
   232 struct DefaultVoidToVoid<DefaultVoid> {	typedef void type; };
   233 
   234 // Translate from 'void' into 'DefaultVoid'
   235 // Everything else is unchanged
   236 template <class T>
   237 struct VoidToDefaultVoid { typedef T type; };
   238 
   239 template <>
   240 struct VoidToDefaultVoid<void> { typedef DefaultVoid type; };
   241 
   242 
   243 
   244 ////////////////////////////////////////////////////////////////////////////////
   245 //						Fast Delegates, part 1:
   246 //
   247 //		Conversion of member function pointer to a standard form
   248 //
   249 ////////////////////////////////////////////////////////////////////////////////
   250 
   251 // GenericClass is a fake class, ONLY used to provide a type.
   252 // It is vitally important that it is never defined, so that the compiler doesn't
   253 // think it can optimize the invocation. For example, Borland generates simpler
   254 // code if it knows the class only uses single inheritance.
   255 
   256 // Compilers using Microsoft's structure need to be treated as a special case.
   257 #ifdef  FASTDLGT_MICROSOFT_MFP
   258 
   259 #ifdef FASTDLGT_HASINHERITANCE_KEYWORDS
   260 	// For Microsoft and Intel, we want to ensure that it's the most efficient type of MFP
   261 	// (4 bytes), even when the /vmg option is used. Declaring an empty class
   262 	// would give 16 byte pointers in this case....
   263 	class __single_inheritance GenericClass;
   264 #endif
   265 	// ...but for Codeplay, an empty class *always* gives 4 byte pointers.
   266 	// If compiled with the /clr option ("managed C++"), the JIT compiler thinks
   267 	// it needs to load GenericClass before it can call any of its functions,
   268 	// (compiles OK but crashes at runtime!), so we need to declare an
   269 	// empty class to make it happy.
   270 	// Codeplay and VC4 can't cope with the unknown_inheritance case either.
   271 	class GenericClass {};
   272 #else
   273 	class GenericClass;
   274 #endif
   275 
   276 // The size of a single inheritance member function pointer.
   277 const int SINGLE_MEMFUNCPTR_SIZE = sizeof(void (GenericClass::*)());
   278 
   279 //						SimplifyMemFunc< >::Convert()
   280 //
   281 //	A template function that converts an arbitrary member function pointer into the
   282 //	simplest possible form of member function pointer, using a supplied 'this' pointer.
   283 //  According to the standard, this can be done legally with reinterpret_cast<>.
   284 //	For (non-standard) compilers which use member function pointers which vary in size
   285 //  depending on the class, we need to use	knowledge of the internal structure of a
   286 //  member function pointer, as used by the compiler. Template specialization is used
   287 //  to distinguish between the sizes. Because some compilers don't support partial
   288 //	template specialisation, I use full specialisation of a wrapper struct.
   289 
   290 // general case -- don't know how to convert it. Force a compile failure
   291 template <int N>
   292 struct SimplifyMemFunc {
   293 	template <class X, class XFuncType, class GenericMemFuncType>
   294 	inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind,
   295 		GenericMemFuncType &bound_func) {
   296 		// Unsupported member function type -- force a compile failure.
   297 	    // (it's illegal to have a array with negative size).
   298 		typedef char ERROR_Unsupported_member_function_pointer_on_this_compiler[N-100];
   299 		return 0;
   300 	}
   301 };
   302 
   303 // For compilers where all member func ptrs are the same size, everything goes here.
   304 // For non-standard compilers, only single_inheritance classes go here.
   305 template <>
   306 struct SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE>  {
   307 	template <class X, class XFuncType, class GenericMemFuncType>
   308 	inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind,
   309 			GenericMemFuncType &bound_func) {
   310 #if defined __DMC__
   311 		// Digital Mars doesn't allow you to cast between abitrary PMF's,
   312 		// even though the standard says you can. The 32-bit compiler lets you
   313 		// static_cast through an int, but the DOS compiler doesn't.
   314 		bound_func = horrible_cast<GenericMemFuncType>(function_to_bind);
   315 #else
   316         bound_func = reinterpret_cast<GenericMemFuncType>(function_to_bind);
   317 #endif
   318         return reinterpret_cast<GenericClass *>(pthis);
   319 	}
   320 };
   321 
   322 ////////////////////////////////////////////////////////////////////////////////
   323 //						Fast Delegates, part 1b:
   324 //
   325 //					Workarounds for Microsoft and Intel
   326 //
   327 ////////////////////////////////////////////////////////////////////////////////
   328 
   329 
   330 // Compilers with member function pointers which violate the standard (MSVC, Intel, Codeplay),
   331 // need to be treated as a special case.
   332 #ifdef FASTDLGT_MICROSOFT_MFP
   333 
   334 // We use unions to perform horrible_casts. I would like to use #pragma pack(push, 1)
   335 // at the start of each function for extra safety, but VC6 seems to ICE
   336 // intermittently if you do this inside a template.
   337 
   338 // __multiple_inheritance classes go here
   339 // Nasty hack for Microsoft and Intel (IA32 and Itanium)
   340 template<>
   341 struct SimplifyMemFunc< SINGLE_MEMFUNCPTR_SIZE + sizeof(int) >  {
   342 	template <class X, class XFuncType, class GenericMemFuncType>
   343 	inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind,
   344 		GenericMemFuncType &bound_func) {
   345 		// We need to use a horrible_cast to do this conversion.
   346 		// In MSVC, a multiple inheritance member pointer is internally defined as:
   347         union {
   348 			XFuncType func;
   349 			struct {
   350 				GenericMemFuncType funcaddress; // points to the actual member function
   351 				int delta;	     // #BYTES to be added to the 'this' pointer
   352 			}s;
   353         } u;
   354 		// Check that the horrible_cast will work
   355 		typedef int ERROR_CantUsehorrible_cast[sizeof(function_to_bind)==sizeof(u.s)? 1 : -1];
   356         u.func = function_to_bind;
   357 		bound_func = u.s.funcaddress;
   358 		return reinterpret_cast<GenericClass *>(reinterpret_cast<char *>(pthis) + u.s.delta);
   359 	}
   360 };
   361 
   362 // virtual inheritance is a real nuisance. It's inefficient and complicated.
   363 // On MSVC and Intel, there isn't enough information in the pointer itself to
   364 // enable conversion to a closure pointer. Earlier versions of this code didn't
   365 // work for all cases, and generated a compile-time error instead.
   366 // But a very clever hack invented by John M. Dlugosz solves this problem.
   367 // My code is somewhat different to his: I have no asm code, and I make no
   368 // assumptions about the calling convention that is used.
   369 
   370 // In VC++ and ICL, a virtual_inheritance member pointer
   371 // is internally defined as:
   372 struct MicrosoftVirtualMFP {
   373 	void (GenericClass::*codeptr)(); // points to the actual member function
   374 	int delta;		// #bytes to be added to the 'this' pointer
   375 	int vtable_index; // or 0 if no virtual inheritance
   376 };
   377 // The CRUCIAL feature of Microsoft/Intel MFPs which we exploit is that the
   378 // m_codeptr member is *always* called, regardless of the values of the other
   379 // members. (This is *not* true for other compilers, eg GCC, which obtain the
   380 // function address from the vtable if a virtual function is being called).
   381 // Dlugosz's trick is to make the codeptr point to a probe function which
   382 // returns the 'this' pointer that was used.
   383 
   384 // Define a generic class that uses virtual inheritance.
   385 // It has a trival member function that returns the value of the 'this' pointer.
   386 struct GenericVirtualClass : virtual public GenericClass
   387 {
   388 	typedef GenericVirtualClass * (GenericVirtualClass::*ProbePtrType)();
   389 	GenericVirtualClass * GetThis() { return this; }
   390 };
   391 
   392 // __virtual_inheritance classes go here
   393 template <>
   394 struct SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE + 2*sizeof(int) >
   395 {
   396 
   397 	template <class X, class XFuncType, class GenericMemFuncType>
   398 	inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind,
   399 		GenericMemFuncType &bound_func) {
   400 		union {
   401 			XFuncType func;
   402 			GenericClass* (X::*ProbeFunc)();
   403 			MicrosoftVirtualMFP s;
   404 		} u;
   405 		u.func = function_to_bind;
   406 		bound_func = reinterpret_cast<GenericMemFuncType>(u.s.codeptr);
   407 		union {
   408 			GenericVirtualClass::ProbePtrType virtfunc;
   409 			MicrosoftVirtualMFP s;
   410 		} u2;
   411 		// Check that the horrible_cast<>s will work
   412 		typedef int ERROR_CantUsehorrible_cast[sizeof(function_to_bind)==sizeof(u.s)
   413 			&& sizeof(function_to_bind)==sizeof(u.ProbeFunc)
   414 			&& sizeof(u2.virtfunc)==sizeof(u2.s) ? 1 : -1];
   415    // Unfortunately, taking the address of a MF prevents it from being inlined, so
   416    // this next line can't be completely optimised away by the compiler.
   417 		u2.virtfunc = &GenericVirtualClass::GetThis;
   418 		u.s.codeptr = u2.s.codeptr;
   419 		return (pthis->*u.ProbeFunc)();
   420 	}
   421 };
   422 
   423 #if (_MSC_VER <1300)
   424 
   425 // Nasty hack for Microsoft Visual C++ 6.0
   426 // unknown_inheritance classes go here
   427 // There is a compiler bug in MSVC6 which generates incorrect code in this case!!
   428 template <>
   429 struct SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE + 3*sizeof(int) >
   430 {
   431 	template <class X, class XFuncType, class GenericMemFuncType>
   432 	inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind,
   433 		GenericMemFuncType &bound_func) {
   434 		// There is an apalling but obscure compiler bug in MSVC6 and earlier:
   435 		// vtable_index and 'vtordisp' are always set to 0 in the
   436 		// unknown_inheritance case!
   437 		// This means that an incorrect function could be called!!!
   438 		// Compiling with the /vmg option leads to potentially incorrect code.
   439 		// This is probably the reason that the IDE has a user interface for specifying
   440 		// the /vmg option, but it is disabled -  you can only specify /vmg on
   441 		// the command line. In VC1.5 and earlier, the compiler would ICE if it ever
   442 		// encountered this situation.
   443 		// It is OK to use the /vmg option if /vmm or /vms is specified.
   444 
   445 		// Fortunately, the wrong function is only called in very obscure cases.
   446 		// It only occurs when a derived class overrides a virtual function declared
   447 		// in a virtual base class, and the member function
   448 		// points to the *Derived* version of that function. The problem can be
   449 		// completely averted in 100% of cases by using the *Base class* for the
   450 		// member fpointer. Ie, if you use the base class as an interface, you'll
   451 		// stay out of trouble.
   452 		// Occasionally, you might want to point directly to a derived class function
   453 		// that isn't an override of a base class. In this case, both vtable_index
   454 		// and 'vtordisp' are zero, but a virtual_inheritance pointer will be generated.
   455 		// We can generate correct code in this case. To prevent an incorrect call from
   456 		// ever being made, on MSVC6 we generate a warning, and call a function to
   457 		// make the program crash instantly.
   458 		typedef char ERROR_VC6CompilerBug[-100];
   459 		return 0;
   460 	}
   461 };
   462 
   463 
   464 #else
   465 
   466 // Nasty hack for Microsoft and Intel (IA32 and Itanium)
   467 // unknown_inheritance classes go here
   468 // This is probably the ugliest bit of code I've ever written. Look at the casts!
   469 // There is a compiler bug in MSVC6 which prevents it from using this code.
   470 template <>
   471 struct SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE + 3*sizeof(int) >
   472 {
   473 	template <class X, class XFuncType, class GenericMemFuncType>
   474 	inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind,
   475 			GenericMemFuncType &bound_func) {
   476 		// The member function pointer is 16 bytes long. We can't use a normal cast, but
   477 		// we can use a union to do the conversion.
   478 		union {
   479 			XFuncType func;
   480 			// In VC++ and ICL, an unknown_inheritance member pointer
   481 			// is internally defined as:
   482 			struct {
   483 				GenericMemFuncType m_funcaddress; // points to the actual member function
   484 				int delta;		// #bytes to be added to the 'this' pointer
   485 				int vtordisp;		// #bytes to add to 'this' to find the vtable
   486 				int vtable_index; // or 0 if no virtual inheritance
   487 			} s;
   488 		} u;
   489 		// Check that the horrible_cast will work
   490 		typedef int ERROR_CantUsehorrible_cast[sizeof(XFuncType)==sizeof(u.s)? 1 : -1];
   491 		u.func = function_to_bind;
   492 		bound_func = u.s.funcaddress;
   493 		int virtual_delta = 0;
   494 		if (u.s.vtable_index) { // Virtual inheritance is used
   495 			// First, get to the vtable.
   496 			// It is 'vtordisp' bytes from the start of the class.
   497 			const int * vtable = *reinterpret_cast<const int *const*>(
   498 				reinterpret_cast<const char *>(pthis) + u.s.vtordisp );
   499 
   500 			// 'vtable_index' tells us where in the table we should be looking.
   501 			virtual_delta = u.s.vtordisp + *reinterpret_cast<const int *>(
   502 				reinterpret_cast<const char *>(vtable) + u.s.vtable_index);
   503 		}
   504 		// The int at 'virtual_delta' gives us the amount to add to 'this'.
   505         // Finally we can add the three components together. Phew!
   506         return reinterpret_cast<GenericClass *>(
   507 			reinterpret_cast<char *>(pthis) + u.s.delta + virtual_delta);
   508 	};
   509 };
   510 #endif // MSVC 7 and greater
   511 
   512 #endif // MS/Intel hacks
   513 
   514 }  // namespace detail
   515 
   516 ////////////////////////////////////////////////////////////////////////////////
   517 //						Fast Delegates, part 2:
   518 //
   519 //	Define the delegate storage, and cope with static functions
   520 //
   521 ////////////////////////////////////////////////////////////////////////////////
   522 
   523 // DelegateMemento -- an opaque structure which can hold an arbitary delegate.
   524 // It knows nothing about the calling convention or number of arguments used by
   525 // the function pointed to.
   526 // It supplies comparison operators so that it can be stored in STL collections.
   527 // It cannot be set to anything other than null, nor invoked directly:
   528 //   it must be converted to a specific delegate.
   529 
   530 // Implementation:
   531 // There are two possible implementations: the Safe method and the Evil method.
   532 //				DelegateMemento - Safe version
   533 //
   534 // This implementation is standard-compliant, but a bit tricky.
   535 // A static function pointer is stored inside the class.
   536 // Here are the valid values:
   537 // +-- Static pointer --+--pThis --+-- pMemFunc-+-- Meaning------+
   538 // |   0				|  0       |   0        | Empty          |
   539 // |   !=0              |(dontcare)|  Invoker   | Static function|
   540 // |   0                |  !=0     |  !=0*      | Method call    |
   541 // +--------------------+----------+------------+----------------+
   542 //  * For Metrowerks, this can be 0. (first virtual function in a 
   543 //       single_inheritance class).
   544 // When stored stored inside a specific delegate, the 'dontcare' entries are replaced
   545 // with a reference to the delegate itself. This complicates the = and == operators
   546 // for the delegate class.
   547 
   548 //				DelegateMemento - Evil version
   549 //
   550 // For compilers where data pointers are at least as big as code pointers, it is
   551 // possible to store the function pointer in the this pointer, using another
   552 // horrible_cast. In this case the DelegateMemento implementation is simple:
   553 // +--pThis --+-- pMemFunc-+-- Meaning---------------------+
   554 // |    0     |  0         | Empty                         |
   555 // |  !=0     |  !=0*      | Static function or method call|
   556 // +----------+------------+-------------------------------+
   557 //  * For Metrowerks, this can be 0. (first virtual function in a 
   558 //       single_inheritance class).
   559 // Note that the Sun C++ and MSVC documentation explicitly state that they 
   560 // support static_cast between void * and function pointers.
   561 
   562 class DelegateMemento {
   563 protected:
   564 	// the data is protected, not private, because many
   565 	// compilers have problems with template friends.
   566 	typedef void (detail::GenericClass::*GenericMemFuncType)(); // arbitrary MFP.
   567 	detail::GenericClass *m_pthis;
   568 	GenericMemFuncType m_pFunction;
   569 
   570 #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
   571 	typedef void (*GenericFuncPtr)(); // arbitrary code pointer
   572 	GenericFuncPtr m_pStaticFunction;
   573 #endif
   574 
   575 public:
   576 #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
   577 	DelegateMemento() : m_pthis(0), m_pFunction(0), m_pStaticFunction(0) {};
   578 	void clear() {
   579 		m_pthis=0; m_pFunction=0; m_pStaticFunction=0;
   580 	}
   581 #else
   582 	DelegateMemento() : m_pthis(0), m_pFunction(0) {};
   583 	void clear() {	m_pthis=0; m_pFunction=0;	}
   584 #endif
   585 public:
   586 #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
   587 	inline bool IsEqual (const DelegateMemento &x) const{
   588 	    // We have to cope with the static function pointers as a special case
   589 		if (m_pFunction!=x.m_pFunction) return false;
   590 		// the static function ptrs must either both be equal, or both be 0.
   591 		if (m_pStaticFunction!=x.m_pStaticFunction) return false;
   592 		if (m_pStaticFunction!=0) return m_pthis==x.m_pthis;
   593 		else return true;
   594 	}
   595 #else // Evil Method
   596 	inline bool IsEqual (const DelegateMemento &x) const{
   597 		return m_pthis==x.m_pthis && m_pFunction==x.m_pFunction;
   598 	}
   599 #endif
   600 	// Provide a strict weak ordering for DelegateMementos.
   601 	inline bool IsLess(const DelegateMemento &right) const {
   602 		// deal with static function pointers first
   603 #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
   604 		if (m_pStaticFunction !=0 || right.m_pStaticFunction!=0)
   605 				return m_pStaticFunction < right.m_pStaticFunction;
   606 #endif
   607 		if (m_pthis !=right.m_pthis) return m_pthis < right.m_pthis;
   608 	// There are no ordering operators for member function pointers,
   609 	// but we can fake one by comparing each byte. The resulting ordering is
   610 	// arbitrary (and compiler-dependent), but it permits storage in ordered STL containers.
   611 		return memcmp(&m_pFunction, &right.m_pFunction, sizeof(m_pFunction)) < 0;
   612 
   613 	}
   614 	// BUGFIX (Mar 2005):
   615 	// We can't just compare m_pFunction because on Metrowerks,
   616 	// m_pFunction can be zero even if the delegate is not empty!
   617 	inline bool operator ! () const		// Is it bound to anything?
   618 	{ return m_pthis==0 && m_pFunction==0; }
   619 	inline bool empty() const		// Is it bound to anything?
   620 	{ return m_pthis==0 && m_pFunction==0; }
   621 public:
   622 	DelegateMemento & operator = (const DelegateMemento &right)  {
   623 		SetMementoFrom(right);
   624 		return *this;
   625 	}
   626 	inline bool operator <(const DelegateMemento &right) {
   627 		return IsLess(right);
   628 	}
   629 	inline bool operator >(const DelegateMemento &right) {
   630 		return right.IsLess(*this);
   631 	}
   632 	DelegateMemento (const DelegateMemento &right)  :
   633 		m_pthis(right.m_pthis), m_pFunction(right.m_pFunction)
   634 #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
   635 		, m_pStaticFunction (right.m_pStaticFunction)
   636 #endif
   637 		{}
   638 protected:
   639 	void SetMementoFrom(const DelegateMemento &right)  {
   640 		m_pFunction = right.m_pFunction;
   641 		m_pthis = right.m_pthis;
   642 #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
   643 		m_pStaticFunction = right.m_pStaticFunction;
   644 #endif
   645 	}
   646 };
   647 
   648 
   649 //						ClosurePtr<>
   650 //
   651 // A private wrapper class that adds function signatures to DelegateMemento.
   652 // It's the class that does most of the actual work.
   653 // The signatures are specified by:
   654 // GenericMemFunc: must be a type of GenericClass member function pointer.
   655 // StaticFuncPtr:  must be a type of function pointer with the same signature
   656 //                 as GenericMemFunc.
   657 // UnvoidStaticFuncPtr: is the same as StaticFuncPtr, except on VC6
   658 //                 where it never returns void (returns DefaultVoid instead).
   659 
   660 // An outer class, FastDelegateN<>, handles the invoking and creates the
   661 // necessary typedefs.
   662 // This class does everything else.
   663 
   664 namespace detail {
   665 
   666 template < class GenericMemFunc, class StaticFuncPtr, class UnvoidStaticFuncPtr>
   667 class ClosurePtr : public DelegateMemento {
   668 public:
   669 	// These functions are for setting the delegate to a member function.
   670 
   671 	// Here's the clever bit: we convert an arbitrary member function into a
   672 	// standard form. XMemFunc should be a member function of class X, but I can't
   673 	// enforce that here. It needs to be enforced by the wrapper class.
   674 	template < class X, class XMemFunc >
   675 	inline void bindmemfunc(X *pthis, XMemFunc function_to_bind ) {
   676 		m_pthis = SimplifyMemFunc< sizeof(function_to_bind) >
   677 			::Convert(pthis, function_to_bind, m_pFunction);
   678 #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
   679 		m_pStaticFunction = 0;
   680 #endif
   681 	}
   682 	// For const member functions, we only need a const class pointer.
   683 	// Since we know that the member function is const, it's safe to
   684 	// remove the const qualifier from the 'this' pointer with a const_cast.
   685 	// VC6 has problems if we just overload 'bindmemfunc', so we give it a different name.
   686 	template < class X, class XMemFunc>
   687 	inline void bindconstmemfunc(const X *pthis, XMemFunc function_to_bind) {
   688 		m_pthis= SimplifyMemFunc< sizeof(function_to_bind) >
   689 			::Convert(const_cast<X*>(pthis), function_to_bind, m_pFunction);
   690 #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
   691 		m_pStaticFunction = 0;
   692 #endif
   693 	}
   694 #ifdef FASTDELEGATE_GCC_BUG_8271	// At present, GCC doesn't recognize constness of MFPs in templates
   695 	template < class X, class XMemFunc>
   696 	inline void bindmemfunc(const X *pthis, XMemFunc function_to_bind) {
   697 		bindconstmemfunc(pthis, function_to_bind);
   698 #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
   699 		m_pStaticFunction = 0;
   700 #endif
   701 	}
   702 #endif
   703 	// These functions are required for invoking the stored function
   704 	inline GenericClass *GetClosureThis() const { return m_pthis; }
   705 	inline GenericMemFunc GetClosureMemPtr() const { return reinterpret_cast<GenericMemFunc>(m_pFunction); }
   706 
   707 // There are a few ways of dealing with static function pointers.
   708 // There's a standard-compliant, but tricky method.
   709 // There's also a straightforward hack, that won't work on DOS compilers using the
   710 // medium memory model. It's so evil that I can't recommend it, but I've
   711 // implemented it anyway because it produces very nice asm code.
   712 
   713 #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
   714 
   715 //				ClosurePtr<> - Safe version
   716 //
   717 // This implementation is standard-compliant, but a bit tricky.
   718 // I store the function pointer inside the class, and the delegate then
   719 // points to itself. Whenever the delegate is copied, these self-references
   720 // must be transformed, and this complicates the = and == operators.
   721 public:
   722 	// The next two functions are for operator ==, =, and the copy constructor.
   723 	// We may need to convert the m_pthis pointers, so that
   724 	// they remain as self-references.
   725 	template< class DerivedClass >
   726 	inline void CopyFrom (DerivedClass *pParent, const DelegateMemento &x) {
   727 		SetMementoFrom(x);
   728 		if (m_pStaticFunction!=0) {
   729 			// transform self references...
   730 			m_pthis=reinterpret_cast<GenericClass *>(pParent);
   731 		}
   732 	}
   733 	// For static functions, the 'static_function_invoker' class in the parent
   734 	// will be called. The parent then needs to call GetStaticFunction() to find out
   735 	// the actual function to invoke.
   736 	template < class DerivedClass, class ParentInvokerSig >
   737 	inline void bindstaticfunc(DerivedClass *pParent, ParentInvokerSig static_function_invoker,
   738 				StaticFuncPtr function_to_bind ) {
   739 		if (function_to_bind==0) { // cope with assignment to 0
   740 			m_pFunction=0;
   741 		} else {
   742 			bindmemfunc(pParent, static_function_invoker);
   743         }
   744 		m_pStaticFunction=reinterpret_cast<GenericFuncPtr>(function_to_bind);
   745 	}
   746 	inline UnvoidStaticFuncPtr GetStaticFunction() const {
   747 		return reinterpret_cast<UnvoidStaticFuncPtr>(m_pStaticFunction);
   748 	}
   749 #else
   750 
   751 //				ClosurePtr<> - Evil version
   752 //
   753 // For compilers where data pointers are at least as big as code pointers, it is
   754 // possible to store the function pointer in the this pointer, using another
   755 // horrible_cast. Invocation isn't any faster, but it saves 4 bytes, and
   756 // speeds up comparison and assignment. If C++ provided direct language support
   757 // for delegates, they would produce asm code that was almost identical to this.
   758 // Note that the Sun C++ and MSVC documentation explicitly state that they
   759 // support static_cast between void * and function pointers.
   760 
   761 	template< class DerivedClass >
   762 	inline void CopyFrom (DerivedClass *pParent, const DelegateMemento &right) {
   763 		SetMementoFrom(right);
   764 	}
   765 	// For static functions, the 'static_function_invoker' class in the parent
   766 	// will be called. The parent then needs to call GetStaticFunction() to find out
   767 	// the actual function to invoke.
   768 	// ******** EVIL, EVIL CODE! *******
   769 	template < 	class DerivedClass, class ParentInvokerSig>
   770 	inline void bindstaticfunc(DerivedClass *pParent, ParentInvokerSig static_function_invoker,
   771 				StaticFuncPtr function_to_bind) {
   772 		if (function_to_bind==0) { // cope with assignment to 0
   773 			m_pFunction=0;
   774 		} else { 
   775 		   // We'll be ignoring the 'this' pointer, but we need to make sure we pass
   776 		   // a valid value to bindmemfunc().
   777 			bindmemfunc(pParent, static_function_invoker);
   778         }
   779 
   780 		// WARNING! Evil hack. We store the function in the 'this' pointer!
   781 		// Ensure that there's a compilation failure if function pointers
   782 		// and data pointers have different sizes.
   783 		// If you get this error, you need to #undef FASTDELEGATE_USESTATICFUNCTIONHACK.
   784 		typedef int ERROR_CantUseEvilMethod[sizeof(GenericClass *)==sizeof(function_to_bind) ? 1 : -1];
   785 		m_pthis = horrible_cast<GenericClass *>(function_to_bind);
   786 		// MSVC, SunC++ and DMC accept the following (non-standard) code:
   787 //		m_pthis = static_cast<GenericClass *>(static_cast<void *>(function_to_bind));
   788 		// BCC32, Comeau and DMC accept this method. MSVC7.1 needs __int64 instead of long
   789 //		m_pthis = reinterpret_cast<GenericClass *>(reinterpret_cast<long>(function_to_bind));
   790 	}
   791 	// ******** EVIL, EVIL CODE! *******
   792 	// This function will be called with an invalid 'this' pointer!!
   793 	// We're just returning the 'this' pointer, converted into
   794 	// a function pointer!
   795 	inline UnvoidStaticFuncPtr GetStaticFunction() const {
   796 		// Ensure that there's a compilation failure if function pointers
   797 		// and data pointers have different sizes.
   798 		// If you get this error, you need to #undef FASTDELEGATE_USESTATICFUNCTIONHACK.
   799 		typedef int ERROR_CantUseEvilMethod[sizeof(UnvoidStaticFuncPtr)==sizeof(this) ? 1 : -1];
   800 		return horrible_cast<UnvoidStaticFuncPtr>(this);
   801 	}
   802 #endif // !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
   803 
   804 	// Does the closure contain this static function?
   805 	inline bool IsEqualToStaticFuncPtr(StaticFuncPtr funcptr){
   806 		if (funcptr==0) return empty();
   807 	// For the Evil method, if it doesn't actually contain a static function, this will return an arbitrary
   808 	// value that is not equal to any valid function pointer.
   809 		else return funcptr==reinterpret_cast<StaticFuncPtr>(GetStaticFunction());
   810 	}
   811 };
   812 
   813 
   814 } // namespace detail
   815 
   816 ////////////////////////////////////////////////////////////////////////////////
   817 //						Fast Delegates, part 3:
   818 //
   819 //				Wrapper classes to ensure type safety
   820 //
   821 ////////////////////////////////////////////////////////////////////////////////
   822 
   823 
   824 // Once we have the member function conversion templates, it's easy to make the
   825 // wrapper classes. So that they will work with as many compilers as possible,
   826 // the classes are of the form
   827 //   FastDelegate3<int, char *, double>
   828 // They can cope with any combination of parameters. The max number of parameters
   829 // allowed is 8, but it is trivial to increase this limit.
   830 // Note that we need to treat const member functions seperately.
   831 // All this class does is to enforce type safety, and invoke the delegate with
   832 // the correct list of parameters.
   833 
   834 // Because of the weird rule about the class of derived member function pointers,
   835 // you sometimes need to apply a downcast to the 'this' pointer.
   836 // This is the reason for the use of "implicit_cast<X*>(pthis)" in the code below.
   837 // If CDerivedClass is derived from CBaseClass, but doesn't override SimpleVirtualFunction,
   838 // without this trick you'd need to write:
   839 //		MyDelegate(static_cast<CBaseClass *>(&d), &CDerivedClass::SimpleVirtualFunction);
   840 // but with the trick you can write
   841 //		MyDelegate(&d, &CDerivedClass::SimpleVirtualFunction);
   842 
   843 // RetType is the type the compiler uses in compiling the template. For VC6,
   844 // it cannot be void. DesiredRetType is the real type which is returned from
   845 // all of the functions. It can be void.
   846 
   847 // Implicit conversion to "bool" is achieved using the safe_bool idiom,
   848 // using member data pointers (MDP). This allows "if (dg)..." syntax
   849 // Because some compilers (eg codeplay) don't have a unique value for a zero
   850 // MDP, an extra padding member is added to the SafeBool struct.
   851 // Some compilers (eg VC6) won't implicitly convert from 0 to an MDP, so
   852 // in that case the static function constructor is not made explicit; this
   853 // allows "if (dg==0) ..." to compile.
   854 
   855 //N=0
   856 template<class RetType=detail::DefaultVoid>
   857 class FastDelegate0 {
   858 private:
   859 	typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
   860 	typedef DesiredRetType (*StaticFunctionPtr)();
   861 	typedef RetType (*UnvoidStaticFunctionPtr)();
   862 	typedef RetType (detail::GenericClass::*GenericMemFn)();
   863 	typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
   864 	ClosureType m_Closure;
   865 public:
   866 	// Typedefs to aid generic programming
   867 	typedef FastDelegate0 type;
   868 
   869 	// Construction and comparison functions
   870 	FastDelegate0() { clear(); }
   871 	FastDelegate0(const FastDelegate0 &x) {
   872 		m_Closure.CopyFrom(this, x.m_Closure); }
   873 	void operator = (const FastDelegate0 &x)  {
   874 		m_Closure.CopyFrom(this, x.m_Closure); }
   875 	bool operator ==(const FastDelegate0 &x) const {
   876 		return m_Closure.IsEqual(x.m_Closure);	}
   877 	bool operator !=(const FastDelegate0 &x) const {
   878 		return !m_Closure.IsEqual(x.m_Closure); }
   879 	bool operator <(const FastDelegate0 &x) const {
   880 		return m_Closure.IsLess(x.m_Closure);	}
   881 	bool operator >(const FastDelegate0 &x) const {
   882 		return x.m_Closure.IsLess(m_Closure);	}
   883 	// Binding to non-const member functions
   884 	template < class X, class Y >
   885 	FastDelegate0(Y *pthis, DesiredRetType (X::* function_to_bind)() ) {
   886 		m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
   887 	template < class X, class Y >
   888 	inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)()) {
   889 		m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind);	}
   890 	// Binding to const member functions.
   891 	template < class X, class Y >
   892 	FastDelegate0(const Y *pthis, DesiredRetType (X::* function_to_bind)() const) {
   893 		m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind);	}
   894 	template < class X, class Y >
   895 	inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)() const) {
   896 		m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind);	}
   897 	// Static functions. We convert them into a member function call.
   898 	// This constructor also provides implicit conversion
   899 	FastDelegate0(DesiredRetType (*function_to_bind)() ) {
   900 		bind(function_to_bind);	}
   901 	// for efficiency, prevent creation of a temporary
   902 	void operator = (DesiredRetType (*function_to_bind)() ) {
   903 		bind(function_to_bind);	}
   904 	inline void bind(DesiredRetType (*function_to_bind)()) {
   905 		m_Closure.bindstaticfunc(this, &FastDelegate0::InvokeStaticFunction,
   906 			function_to_bind); }
   907 	// Invoke the delegate
   908 	RetType operator() () const {
   909 	return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(); }
   910 	// Implicit conversion to "bool" using the safe_bool idiom
   911 private:
   912 	typedef struct SafeBoolStruct {
   913 		int a_data_pointer_to_this_is_0_on_buggy_compilers;
   914 		StaticFunctionPtr m_nonzero;
   915 	} UselessTypedef;
   916     typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type;
   917 public:
   918 	operator unspecified_bool_type() const {
   919         return empty()? 0: &SafeBoolStruct::m_nonzero;
   920     }
   921 	// necessary to allow ==0 to work despite the safe_bool idiom
   922 	inline bool operator==(StaticFunctionPtr funcptr) {
   923 		return m_Closure.IsEqualToStaticFuncPtr(funcptr);	}
   924 	inline bool operator!=(StaticFunctionPtr funcptr) { 
   925 		return !m_Closure.IsEqualToStaticFuncPtr(funcptr);    }
   926 	inline bool operator ! () const	{	// Is it bound to anything?
   927 			return !m_Closure; }
   928 	inline bool empty() const	{
   929 			return !m_Closure; }
   930 	void clear() { m_Closure.clear();}
   931 	// Conversion to and from the DelegateMemento storage class
   932 	const DelegateMemento & GetMemento() { return m_Closure; }
   933 	void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); }
   934 
   935 private:	// Invoker for static functions
   936 	RetType InvokeStaticFunction() const {
   937 	return (*(m_Closure.GetStaticFunction()))(); }
   938 };
   939 
   940 //N=1
   941 template<class Param1, class RetType=detail::DefaultVoid>
   942 class FastDelegate1 {
   943 private:
   944 	typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
   945 	typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1);
   946 	typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1);
   947 	typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1);
   948 	typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
   949 	ClosureType m_Closure;
   950 public:
   951 	// Typedefs to aid generic programming
   952 	typedef FastDelegate1 type;
   953 
   954 	// Construction and comparison functions
   955 	FastDelegate1() { clear(); }
   956 	FastDelegate1(const FastDelegate1 &x) {
   957 		m_Closure.CopyFrom(this, x.m_Closure); }
   958 	void operator = (const FastDelegate1 &x)  {
   959 		m_Closure.CopyFrom(this, x.m_Closure); }
   960 	bool operator ==(const FastDelegate1 &x) const {
   961 		return m_Closure.IsEqual(x.m_Closure);	}
   962 	bool operator !=(const FastDelegate1 &x) const {
   963 		return !m_Closure.IsEqual(x.m_Closure); }
   964 	bool operator <(const FastDelegate1 &x) const {
   965 		return m_Closure.IsLess(x.m_Closure);	}
   966 	bool operator >(const FastDelegate1 &x) const {
   967 		return x.m_Closure.IsLess(m_Closure);	}
   968 	// Binding to non-const member functions
   969 	template < class X, class Y >
   970 	FastDelegate1(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1) ) {
   971 		m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
   972 	template < class X, class Y >
   973 	inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1)) {
   974 		m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind);	}
   975 	// Binding to const member functions.
   976 	template < class X, class Y >
   977 	FastDelegate1(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1) const) {
   978 		m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind);	}
   979 	template < class X, class Y >
   980 	inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1) const) {
   981 		m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind);	}
   982 	// Static functions. We convert them into a member function call.
   983 	// This constructor also provides implicit conversion
   984 	FastDelegate1(DesiredRetType (*function_to_bind)(Param1 p1) ) {
   985 		bind(function_to_bind);	}
   986 	// for efficiency, prevent creation of a temporary
   987 	void operator = (DesiredRetType (*function_to_bind)(Param1 p1) ) {
   988 		bind(function_to_bind);	}
   989 	inline void bind(DesiredRetType (*function_to_bind)(Param1 p1)) {
   990 		m_Closure.bindstaticfunc(this, &FastDelegate1::InvokeStaticFunction,
   991 			function_to_bind); }
   992 	// Invoke the delegate
   993 	RetType operator() (Param1 p1) const {
   994 	return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1); }
   995 	// Implicit conversion to "bool" using the safe_bool idiom
   996 private:
   997 	typedef struct SafeBoolStruct {
   998 		int a_data_pointer_to_this_is_0_on_buggy_compilers;
   999 		StaticFunctionPtr m_nonzero;
  1000 	} UselessTypedef;
  1001     typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type;
  1002 public:
  1003 	operator unspecified_bool_type() const {
  1004         return empty()? 0: &SafeBoolStruct::m_nonzero;
  1005     }
  1006 	// necessary to allow ==0 to work despite the safe_bool idiom
  1007 	inline bool operator==(StaticFunctionPtr funcptr) {
  1008 		return m_Closure.IsEqualToStaticFuncPtr(funcptr);	}
  1009 	inline bool operator!=(StaticFunctionPtr funcptr) { 
  1010 		return !m_Closure.IsEqualToStaticFuncPtr(funcptr);    }
  1011 	inline bool operator ! () const	{	// Is it bound to anything?
  1012 			return !m_Closure; }
  1013 	inline bool empty() const	{
  1014 			return !m_Closure; }
  1015 	void clear() { m_Closure.clear();}
  1016 	// Conversion to and from the DelegateMemento storage class
  1017 	const DelegateMemento & GetMemento() { return m_Closure; }
  1018 	void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); }
  1019 
  1020 private:	// Invoker for static functions
  1021 	RetType InvokeStaticFunction(Param1 p1) const {
  1022 	return (*(m_Closure.GetStaticFunction()))(p1); }
  1023 };
  1024 
  1025 //N=2
  1026 template<class Param1, class Param2, class RetType=detail::DefaultVoid>
  1027 class FastDelegate2 {
  1028 private:
  1029 	typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
  1030 	typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1, Param2 p2);
  1031 	typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2);
  1032 	typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1, Param2 p2);
  1033 	typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
  1034 	ClosureType m_Closure;
  1035 public:
  1036 	// Typedefs to aid generic programming
  1037 	typedef FastDelegate2 type;
  1038 
  1039 	// Construction and comparison functions
  1040 	FastDelegate2() { clear(); }
  1041 	FastDelegate2(const FastDelegate2 &x) {
  1042 		m_Closure.CopyFrom(this, x.m_Closure); }
  1043 	void operator = (const FastDelegate2 &x)  {
  1044 		m_Closure.CopyFrom(this, x.m_Closure); }
  1045 	bool operator ==(const FastDelegate2 &x) const {
  1046 		return m_Closure.IsEqual(x.m_Closure);	}
  1047 	bool operator !=(const FastDelegate2 &x) const {
  1048 		return !m_Closure.IsEqual(x.m_Closure); }
  1049 	bool operator <(const FastDelegate2 &x) const {
  1050 		return m_Closure.IsLess(x.m_Closure);	}
  1051 	bool operator >(const FastDelegate2 &x) const {
  1052 		return x.m_Closure.IsLess(m_Closure);	}
  1053 	// Binding to non-const member functions
  1054 	template < class X, class Y >
  1055 	FastDelegate2(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2) ) {
  1056 		m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
  1057 	template < class X, class Y >
  1058 	inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2)) {
  1059 		m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind);	}
  1060 	// Binding to const member functions.
  1061 	template < class X, class Y >
  1062 	FastDelegate2(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2) const) {
  1063 		m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind);	}
  1064 	template < class X, class Y >
  1065 	inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2) const) {
  1066 		m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind);	}
  1067 	// Static functions. We convert them into a member function call.
  1068 	// This constructor also provides implicit conversion
  1069 	FastDelegate2(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2) ) {
  1070 		bind(function_to_bind);	}
  1071 	// for efficiency, prevent creation of a temporary
  1072 	void operator = (DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2) ) {
  1073 		bind(function_to_bind);	}
  1074 	inline void bind(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2)) {
  1075 		m_Closure.bindstaticfunc(this, &FastDelegate2::InvokeStaticFunction,
  1076 			function_to_bind); }
  1077 	// Invoke the delegate
  1078 	RetType operator() (Param1 p1, Param2 p2) const {
  1079 	return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2); }
  1080 	// Implicit conversion to "bool" using the safe_bool idiom
  1081 private:
  1082 	typedef struct SafeBoolStruct {
  1083 		int a_data_pointer_to_this_is_0_on_buggy_compilers;
  1084 		StaticFunctionPtr m_nonzero;
  1085 	} UselessTypedef;
  1086     typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type;
  1087 public:
  1088 	operator unspecified_bool_type() const {
  1089         return empty()? 0: &SafeBoolStruct::m_nonzero;
  1090     }
  1091 	// necessary to allow ==0 to work despite the safe_bool idiom
  1092 	inline bool operator==(StaticFunctionPtr funcptr) {
  1093 		return m_Closure.IsEqualToStaticFuncPtr(funcptr);	}
  1094 	inline bool operator!=(StaticFunctionPtr funcptr) { 
  1095 		return !m_Closure.IsEqualToStaticFuncPtr(funcptr);    }
  1096 	inline bool operator ! () const	{	// Is it bound to anything?
  1097 			return !m_Closure; }
  1098 	inline bool empty() const	{
  1099 			return !m_Closure; }
  1100 	void clear() { m_Closure.clear();}
  1101 	// Conversion to and from the DelegateMemento storage class
  1102 	const DelegateMemento & GetMemento() { return m_Closure; }
  1103 	void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); }
  1104 
  1105 private:	// Invoker for static functions
  1106 	RetType InvokeStaticFunction(Param1 p1, Param2 p2) const {
  1107 	return (*(m_Closure.GetStaticFunction()))(p1, p2); }
  1108 };
  1109 
  1110 //N=3
  1111 template<class Param1, class Param2, class Param3, class RetType=detail::DefaultVoid>
  1112 class FastDelegate3 {
  1113 private:
  1114 	typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
  1115 	typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3);
  1116 	typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3);
  1117 	typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1, Param2 p2, Param3 p3);
  1118 	typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
  1119 	ClosureType m_Closure;
  1120 public:
  1121 	// Typedefs to aid generic programming
  1122 	typedef FastDelegate3 type;
  1123 
  1124 	// Construction and comparison functions
  1125 	FastDelegate3() { clear(); }
  1126 	FastDelegate3(const FastDelegate3 &x) {
  1127 		m_Closure.CopyFrom(this, x.m_Closure); }
  1128 	void operator = (const FastDelegate3 &x)  {
  1129 		m_Closure.CopyFrom(this, x.m_Closure); }
  1130 	bool operator ==(const FastDelegate3 &x) const {
  1131 		return m_Closure.IsEqual(x.m_Closure);	}
  1132 	bool operator !=(const FastDelegate3 &x) const {
  1133 		return !m_Closure.IsEqual(x.m_Closure); }
  1134 	bool operator <(const FastDelegate3 &x) const {
  1135 		return m_Closure.IsLess(x.m_Closure);	}
  1136 	bool operator >(const FastDelegate3 &x) const {
  1137 		return x.m_Closure.IsLess(m_Closure);	}
  1138 	// Binding to non-const member functions
  1139 	template < class X, class Y >
  1140 	FastDelegate3(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3) ) {
  1141 		m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
  1142 	template < class X, class Y >
  1143 	inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3)) {
  1144 		m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind);	}
  1145 	// Binding to const member functions.
  1146 	template < class X, class Y >
  1147 	FastDelegate3(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3) const) {
  1148 		m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind);	}
  1149 	template < class X, class Y >
  1150 	inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3) const) {
  1151 		m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind);	}
  1152 	// Static functions. We convert them into a member function call.
  1153 	// This constructor also provides implicit conversion
  1154 	FastDelegate3(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3) ) {
  1155 		bind(function_to_bind);	}
  1156 	// for efficiency, prevent creation of a temporary
  1157 	void operator = (DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3) ) {
  1158 		bind(function_to_bind);	}
  1159 	inline void bind(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3)) {
  1160 		m_Closure.bindstaticfunc(this, &FastDelegate3::InvokeStaticFunction,
  1161 			function_to_bind); }
  1162 	// Invoke the delegate
  1163 	RetType operator() (Param1 p1, Param2 p2, Param3 p3) const {
  1164 	return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3); }
  1165 	// Implicit conversion to "bool" using the safe_bool idiom
  1166 private:
  1167 	typedef struct SafeBoolStruct {
  1168 		int a_data_pointer_to_this_is_0_on_buggy_compilers;
  1169 		StaticFunctionPtr m_nonzero;
  1170 	} UselessTypedef;
  1171     typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type;
  1172 public:
  1173 	operator unspecified_bool_type() const {
  1174         return empty()? 0: &SafeBoolStruct::m_nonzero;
  1175     }
  1176 	// necessary to allow ==0 to work despite the safe_bool idiom
  1177 	inline bool operator==(StaticFunctionPtr funcptr) {
  1178 		return m_Closure.IsEqualToStaticFuncPtr(funcptr);	}
  1179 	inline bool operator!=(StaticFunctionPtr funcptr) { 
  1180 		return !m_Closure.IsEqualToStaticFuncPtr(funcptr);    }
  1181 	inline bool operator ! () const	{	// Is it bound to anything?
  1182 			return !m_Closure; }
  1183 	inline bool empty() const	{
  1184 			return !m_Closure; }
  1185 	void clear() { m_Closure.clear();}
  1186 	// Conversion to and from the DelegateMemento storage class
  1187 	const DelegateMemento & GetMemento() { return m_Closure; }
  1188 	void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); }
  1189 
  1190 private:	// Invoker for static functions
  1191 	RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3) const {
  1192 	return (*(m_Closure.GetStaticFunction()))(p1, p2, p3); }
  1193 };
  1194 
  1195 //N=4
  1196 template<class Param1, class Param2, class Param3, class Param4, class RetType=detail::DefaultVoid>
  1197 class FastDelegate4 {
  1198 private:
  1199 	typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
  1200 	typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4);
  1201 	typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4);
  1202 	typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1, Param2 p2, Param3 p3, Param4 p4);
  1203 	typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
  1204 	ClosureType m_Closure;
  1205 public:
  1206 	// Typedefs to aid generic programming
  1207 	typedef FastDelegate4 type;
  1208 
  1209 	// Construction and comparison functions
  1210 	FastDelegate4() { clear(); }
  1211 	FastDelegate4(const FastDelegate4 &x) {
  1212 		m_Closure.CopyFrom(this, x.m_Closure); }
  1213 	void operator = (const FastDelegate4 &x)  {
  1214 		m_Closure.CopyFrom(this, x.m_Closure); }
  1215 	bool operator ==(const FastDelegate4 &x) const {
  1216 		return m_Closure.IsEqual(x.m_Closure);	}
  1217 	bool operator !=(const FastDelegate4 &x) const {
  1218 		return !m_Closure.IsEqual(x.m_Closure); }
  1219 	bool operator <(const FastDelegate4 &x) const {
  1220 		return m_Closure.IsLess(x.m_Closure);	}
  1221 	bool operator >(const FastDelegate4 &x) const {
  1222 		return x.m_Closure.IsLess(m_Closure);	}
  1223 	// Binding to non-const member functions
  1224 	template < class X, class Y >
  1225 	FastDelegate4(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4) ) {
  1226 		m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
  1227 	template < class X, class Y >
  1228 	inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4)) {
  1229 		m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind);	}
  1230 	// Binding to const member functions.
  1231 	template < class X, class Y >
  1232 	FastDelegate4(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4) const) {
  1233 		m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind);	}
  1234 	template < class X, class Y >
  1235 	inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4) const) {
  1236 		m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind);	}
  1237 	// Static functions. We convert them into a member function call.
  1238 	// This constructor also provides implicit conversion
  1239 	FastDelegate4(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4) ) {
  1240 		bind(function_to_bind);	}
  1241 	// for efficiency, prevent creation of a temporary
  1242 	void operator = (DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4) ) {
  1243 		bind(function_to_bind);	}
  1244 	inline void bind(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4)) {
  1245 		m_Closure.bindstaticfunc(this, &FastDelegate4::InvokeStaticFunction,
  1246 			function_to_bind); }
  1247 	// Invoke the delegate
  1248 	RetType operator() (Param1 p1, Param2 p2, Param3 p3, Param4 p4) const {
  1249 	return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3, p4); }
  1250 	// Implicit conversion to "bool" using the safe_bool idiom
  1251 private:
  1252 	typedef struct SafeBoolStruct {
  1253 		int a_data_pointer_to_this_is_0_on_buggy_compilers;
  1254 		StaticFunctionPtr m_nonzero;
  1255 	} UselessTypedef;
  1256     typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type;
  1257 public:
  1258 	operator unspecified_bool_type() const {
  1259         return empty()? 0: &SafeBoolStruct::m_nonzero;
  1260     }
  1261 	// necessary to allow ==0 to work despite the safe_bool idiom
  1262 	inline bool operator==(StaticFunctionPtr funcptr) {
  1263 		return m_Closure.IsEqualToStaticFuncPtr(funcptr);	}
  1264 	inline bool operator!=(StaticFunctionPtr funcptr) { 
  1265 		return !m_Closure.IsEqualToStaticFuncPtr(funcptr);    }
  1266 	inline bool operator ! () const	{	// Is it bound to anything?
  1267 			return !m_Closure; }
  1268 	inline bool empty() const	{
  1269 			return !m_Closure; }
  1270 	void clear() { m_Closure.clear();}
  1271 	// Conversion to and from the DelegateMemento storage class
  1272 	const DelegateMemento & GetMemento() { return m_Closure; }
  1273 	void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); }
  1274 
  1275 private:	// Invoker for static functions
  1276 	RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3, Param4 p4) const {
  1277 	return (*(m_Closure.GetStaticFunction()))(p1, p2, p3, p4); }
  1278 };
  1279 
  1280 //N=5
  1281 template<class Param1, class Param2, class Param3, class Param4, class Param5, class RetType=detail::DefaultVoid>
  1282 class FastDelegate5 {
  1283 private:
  1284 	typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
  1285 	typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5);
  1286 	typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5);
  1287 	typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5);
  1288 	typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
  1289 	ClosureType m_Closure;
  1290 public:
  1291 	// Typedefs to aid generic programming
  1292 	typedef FastDelegate5 type;
  1293 
  1294 	// Construction and comparison functions
  1295 	FastDelegate5() { clear(); }
  1296 	FastDelegate5(const FastDelegate5 &x) {
  1297 		m_Closure.CopyFrom(this, x.m_Closure); }
  1298 	void operator = (const FastDelegate5 &x)  {
  1299 		m_Closure.CopyFrom(this, x.m_Closure); }
  1300 	bool operator ==(const FastDelegate5 &x) const {
  1301 		return m_Closure.IsEqual(x.m_Closure);	}
  1302 	bool operator !=(const FastDelegate5 &x) const {
  1303 		return !m_Closure.IsEqual(x.m_Closure); }
  1304 	bool operator <(const FastDelegate5 &x) const {
  1305 		return m_Closure.IsLess(x.m_Closure);	}
  1306 	bool operator >(const FastDelegate5 &x) const {
  1307 		return x.m_Closure.IsLess(m_Closure);	}
  1308 	// Binding to non-const member functions
  1309 	template < class X, class Y >
  1310 	FastDelegate5(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) ) {
  1311 		m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
  1312 	template < class X, class Y >
  1313 	inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5)) {
  1314 		m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind);	}
  1315 	// Binding to const member functions.
  1316 	template < class X, class Y >
  1317 	FastDelegate5(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) const) {
  1318 		m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind);	}
  1319 	template < class X, class Y >
  1320 	inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) const) {
  1321 		m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind);	}
  1322 	// Static functions. We convert them into a member function call.
  1323 	// This constructor also provides implicit conversion
  1324 	FastDelegate5(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) ) {
  1325 		bind(function_to_bind);	}
  1326 	// for efficiency, prevent creation of a temporary
  1327 	void operator = (DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) ) {
  1328 		bind(function_to_bind);	}
  1329 	inline void bind(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5)) {
  1330 		m_Closure.bindstaticfunc(this, &FastDelegate5::InvokeStaticFunction,
  1331 			function_to_bind); }
  1332 	// Invoke the delegate
  1333 	RetType operator() (Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) const {
  1334 	return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3, p4, p5); }
  1335 	// Implicit conversion to "bool" using the safe_bool idiom
  1336 private:
  1337 	typedef struct SafeBoolStruct {
  1338 		int a_data_pointer_to_this_is_0_on_buggy_compilers;
  1339 		StaticFunctionPtr m_nonzero;
  1340 	} UselessTypedef;
  1341     typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type;
  1342 public:
  1343 	operator unspecified_bool_type() const {
  1344         return empty()? 0: &SafeBoolStruct::m_nonzero;
  1345     }
  1346 	// necessary to allow ==0 to work despite the safe_bool idiom
  1347 	inline bool operator==(StaticFunctionPtr funcptr) {
  1348 		return m_Closure.IsEqualToStaticFuncPtr(funcptr);	}
  1349 	inline bool operator!=(StaticFunctionPtr funcptr) { 
  1350 		return !m_Closure.IsEqualToStaticFuncPtr(funcptr);    }
  1351 	inline bool operator ! () const	{	// Is it bound to anything?
  1352 			return !m_Closure; }
  1353 	inline bool empty() const	{
  1354 			return !m_Closure; }
  1355 	void clear() { m_Closure.clear();}
  1356 	// Conversion to and from the DelegateMemento storage class
  1357 	const DelegateMemento & GetMemento() { return m_Closure; }
  1358 	void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); }
  1359 
  1360 private:	// Invoker for static functions
  1361 	RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) const {
  1362 	return (*(m_Closure.GetStaticFunction()))(p1, p2, p3, p4, p5); }
  1363 };
  1364 
  1365 //N=6
  1366 template<class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class RetType=detail::DefaultVoid>
  1367 class FastDelegate6 {
  1368 private:
  1369 	typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
  1370 	typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6);
  1371 	typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6);
  1372 	typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6);
  1373 	typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
  1374 	ClosureType m_Closure;
  1375 public:
  1376 	// Typedefs to aid generic programming
  1377 	typedef FastDelegate6 type;
  1378 
  1379 	// Construction and comparison functions
  1380 	FastDelegate6() { clear(); }
  1381 	FastDelegate6(const FastDelegate6 &x) {
  1382 		m_Closure.CopyFrom(this, x.m_Closure); }
  1383 	void operator = (const FastDelegate6 &x)  {
  1384 		m_Closure.CopyFrom(this, x.m_Closure); }
  1385 	bool operator ==(const FastDelegate6 &x) const {
  1386 		return m_Closure.IsEqual(x.m_Closure);	}
  1387 	bool operator !=(const FastDelegate6 &x) const {
  1388 		return !m_Closure.IsEqual(x.m_Closure); }
  1389 	bool operator <(const FastDelegate6 &x) const {
  1390 		return m_Closure.IsLess(x.m_Closure);	}
  1391 	bool operator >(const FastDelegate6 &x) const {
  1392 		return x.m_Closure.IsLess(m_Closure);	}
  1393 	// Binding to non-const member functions
  1394 	template < class X, class Y >
  1395 	FastDelegate6(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) ) {
  1396 		m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
  1397 	template < class X, class Y >
  1398 	inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6)) {
  1399 		m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind);	}
  1400 	// Binding to const member functions.
  1401 	template < class X, class Y >
  1402 	FastDelegate6(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) const) {
  1403 		m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind);	}
  1404 	template < class X, class Y >
  1405 	inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) const) {
  1406 		m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind);	}
  1407 	// Static functions. We convert them into a member function call.
  1408 	// This constructor also provides implicit conversion
  1409 	FastDelegate6(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) ) {
  1410 		bind(function_to_bind);	}
  1411 	// for efficiency, prevent creation of a temporary
  1412 	void operator = (DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) ) {
  1413 		bind(function_to_bind);	}
  1414 	inline void bind(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6)) {
  1415 		m_Closure.bindstaticfunc(this, &FastDelegate6::InvokeStaticFunction,
  1416 			function_to_bind); }
  1417 	// Invoke the delegate
  1418 	RetType operator() (Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) const {
  1419 	return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3, p4, p5, p6); }
  1420 	// Implicit conversion to "bool" using the safe_bool idiom
  1421 private:
  1422 	typedef struct SafeBoolStruct {
  1423 		int a_data_pointer_to_this_is_0_on_buggy_compilers;
  1424 		StaticFunctionPtr m_nonzero;
  1425 	} UselessTypedef;
  1426     typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type;
  1427 public:
  1428 	operator unspecified_bool_type() const {
  1429         return empty()? 0: &SafeBoolStruct::m_nonzero;
  1430     }
  1431 	// necessary to allow ==0 to work despite the safe_bool idiom
  1432 	inline bool operator==(StaticFunctionPtr funcptr) {
  1433 		return m_Closure.IsEqualToStaticFuncPtr(funcptr);	}
  1434 	inline bool operator!=(StaticFunctionPtr funcptr) { 
  1435 		return !m_Closure.IsEqualToStaticFuncPtr(funcptr);    }
  1436 	inline bool operator ! () const	{	// Is it bound to anything?
  1437 			return !m_Closure; }
  1438 	inline bool empty() const	{
  1439 			return !m_Closure; }
  1440 	void clear() { m_Closure.clear();}
  1441 	// Conversion to and from the DelegateMemento storage class
  1442 	const DelegateMemento & GetMemento() { return m_Closure; }
  1443 	void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); }
  1444 
  1445 private:	// Invoker for static functions
  1446 	RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) const {
  1447 	return (*(m_Closure.GetStaticFunction()))(p1, p2, p3, p4, p5, p6); }
  1448 };
  1449 
  1450 //N=7
  1451 template<class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class RetType=detail::DefaultVoid>
  1452 class FastDelegate7 {
  1453 private:
  1454 	typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
  1455 	typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7);
  1456 	typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7);
  1457 	typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7);
  1458 	typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
  1459 	ClosureType m_Closure;
  1460 public:
  1461 	// Typedefs to aid generic programming
  1462 	typedef FastDelegate7 type;
  1463 
  1464 	// Construction and comparison functions
  1465 	FastDelegate7() { clear(); }
  1466 	FastDelegate7(const FastDelegate7 &x) {
  1467 		m_Closure.CopyFrom(this, x.m_Closure); }
  1468 	void operator = (const FastDelegate7 &x)  {
  1469 		m_Closure.CopyFrom(this, x.m_Closure); }
  1470 	bool operator ==(const FastDelegate7 &x) const {
  1471 		return m_Closure.IsEqual(x.m_Closure);	}
  1472 	bool operator !=(const FastDelegate7 &x) const {
  1473 		return !m_Closure.IsEqual(x.m_Closure); }
  1474 	bool operator <(const FastDelegate7 &x) const {
  1475 		return m_Closure.IsLess(x.m_Closure);	}
  1476 	bool operator >(const FastDelegate7 &x) const {
  1477 		return x.m_Closure.IsLess(m_Closure);	}
  1478 	// Binding to non-const member functions
  1479 	template < class X, class Y >
  1480 	FastDelegate7(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) ) {
  1481 		m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
  1482 	template < class X, class Y >
  1483 	inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7)) {
  1484 		m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind);	}
  1485 	// Binding to const member functions.
  1486 	template < class X, class Y >
  1487 	FastDelegate7(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) const) {
  1488 		m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind);	}
  1489 	template < class X, class Y >
  1490 	inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) const) {
  1491 		m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind);	}
  1492 	// Static functions. We convert them into a member function call.
  1493 	// This constructor also provides implicit conversion
  1494 	FastDelegate7(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) ) {
  1495 		bind(function_to_bind);	}
  1496 	// for efficiency, prevent creation of a temporary
  1497 	void operator = (DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) ) {
  1498 		bind(function_to_bind);	}
  1499 	inline void bind(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7)) {
  1500 		m_Closure.bindstaticfunc(this, &FastDelegate7::InvokeStaticFunction,
  1501 			function_to_bind); }
  1502 	// Invoke the delegate
  1503 	RetType operator() (Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) const {
  1504 	return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3, p4, p5, p6, p7); }
  1505 	// Implicit conversion to "bool" using the safe_bool idiom
  1506 private:
  1507 	typedef struct SafeBoolStruct {
  1508 		int a_data_pointer_to_this_is_0_on_buggy_compilers;
  1509 		StaticFunctionPtr m_nonzero;
  1510 	} UselessTypedef;
  1511     typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type;
  1512 public:
  1513 	operator unspecified_bool_type() const {
  1514         return empty()? 0: &SafeBoolStruct::m_nonzero;
  1515     }
  1516 	// necessary to allow ==0 to work despite the safe_bool idiom
  1517 	inline bool operator==(StaticFunctionPtr funcptr) {
  1518 		return m_Closure.IsEqualToStaticFuncPtr(funcptr);	}
  1519 	inline bool operator!=(StaticFunctionPtr funcptr) { 
  1520 		return !m_Closure.IsEqualToStaticFuncPtr(funcptr);    }
  1521 	inline bool operator ! () const	{	// Is it bound to anything?
  1522 			return !m_Closure; }
  1523 	inline bool empty() const	{
  1524 			return !m_Closure; }
  1525 	void clear() { m_Closure.clear();}
  1526 	// Conversion to and from the DelegateMemento storage class
  1527 	const DelegateMemento & GetMemento() { return m_Closure; }
  1528 	void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); }
  1529 
  1530 private:	// Invoker for static functions
  1531 	RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) const {
  1532 	return (*(m_Closure.GetStaticFunction()))(p1, p2, p3, p4, p5, p6, p7); }
  1533 };
  1534 
  1535 
  1536 ////////////////////////////////////////////////////////////////////////////////
  1537 //						Fast Delegates, part 4:
  1538 //
  1539 //				FastDelegate<> class (Original author: Jody Hagins)
  1540 //	Allows boost::function style syntax like:
  1541 //			FastDelegate< double (int, long) >
  1542 // instead of:
  1543 //			FastDelegate2< int, long, double >
  1544 //
  1545 ////////////////////////////////////////////////////////////////////////////////
  1546 
  1547 #ifdef FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
  1548 
  1549 // Declare FastDelegate as a class template.  It will be specialized
  1550 // later for all number of arguments.
  1551 template <typename Signature>
  1552 class FastDelegate;
  1553 
  1554 //N=0
  1555 // Specialization to allow use of
  1556 // FastDelegate< R (  ) >
  1557 // instead of
  1558 // FastDelegate0 < R >
  1559 template<typename R>
  1560 class FastDelegate< R (  ) >
  1561   // Inherit from FastDelegate0 so that it can be treated just like a FastDelegate0
  1562   : public FastDelegate0 < R >
  1563 {
  1564 public:
  1565   // Make using the base type a bit easier via typedef.
  1566   typedef FastDelegate0 < R > BaseType;
  1567 
  1568   // Allow users access to the specific type of this delegate.
  1569   typedef FastDelegate SelfType;
  1570 
  1571   // Mimic the base class constructors.
  1572   FastDelegate() : BaseType() { }
  1573 
  1574   template < class X, class Y >
  1575   FastDelegate(Y * pthis,
  1576     R (X::* function_to_bind)(  ))
  1577     : BaseType(pthis, function_to_bind)  { }
  1578 
  1579   template < class X, class Y >
  1580   FastDelegate(const Y *pthis,
  1581       R (X::* function_to_bind)(  ) const)
  1582     : BaseType(pthis, function_to_bind)
  1583   {  }
  1584 
  1585   FastDelegate(R (*function_to_bind)(  ))
  1586     : BaseType(function_to_bind)  { }
  1587   void operator = (const BaseType &x)  {
  1588 		*static_cast<BaseType*>(this) = x; }
  1589 };
  1590 
  1591 //N=1
  1592 // Specialization to allow use of
  1593 // FastDelegate< R ( Param1 ) >
  1594 // instead of
  1595 // FastDelegate1 < Param1, R >
  1596 template<typename R, class Param1>
  1597 class FastDelegate< R ( Param1 ) >
  1598   // Inherit from FastDelegate1 so that it can be treated just like a FastDelegate1
  1599   : public FastDelegate1 < Param1, R >
  1600 {
  1601 public:
  1602   // Make using the base type a bit easier via typedef.
  1603   typedef FastDelegate1 < Param1, R > BaseType;
  1604 
  1605   // Allow users access to the specific type of this delegate.
  1606   typedef FastDelegate SelfType;
  1607 
  1608   // Mimic the base class constructors.
  1609   FastDelegate() : BaseType() { }
  1610 
  1611   template < class X, class Y >
  1612   FastDelegate(Y * pthis,
  1613     R (X::* function_to_bind)( Param1 p1 ))
  1614     : BaseType(pthis, function_to_bind)  { }
  1615 
  1616   template < class X, class Y >
  1617   FastDelegate(const Y *pthis,
  1618       R (X::* function_to_bind)( Param1 p1 ) const)
  1619     : BaseType(pthis, function_to_bind)
  1620   {  }
  1621 
  1622   FastDelegate(R (*function_to_bind)( Param1 p1 ))
  1623     : BaseType(function_to_bind)  { }
  1624   void operator = (const BaseType &x)  {
  1625 		*static_cast<BaseType*>(this) = x; }
  1626 };
  1627 
  1628 //N=2
  1629 // Specialization to allow use of
  1630 // FastDelegate< R ( Param1, Param2 ) >
  1631 // instead of
  1632 // FastDelegate2 < Param1, Param2, R >
  1633 template<typename R, class Param1, class Param2>
  1634 class FastDelegate< R ( Param1, Param2 ) >
  1635   // Inherit from FastDelegate2 so that it can be treated just like a FastDelegate2
  1636   : public FastDelegate2 < Param1, Param2, R >
  1637 {
  1638 public:
  1639   // Make using the base type a bit easier via typedef.
  1640   typedef FastDelegate2 < Param1, Param2, R > BaseType;
  1641 
  1642   // Allow users access to the specific type of this delegate.
  1643   typedef FastDelegate SelfType;
  1644 
  1645   // Mimic the base class constructors.
  1646   FastDelegate() : BaseType() { }
  1647 
  1648   template < class X, class Y >
  1649   FastDelegate(Y * pthis,
  1650     R (X::* function_to_bind)( Param1 p1, Param2 p2 ))
  1651     : BaseType(pthis, function_to_bind)  { }
  1652 
  1653   template < class X, class Y >
  1654   FastDelegate(const Y *pthis,
  1655       R (X::* function_to_bind)( Param1 p1, Param2 p2 ) const)
  1656     : BaseType(pthis, function_to_bind)
  1657   {  }
  1658 
  1659   FastDelegate(R (*function_to_bind)( Param1 p1, Param2 p2 ))
  1660     : BaseType(function_to_bind)  { }
  1661   void operator = (const BaseType &x)  {
  1662 		*static_cast<BaseType*>(this) = x; }
  1663 };
  1664 
  1665 //N=3
  1666 // Specialization to allow use of
  1667 // FastDelegate< R ( Param1, Param2, Param3 ) >
  1668 // instead of
  1669 // FastDelegate3 < Param1, Param2, Param3, R >
  1670 template<typename R, class Param1, class Param2, class Param3>
  1671 class FastDelegate< R ( Param1, Param2, Param3 ) >
  1672   // Inherit from FastDelegate3 so that it can be treated just like a FastDelegate3
  1673   : public FastDelegate3 < Param1, Param2, Param3, R >
  1674 {
  1675 public:
  1676   // Make using the base type a bit easier via typedef.
  1677   typedef FastDelegate3 < Param1, Param2, Param3, R > BaseType;
  1678 
  1679   // Allow users access to the specific type of this delegate.
  1680   typedef FastDelegate SelfType;
  1681 
  1682   // Mimic the base class constructors.
  1683   FastDelegate() : BaseType() { }
  1684 
  1685   template < class X, class Y >
  1686   FastDelegate(Y * pthis,
  1687     R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3 ))
  1688     : BaseType(pthis, function_to_bind)  { }
  1689 
  1690   template < class X, class Y >
  1691   FastDelegate(const Y *pthis,
  1692       R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3 ) const)
  1693     : BaseType(pthis, function_to_bind)
  1694   {  }
  1695 
  1696   FastDelegate(R (*function_to_bind)( Param1 p1, Param2 p2, Param3 p3 ))
  1697     : BaseType(function_to_bind)  { }
  1698   void operator = (const BaseType &x)  {
  1699 		*static_cast<BaseType*>(this) = x; }
  1700 };
  1701 
  1702 //N=4
  1703 // Specialization to allow use of
  1704 // FastDelegate< R ( Param1, Param2, Param3, Param4 ) >
  1705 // instead of
  1706 // FastDelegate4 < Param1, Param2, Param3, Param4, R >
  1707 template<typename R, class Param1, class Param2, class Param3, class Param4>
  1708 class FastDelegate< R ( Param1, Param2, Param3, Param4 ) >
  1709   // Inherit from FastDelegate4 so that it can be treated just like a FastDelegate4
  1710   : public FastDelegate4 < Param1, Param2, Param3, Param4, R >
  1711 {
  1712 public:
  1713   // Make using the base type a bit easier via typedef.
  1714   typedef FastDelegate4 < Param1, Param2, Param3, Param4, R > BaseType;
  1715 
  1716   // Allow users access to the specific type of this delegate.
  1717   typedef FastDelegate SelfType;
  1718 
  1719   // Mimic the base class constructors.
  1720   FastDelegate() : BaseType() { }
  1721 
  1722   template < class X, class Y >
  1723   FastDelegate(Y * pthis,
  1724     R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4 ))
  1725     : BaseType(pthis, function_to_bind)  { }
  1726 
  1727   template < class X, class Y >
  1728   FastDelegate(const Y *pthis,
  1729       R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4 ) const)
  1730     : BaseType(pthis, function_to_bind)
  1731   {  }
  1732 
  1733   FastDelegate(R (*function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4 ))
  1734     : BaseType(function_to_bind)  { }
  1735   void operator = (const BaseType &x)  {
  1736 		*static_cast<BaseType*>(this) = x; }
  1737 };
  1738 
  1739 //N=5
  1740 // Specialization to allow use of
  1741 // FastDelegate< R ( Param1, Param2, Param3, Param4, Param5 ) >
  1742 // instead of
  1743 // FastDelegate5 < Param1, Param2, Param3, Param4, Param5, R >
  1744 template<typename R, class Param1, class Param2, class Param3, class Param4, class Param5>
  1745 class FastDelegate< R ( Param1, Param2, Param3, Param4, Param5 ) >
  1746   // Inherit from FastDelegate5 so that it can be treated just like a FastDelegate5
  1747   : public FastDelegate5 < Param1, Param2, Param3, Param4, Param5, R >
  1748 {
  1749 public:
  1750   // Make using the base type a bit easier via typedef.
  1751   typedef FastDelegate5 < Param1, Param2, Param3, Param4, Param5, R > BaseType;
  1752 
  1753   // Allow users access to the specific type of this delegate.
  1754   typedef FastDelegate SelfType;
  1755 
  1756   // Mimic the base class constructors.
  1757   FastDelegate() : BaseType() { }
  1758 
  1759   template < class X, class Y >
  1760   FastDelegate(Y * pthis,
  1761     R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5 ))
  1762     : BaseType(pthis, function_to_bind)  { }
  1763 
  1764   template < class X, class Y >
  1765   FastDelegate(const Y *pthis,
  1766       R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5 ) const)
  1767     : BaseType(pthis, function_to_bind)
  1768   {  }
  1769 
  1770   FastDelegate(R (*function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5 ))
  1771     : BaseType(function_to_bind)  { }
  1772   void operator = (const BaseType &x)  {
  1773 		*static_cast<BaseType*>(this) = x; }
  1774 };
  1775 
  1776 //N=6
  1777 // Specialization to allow use of
  1778 // FastDelegate< R ( Param1, Param2, Param3, Param4, Param5, Param6 ) >
  1779 // instead of
  1780 // FastDelegate6 < Param1, Param2, Param3, Param4, Param5, Param6, R >
  1781 template<typename R, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6>
  1782 class FastDelegate< R ( Param1, Param2, Param3, Param4, Param5, Param6 ) >
  1783   // Inherit from FastDelegate6 so that it can be treated just like a FastDelegate6
  1784   : public FastDelegate6 < Param1, Param2, Param3, Param4, Param5, Param6, R >
  1785 {
  1786 public:
  1787   // Make using the base type a bit easier via typedef.
  1788   typedef FastDelegate6 < Param1, Param2, Param3, Param4, Param5, Param6, R > BaseType;
  1789 
  1790   // Allow users access to the specific type of this delegate.
  1791   typedef FastDelegate SelfType;
  1792 
  1793   // Mimic the base class constructors.
  1794   FastDelegate() : BaseType() { }
  1795 
  1796   template < class X, class Y >
  1797   FastDelegate(Y * pthis,
  1798     R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6 ))
  1799     : BaseType(pthis, function_to_bind)  { }
  1800 
  1801   template < class X, class Y >
  1802   FastDelegate(const Y *pthis,
  1803       R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6 ) const)
  1804     : BaseType(pthis, function_to_bind)
  1805   {  }
  1806 
  1807   FastDelegate(R (*function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6 ))
  1808     : BaseType(function_to_bind)  { }
  1809   void operator = (const BaseType &x)  {
  1810 		*static_cast<BaseType*>(this) = x; }
  1811 };
  1812 
  1813 //N=7
  1814 // Specialization to allow use of
  1815 // FastDelegate< R ( Param1, Param2, Param3, Param4, Param5, Param6, Param7 ) >
  1816 // instead of
  1817 // FastDelegate7 < Param1, Param2, Param3, Param4, Param5, Param6, Param7, R >
  1818 template<typename R, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7>
  1819 class FastDelegate< R ( Param1, Param2, Param3, Param4, Param5, Param6, Param7 ) >
  1820   // Inherit from FastDelegate7 so that it can be treated just like a FastDelegate7
  1821   : public FastDelegate7 < Param1, Param2, Param3, Param4, Param5, Param6, Param7, R >
  1822 {
  1823 public:
  1824   // Make using the base type a bit easier via typedef.
  1825   typedef FastDelegate7 < Param1, Param2, Param3, Param4, Param5, Param6, Param7, R > BaseType;
  1826 
  1827   // Allow users access to the specific type of this delegate.
  1828   typedef FastDelegate SelfType;
  1829 
  1830   // Mimic the base class constructors.
  1831   FastDelegate() : BaseType() { }
  1832 
  1833   template < class X, class Y >
  1834   FastDelegate(Y * pthis,
  1835     R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7 ))
  1836     : BaseType(pthis, function_to_bind)  { }
  1837 
  1838   template < class X, class Y >
  1839   FastDelegate(const Y *pthis,
  1840       R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7 ) const)
  1841     : BaseType(pthis, function_to_bind)
  1842   {  }
  1843 
  1844   FastDelegate(R (*function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7 ))
  1845     : BaseType(function_to_bind)  { }
  1846   void operator = (const BaseType &x)  {
  1847 		*static_cast<BaseType*>(this) = x; }
  1848 };
  1849 
  1850 
  1851 #endif //FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
  1852 
  1853 ////////////////////////////////////////////////////////////////////////////////
  1854 //						Fast Delegates, part 5:
  1855 //
  1856 //				MakeDelegate() helper function
  1857 //
  1858 //			MakeDelegate(&x, &X::func) returns a fastdelegate of the type
  1859 //			necessary for calling x.func() with the correct number of arguments.
  1860 //			This makes it possible to eliminate many typedefs from user code.
  1861 //
  1862 ////////////////////////////////////////////////////////////////////////////////
  1863 
  1864 // Also declare overloads of a MakeDelegate() global function to
  1865 // reduce the need for typedefs.
  1866 // We need seperate overloads for const and non-const member functions.
  1867 // Also, because of the weird rule about the class of derived member function pointers,
  1868 // implicit downcasts may need to be applied later to the 'this' pointer.
  1869 // That's why two classes (X and Y) appear in the definitions. Y must be implicitly
  1870 // castable to X.
  1871 
  1872 // Workaround for VC6. VC6 needs void return types converted into DefaultVoid.
  1873 // GCC 3.2 and later won't compile this unless it's preceded by 'typename',
  1874 // but VC6 doesn't allow 'typename' in this context.
  1875 // So, I have to use a macro.
  1876 
  1877 #ifdef FASTDLGT_VC6
  1878 #define FASTDLGT_RETTYPE detail::VoidToDefaultVoid<RetType>::type
  1879 #else
  1880 #define FASTDLGT_RETTYPE RetType
  1881 #endif
  1882 
  1883 //N=0
  1884 template <class X, class Y, class RetType>
  1885 FastDelegate0<FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)()) {
  1886 	return FastDelegate0<FASTDLGT_RETTYPE>(x, func);
  1887 }
  1888 
  1889 template <class X, class Y, class RetType>
  1890 FastDelegate0<FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)() const) {
  1891 	return FastDelegate0<FASTDLGT_RETTYPE>(x, func);
  1892 }
  1893 
  1894 template <class RetType>
  1895 FastDelegate0<FASTDLGT_RETTYPE> MakeDelegate(RetType (*func)()) {
  1896 	return FastDelegate0<FASTDLGT_RETTYPE>(func);
  1897 }
  1898 
  1899 //N=1
  1900 template <class X, class Y, class Param1, class RetType>
  1901 FastDelegate1<Param1, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1)) {
  1902 	return FastDelegate1<Param1, FASTDLGT_RETTYPE>(x, func);
  1903 }
  1904 
  1905 template <class X, class Y, class Param1, class RetType>
  1906 FastDelegate1<Param1, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1) const) {
  1907 	return FastDelegate1<Param1, FASTDLGT_RETTYPE>(x, func);
  1908 }
  1909 
  1910 template <class Param1, class RetType>
  1911 FastDelegate1<Param1, FASTDLGT_RETTYPE> MakeDelegate(RetType (*func)(Param1 p1)) {
  1912 	return FastDelegate1<Param1, FASTDLGT_RETTYPE>(func);
  1913 }
  1914 
  1915 //N=2
  1916 template <class X, class Y, class Param1, class Param2, class RetType>
  1917 FastDelegate2<Param1, Param2, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2)) {
  1918 	return FastDelegate2<Param1, Param2, FASTDLGT_RETTYPE>(x, func);
  1919 }
  1920 
  1921 template <class X, class Y, class Param1, class Param2, class RetType>
  1922 FastDelegate2<Param1, Param2, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2) const) {
  1923 	return FastDelegate2<Param1, Param2, FASTDLGT_RETTYPE>(x, func);
  1924 }
  1925 
  1926 template <class Param1, class Param2, class RetType>
  1927 FastDelegate2<Param1, Param2, FASTDLGT_RETTYPE> MakeDelegate(RetType (*func)(Param1 p1, Param2 p2)) {
  1928 	return FastDelegate2<Param1, Param2, FASTDLGT_RETTYPE>(func);
  1929 }
  1930 
  1931 //N=3
  1932 template <class X, class Y, class Param1, class Param2, class Param3, class RetType>
  1933 FastDelegate3<Param1, Param2, Param3, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3)) {
  1934 	return FastDelegate3<Param1, Param2, Param3, FASTDLGT_RETTYPE>(x, func);
  1935 }
  1936 
  1937 template <class X, class Y, class Param1, class Param2, class Param3, class RetType>
  1938 FastDelegate3<Param1, Param2, Param3, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3) const) {
  1939 	return FastDelegate3<Param1, Param2, Param3, FASTDLGT_RETTYPE>(x, func);
  1940 }
  1941 
  1942 template <class Param1, class Param2, class Param3, class RetType>
  1943 FastDelegate3<Param1, Param2, Param3, FASTDLGT_RETTYPE> MakeDelegate(RetType (*func)(Param1 p1, Param2 p2, Param3 p3)) {
  1944 	return FastDelegate3<Param1, Param2, Param3, FASTDLGT_RETTYPE>(func);
  1945 }
  1946 
  1947 //N=4
  1948 template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class RetType>
  1949 FastDelegate4<Param1, Param2, Param3, Param4, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4)) {
  1950 	return FastDelegate4<Param1, Param2, Param3, Param4, FASTDLGT_RETTYPE>(x, func);
  1951 }
  1952 
  1953 template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class RetType>
  1954 FastDelegate4<Param1, Param2, Param3, Param4, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4) const) {
  1955 	return FastDelegate4<Param1, Param2, Param3, Param4, FASTDLGT_RETTYPE>(x, func);
  1956 }
  1957 
  1958 template <class Param1, class Param2, class Param3, class Param4, class RetType>
  1959 FastDelegate4<Param1, Param2, Param3, Param4, FASTDLGT_RETTYPE> MakeDelegate(RetType (*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4)) {
  1960 	return FastDelegate4<Param1, Param2, Param3, Param4, FASTDLGT_RETTYPE>(func);
  1961 }
  1962 
  1963 //N=5
  1964 template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class RetType>
  1965 FastDelegate5<Param1, Param2, Param3, Param4, Param5, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5)) {
  1966 	return FastDelegate5<Param1, Param2, Param3, Param4, Param5, FASTDLGT_RETTYPE>(x, func);
  1967 }
  1968 
  1969 template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class RetType>
  1970 FastDelegate5<Param1, Param2, Param3, Param4, Param5, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) const) {
  1971 	return FastDelegate5<Param1, Param2, Param3, Param4, Param5, FASTDLGT_RETTYPE>(x, func);
  1972 }
  1973 
  1974 template <class Param1, class Param2, class Param3, class Param4, class Param5, class RetType>
  1975 FastDelegate5<Param1, Param2, Param3, Param4, Param5, FASTDLGT_RETTYPE> MakeDelegate(RetType (*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5)) {
  1976 	return FastDelegate5<Param1, Param2, Param3, Param4, Param5, FASTDLGT_RETTYPE>(func);
  1977 }
  1978 
  1979 //N=6
  1980 template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class RetType>
  1981 FastDelegate6<Param1, Param2, Param3, Param4, Param5, Param6, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6)) {
  1982 	return FastDelegate6<Param1, Param2, Param3, Param4, Param5, Param6, FASTDLGT_RETTYPE>(x, func);
  1983 }
  1984 
  1985 template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class RetType>
  1986 FastDelegate6<Param1, Param2, Param3, Param4, Param5, Param6, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) const) {
  1987 	return FastDelegate6<Param1, Param2, Param3, Param4, Param5, Param6, FASTDLGT_RETTYPE>(x, func);
  1988 }
  1989 
  1990 template <class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class RetType>
  1991 FastDelegate6<Param1, Param2, Param3, Param4, Param5, Param6, FASTDLGT_RETTYPE> MakeDelegate(RetType (*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6)) {
  1992 	return FastDelegate6<Param1, Param2, Param3, Param4, Param5, Param6, FASTDLGT_RETTYPE>(func);
  1993 }
  1994 
  1995 //N=7
  1996 template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class RetType>
  1997 FastDelegate7<Param1, Param2, Param3, Param4, Param5, Param6, Param7, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7)) {
  1998 	return FastDelegate7<Param1, Param2, Param3, Param4, Param5, Param6, Param7, FASTDLGT_RETTYPE>(x, func);
  1999 }
  2000 
  2001 template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class RetType>
  2002 FastDelegate7<Param1, Param2, Param3, Param4, Param5, Param6, Param7, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) const) {
  2003 	return FastDelegate7<Param1, Param2, Param3, Param4, Param5, Param6, Param7, FASTDLGT_RETTYPE>(x, func);
  2004 }
  2005 
  2006 template <class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class RetType>
  2007 FastDelegate7<Param1, Param2, Param3, Param4, Param5, Param6, Param7, FASTDLGT_RETTYPE> MakeDelegate(RetType (*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7)) {
  2008 	return FastDelegate7<Param1, Param2, Param3, Param4, Param5, Param6, Param7, FASTDLGT_RETTYPE>(func);
  2009 }
  2010 
  2011 
  2012  // clean up after ourselves...
  2013 #undef FASTDLGT_RETTYPE
  2014 
  2015 } // namespace fastdelegate
  2016 
  2017 #endif // !defined(FASTDELEGATE_H)