1.FastDelegate.h
// FastDelegate.h // Efficient delegates in C++ that generate only two lines of asm code! // Documentation is found at http://www.codeproject.com/cpp/FastDelegate.asp // // - Don Clugston, Mar 2004. // Major contributions were made by Jody Hagins. // History: // 24-Apr-04 1.0 * Submitted to CodeProject. // 28-Apr-04 1.1 * Prevent most unsafe uses of evil static function hack. // * Improved syntax for horrible_cast (thanks Paul Bludov). // * Tested on Metrowerks MWCC and Intel ICL (IA32) // * Compiled, but not run, on Comeau C++ and Intel Itanium ICL. // 27-Jun-04 1.2 * Now works on Borland C++ Builder 5.5 // * Now works on /clr "managed C++" code on VC7, VC7.1 // * Comeau C++ now compiles without warnings. // * Prevent the virtual inheritance case from being used on // VC6 and earlier, which generate incorrect code. // * Improved warning and error messages. Non-standard hacks // now have compile-time checks to make them safer. // * implicit_cast used instead of static_cast in many cases. // * If calling a const member function, a const class pointer can be used. // * MakeDelegate() global helper function added to simplify pass-by-value. // * Added fastdelegate.clear() // 16-Jul-04 1.2.1* Workaround for gcc bug (const member function pointers in templates) // 30-Oct-04 1.3 * Support for (non-void) return values. // * No more workarounds in client code! // MSVC and Intel now use a clever hack invented by John Dlugosz: // - The FASTDELEGATEDECLARE workaround is no longer necessary. // - No more warning messages for VC6 // * Less use of macros. Error messages should be more comprehensible. // * Added include guards // * Added FastDelegate::empty() to test if invocation is safe (Thanks Neville Franks). // * Now tested on VS 2005 Express Beta, PGI C++ // 24-Dec-04 1.4 * Added DelegateMemento, to allow collections of disparate delegates. // * <,>,<=,>= comparison operators to allow storage in ordered containers. // * Substantial reduction of code size, especially the 'Closure' class. // * Standardised all the compiler-specific workarounds. // * MFP conversion now works for CodePlay (but not yet supported in the full code). // * Now compiles without warnings on _any_ supported compiler, including BCC 5.5.1 // * New syntax: FastDelegate< int (char *, double) >. // 14-Feb-05 1.4.1* Now treats =0 as equivalent to .clear(), ==0 as equivalent to .empty(). (Thanks elfric). // * Now tested on Intel ICL for AMD64, VS2005 Beta for AMD64 and Itanium. // 30-Mar-05 1.5 * Safebool idiom: "if (dg)" is now equivalent to "if (!dg.empty())" // * Fully supported by CodePlay VectorC // * Bugfix for Metrowerks: empty() was buggy because a valid MFP can be 0 on MWCC! // * More optimal assignment,== and != operators for static function pointers. #ifndef FASTDELEGATE_H #define FASTDELEGATE_H #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include <memory.h> // to allow '<','>' comparisons //////////////////////////////////////////////////////////////////////////////// // Configuration options // //////////////////////////////////////////////////////////////////////////////// // Uncomment the following #define for optimally-sized delegates. // In this case, the generated asm code is almost identical to the code you'd get // if the compiler had native support for delegates. // It will not work on systems where sizeof(dataptr) < sizeof(codeptr). // Thus, it will not work for DOS compilers using the medium model. // It will also probably fail on some DSP systems. // 取消注释下面的#define获得委托的大小优化 // 如果编译器本地支持委托,在这种情况下, // 你会得到几乎与asm一样的代码 // 它不会工作在那些 sizeof(dataptr) < sizeof(codeptr)的平台上 // 因此它不能支持使用 the medium model的dos编译器 // 在某些dsp系统上也不能工作 #define FASTDELEGATE_USESTATICFUNCTIONHACK // Uncomment the next line to allow function declarator syntax. // It is automatically enabled for those compilers where it is known to work. //#define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX //////////////////////////////////////////////////////////////////////////////// // Compiler identification for workarounds // //////////////////////////////////////////////////////////////////////////////// // Compiler identification. It's not easy to identify Visual C++ because // many vendors fraudulently define Microsoft's identifiers. #if defined(_MSC_VER) && !defined(__MWERKS__) && !defined(__VECTOR_C) && !defined(__ICL) && !defined(__BORLANDC__) #define FASTDLGT_ISMSVC #if (_MSC_VER <1300) // Many workarounds are required for VC6. #define FASTDLGT_VC6 #pragma warning(disable:4786) // disable this ridiculous warning #endif #endif // Does the compiler uses Microsoft's member function pointer structure? // If so, it needs special treatment. // Metrowerks CodeWarrior, Intel, and CodePlay fraudulently define Microsoft's // identifier, _MSC_VER. We need to filter Metrowerks out. #if defined(_MSC_VER) && !defined(__MWERKS__) #define FASTDLGT_MICROSOFT_MFP #if !defined(__VECTOR_C) // CodePlay doesn't have the __single/multi/virtual_inheritance keywords #define FASTDLGT_HASINHERITANCE_KEYWORDS #endif #endif // Does it allow function declarator syntax? The following compilers are known to work: #if defined(FASTDLGT_ISMSVC) && (_MSC_VER >=1310) // VC 7.1 #define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX #endif // Gcc(2.95+), and versions of Digital Mars, Intel and Comeau in common use. #if defined (__DMC__) || defined(__GNUC__) || defined(__ICL) || defined(__COMO__) #define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX #endif // It works on Metrowerks MWCC 3.2.2. From boost.Config it should work on earlier ones too. #if defined (__MWERKS__) #define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX #endif #ifdef __GNUC__ // Workaround GCC bug #8271 // At present, GCC doesn't recognize constness of MFPs in templates #define FASTDELEGATE_GCC_BUG_8271 #endif //////////////////////////////////////////////////////////////////////////////// // General tricks used in this code // // (a) Error messages are generated by typdefing an array of negative size to // generate compile-time errors. // (b) Warning messages on MSVC are generated by declaring unused variables, and // enabling the "variable XXX is never used" warning. // (c) Unions are used in a few compiler-specific cases to perform illegal casts. // (d) For Microsoft and Intel, when adjusting the 'this' pointer, it's cast to // (char *) first to ensure that the correct number of *bytes* are added. // // 用于此代码的通用技巧 // (a) 通过定义小于0的负数大小的数组来生成错误信息,以生成编译期错误 // (b) 在msvc上的警告信息通过声明未使用变量,并使msvc允许"variable XXX is never used"警告 // (c) 联合(Unions)在一些编译器特定情况下,以执行‘不合标准’的计算 // (d) 对于Microsoft 和Intel,当调整“this”指针时,首先转化到 (char*)来保证正确数量的字节(byte)被添加 //////////////////////////////////////////////////////////////////////////////// // Helper templates -- 帮助类模板 // //////////////////////////////////////////////////////////////////////////////// namespace fastdelegate { namespace detail { // we'll hide the implementation details in a nested namespace. // implicit_cast< > // I believe this was originally going to be in the C++ standard but // was left out by accident. It's even milder than static_cast. // I use it instead of static_cast<> to emphasize that I'm not doing // anything nasty. // Usage is identical to static_cast<> // implicit_cast< > 隐式转换 // 我相信它应该留在c++标准中,但是意外却没有。它甚至比static_cast还要好 // 我使用它以代替static_cast来强调“我没有做任何肮脏(不安全)的事情” // 用法与static_cast 一致 template <class OutputClass, class InputClass> inline OutputClass implicit_cast(InputClass input){ return input; } // horrible_cast< > // This is truly evil. It completely subverts C++'s type system, allowing you // to cast from any class to any other class. Technically, using a union // to perform the cast is undefined behaviour (even in C). But we can see if // it is OK by checking that the union is the same size as each of its members. // horrible_cast<> should only be used for compiler-specific workarounds. // Usage is identical to reinterpret_cast<>. // This union is declared outside the horrible_cast because BCC 5.5.1 // can't inline a function with a nested class, and gives a warning. // horrible_cast< > // 它确实邪恶。它完全颠覆了c++的类型系统,允许你来从一个类型转换到任何其他类型。 // 从技术上讲,用union来进行转换是不明确的行文(undefined behaviour)(即便在c中)。 // 但是我们可以通过测试union的大小是否与他的每一个成员的大小一致来判断union是否正确。 // horrible_cast<>应该只工作在编译器特定的工作区域内。 // 使用方法与reinterpret_cast<>相同 // 该union在horrible_cast之外声明,因为BCC 5.5.1不能够内敛带有内嵌类的函数,并给出一个警告。 template <class OutputClass, class InputClass> union horrible_union{ OutputClass out; InputClass in; }; template <class OutputClass, class InputClass> inline OutputClass horrible_cast(const InputClass input){ horrible_union<OutputClass, InputClass> u; // Cause a compile-time error if in, out and u are not the same size. // If the compile fails here, it means the compiler has peculiar // unions which would prevent the cast from working. // 产生编译期错误,如果输入、输出与u的大小不一致 // 如果编译失败,表示编译器有peculiar unions,它阻止‘转换(cast)’进行 typedef int ERROR_CantUseHorrible_cast[sizeof(InputClass)==sizeof(u) && sizeof(InputClass)==sizeof(OutputClass) ? 1 : -1]; u.in = input; return u.out; } //////////////////////////////////////////////////////////////////////////////// // Workarounds - 作业区 // //////////////////////////////////////////////////////////////////////////////// // Backwards compatibility: This macro used to be necessary in the virtual inheritance // case for Intel and Microsoft. Now it just forward-declares the class. // 向后兼容性:过去在Intel 和 Microsoft平台中虚继承情况下,在使用这个宏是有必要的。现在它只是前向声明该类。 #define FASTDELEGATEDECLARE(CLASSNAME) class CLASSNAME; // Prevent use of the static function hack with the DOS medium model. #ifdef __MEDIUM__ #undef FASTDELEGATE_USESTATICFUNCTIONHACK #endif // DefaultVoid - a workaround for 'void' templates in VC6. // // (1) VC6 and earlier do not allow 'void' as a default template argument. // (2) They also doesn't allow you to return 'void' from a function. // // Workaround for (1): Declare a dummy type 'DefaultVoid' which we use // when we'd like to use 'void'. We convert it into 'void' and back // using the templates DefaultVoidToVoid<> and VoidToDefaultVoid<>. // Workaround for (2): On VC6, the code for calling a void function is // identical to the code for calling a non-void function in which the // return value is never used, provided the return value is returned // in the EAX register, rather than on the stack. // This is true for most fundamental types such as int, enum, void *. // Const void * is the safest option since it doesn't participate // in any automatic conversions. But on a 16-bit compiler it might // cause extra code to be generated, so we disable it for all compilers // except for VC6 (and VC5). /* (1) vc6及早期的编译器不支持 void类型作为默认模板参数 (2) 他们也不允许你从一个函数里返回 ‘void’ */ #ifdef FASTDLGT_VC6 // VC6 workaround typedef const void * DefaultVoid; #else // On any other compiler, just use a normal void. typedef void DefaultVoid; #endif // Translate from 'DefaultVoid' to 'void'. // Everything else is unchanged template <class T> struct DefaultVoidToVoid { typedef T type; }; template <> struct DefaultVoidToVoid<DefaultVoid> { typedef void type; }; // Translate from 'void' into 'DefaultVoid' // Everything else is unchanged template <class T> struct VoidToDefaultVoid { typedef T type; }; template <> struct VoidToDefaultVoid<void> { typedef DefaultVoid type; }; //////////////////////////////////////////////////////////////////////////////// // Fast Delegates, part 1: // // Conversion of member function pointer to a standard form // 转换成员函数指针到一种标准形式 // //////////////////////////////////////////////////////////////////////////////// /* Implementations of Member Function Pointers So, how do compilers typically implement member function pointers? Here are some results obtained by applying the sizeof operator to various structures (an int, a void * data pointer, a code pointer (i.e., a pointer to a static function), and a member function pointer to a class with single-, multiple-, virtual- inheritance, or unknown (i.e., forward declared)) for a variety of 32, 64 and 16-bit compilers. */ /* Compiler Options int DataPtr CodePtr Single Multi Virtual Unknown MSVC 4 4 4 4 8 12 16 MSVC /vmg 4 4 4 16# 16# 16# 16 MSVC /vmg /vmm 4 4 4 8# 8# -- 8 Intel_IA32 4 4 4 4 8 12 16 Intel_IA32 /vmg /vmm 4 4 4 4 8 -- 8 Intel_Itanium 4 8 8 8 12 16 20 G++ 4 4 4 8 8 8 8 Comeau 4 4 4 8 8 8 8 DMC 4 4 4 4 4 4 4 BCC32 4 4 4 12 12 12 12 BCC32/Vmd 4 4 4 4 8 12 12 WCL386 4 4 4 12 12 12 12 CodeWarrior 4 4 4 12 12 12 12 XLC 4 8 8 20 20 20 20 DMC small 2 2 2 2 2 2 2 medium 2 2 4 4 4 4 4 WCL small 2 2 2 6 6 6 6 compact 2 4 2 6 6 6 6 medium 2 2 4 8 8 8 8 large 2 4 4 8 8 8 8 # Or 4,8, or 12 if the __single/ __multi/ __virtual_inheritance keyword is used. */ // GenericClass is a fake class, ONLY used to provide a type. // It is vitally important that it is never defined, so that the compiler doesn't // think it can optimize the invocation. For example, Borland generates simpler // code if it knows the class only uses single inheritance. // Compilers using Microsoft's structure need to be treated as a special case. // GenericClass是个伪类,只是用来提供一个类型 // 它及其重要:它从不被定义,因此编译器认为不能对它的调用进行优化 // 如:Borland生成更简单的代码如果它知道该类只使用了单一继承 // 使用Microsoft的结构的编译器需要特殊对待 #ifdef FASTDLGT_MICROSOFT_MFP #pragma message("FASTDLGT_MICROSOFT_MFP is defined") #ifdef FASTDLGT_HASINHERITANCE_KEYWORDS #pragma message("FASTDLGT_HASINHERITANCE_KEYWORDS is defined") // For Microsoft and Intel, we want to ensure that it's the most efficient type of MFP // (4 bytes), even when the /vmg option is used. Declaring an empty class // would give 16 byte pointers in this case.... class __single_inheritance GenericClass; #endif // ...but for Codeplay, an empty class *always* gives 4 byte pointers. // If compiled with the /clr option ("managed C++"), the JIT compiler thinks // it needs to load GenericClass before it can call any of its functions, // (compiles OK but crashes at runtime!), so we need to declare an // empty class to make it happy. // Codeplay and VC4 can't cope with the unknown_inheritance case either. class GenericClass {}; #else class GenericClass; #endif // The size of a single inheritance member function pointer. // 单一继承下成员函数指针的大小 const int SINGLE_MEMFUNCPTR_SIZE = sizeof(void (GenericClass::*)()); // SimplifyMemFunc< >::Convert() // // A template function that converts an arbitrary member function pointer into the // simplest possible form of member function pointer, using a supplied 'this' pointer. // According to the standard, this can be done legally with reinterpret_cast<>. // For (non-standard) compilers which use member function pointers which vary in size // depending on the class, we need to use knowledge of the internal structure of a // member function pointer, as used by the compiler. Template specialization is used // to distinguish between the sizes. Because some compilers don't support partial // template specialisation, I use full specialisation of a wrapper struct. // 使用一个提供的‘this’指针,该模板函数将任意一个成员函数指针转化为成员函数指针尽可能简单的形式。 // 根据标准,该操作可以被reinterpret_cast合法转换。 // 对于非标准的编译器,它们的成员函数指针根据类的不同在大小上有变化, // 因此我们需要知道成员函数指针内部结构,该结构也被编译器所使用 // 模板特化以区分不同的大小。因某些编译器不支持偏特化,我列出所有具体的封装结构 // general case -- don't know how to convert it. Force a compile failure // 一般情况-- 不知道如何转换:强制生成编译错误 template <int N> struct SimplifyMemFunc { template <class X, class XFuncType, class GenericMemFuncType> inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind, GenericMemFuncType &bound_func) { // Unsupported member function type -- force a compile failure. // (it's illegal to have a array with negative size). typedef char ERROR_Unsupported_member_function_pointer_on_this_compiler[N-100]; return 0; } }; // For compilers where all member func ptrs are the same size, everything goes here. // For non-standard compilers, only single_inheritance classes go here. // 对于 所有的成员函数指针都是相同大小的编译器,都应用于此处 // 对于非标准的编译器,只有single_inheritance的类适用 template <> struct SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE> { template <class X, class XFuncType, class GenericMemFuncType> inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind, GenericMemFuncType &bound_func) { #if defined __DMC__ // Digital Mars doesn't allow you to cast between abitrary PMF's, // even though the standard says you can. The 32-bit compiler lets you // static_cast through an int, but the DOS compiler doesn't. bound_func = horrible_cast<GenericMemFuncType>(function_to_bind); #else bound_func = reinterpret_cast<GenericMemFuncType>(function_to_bind); #endif return reinterpret_cast<GenericClass *>(pthis); } }; //////////////////////////////////////////////////////////////////////////////// // Fast Delegates, part 1b: // // Workarounds for Microsoft and Intel // //////////////////////////////////////////////////////////////////////////////// // Compilers with member function pointers which violate the standard (MSVC, Intel, Codeplay), // need to be treated as a special case. // 对于那些违反标准的编译器(msvc,Intel,CodePlay)需要特殊处理 #ifdef FASTDLGT_MICROSOFT_MFP // We use unions to perform horrible_casts. I would like to use #pragma pack(push, 1) // at the start of each function for extra safety, but VC6 seems to ICE // intermittently if you do this inside a template. // 我们使用联合来执行 horrible_casts.我倾向在每个函数 // 的开始处使用 #pragma pack(push, 1)来保证额外的安全,但是vc6... // __multiple_inheritance classes go here // Nasty hack for Microsoft and Intel (IA32 and Itanium) // 多重继承类-此处适用 // Nasty hack for Microsoft and Intel (IA32 and Itanium) template<> struct SimplifyMemFunc< SINGLE_MEMFUNCPTR_SIZE + sizeof(int) > { template <class X, class XFuncType, class GenericMemFuncType> inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind, GenericMemFuncType &bound_func) { // We need to use a horrible_cast to do this conversion. // In MSVC, a multiple inheritance member pointer is internally defined as: // 我们需要使用horrible_cast来完成这个转换 // 在msvc里,多重继承成员指针内部定义为: union { XFuncType func; struct { GenericMemFuncType funcaddress; // points to the actual member function - 指向实际的成员函数 int delta; // #BYTES to be added to the 'this' pointer - 多少bytes需要添加到this上,即偏移 }s; } u; // Check that the horrible_cast will work typedef int ERROR_CantUsehorrible_cast[sizeof(function_to_bind)==sizeof(u.s)? 1 : -1]; u.func = function_to_bind; bound_func = u.s.funcaddress; return reinterpret_cast<GenericClass *>(reinterpret_cast<char *>(pthis) + u.s.delta); } }; // virtual inheritance is a real nuisance. It's inefficient and complicated. // On MSVC and Intel, there isn't enough information in the pointer itself to // enable conversion to a closure pointer. Earlier versions of this code didn't // work for all cases, and generated a compile-time error instead. // But a very clever hack invented by John M. Dlugosz solves this problem. // My code is somewhat different to his: I have no asm code, and I make no // assumptions about the calling convention that is used. // In VC++ and ICL, a virtual_inheritance member pointer // is internally defined as: // 虚拟继承是真正的麻烦所在:效率低下而且复杂。 // 在Msvc和intel上,指针自身没有足够的信息来转化为一个闭包的指针。 // 该代码的早期版本不适用所有的情况,并生成编译期错误信息 // 但是John M. Dlugosz用一个非常聪明的手段解决了这个问题。 // 可我的代码稍有不同:我没有使用asm代码,并且我没有假设所使用的调用约定 // 在vc++ 和 icl,虚继承成员指针 内部定义为如下: struct MicrosoftVirtualMFP { void (GenericClass::*codeptr)(); // points to the actual member function -指向实际成员函数 int delta; // #bytes to be added to the 'this' pointer -需要添加多少bytes到‘this’指针上 int vtable_index; // or 0 if no virtual inheritance -如果没有虚继承则为0 }; // The CRUCIAL feature of Microsoft/Intel MFPs which we exploit is that the // m_codeptr member is *always* called, regardless of the values of the other // members. (This is *not* true for other compilers, eg GCC, which obtain the // function address from the vtable if a virtual function is being called). // Dlugosz's trick is to make the codeptr point to a probe function which // returns the 'this' pointer that was used. // Define a generic class that uses virtual inheritance. // It has a trival member function that returns the value of the 'this' pointer. // Microsoft/Intel MFPs上一个非常重要的特点是:我们发现-不管其他成员的值是多少, //‘m_codeptr’成员“总是”被调用(这对于其他编译器则不是这样, // 例如gcc,如果一个虚函数被调用,它从vtable中获取函数地址) // Dlugosz的技巧是使codeptr指针指向一个探针(probe)函数,该函数返回值是使用的‘this’指针。 // 定义一个使用虚继承的泛类 // 它有一个trival成员函数,返回‘this’指针的值 struct GenericVirtualClass : virtual public GenericClass { typedef GenericVirtualClass * (GenericVirtualClass::*ProbePtrType)(); GenericVirtualClass * GetThis() { return this; } }; /* struct MicrosoftVirtualMFP { void (GenericClass::*codeptr)(); // points to the actual member function -指向实际成员函数 int delta; // #bytes to be added to the 'this' pointer -需要添加多少bytes到‘this’指针上 int vtable_index; // or 0 if no virtual inheritance -如果没有虚继承则为0 }; */ // __virtual_inheritance classes go here template <> struct SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE + 2*sizeof(int) > { template <class X, class XFuncType, class GenericMemFuncType> inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind, GenericMemFuncType &bound_func) { union { XFuncType func; GenericClass* (X::*ProbeFunc)(); MicrosoftVirtualMFP s; } u; u.func = function_to_bind; bound_func = reinterpret_cast<GenericMemFuncType>(u.s.codeptr); union { GenericVirtualClass::ProbePtrType virtfunc; MicrosoftVirtualMFP s; } u2; // Check that the horrible_cast<>s will work // 检测horrible_cast是否工作 typedef int ERROR_CantUsehorrible_cast[sizeof(function_to_bind)==sizeof(u.s) && sizeof(function_to_bind)==sizeof(u.ProbeFunc) && sizeof(u2.virtfunc)==sizeof(u2.s) ? 1 : -1]; // Unfortunately, taking the address of a MF prevents it from being inlined, so // this next line can't be completely optimised away by the compiler. // 不幸的是,取MF的地址阻止了它被内联,因此下面这行代码不能够被编译器完全优化 u2.virtfunc = &GenericVirtualClass::GetThis; u.s.codeptr = u2.s.codeptr; /* ProbeFunc = GetThis(); this->Func = Func('this); // 'this may be not equal this depends on "ProbeFunc"'s info */ return (pthis->*u.ProbeFunc)(); } }; #if (_MSC_VER <1300) // Nasty hack for Microsoft Visual C++ 6.0 // unknown_inheritance classes go here // There is a compiler bug in MSVC6 which generates incorrect code in this case!! // 未知继承类-适用此处 // 这是vc6的编译器的一个bug在这种情况下,生成错误的代码 template <> struct SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE + 3*sizeof(int) > { template <class X, class XFuncType, class GenericMemFuncType> inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind, GenericMemFuncType &bound_func) { // There is an apalling but obscure compiler bug in MSVC6 and earlier: // vtable_index and 'vtordisp' are always set to 0 in the // unknown_inheritance case! // This means that an incorrect function could be called!!! // Compiling with the /vmg option leads to potentially incorrect code. // This is probably the reason that the IDE has a user interface for specifying // the /vmg option, but it is disabled - you can only specify /vmg on // the command line. In VC1.5 and earlier, the compiler would ICE if it ever // encountered this situation. // It is OK to use the /vmg option if /vmm or /vms is specified. // Fortunately, the wrong function is only called in very obscure cases. // It only occurs when a derived class overrides a virtual function declared // in a virtual base class, and the member function // points to the *Derived* version of that function. The problem can be // completely averted in 100% of cases by using the *Base class* for the // member fpointer. Ie, if you use the base class as an interface, you'll // stay out of trouble. // Occasionally, you might want to point directly to a derived class function // that isn't an override of a base class. In this case, both vtable_index // and 'vtordisp' are zero, but a virtual_inheritance pointer will be generated. // We can generate correct code in this case. To prevent an incorrect call from // ever being made, on MSVC6 we generate a warning, and call a function to // make the program crash instantly. typedef char ERROR_VC6CompilerBug[-100]; return 0; } }; #else // Nasty hack for Microsoft and Intel (IA32 and Itanium) // unknown_inheritance classes go here // This is probably the ugliest bit of code I've ever written. Look at the casts! // There is a compiler bug in MSVC6 which prevents it from using this code. template <> struct SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE + 3*sizeof(int) > { template <class X, class XFuncType, class GenericMemFuncType> inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind, GenericMemFuncType &bound_func) { // The member function pointer is 16 bytes long. We can't use a normal cast, but // we can use a union to do the conversion. // 成员函数指针的大小是16个字节。我们不能使用正当的转换, // 但是我们可以使用union来进行转换。 union { XFuncType func; // In VC++ and ICL, an unknown_inheritance member pointer // is internally defined as: // 在vc++和icl,一个未知继承的成员指针内部定义为: struct { GenericMemFuncType m_funcaddress; // points to the actual member function -指向实际成员函数 int delta; // #bytes to be added to the 'this' pointer -加到‘this’指针的值 int vtordisp; // #bytes to add to 'this' to find the vtable -加到‘this’指针值来寻址vtable int vtable_index; // or 0 if no virtual inheritance -如果没有虚继承则为0 } s; } u; // Check that the horrible_cast will work typedef int ERROR_CantUsehorrible_cast[sizeof(XFuncType)==sizeof(u.s)? 1 : -1]; u.func = function_to_bind; bound_func = u.s.funcaddress; int virtual_delta = 0; if (u.s.vtable_index) { // Virtual inheritance is used // First, get to the vtable. // It is 'vtordisp' bytes from the start of the class. // 首先找到vtable, // 它在类开始处‘vtordisp’字节的偏移处 const int * vtable = *reinterpret_cast<const int *const*>( reinterpret_cast<const char *>(pthis) + u.s.vtordisp ); // 'vtable_index' tells us where in the table we should be looking. // ‘vtable_index’表示 该函数在表中的哪个具体位置 virtual_delta = u.s.vtordisp + *reinterpret_cast<const int *>( reinterpret_cast<const char *>(vtable) + u.s.vtable_index); } // The int at 'virtual_delta' gives us the amount to add to 'this'. // Finally we can add the three components together. Phew! return reinterpret_cast<GenericClass *>( reinterpret_cast<char *>(pthis) + u.s.delta + virtual_delta); }; }; #endif // MSVC 7 and greater #endif // MS/Intel hacks } // namespace detail //////////////////////////////////////////////////////////////////////////////// // Fast Delegates, part 2: // // Define the delegate storage, and cope with static functions // 定义代理存储,及静态函数处理 // //////////////////////////////////////////////////////////////////////////////// // DelegateMemento -- an opaque structure which can hold an arbitary delegate. // It knows nothing about the calling convention or number of arguments used by // the function pointed to. // It supplies comparison operators so that it can be stored in STL collections. // It cannot be set to anything other than null, nor invoked directly: // it must be converted to a specific delegate. // DelegateMemento(代理备忘录)--一个保存任意delegate的不透明的结构。 // 它对函数指向的调用转换或者参数的数目一无所知。 // 它提供比较操作符,因此可以存放在stl中。 // It cannot be set to anything other than null, nor invoked directly: // it must be converted to a specific delegate. // Implementation: // There are two possible implementations: the Safe method and the Evil method. // DelegateMemento - Safe version // // This implementation is standard-compliant, but a bit tricky. - 这个实现是标准兼容的,但有点棘手 // A static function pointer is stored inside the class. // Here are the valid values: // +-- Static pointer --+--pThis --+-- pMemFunc-+-- Meaning------+ // | 0 | 0 | 0 | Empty | // | !=0 |(dontcare)| Invoker | Static function| // | 0 | !=0 | !=0* | Method call | // +--------------------+----------+------------+----------------+ // * For Metrowerks, this can be 0. (first virtual function in a // single_inheritance class). // When stored stored inside a specific delegate, the 'dontcare' entries are replaced // with a reference to the delegate itself. This complicates the = and == operators // for the delegate class. // DelegateMemento - Evil version // // For compilers where data pointers are at least as big as code pointers, it is // possible to store the function pointer in the this pointer, using another // horrible_cast. In this case the DelegateMemento implementation is simple: // +--pThis --+-- pMemFunc-+-- Meaning---------------------+ // | 0 | 0 | Empty | // | !=0 | !=0* | Static function or method call| // +----------+------------+-------------------------------+ // * For Metrowerks, this can be 0. (first virtual function in a // single_inheritance class). // Note that the Sun C++ and MSVC documentation explicitly state that they // support static_cast between void * and function pointers. class DelegateMemento { protected: // the data is protected, not private, because many // compilers have problems with template friends. typedef void (detail::GenericClass::*GenericMemFuncType)(); // arbitrary MFP. detail::GenericClass *m_pthis; GenericMemFuncType m_pFunction; #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK) typedef void (*GenericFuncPtr)(); // arbitrary code pointer -任意代码指针 GenericFuncPtr m_pStaticFunction; #endif public: #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK) DelegateMemento() : m_pthis(0), m_pFunction(0), m_pStaticFunction(0) {}; void clear() { m_pthis=0; m_pFunction=0; m_pStaticFunction=0; } #else DelegateMemento() : m_pthis(0), m_pFunction(0) {}; void clear() { m_pthis=0; m_pFunction=0; } #endif public: #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK) inline bool IsEqual (const DelegateMemento &x) const{ // We have to cope with the static function pointers as a special case // 静态函数指针按特殊情况处理 if (m_pFunction!=x.m_pFunction) return false; // the static function ptrs must either both be equal, or both be 0. // 静态函数指针要么都相同,要么都为0 if (m_pStaticFunction!=x.m_pStaticFunction) return false; if (m_pStaticFunction!=0) return m_pthis==x.m_pthis; else return true; } #else // Evil Method inline bool IsEqual (const DelegateMemento &x) const{ return m_pthis==x.m_pthis && m_pFunction==x.m_pFunction; } #endif // Provide a strict weak ordering for DelegateMementos. inline bool IsLess(const DelegateMemento &right) const { // deal with static function pointers first #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK) if (m_pStaticFunction !=0 || right.m_pStaticFunction!=0) return m_pStaticFunction < right.m_pStaticFunction; #endif if (m_pthis !=right.m_pthis) return m_pthis < right.m_pthis; // There are no ordering operators for member function pointers, // but we can fake one by comparing each byte. The resulting ordering is // arbitrary (and compiler-dependent), but it permits storage in ordered STL containers. return memcmp(&m_pFunction, &right.m_pFunction, sizeof(m_pFunction)) < 0; } // BUGFIX (Mar 2005): // We can't just compare m_pFunction because on Metrowerks, // m_pFunction can be zero even if the delegate is not empty! inline bool operator ! () const // Is it bound to anything? { return m_pthis==0 && m_pFunction==0; } inline bool empty() const // Is it bound to anything? { return m_pthis==0 && m_pFunction==0; } public: DelegateMemento & operator = (const DelegateMemento &right) { SetMementoFrom(right); return *this; } inline bool operator <(const DelegateMemento &right) { return IsLess(right); } inline bool operator >(const DelegateMemento &right) { return right.IsLess(*this); } DelegateMemento (const DelegateMemento &right) : m_pFunction(right.m_pFunction), m_pthis(right.m_pthis) #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK) , m_pStaticFunction (right.m_pStaticFunction) #endif {} protected: void SetMementoFrom(const DelegateMemento &right) { m_pFunction = right.m_pFunction; m_pthis = right.m_pthis; #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK) m_pStaticFunction = right.m_pStaticFunction; #endif } }; // ClosurePtr<> // // A private wrapper class that adds function signatures to DelegateMemento. // It's the class that does most of the actual work. // The signatures are specified by: // GenericMemFunc: must be a type of GenericClass member function pointer. // StaticFuncPtr: must be a type of function pointer with the same signature // as GenericMemFunc. // UnvoidStaticFuncPtr: is the same as StaticFuncPtr, except on VC6 // where it never returns void (returns DefaultVoid instead). // An outer class, FastDelegateN<>, handles the invoking and creates the // necessary typedefs. // This class does everything else. // 一个私有的封装类-添加函数签名到DelegateMemento。 // 该类做了大部分实际工作 // 签名具体指: // GenericMemFunc: 必须是GenericClass成员函数指针类型 // StaticFuncPtr: 必须是与GenericMemFunc相同签名的函数指针类型 // UnvoidStaticFuncPtr: 除vc6(它不返回void,而是用DefaultVoid代替),与StaticFuncPtr相同。 // // 暴露的类 FastDelegateN<> ,处理调用和创建必要的类型定义。 // 该类做其他的工作 namespace detail { template < class GenericMemFunc, class StaticFuncPtr, class UnvoidStaticFuncPtr> class ClosurePtr : public DelegateMemento { public: // These functions are for setting the delegate to a member function. // 这些函数用来设置‘委托’为一个成员函数 // Here's the clever bit: we convert an arbitrary member function into a // standard form. XMemFunc should be a member function of class X, but I can't // enforce that here. It needs to be enforced by the wrapper class. // 奥妙之处在:我们将任意的成员函数转换为一个标准的形式。 // XMemFunc应该是一个class X类的一个成员函数,但是我不能在那里强制执行。 // 它需要通过封装类来强制执行调用。 template < class X, class XMemFunc > inline void bindmemfunc(X *pthis, XMemFunc function_to_bind ) { m_pthis = SimplifyMemFunc< sizeof(function_to_bind) > ::Convert(pthis, function_to_bind, m_pFunction); #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK) m_pStaticFunction = 0; #endif } // For const member functions, we only need a const class pointer. // Since we know that the member function is const, it's safe to // remove the const qualifier from the 'this' pointer with a const_cast. // VC6 has problems if we just overload 'bindmemfunc', so we give it a different name. // 对于const类型的成员函数,我们只需要一个const 类指针。 // 既然我们知道成员函数是 const类型,用const_cast从‘this’指针去掉const限定词是安全的。 // vc6存在问题,如果我们只重载‘bindmemfunc’,因此我们给他起了一个不同的名字。 template < class X, class XMemFunc> inline void bindconstmemfunc(const X *pthis, XMemFunc function_to_bind) { m_pthis= SimplifyMemFunc< sizeof(function_to_bind) > ::Convert(const_cast<X*>(pthis), function_to_bind, m_pFunction); #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK) m_pStaticFunction = 0; #endif } #ifdef FASTDELEGATE_GCC_BUG_8271 // At present, GCC doesn't recognize constness of MFPs in templates template < class X, class XMemFunc> inline void bindmemfunc(const X *pthis, XMemFunc function_to_bind) { bindconstmemfunc(pthis, function_to_bind); #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK) m_pStaticFunction = 0; #endif } #endif // These functions are required for invoking the stored function // 这些函数用于调用存储的函数 inline GenericClass *GetClosureThis() const { return m_pthis; } inline GenericMemFunc GetClosureMemPtr() const { return reinterpret_cast<GenericMemFunc>(m_pFunction); } // There are a few ways of dealing with static function pointers. // There's a standard-compliant, but tricky method. // There's also a straightforward hack, that won't work on DOS compilers using the // medium memory model. It's so evil that I can't recommend it, but I've // implemented it anyway because it produces very nice asm code. // 有几种方法来处理静态函数指针。 // 标准兼容模式,但是很猥琐。 // 也有简单的奇技,但是无法为dos编译器 // 在medium内存模式下工作。它过于邪恶,因此我并不推荐, // 但是我已经实现了这种方法,因为它提供非常好的asm代码。 // #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK) // ClosurePtr<> - Safe version // // This implementation is standard-compliant, but a bit tricky. // I store the function pointer inside the class, and the delegate then // points to itself. Whenever the delegate is copied, these self-references // must be transformed, and this complicates the = and == operators. // 这种实现是标准兼容的,但是有一点麻烦。 // 我在类内部存放了函数指针,然后‘委托’指向自身。 // 无论何时,当委托被拷贝,这些自引用必须进行转换, // 这使 = 和 == 操作符变得复杂。 public: // The next two functions are for operator ==, =, and the copy constructor. // We may need to convert the m_pthis pointers, so that // they remain as self-references. // 下面的2个函数以实现操作符 ==, = ,以及拷贝构造函数。 // 我们需要转换 m_pthis指针,这样他们报纸自引用。 template< class DerivedClass > inline void CopyFrom (DerivedClass *pParent, const DelegateMemento &x) { SetMementoFrom(x); if (m_pStaticFunction!=0) { // transform self references... m_pthis=reinterpret_cast<GenericClass *>(pParent); } } // For static functions, the 'static_function_invoker' class in the parent // will be called. The parent then needs to call GetStaticFunction() to find out // the actual function to invoke. // 对于静态函数,在父类中的‘static_function_invoker’类会被调用。 // 父类然后需要调用 GetStaticFunction()以找到实际的函数来调用。 template < class DerivedClass, class ParentInvokerSig > inline void bindstaticfunc(DerivedClass *pParent, ParentInvokerSig static_function_invoker, StaticFuncPtr function_to_bind ) { if (function_to_bind==0) { // cope with assignment to 0 m_pFunction=0; } else { bindmemfunc(pParent, static_function_invoker); } m_pStaticFunction=reinterpret_cast<GenericFuncPtr>(function_to_bind); } inline UnvoidStaticFuncPtr GetStaticFunction() const { return reinterpret_cast<UnvoidStaticFuncPtr>(m_pStaticFunction); } #else // ClosurePtr<> - Evil version - ‘邪恶’的方法 // // For compilers where data pointers are at least as big as code pointers, it is // possible to store the function pointer in the this pointer, using another // horrible_cast. Invocation isn't any faster, but it saves 4 bytes, and // speeds up comparison and assignment. If C++ provided direct language support // for delegates, they would produce asm code that was almost identical to this. // Note that the Sun C++ and MSVC documentation explicitly state that they // support static_cast between void * and function pointers. // 对于数据指针至少和 代码指针一样大的编译器来说, // 使用horrible_cast 可以把函数指针放到this指针里。 // 调用不是变快,但是节省4个字节,并且使比较和赋值更快。 // 如果c++提供直接的委托语言支持,他们产生几乎同样的asm代码。 // 注意,sun C++ 和 msvc显式声明他们支持static_cast以转换 void * 和 function pointers. template< class DerivedClass > inline void CopyFrom (DerivedClass *pParent, const DelegateMemento &right) { SetMementoFrom(right); } // For static functions, the 'static_function_invoker' class in the parent // will be called. The parent then needs to call GetStaticFunction() to find out // the actual function to invoke. // 对于静态函数,static_function_invoker 类在父类里会被调用。父类然后会调用GetStaticFunction // 以找到实际的调用函数 // ******** EVIL, EVIL CODE! ******* template < class DerivedClass, class ParentInvokerSig> inline void bindstaticfunc(DerivedClass *pParent, ParentInvokerSig static_function_invoker, StaticFuncPtr function_to_bind) { if (function_to_bind==0) { // cope with assignment to 0 m_pFunction=0; } else { // We'll be ignoring the 'this' pointer, but we need to make sure we pass // a valid value to bindmemfunc(). bindmemfunc(pParent, static_function_invoker); } // WARNING! Evil hack. We store the function in the 'this' pointer! // Ensure that there's a compilation failure if function pointers // and data pointers have different sizes. // If you get this error, you need to #undef FASTDELEGATE_USESTATICFUNCTIONHACK. // 警告:Evil hack。我们把函数指针保存在‘this’指针中。 // 确保如果函数指针和数据指针大小不同,产生编译错误。 // 如果你获得这个错误,你需要 #undef FASTDELEGATE_USESTATICFUNCTIONHACK. typedef int ERROR_CantUseEvilMethod[sizeof(GenericClass *)==sizeof(function_to_bind) ? 1 : -1]; m_pthis = horrible_cast<GenericClass *>(function_to_bind); // MSVC, SunC++ and DMC accept the following (non-standard) code: // m_pthis = static_cast<GenericClass *>(static_cast<void *>(function_to_bind)); // BCC32, Comeau and DMC accept this method. MSVC7.1 needs __int64 instead of long // m_pthis = reinterpret_cast<GenericClass *>(reinterpret_cast<long>(function_to_bind)); } // ******** EVIL, EVIL CODE! ******* // This function will be called with an invalid 'this' pointer!! // We're just returning the 'this' pointer, converted into // a function pointer! // 这个函数将会被一个无效的‘this’指针调用。 // this->func = func(this) 在这里 this的地址是 静态函数的地址 // 我们只需返回‘this’指针,把它转化为一个函数指针。 inline UnvoidStaticFuncPtr GetStaticFunction() const { // Ensure that there's a compilation failure if function pointers // and data pointers have different sizes. // If you get this error, you need to #undef FASTDELEGATE_USESTATICFUNCTIONHACK. typedef int ERROR_CantUseEvilMethod[sizeof(UnvoidStaticFuncPtr)==sizeof(this) ? 1 : -1]; return horrible_cast<UnvoidStaticFuncPtr>(this); } #endif // !defined(FASTDELEGATE_USESTATICFUNCTIONHACK) // Does the closure contain this static function? inline bool IsEqualToStaticFuncPtr(StaticFuncPtr funcptr){ if (funcptr==0) return empty(); // For the Evil method, if it doesn't actually contain a static function, this will return an arbitrary // value that is not equal to any valid function pointer. else return funcptr==reinterpret_cast<StaticFuncPtr>(GetStaticFunction()); } }; } // namespace detail //////////////////////////////////////////////////////////////////////////////// // Fast Delegates, part 3: // // Wrapper classes to ensure type safety // 保证类型安全的封装类 // //////////////////////////////////////////////////////////////////////////////// // Once we have the member function conversion templates, it's easy to make the // wrapper classes. So that they will work with as many compilers as possible, // the classes are of the form // FastDelegate3<int, char *, double> // They can cope with any combination of parameters. The max number of parameters // allowed is 8, but it is trivial to increase this limit. // Note that we need to treat const member functions seperately. // All this class does is to enforce type safety, and invoke the delegate with // the correct list of parameters. // 一旦我们有了 成员函数转换模板,生成封装类则是非常容易。 // 因此他们将适用于尽可能多的编译器。 // 类将是这样的形式: // FastDelegate3<int, char *, double> // 他们处理不同组合的参数,最大参数个数是8,但是允许增加参数个数限制。 // 注意我们需要单独处理const 成员函数。 // 类做的所有工作都是强化类型安全,及以正确的参数列表来调用委托 // Because of the weird rule about the class of derived member function pointers, // you sometimes need to apply a downcast to the 'this' pointer. // This is the reason for the use of "implicit_cast<X*>(pthis)" in the code below. // If CDerivedClass is derived from CBaseClass, but doesn't override SimpleVirtualFunction, // without this trick you'd need to write: // MyDelegate(static_cast<CBaseClass *>(&d), &CDerivedClass::SimpleVirtualFunction); // but with the trick you can write // MyDelegate(&d, &CDerivedClass::SimpleVirtualFunction); // 对于那些有派生成员函数指针的类来说,你有时需要对‘this’指针进行 downcast // 这就是下面的代码会用到implicit_cast<X*>(pthis)的原因 // 如果CDerivedClass派生于CBaseClass,但是并没有重载SimpleVirtualFunction, // 若没有这个技巧,你需要这样写: // MyDelegate(static_cast<CBaseClass *>(&d), &CDerivedClass::SimpleVirtualFunction); // 但是用的话,你就可以这样: // MyDelegate(&d, &CDerivedClass::SimpleVirtualFunction); // RetType is the type the compiler uses in compiling the template. For VC6, // it cannot be void. DesiredRetType is the real type which is returned from // all of the functions. It can be void. // RetType类型是编译器在编译模板所需。对于vc,他不能是void。 // DesiredRetType是从所有函数返回的真正的类型。它可以是void。 // Implicit conversion to "bool" is achieved using the safe_bool idiom, // using member data pointers (MDP). This allows "if (dg)..." syntax // Because some compilers (eg codeplay) don't have a unique value for a zero // MDP, an extra padding member is added to the SafeBool struct. // Some compilers (eg VC6) won't implicitly convert from 0 to an MDP, so // in that case the static function constructor is not made explicit; this // allows "if (dg==0) ..." to compile. // 隐式转换为“bool”由safe_bool来完成,使用成员函数指针(MDP) // 这允许“if(dg)”语法,因为一些编译器(如 codeplay)对0没有唯一值 // MDP,一个额外的填充成员添加到SafeBool 结构。 // 一些编译器(如:vc6)不能隐式将0转换到mdp, // in that case the static function constructor is not made explicit; // 这允许“if(dg == 0)...”来编译。 //N=0 template<class RetType=detail::DefaultVoid> class FastDelegate0 { private: typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType; typedef DesiredRetType (*StaticFunctionPtr)(); typedef RetType (*UnvoidStaticFunctionPtr)(); typedef RetType (detail::GenericClass::*GenericMemFn)(); typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType; ClosureType m_Closure; public: // Typedefs to aid generic programming // 类型定义以辅助泛式编程 typedef FastDelegate0 type; // Construction and comparison functions FastDelegate0() { clear(); } FastDelegate0(const FastDelegate0 &x) { m_Closure.CopyFrom(this, x.m_Closure); } void operator = (const FastDelegate0 &x) { m_Closure.CopyFrom(this, x.m_Closure); } bool operator ==(const FastDelegate0 &x) const { return m_Closure.IsEqual(x.m_Closure); } bool operator !=(const FastDelegate0 &x) const { return !m_Closure.IsEqual(x.m_Closure); } bool operator <(const FastDelegate0 &x) const { return m_Closure.IsLess(x.m_Closure); } bool operator >(const FastDelegate0 &x) const { return x.m_Closure.IsLess(m_Closure); } // Binding to non-const member functions // 绑定到non-const 成员函数 template < class X, class Y > FastDelegate0(Y *pthis, DesiredRetType (X::* function_to_bind)() ) { m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); } template < class X, class Y > inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)()) { m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); } // Binding to const member functions. // 绑定到 const成员函数。 template < class X, class Y > FastDelegate0(const Y *pthis, DesiredRetType (X::* function_to_bind)() const) { m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind); } template < class X, class Y > inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)() const) { m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind); } // Static functions. We convert them into a member function call. // This constructor also provides implicit conversion // 静态函数。我们把它们转换为成员函数调用 // 此构造函数也提供隐式转化 FastDelegate0(DesiredRetType (*function_to_bind)() ) { bind(function_to_bind); } // for efficiency, prevent creation of a temporary // 考虑到效率,禁止临时对象创建 void operator = (DesiredRetType (*function_to_bind)() ) { bind(function_to_bind); } inline void bind(DesiredRetType (*function_to_bind)()) { m_Closure.bindstaticfunc(this, &FastDelegate0::InvokeStaticFunction, function_to_bind); } // Invoke the delegate // 调用其委托。 RetType operator() () const { return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(); } private: // Implicit conversion to "bool" using the safe_bool idiom // 使用safe_bool隐式转换为“bool” typedef struct SafeBoolStruct { int a_data_pointer_to_this_is_0_on_buggy_compilers; StaticFunctionPtr m_nonzero; } UselessTypedef; typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type; public: operator unspecified_bool_type() const { return empty()? 0: &SafeBoolStruct::m_nonzero; } // necessary to allow ==0 to work despite the safe_bool idiom inline bool operator==(StaticFunctionPtr funcptr) { return m_Closure.IsEqualToStaticFuncPtr(funcptr); } inline bool operator!=(StaticFunctionPtr funcptr) { return !m_Closure.IsEqualToStaticFuncPtr(funcptr); } inline bool operator ! () const { // Is it bound to anything? return !m_Closure; } inline bool empty() const { return !m_Closure; } void clear() { m_Closure.clear();} // Conversion to and from the DelegateMemento storage class const DelegateMemento & GetMemento() { return m_Closure; } void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); } private: // Invoker for static functions RetType InvokeStaticFunction() const { return (*(m_Closure.GetStaticFunction()))(); } }; //N=1 template<class Param1, class RetType=detail::DefaultVoid> class FastDelegate1 { private: typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType; typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1); typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1); typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1); typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType; ClosureType m_Closure; public: // Typedefs to aid generic programming typedef FastDelegate1 type; // Construction and comparison functions FastDelegate1() { clear(); } FastDelegate1(const FastDelegate1 &x) { m_Closure.CopyFrom(this, x.m_Closure); } void operator = (const FastDelegate1 &x) { m_Closure.CopyFrom(this, x.m_Closure); } bool operator ==(const FastDelegate1 &x) const { return m_Closure.IsEqual(x.m_Closure); } bool operator !=(const FastDelegate1 &x) const { return !m_Closure.IsEqual(x.m_Closure); } bool operator <(const FastDelegate1 &x) const { return m_Closure.IsLess(x.m_Closure); } bool operator >(const FastDelegate1 &x) const { return x.m_Closure.IsLess(m_Closure); } // Binding to non-const member functions template < class X, class Y > FastDelegate1(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1) ) { m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); } template < class X, class Y > inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1)) { m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); } // Binding to const member functions. template < class X, class Y > FastDelegate1(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1) const) { m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind); } template < class X, class Y > inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1) const) { m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind); } // Static functions. We convert them into a member function call. // This constructor also provides implicit conversion FastDelegate1(DesiredRetType (*function_to_bind)(Param1 p1) ) { bind(function_to_bind); } // for efficiency, prevent creation of a temporary void operator = (DesiredRetType (*function_to_bind)(Param1 p1) ) { bind(function_to_bind); } inline void bind(DesiredRetType (*function_to_bind)(Param1 p1)) { m_Closure.bindstaticfunc(this, &FastDelegate1::InvokeStaticFunction, function_to_bind); } // Invoke the delegate RetType operator() (Param1 p1) const { return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1); } // Implicit conversion to "bool" using the safe_bool idiom private: typedef struct SafeBoolStruct { int a_data_pointer_to_this_is_0_on_buggy_compilers; StaticFunctionPtr m_nonzero; } UselessTypedef; typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type; public: operator unspecified_bool_type() const { return empty()? 0: &SafeBoolStruct::m_nonzero; } // necessary to allow ==0 to work despite the safe_bool idiom inline bool operator==(StaticFunctionPtr funcptr) { return m_Closure.IsEqualToStaticFuncPtr(funcptr); } inline bool operator!=(StaticFunctionPtr funcptr) { return !m_Closure.IsEqualToStaticFuncPtr(funcptr); } inline bool operator ! () const { // Is it bound to anything? return !m_Closure; } inline bool empty() const { return !m_Closure; } void clear() { m_Closure.clear();} // Conversion to and from the DelegateMemento storage class const DelegateMemento & GetMemento() { return m_Closure; } void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); } private: // Invoker for static functions RetType InvokeStaticFunction(Param1 p1) const { return (*(m_Closure.GetStaticFunction()))(p1); } }; //N=2 template<class Param1, class Param2, class RetType=detail::DefaultVoid> class FastDelegate2 { private: typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType; typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1, Param2 p2); typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2); typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1, Param2 p2); typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType; ClosureType m_Closure; public: // Typedefs to aid generic programming typedef FastDelegate2 type; // Construction and comparison functions FastDelegate2() { clear(); } FastDelegate2(const FastDelegate2 &x) { m_Closure.CopyFrom(this, x.m_Closure); } void operator = (const FastDelegate2 &x) { m_Closure.CopyFrom(this, x.m_Closure); } bool operator ==(const FastDelegate2 &x) const { return m_Closure.IsEqual(x.m_Closure); } bool operator !=(const FastDelegate2 &x) const { return !m_Closure.IsEqual(x.m_Closure); } bool operator <(const FastDelegate2 &x) const { return m_Closure.IsLess(x.m_Closure); } bool operator >(const FastDelegate2 &x) const { return x.m_Closure.IsLess(m_Closure); } // Binding to non-const member functions template < class X, class Y > FastDelegate2(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2) ) { m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); } template < class X, class Y > inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2)) { m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); } // Binding to const member functions. template < class X, class Y > FastDelegate2(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2) const) { m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind); } template < class X, class Y > inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2) const) { m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind); } // Static functions. We convert them into a member function call. // This constructor also provides implicit conversion FastDelegate2(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2) ) { bind(function_to_bind); } // for efficiency, prevent creation of a temporary void operator = (DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2) ) { bind(function_to_bind); } inline void bind(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2)) { m_Closure.bindstaticfunc(this, &FastDelegate2::InvokeStaticFunction, function_to_bind); } // Invoke the delegate RetType operator() (Param1 p1, Param2 p2) const { return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2); } // Implicit conversion to "bool" using the safe_bool idiom private: typedef struct SafeBoolStruct { int a_data_pointer_to_this_is_0_on_buggy_compilers; StaticFunctionPtr m_nonzero; } UselessTypedef; typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type; public: operator unspecified_bool_type() const { return empty()? 0: &SafeBoolStruct::m_nonzero; } // necessary to allow ==0 to work despite the safe_bool idiom inline bool operator==(StaticFunctionPtr funcptr) { return m_Closure.IsEqualToStaticFuncPtr(funcptr); } inline bool operator!=(StaticFunctionPtr funcptr) { return !m_Closure.IsEqualToStaticFuncPtr(funcptr); } inline bool operator ! () const { // Is it bound to anything? return !m_Closure; } inline bool empty() const { return !m_Closure; } void clear() { m_Closure.clear();} // Conversion to and from the DelegateMemento storage class const DelegateMemento & GetMemento() { return m_Closure; } void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); } private: // Invoker for static functions RetType InvokeStaticFunction(Param1 p1, Param2 p2) const { return (*(m_Closure.GetStaticFunction()))(p1, p2); } }; //N=3 template<class Param1, class Param2, class Param3, class RetType=detail::DefaultVoid> class FastDelegate3 { private: typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType; typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3); typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3); typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1, Param2 p2, Param3 p3); typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType; ClosureType m_Closure; public: // Typedefs to aid generic programming typedef FastDelegate3 type; // Construction and comparison functions FastDelegate3() { clear(); } FastDelegate3(const FastDelegate3 &x) { m_Closure.CopyFrom(this, x.m_Closure); } void operator = (const FastDelegate3 &x) { m_Closure.CopyFrom(this, x.m_Closure); } bool operator ==(const FastDelegate3 &x) const { return m_Closure.IsEqual(x.m_Closure); } bool operator !=(const FastDelegate3 &x) const { return !m_Closure.IsEqual(x.m_Closure); } bool operator <(const FastDelegate3 &x) const { return m_Closure.IsLess(x.m_Closure); } bool operator >(const FastDelegate3 &x) const { return x.m_Closure.IsLess(m_Closure); } // Binding to non-const member functions template < class X, class Y > FastDelegate3(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3) ) { m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); } template < class X, class Y > inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3)) { m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); } // Binding to const member functions. template < class X, class Y > FastDelegate3(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3) const) { m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind); } template < class X, class Y > inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3) const) { m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind); } // Static functions. We convert them into a member function call. // This constructor also provides implicit conversion FastDelegate3(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3) ) { bind(function_to_bind); } // for efficiency, prevent creation of a temporary void operator = (DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3) ) { bind(function_to_bind); } inline void bind(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3)) { m_Closure.bindstaticfunc(this, &FastDelegate3::InvokeStaticFunction, function_to_bind); } // Invoke the delegate RetType operator() (Param1 p1, Param2 p2, Param3 p3) const { return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3); } // Implicit conversion to "bool" using the safe_bool idiom private: typedef struct SafeBoolStruct { int a_data_pointer_to_this_is_0_on_buggy_compilers; StaticFunctionPtr m_nonzero; } UselessTypedef; typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type; public: operator unspecified_bool_type() const { return empty()? 0: &SafeBoolStruct::m_nonzero; } // necessary to allow ==0 to work despite the safe_bool idiom inline bool operator==(StaticFunctionPtr funcptr) { return m_Closure.IsEqualToStaticFuncPtr(funcptr); } inline bool operator!=(StaticFunctionPtr funcptr) { return !m_Closure.IsEqualToStaticFuncPtr(funcptr); } inline bool operator ! () const { // Is it bound to anything? return !m_Closure; } inline bool empty() const { return !m_Closure; } void clear() { m_Closure.clear();} // Conversion to and from the DelegateMemento storage class const DelegateMemento & GetMemento() { return m_Closure; } void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); } private: // Invoker for static functions RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3) const { return (*(m_Closure.GetStaticFunction()))(p1, p2, p3); } }; //N=4 template<class Param1, class Param2, class Param3, class Param4, class RetType=detail::DefaultVoid> class FastDelegate4 { private: typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType; typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4); typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4); typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1, Param2 p2, Param3 p3, Param4 p4); typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType; ClosureType m_Closure; public: // Typedefs to aid generic programming typedef FastDelegate4 type; // Construction and comparison functions FastDelegate4() { clear(); } FastDelegate4(const FastDelegate4 &x) { m_Closure.CopyFrom(this, x.m_Closure); } void operator = (const FastDelegate4 &x) { m_Closure.CopyFrom(this, x.m_Closure); } bool operator ==(const FastDelegate4 &x) const { return m_Closure.IsEqual(x.m_Closure); } bool operator !=(const FastDelegate4 &x) const { return !m_Closure.IsEqual(x.m_Closure); } bool operator <(const FastDelegate4 &x) const { return m_Closure.IsLess(x.m_Closure); } bool operator >(const FastDelegate4 &x) const { return x.m_Closure.IsLess(m_Closure); } // Binding to non-const member functions template < class X, class Y > FastDelegate4(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4) ) { m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); } template < class X, class Y > inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4)) { m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); } // Binding to const member functions. template < class X, class Y > FastDelegate4(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4) const) { m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind); } template < class X, class Y > inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4) const) { m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind); } // Static functions. We convert them into a member function call. // This constructor also provides implicit conversion FastDelegate4(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4) ) { bind(function_to_bind); } // for efficiency, prevent creation of a temporary void operator = (DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4) ) { bind(function_to_bind); } inline void bind(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4)) { m_Closure.bindstaticfunc(this, &FastDelegate4::InvokeStaticFunction, function_to_bind); } // Invoke the delegate RetType operator() (Param1 p1, Param2 p2, Param3 p3, Param4 p4) const { return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3, p4); } // Implicit conversion to "bool" using the safe_bool idiom private: typedef struct SafeBoolStruct { int a_data_pointer_to_this_is_0_on_buggy_compilers; StaticFunctionPtr m_nonzero; } UselessTypedef; typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type; public: operator unspecified_bool_type() const { return empty()? 0: &SafeBoolStruct::m_nonzero; } // necessary to allow ==0 to work despite the safe_bool idiom inline bool operator==(StaticFunctionPtr funcptr) { return m_Closure.IsEqualToStaticFuncPtr(funcptr); } inline bool operator!=(StaticFunctionPtr funcptr) { return !m_Closure.IsEqualToStaticFuncPtr(funcptr); } inline bool operator ! () const { // Is it bound to anything? return !m_Closure; } inline bool empty() const { return !m_Closure; } void clear() { m_Closure.clear();} // Conversion to and from the DelegateMemento storage class const DelegateMemento & GetMemento() { return m_Closure; } void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); } private: // Invoker for static functions RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3, Param4 p4) const { return (*(m_Closure.GetStaticFunction()))(p1, p2, p3, p4); } }; //N=5 template<class Param1, class Param2, class Param3, class Param4, class Param5, class RetType=detail::DefaultVoid> class FastDelegate5 { private: typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType; typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5); typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5); typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5); typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType; ClosureType m_Closure; public: // Typedefs to aid generic programming typedef FastDelegate5 type; // Construction and comparison functions FastDelegate5() { clear(); } FastDelegate5(const FastDelegate5 &x) { m_Closure.CopyFrom(this, x.m_Closure); } void operator = (const FastDelegate5 &x) { m_Closure.CopyFrom(this, x.m_Closure); } bool operator ==(const FastDelegate5 &x) const { return m_Closure.IsEqual(x.m_Closure); } bool operator !=(const FastDelegate5 &x) const { return !m_Closure.IsEqual(x.m_Closure); } bool operator <(const FastDelegate5 &x) const { return m_Closure.IsLess(x.m_Closure); } bool operator >(const FastDelegate5 &x) const { return x.m_Closure.IsLess(m_Closure); } // Binding to non-const member functions template < class X, class Y > FastDelegate5(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) ) { m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); } template < class X, class Y > inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5)) { m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); } // Binding to const member functions. template < class X, class Y > FastDelegate5(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) const) { m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind); } template < class X, class Y > inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) const) { m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind); } // Static functions. We convert them into a member function call. // This constructor also provides implicit conversion FastDelegate5(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) ) { bind(function_to_bind); } // for efficiency, prevent creation of a temporary void operator = (DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) ) { bind(function_to_bind); } inline void bind(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5)) { m_Closure.bindstaticfunc(this, &FastDelegate5::InvokeStaticFunction, function_to_bind); } // Invoke the delegate RetType operator() (Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) const { return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3, p4, p5); } // Implicit conversion to "bool" using the safe_bool idiom private: typedef struct SafeBoolStruct { int a_data_pointer_to_this_is_0_on_buggy_compilers; StaticFunctionPtr m_nonzero; } UselessTypedef; typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type; public: operator unspecified_bool_type() const { return empty()? 0: &SafeBoolStruct::m_nonzero; } // necessary to allow ==0 to work despite the safe_bool idiom inline bool operator==(StaticFunctionPtr funcptr) { return m_Closure.IsEqualToStaticFuncPtr(funcptr); } inline bool operator!=(StaticFunctionPtr funcptr) { return !m_Closure.IsEqualToStaticFuncPtr(funcptr); } inline bool operator ! () const { // Is it bound to anything? return !m_Closure; } inline bool empty() const { return !m_Closure; } void clear() { m_Closure.clear();} // Conversion to and from the DelegateMemento storage class const DelegateMemento & GetMemento() { return m_Closure; } void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); } private: // Invoker for static functions RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) const { return (*(m_Closure.GetStaticFunction()))(p1, p2, p3, p4, p5); } }; //N=6 template<class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class RetType=detail::DefaultVoid> class FastDelegate6 { private: typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType; typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6); typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6); typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6); typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType; ClosureType m_Closure; public: // Typedefs to aid generic programming typedef FastDelegate6 type; // Construction and comparison functions FastDelegate6() { clear(); } FastDelegate6(const FastDelegate6 &x) { m_Closure.CopyFrom(this, x.m_Closure); } void operator = (const FastDelegate6 &x) { m_Closure.CopyFrom(this, x.m_Closure); } bool operator ==(const FastDelegate6 &x) const { return m_Closure.IsEqual(x.m_Closure); } bool operator !=(const FastDelegate6 &x) const { return !m_Closure.IsEqual(x.m_Closure); } bool operator <(const FastDelegate6 &x) const { return m_Closure.IsLess(x.m_Closure); } bool operator >(const FastDelegate6 &x) const { return x.m_Closure.IsLess(m_Closure); } // Binding to non-const member functions template < class X, class Y > FastDelegate6(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) ) { m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); } template < class X, class Y > inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6)) { m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); } // Binding to const member functions. template < class X, class Y > FastDelegate6(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) const) { m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind); } template < class X, class Y > inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) const) { m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind); } // Static functions. We convert them into a member function call. // This constructor also provides implicit conversion FastDelegate6(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) ) { bind(function_to_bind); } // for efficiency, prevent creation of a temporary void operator = (DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) ) { bind(function_to_bind); } inline void bind(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6)) { m_Closure.bindstaticfunc(this, &FastDelegate6::InvokeStaticFunction, function_to_bind); } // Invoke the delegate RetType operator() (Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) const { return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3, p4, p5, p6); } // Implicit conversion to "bool" using the safe_bool idiom private: typedef struct SafeBoolStruct { int a_data_pointer_to_this_is_0_on_buggy_compilers; StaticFunctionPtr m_nonzero; } UselessTypedef; typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type; public: operator unspecified_bool_type() const { return empty()? 0: &SafeBoolStruct::m_nonzero; } // necessary to allow ==0 to work despite the safe_bool idiom inline bool operator==(StaticFunctionPtr funcptr) { return m_Closure.IsEqualToStaticFuncPtr(funcptr); } inline bool operator!=(StaticFunctionPtr funcptr) { return !m_Closure.IsEqualToStaticFuncPtr(funcptr); } inline bool operator ! () const { // Is it bound to anything? return !m_Closure; } inline bool empty() const { return !m_Closure; } void clear() { m_Closure.clear();} // Conversion to and from the DelegateMemento storage class const DelegateMemento & GetMemento() { return m_Closure; } void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); } private: // Invoker for static functions RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) const { return (*(m_Closure.GetStaticFunction()))(p1, p2, p3, p4, p5, p6); } }; //N=7 template<class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class RetType=detail::DefaultVoid> class FastDelegate7 { private: typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType; typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7); typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7); typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7); typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType; ClosureType m_Closure; public: // Typedefs to aid generic programming typedef FastDelegate7 type; // Construction and comparison functions FastDelegate7() { clear(); } FastDelegate7(const FastDelegate7 &x) { m_Closure.CopyFrom(this, x.m_Closure); } void operator = (const FastDelegate7 &x) { m_Closure.CopyFrom(this, x.m_Closure); } bool operator ==(const FastDelegate7 &x) const { return m_Closure.IsEqual(x.m_Closure); } bool operator !=(const FastDelegate7 &x) const { return !m_Closure.IsEqual(x.m_Closure); } bool operator <(const FastDelegate7 &x) const { return m_Closure.IsLess(x.m_Closure); } bool operator >(const FastDelegate7 &x) const { return x.m_Closure.IsLess(m_Closure); } // Binding to non-const member functions template < class X, class Y > FastDelegate7(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) ) { m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); } template < class X, class Y > inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7)) { m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); } // Binding to const member functions. template < class X, class Y > FastDelegate7(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) const) { m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind); } template < class X, class Y > 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) { m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind); } // Static functions. We convert them into a member function call. // This constructor also provides implicit conversion FastDelegate7(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) ) { bind(function_to_bind); } // for efficiency, prevent creation of a temporary void operator = (DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) ) { bind(function_to_bind); } inline void bind(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7)) { m_Closure.bindstaticfunc(this, &FastDelegate7::InvokeStaticFunction, function_to_bind); } // Invoke the delegate RetType operator() (Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) const { return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3, p4, p5, p6, p7); } // Implicit conversion to "bool" using the safe_bool idiom private: typedef struct SafeBoolStruct { int a_data_pointer_to_this_is_0_on_buggy_compilers; StaticFunctionPtr m_nonzero; } UselessTypedef; typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type; public: operator unspecified_bool_type() const { return empty()? 0: &SafeBoolStruct::m_nonzero; } // necessary to allow ==0 to work despite the safe_bool idiom inline bool operator==(StaticFunctionPtr funcptr) { return m_Closure.IsEqualToStaticFuncPtr(funcptr); } inline bool operator!=(StaticFunctionPtr funcptr) { return !m_Closure.IsEqualToStaticFuncPtr(funcptr); } inline bool operator ! () const { // Is it bound to anything? return !m_Closure; } inline bool empty() const { return !m_Closure; } void clear() { m_Closure.clear();} // Conversion to and from the DelegateMemento storage class const DelegateMemento & GetMemento() { return m_Closure; } void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); } private: // Invoker for static functions RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) const { return (*(m_Closure.GetStaticFunction()))(p1, p2, p3, p4, p5, p6, p7); } }; //N=8 template<class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8, class RetType=detail::DefaultVoid> class FastDelegate8 { private: typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType; typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8); typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8); typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8); typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType; ClosureType m_Closure; public: // Typedefs to aid generic programming typedef FastDelegate8 type; // Construction and comparison functions FastDelegate8() { clear(); } FastDelegate8(const FastDelegate8 &x) { m_Closure.CopyFrom(this, x.m_Closure); } void operator = (const FastDelegate8 &x) { m_Closure.CopyFrom(this, x.m_Closure); } bool operator ==(const FastDelegate8 &x) const { return m_Closure.IsEqual(x.m_Closure); } bool operator !=(const FastDelegate8 &x) const { return !m_Closure.IsEqual(x.m_Closure); } bool operator <(const FastDelegate8 &x) const { return m_Closure.IsLess(x.m_Closure); } bool operator >(const FastDelegate8 &x) const { return x.m_Closure.IsLess(m_Closure); } // Binding to non-const member functions template < class X, class Y > FastDelegate8(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8) ) { m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); } template < class X, class Y > 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)) { m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); } // Binding to const member functions. template < class X, class Y > 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) { m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind); } template < class X, class Y > 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) { m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind); } // Static functions. We convert them into a member function call. // This constructor also provides implicit conversion FastDelegate8(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8) ) { bind(function_to_bind); } // for efficiency, prevent creation of a temporary void operator = (DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8) ) { bind(function_to_bind); } inline void bind(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8)) { m_Closure.bindstaticfunc(this, &FastDelegate8::InvokeStaticFunction, function_to_bind); } // Invoke the delegate RetType operator() (Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8) const { return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3, p4, p5, p6, p7, p8); } // Implicit conversion to "bool" using the safe_bool idiom private: typedef struct SafeBoolStruct { int a_data_pointer_to_this_is_0_on_buggy_compilers; StaticFunctionPtr m_nonzero; } UselessTypedef; typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type; public: operator unspecified_bool_type() const { return empty()? 0: &SafeBoolStruct::m_nonzero; } // necessary to allow ==0 to work despite the safe_bool idiom inline bool operator==(StaticFunctionPtr funcptr) { return m_Closure.IsEqualToStaticFuncPtr(funcptr); } inline bool operator!=(StaticFunctionPtr funcptr) { return !m_Closure.IsEqualToStaticFuncPtr(funcptr); } inline bool operator ! () const { // Is it bound to anything? return !m_Closure; } inline bool empty() const { return !m_Closure; } void clear() { m_Closure.clear();} // Conversion to and from the DelegateMemento storage class const DelegateMemento & GetMemento() { return m_Closure; } void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); } private: // Invoker for static functions RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8) const { return (*(m_Closure.GetStaticFunction()))(p1, p2, p3, p4, p5, p6, p7, p8); } }; //////////////////////////////////////////////////////////////////////////////// // Fast Delegates, part 4: // // FastDelegate<> class (Original author: Jody Hagins) // Allows boost::function style syntax like: // FastDelegate< double (int, long) > // instead of: // FastDelegate2< int, long, double > // //////////////////////////////////////////////////////////////////////////////// #ifdef FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX // Declare FastDelegate as a class template. It will be specialized // later for all number of arguments. template <typename Signature> class FastDelegate; //N=0 // Specialization to allow use of // FastDelegate< R ( ) > // instead of // FastDelegate0 < R > template<typename R> class FastDelegate< R ( ) > // Inherit from FastDelegate0 so that it can be treated just like a FastDelegate0 : public FastDelegate0 < R > { public: // Make using the base type a bit easier via typedef. typedef FastDelegate0 < R > BaseType; // Allow users access to the specific type of this delegate. typedef FastDelegate SelfType; // Mimic the base class constructors. FastDelegate() : BaseType() { } template < class X, class Y > FastDelegate(Y * pthis, R (X::* function_to_bind)( )) : BaseType(pthis, function_to_bind) { } template < class X, class Y > FastDelegate(const Y *pthis, R (X::* function_to_bind)( ) const) : BaseType(pthis, function_to_bind) { } FastDelegate(R (*function_to_bind)( )) : BaseType(function_to_bind) { } void operator = (const BaseType &x) { *static_cast<BaseType*>(this) = x; } }; //N=1 // Specialization to allow use of // FastDelegate< R ( Param1 ) > // instead of // FastDelegate1 < Param1, R > template<typename R, class Param1> class FastDelegate< R ( Param1 ) > // Inherit from FastDelegate1 so that it can be treated just like a FastDelegate1 : public FastDelegate1 < Param1, R > { public: // Make using the base type a bit easier via typedef. typedef FastDelegate1 < Param1, R > BaseType; // Allow users access to the specific type of this delegate. typedef FastDelegate SelfType; // Mimic the base class constructors. FastDelegate() : BaseType() { } template < class X, class Y > FastDelegate(Y * pthis, R (X::* function_to_bind)( Param1 p1 )) : BaseType(pthis, function_to_bind) { } template < class X, class Y > FastDelegate(const Y *pthis, R (X::* function_to_bind)( Param1 p1 ) const) : BaseType(pthis, function_to_bind) { } FastDelegate(R (*function_to_bind)( Param1 p1 )) : BaseType(function_to_bind) { } void operator = (const BaseType &x) { *static_cast<BaseType*>(this) = x; } }; //N=2 // Specialization to allow use of // FastDelegate< R ( Param1, Param2 ) > // instead of // FastDelegate2 < Param1, Param2, R > template<typename R, class Param1, class Param2> class FastDelegate< R ( Param1, Param2 ) > // Inherit from FastDelegate2 so that it can be treated just like a FastDelegate2 : public FastDelegate2 < Param1, Param2, R > { public: // Make using the base type a bit easier via typedef. typedef FastDelegate2 < Param1, Param2, R > BaseType; // Allow users access to the specific type of this delegate. typedef FastDelegate SelfType; // Mimic the base class constructors. FastDelegate() : BaseType() { } template < class X, class Y > FastDelegate(Y * pthis, R (X::* function_to_bind)( Param1 p1, Param2 p2 )) : BaseType(pthis, function_to_bind) { } template < class X, class Y > FastDelegate(const Y *pthis, R (X::* function_to_bind)( Param1 p1, Param2 p2 ) const) : BaseType(pthis, function_to_bind) { } FastDelegate(R (*function_to_bind)( Param1 p1, Param2 p2 )) : BaseType(function_to_bind) { } void operator = (const BaseType &x) { *static_cast<BaseType*>(this) = x; } }; //N=3 // Specialization to allow use of // FastDelegate< R ( Param1, Param2, Param3 ) > // instead of // FastDelegate3 < Param1, Param2, Param3, R > template<typename R, class Param1, class Param2, class Param3> class FastDelegate< R ( Param1, Param2, Param3 ) > // Inherit from FastDelegate3 so that it can be treated just like a FastDelegate3 : public FastDelegate3 < Param1, Param2, Param3, R > { public: // Make using the base type a bit easier via typedef. typedef FastDelegate3 < Param1, Param2, Param3, R > BaseType; // Allow users access to the specific type of this delegate. typedef FastDelegate SelfType; // Mimic the base class constructors. FastDelegate() : BaseType() { } template < class X, class Y > FastDelegate(Y * pthis, R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3 )) : BaseType(pthis, function_to_bind) { } template < class X, class Y > FastDelegate(const Y *pthis, R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3 ) const) : BaseType(pthis, function_to_bind) { } FastDelegate(R (*function_to_bind)( Param1 p1, Param2 p2, Param3 p3 )) : BaseType(function_to_bind) { } void operator = (const BaseType &x) { *static_cast<BaseType*>(this) = x; } }; //N=4 // Specialization to allow use of // FastDelegate< R ( Param1, Param2, Param3, Param4 ) > // instead of // FastDelegate4 < Param1, Param2, Param3, Param4, R > template<typename R, class Param1, class Param2, class Param3, class Param4> class FastDelegate< R ( Param1, Param2, Param3, Param4 ) > // Inherit from FastDelegate4 so that it can be treated just like a FastDelegate4 : public FastDelegate4 < Param1, Param2, Param3, Param4, R > { public: // Make using the base type a bit easier via typedef. typedef FastDelegate4 < Param1, Param2, Param3, Param4, R > BaseType; // Allow users access to the specific type of this delegate. typedef FastDelegate SelfType; // Mimic the base class constructors. FastDelegate() : BaseType() { } template < class X, class Y > FastDelegate(Y * pthis, R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4 )) : BaseType(pthis, function_to_bind) { } template < class X, class Y > FastDelegate(const Y *pthis, R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4 ) const) : BaseType(pthis, function_to_bind) { } FastDelegate(R (*function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4 )) : BaseType(function_to_bind) { } void operator = (const BaseType &x) { *static_cast<BaseType*>(this) = x; } }; //N=5 // Specialization to allow use of // FastDelegate< R ( Param1, Param2, Param3, Param4, Param5 ) > // instead of // FastDelegate5 < Param1, Param2, Param3, Param4, Param5, R > template<typename R, class Param1, class Param2, class Param3, class Param4, class Param5> class FastDelegate< R ( Param1, Param2, Param3, Param4, Param5 ) > // Inherit from FastDelegate5 so that it can be treated just like a FastDelegate5 : public FastDelegate5 < Param1, Param2, Param3, Param4, Param5, R > { public: // Make using the base type a bit easier via typedef. typedef FastDelegate5 < Param1, Param2, Param3, Param4, Param5, R > BaseType; // Allow users access to the specific type of this delegate. typedef FastDelegate SelfType; // Mimic the base class constructors. FastDelegate() : BaseType() { } template < class X, class Y > FastDelegate(Y * pthis, R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5 )) : BaseType(pthis, function_to_bind) { } template < class X, class Y > FastDelegate(const Y *pthis, R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5 ) const) : BaseType(pthis, function_to_bind) { } FastDelegate(R (*function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5 )) : BaseType(function_to_bind) { } void operator = (const BaseType &x) { *static_cast<BaseType*>(this) = x; } }; //N=6 // Specialization to allow use of // FastDelegate< R ( Param1, Param2, Param3, Param4, Param5, Param6 ) > // instead of // FastDelegate6 < Param1, Param2, Param3, Param4, Param5, Param6, R > template<typename R, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6> class FastDelegate< R ( Param1, Param2, Param3, Param4, Param5, Param6 ) > // Inherit from FastDelegate6 so that it can be treated just like a FastDelegate6 : public FastDelegate6 < Param1, Param2, Param3, Param4, Param5, Param6, R > { public: // Make using the base type a bit easier via typedef. typedef FastDelegate6 < Param1, Param2, Param3, Param4, Param5, Param6, R > BaseType; // Allow users access to the specific type of this delegate. typedef FastDelegate SelfType; // Mimic the base class constructors. FastDelegate() : BaseType() { } template < class X, class Y > FastDelegate(Y * pthis, R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6 )) : BaseType(pthis, function_to_bind) { } template < class X, class Y > FastDelegate(const Y *pthis, R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6 ) const) : BaseType(pthis, function_to_bind) { } FastDelegate(R (*function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6 )) : BaseType(function_to_bind) { } void operator = (const BaseType &x) { *static_cast<BaseType*>(this) = x; } }; //N=7 // Specialization to allow use of // FastDelegate< R ( Param1, Param2, Param3, Param4, Param5, Param6, Param7 ) > // instead of // FastDelegate7 < Param1, Param2, Param3, Param4, Param5, Param6, Param7, R > template<typename R, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7> class FastDelegate< R ( Param1, Param2, Param3, Param4, Param5, Param6, Param7 ) > // Inherit from FastDelegate7 so that it can be treated just like a FastDelegate7 : public FastDelegate7 < Param1, Param2, Param3, Param4, Param5, Param6, Param7, R > { public: // Make using the base type a bit easier via typedef. typedef FastDelegate7 < Param1, Param2, Param3, Param4, Param5, Param6, Param7, R > BaseType; // Allow users access to the specific type of this delegate. typedef FastDelegate SelfType; // Mimic the base class constructors. FastDelegate() : BaseType() { } template < class X, class Y > FastDelegate(Y * pthis, R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7 )) : BaseType(pthis, function_to_bind) { } template < class X, class Y > FastDelegate(const Y *pthis, R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7 ) const) : BaseType(pthis, function_to_bind) { } FastDelegate(R (*function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7 )) : BaseType(function_to_bind) { } void operator = (const BaseType &x) { *static_cast<BaseType*>(this) = x; } }; //N=8 // Specialization to allow use of // FastDelegate< R ( Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8 ) > // instead of // FastDelegate8 < Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, R > template<typename R, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8> class FastDelegate< R ( Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8 ) > // Inherit from FastDelegate8 so that it can be treated just like a FastDelegate8 : public FastDelegate8 < Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, R > { public: // Make using the base type a bit easier via typedef. typedef FastDelegate8 < Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, R > BaseType; // Allow users access to the specific type of this delegate. typedef FastDelegate SelfType; // Mimic the base class constructors. FastDelegate() : BaseType() { } template < class X, class Y > FastDelegate(Y * pthis, R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8 )) : BaseType(pthis, function_to_bind) { } template < class X, class Y > FastDelegate(const Y *pthis, R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8 ) const) : BaseType(pthis, function_to_bind) { } FastDelegate(R (*function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8 )) : BaseType(function_to_bind) { } void operator = (const BaseType &x) { *static_cast<BaseType*>(this) = x; } }; #endif //FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX //////////////////////////////////////////////////////////////////////////////// // Fast Delegates, part 5: // // MakeDelegate() helper function // // MakeDelegate(&x, &X::func) returns a fastdelegate of the type // necessary for calling x.func() with the correct number of arguments. // This makes it possible to eliminate many typedefs from user code. // //////////////////////////////////////////////////////////////////////////////// // Also declare overloads of a MakeDelegate() global function to // reduce the need for typedefs. // We need seperate overloads for const and non-const member functions. // Also, because of the weird rule about the class of derived member function pointers, // implicit downcasts may need to be applied later to the 'this' pointer. // That's why two classes (X and Y) appear in the definitions. Y must be implicitly // castable to X. // Workaround for VC6. VC6 needs void return types converted into DefaultVoid. // GCC 3.2 and later won't compile this unless it's preceded by 'typename', // but VC6 doesn't allow 'typename' in this context. // So, I have to use a macro. #ifdef FASTDLGT_VC6 #define FASTDLGT_RETTYPE detail::VoidToDefaultVoid<RetType>::type #else #define FASTDLGT_RETTYPE RetType #endif //N=0 template <class X, class Y, class RetType> FastDelegate0<FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)()) { return FastDelegate0<FASTDLGT_RETTYPE>(x, func); } template <class X, class Y, class RetType> FastDelegate0<FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)() const) { return FastDelegate0<FASTDLGT_RETTYPE>(x, func); } //N=1 template <class X, class Y, class Param1, class RetType> FastDelegate1<Param1, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1)) { return FastDelegate1<Param1, FASTDLGT_RETTYPE>(x, func); } template <class X, class Y, class Param1, class RetType> FastDelegate1<Param1, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1) const) { return FastDelegate1<Param1, FASTDLGT_RETTYPE>(x, func); } //N=2 template <class X, class Y, class Param1, class Param2, class RetType> FastDelegate2<Param1, Param2, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2)) { return FastDelegate2<Param1, Param2, FASTDLGT_RETTYPE>(x, func); } template <class X, class Y, class Param1, class Param2, class RetType> FastDelegate2<Param1, Param2, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2) const) { return FastDelegate2<Param1, Param2, FASTDLGT_RETTYPE>(x, func); } //N=3 template <class X, class Y, class Param1, class Param2, class Param3, class RetType> FastDelegate3<Param1, Param2, Param3, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3)) { return FastDelegate3<Param1, Param2, Param3, FASTDLGT_RETTYPE>(x, func); } template <class X, class Y, class Param1, class Param2, class Param3, class RetType> FastDelegate3<Param1, Param2, Param3, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3) const) { return FastDelegate3<Param1, Param2, Param3, FASTDLGT_RETTYPE>(x, func); } //N=4 template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class RetType> FastDelegate4<Param1, Param2, Param3, Param4, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4)) { return FastDelegate4<Param1, Param2, Param3, Param4, FASTDLGT_RETTYPE>(x, func); } template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class RetType> FastDelegate4<Param1, Param2, Param3, Param4, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4) const) { return FastDelegate4<Param1, Param2, Param3, Param4, FASTDLGT_RETTYPE>(x, func); } //N=5 template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class RetType> FastDelegate5<Param1, Param2, Param3, Param4, Param5, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5)) { return FastDelegate5<Param1, Param2, Param3, Param4, Param5, FASTDLGT_RETTYPE>(x, func); } template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class RetType> FastDelegate5<Param1, Param2, Param3, Param4, Param5, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) const) { return FastDelegate5<Param1, Param2, Param3, Param4, Param5, FASTDLGT_RETTYPE>(x, func); } //N=6 template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class RetType> FastDelegate6<Param1, Param2, Param3, Param4, Param5, Param6, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6)) { return FastDelegate6<Param1, Param2, Param3, Param4, Param5, Param6, FASTDLGT_RETTYPE>(x, func); } template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class RetType> FastDelegate6<Param1, Param2, Param3, Param4, Param5, Param6, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) const) { return FastDelegate6<Param1, Param2, Param3, Param4, Param5, Param6, FASTDLGT_RETTYPE>(x, func); } //N=7 template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class RetType> FastDelegate7<Param1, Param2, Param3, Param4, Param5, Param6, Param7, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7)) { return FastDelegate7<Param1, Param2, Param3, Param4, Param5, Param6, Param7, FASTDLGT_RETTYPE>(x, func); } template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class RetType> FastDelegate7<Param1, Param2, Param3, Param4, Param5, Param6, Param7, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) const) { return FastDelegate7<Param1, Param2, Param3, Param4, Param5, Param6, Param7, FASTDLGT_RETTYPE>(x, func); } //N=8 template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8, class RetType> FastDelegate8<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)) { return FastDelegate8<Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, FASTDLGT_RETTYPE>(x, func); } template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8, class RetType> FastDelegate8<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) { return FastDelegate8<Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, FASTDLGT_RETTYPE>(x, func); } // clean up after ourselves... #undef FASTDLGT_RETTYPE } // namespace fastdelegate #endif // !defined(FASTDELEGATE_H)
2.FastDelegateBind.h
// FastDelegateBind.h // Helper file for FastDelegates. Provides bind() function, enabling // FastDelegates to be rapidly compared to programs using boost::function and boost::bind. // // Documentation is found at http://www.codeproject.com/cpp/FastDelegate.asp // // Original author: Jody Hagins. // Minor changes by Don Clugston. // // Warning: The arguments to 'bind' are ignored! No actual binding is performed. // The behaviour is equivalent to boost::bind only when the basic placeholder // arguments _1, _2, _3, etc are used in order. // // HISTORY: // 1.4 Dec 2004. Initial release as part of FastDelegate 1.4. #ifndef FASTDELEGATEBIND_H #define FASTDELEGATEBIND_H #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 //////////////////////////////////////////////////////////////////////////////// // FastDelegate bind() // // bind() helper function for boost compatibility. // (Original author: Jody Hagins). // bind()帮助类,以与boost相兼容(原始作者:Jody Hagins) // // Add another helper, so FastDelegate can be a dropin replacement // for boost::bind (in a fair number of cases). // Note the elipses, because boost::bind() takes place holders // but FastDelegate does not care about them. Getting the place holder // mechanism to work, and play well with boost is a bit tricky, so // we do the "easy" thing... // Assume we have the following code... // using boost::bind; // bind(&Foo:func, &foo, _1, _2); // we should be able to replace the "using" with... // using fastdelegate::bind; // and everything should work fine... //////////////////////////////////////////////////////////////////////////////// #ifdef FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX namespace fastdelegate { //N=0 template <class X, class Y, class RetType> FastDelegate< RetType ( ) > bind( RetType (X::*func)( ), Y * y, ...) { return FastDelegate< RetType ( ) >(y, func); } template <class X, class Y, class RetType> FastDelegate< RetType ( ) > bind( RetType (X::*func)( ) const, Y * y, ...) { return FastDelegate< RetType ( ) >(y, func); } //N=1 template <class X, class Y, class RetType, class Param1> FastDelegate< RetType ( Param1 p1 ) > bind( RetType (X::*func)( Param1 p1 ), Y * y, ...) { return FastDelegate< RetType ( Param1 p1 ) >(y, func); } template <class X, class Y, class RetType, class Param1> FastDelegate< RetType ( Param1 p1 ) > bind( RetType (X::*func)( Param1 p1 ) const, Y * y, ...) { return FastDelegate< RetType ( Param1 p1 ) >(y, func); } //N=2 template <class X, class Y, class RetType, class Param1, class Param2> FastDelegate< RetType ( Param1 p1, Param2 p2 ) > bind( RetType (X::*func)( Param1 p1, Param2 p2 ), Y * y, ...) { return FastDelegate< RetType ( Param1 p1, Param2 p2 ) >(y, func); } template <class X, class Y, class RetType, class Param1, class Param2> FastDelegate< RetType ( Param1 p1, Param2 p2 ) > bind( RetType (X::*func)( Param1 p1, Param2 p2 ) const, Y * y, ...) { return FastDelegate< RetType ( Param1 p1, Param2 p2 ) >(y, func); } //N=3 template <class X, class Y, class RetType, class Param1, class Param2, class Param3> FastDelegate< RetType ( Param1 p1, Param2 p2, Param3 p3 ) > bind( RetType (X::*func)( Param1 p1, Param2 p2, Param3 p3 ), Y * y, ...) { return FastDelegate< RetType ( Param1 p1, Param2 p2, Param3 p3 ) >(y, func); } template <class X, class Y, class RetType, class Param1, class Param2, class Param3> FastDelegate< RetType ( Param1 p1, Param2 p2, Param3 p3 ) > bind( RetType (X::*func)( Param1 p1, Param2 p2, Param3 p3 ) const, Y * y, ...) { return FastDelegate< RetType ( Param1 p1, Param2 p2, Param3 p3 ) >(y, func); } //N=4 template <class X, class Y, class RetType, class Param1, class Param2, class Param3, class Param4> FastDelegate< RetType ( Param1 p1, Param2 p2, Param3 p3, Param4 p4 ) > bind( RetType (X::*func)( Param1 p1, Param2 p2, Param3 p3, Param4 p4 ), Y * y, ...) { return FastDelegate< RetType ( Param1 p1, Param2 p2, Param3 p3, Param4 p4 ) >(y, func); } template <class X, class Y, class RetType, class Param1, class Param2, class Param3, class Param4> FastDelegate< RetType ( Param1 p1, Param2 p2, Param3 p3, Param4 p4 ) > bind( RetType (X::*func)( Param1 p1, Param2 p2, Param3 p3, Param4 p4 ) const, Y * y, ...) { return FastDelegate< RetType ( Param1 p1, Param2 p2, Param3 p3, Param4 p4 ) >(y, func); } //N=5 template <class X, class Y, class RetType, class Param1, class Param2, class Param3, class Param4, class Param5> FastDelegate< RetType ( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5 ) > bind( RetType (X::*func)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5 ), Y * y, ...) { return FastDelegate< RetType ( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5 ) >(y, func); } template <class X, class Y, class RetType, class Param1, class Param2, class Param3, class Param4, class Param5> FastDelegate< RetType ( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5 ) > bind( RetType (X::*func)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5 ) const, Y * y, ...) { return FastDelegate< RetType ( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5 ) >(y, func); } //N=6 template <class X, class Y, class RetType, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6> FastDelegate< RetType ( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6 ) > bind( RetType (X::*func)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6 ), Y * y, ...) { return FastDelegate< RetType ( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6 ) >(y, func); } template <class X, class Y, class RetType, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6> FastDelegate< RetType ( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6 ) > bind( RetType (X::*func)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6 ) const, Y * y, ...) { return FastDelegate< RetType ( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6 ) >(y, func); } //N=7 template <class X, class Y, class RetType, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7> FastDelegate< RetType ( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7 ) > bind( RetType (X::*func)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7 ), Y * y, ...) { return FastDelegate< RetType ( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7 ) >(y, func); } template <class X, class Y, class RetType, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7> FastDelegate< RetType ( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7 ) > bind( RetType (X::*func)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7 ) const, Y * y, ...) { return FastDelegate< RetType ( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7 ) >(y, func); } //N=8 template <class X, class Y, class RetType, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8> FastDelegate< RetType ( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8 ) > bind( RetType (X::*func)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8 ), Y * y, ...) { return FastDelegate< RetType ( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8 ) >(y, func); } template <class X, class Y, class RetType, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8> FastDelegate< RetType ( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8 ) > bind( RetType (X::*func)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8 ) const, Y * y, ...) { return FastDelegate< RetType ( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8 ) >(y, func); } #endif //FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX } // namespace fastdelegate #endif // !defined(FASTDELEGATEBIND_H)