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