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