dvander@347: // FastDelegate.h dvander@347: // Efficient delegates in C++ that generate only two lines of asm code! dvander@347: // Documentation is found at http://www.codeproject.com/cpp/FastDelegate.asp dvander@347: // dvander@347: // - Don Clugston, Mar 2004. dvander@347: // Major contributions were made by Jody Hagins. dvander@347: // History: dvander@347: // 24-Apr-04 1.0 * Submitted to CodeProject. dvander@347: // 28-Apr-04 1.1 * Prevent most unsafe uses of evil static function hack. dvander@347: // * Improved syntax for horrible_cast (thanks Paul Bludov). dvander@347: // * Tested on Metrowerks MWCC and Intel ICL (IA32) dvander@347: // * Compiled, but not run, on Comeau C++ and Intel Itanium ICL. dvander@347: // 27-Jun-04 1.2 * Now works on Borland C++ Builder 5.5 dvander@347: // * Now works on /clr "managed C++" code on VC7, VC7.1 dvander@347: // * Comeau C++ now compiles without warnings. dvander@347: // * Prevent the virtual inheritance case from being used on dvander@347: // VC6 and earlier, which generate incorrect code. dvander@347: // * Improved warning and error messages. Non-standard hacks dvander@347: // now have compile-time checks to make them safer. dvander@347: // * implicit_cast used instead of static_cast in many cases. dvander@347: // * If calling a const member function, a const class pointer can be used. dvander@347: // * MakeDelegate() global helper function added to simplify pass-by-value. dvander@347: // * Added fastdelegate.clear() dvander@347: // 16-Jul-04 1.2.1* Workaround for gcc bug (const member function pointers in templates) dvander@347: // 30-Oct-04 1.3 * Support for (non-void) return values. dvander@347: // * No more workarounds in client code! dvander@347: // MSVC and Intel now use a clever hack invented by John Dlugosz: dvander@347: // - The FASTDELEGATEDECLARE workaround is no longer necessary. dvander@347: // - No more warning messages for VC6 dvander@347: // * Less use of macros. Error messages should be more comprehensible. dvander@347: // * Added include guards dvander@347: // * Added FastDelegate::empty() to test if invocation is safe (Thanks Neville Franks). dvander@347: // * Now tested on VS 2005 Express Beta, PGI C++ dvander@347: // 24-Dec-04 1.4 * Added DelegateMemento, to allow collections of disparate delegates. dvander@347: // * <,>,<=,>= comparison operators to allow storage in ordered containers. dvander@347: // * Substantial reduction of code size, especially the 'Closure' class. dvander@347: // * Standardised all the compiler-specific workarounds. dvander@347: // * MFP conversion now works for CodePlay (but not yet supported in the full code). dvander@347: // * Now compiles without warnings on _any_ supported compiler, including BCC 5.5.1 dvander@347: // * New syntax: FastDelegate< int (char *, double) >. dvander@347: // 14-Feb-05 1.4.1* Now treats =0 as equivalent to .clear(), ==0 as equivalent to .empty(). (Thanks elfric). dvander@347: // * Now tested on Intel ICL for AMD64, VS2005 Beta for AMD64 and Itanium. dvander@347: // 30-Mar-05 1.5 * Safebool idiom: "if (dg)" is now equivalent to "if (!dg.empty())" dvander@347: // * Fully supported by CodePlay VectorC dvander@347: // * Bugfix for Metrowerks: empty() was buggy because a valid MFP can be 0 on MWCC! dvander@347: // * More optimal assignment,== and != operators for static function pointers. dvander@347: dvander@347: #ifndef FASTDELEGATE_H dvander@347: #define FASTDELEGATE_H dvander@347: dvander@347: #ifdef _MSC_VER dvander@347: # if _MSC_VER > 1000 dvander@347: # pragma once dvander@347: # endif // _MSC_VER > 1000 dvander@347: #endif // #ifdef _MSC_VER dvander@347: dvander@347: #include // to allow <,> comparisons dvander@347: dvander@347: //////////////////////////////////////////////////////////////////////////////// dvander@347: // Configuration options dvander@347: // dvander@347: //////////////////////////////////////////////////////////////////////////////// dvander@347: dvander@347: // Uncomment the following #define for optimally-sized delegates. dvander@347: // In this case, the generated asm code is almost identical to the code you'd get dvander@347: // if the compiler had native support for delegates. dvander@347: // It will not work on systems where sizeof(dataptr) < sizeof(codeptr). dvander@347: // Thus, it will not work for DOS compilers using the medium model. dvander@347: // It will also probably fail on some DSP systems. dvander@347: #define FASTDELEGATE_USESTATICFUNCTIONHACK dvander@347: dvander@347: // Uncomment the next line to allow function declarator syntax. dvander@347: // It is automatically enabled for those compilers where it is known to work. dvander@347: //#define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX dvander@347: dvander@347: //////////////////////////////////////////////////////////////////////////////// dvander@347: // Compiler identification for workarounds dvander@347: // dvander@347: //////////////////////////////////////////////////////////////////////////////// dvander@347: dvander@347: // Compiler identification. It's not easy to identify Visual C++ because dvander@347: // many vendors fraudulently define Microsoft's identifiers. dvander@347: #if defined(_MSC_VER) && !defined(__MWERKS__) && !defined(__VECTOR_C) && !defined(__ICL) && !defined(__BORLANDC__) dvander@347: #define FASTDLGT_ISMSVC dvander@347: dvander@347: #if (_MSC_VER <1300) // Many workarounds are required for VC6. dvander@347: #define FASTDLGT_VC6 dvander@347: #pragma warning(disable:4786) // disable this ridiculous warning dvander@347: #endif dvander@347: dvander@347: #endif dvander@347: dvander@347: // Does the compiler uses Microsoft's member function pointer structure? dvander@347: // If so, it needs special treatment. dvander@347: // Metrowerks CodeWarrior, Intel, and CodePlay fraudulently define Microsoft's dvander@347: // identifier, _MSC_VER. We need to filter Metrowerks out. dvander@347: #if defined(_MSC_VER) && !defined(__MWERKS__) dvander@347: #define FASTDLGT_MICROSOFT_MFP dvander@347: dvander@347: #if !defined(__VECTOR_C) dvander@347: // CodePlay doesn't have the __single/multi/virtual_inheritance keywords dvander@347: #define FASTDLGT_HASINHERITANCE_KEYWORDS dvander@347: #endif dvander@347: #endif dvander@347: dvander@347: // Does it allow function declarator syntax? The following compilers are known to work: dvander@347: #if defined(FASTDLGT_ISMSVC) && (_MSC_VER >=1310) // VC 7.1 dvander@347: #define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX dvander@347: #endif dvander@347: dvander@347: // Gcc(2.95+), and versions of Digital Mars, Intel and Comeau in common use. dvander@347: #if defined (__DMC__) || defined(__GNUC__) || defined(__ICL) || defined(__COMO__) dvander@347: #define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX dvander@347: #endif dvander@347: dvander@347: // It works on Metrowerks MWCC 3.2.2. From boost.Config it should work on earlier ones too. dvander@347: #if defined (__MWERKS__) dvander@347: #define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX dvander@347: #endif dvander@347: dvander@347: #ifdef __GNUC__ // Workaround GCC bug #8271 dvander@347: // At present, GCC doesn't recognize constness of MFPs in templates dvander@347: #define FASTDELEGATE_GCC_BUG_8271 dvander@347: #endif dvander@347: dvander@347: dvander@347: dvander@347: //////////////////////////////////////////////////////////////////////////////// dvander@347: // General tricks used in this code dvander@347: // dvander@347: // (a) Error messages are generated by typdefing an array of negative size to dvander@347: // generate compile-time errors. dvander@347: // (b) Warning messages on MSVC are generated by declaring unused variables, and dvander@347: // enabling the "variable XXX is never used" warning. dvander@347: // (c) Unions are used in a few compiler-specific cases to perform illegal casts. dvander@347: // (d) For Microsoft and Intel, when adjusting the 'this' pointer, it's cast to dvander@347: // (char *) first to ensure that the correct number of *bytes* are added. dvander@347: // dvander@347: //////////////////////////////////////////////////////////////////////////////// dvander@347: // Helper templates dvander@347: // dvander@347: //////////////////////////////////////////////////////////////////////////////// dvander@347: dvander@347: dvander@347: namespace fastdelegate { dvander@347: namespace detail { // we'll hide the implementation details in a nested namespace. dvander@347: dvander@347: // implicit_cast< > dvander@347: // I believe this was originally going to be in the C++ standard but dvander@347: // was left out by accident. It's even milder than static_cast. dvander@347: // I use it instead of static_cast<> to emphasize that I'm not doing dvander@347: // anything nasty. dvander@347: // Usage is identical to static_cast<> dvander@347: template dvander@347: inline OutputClass implicit_cast(InputClass input){ dvander@347: return input; dvander@347: } dvander@347: dvander@347: // horrible_cast< > dvander@347: // This is truly evil. It completely subverts C++'s type system, allowing you dvander@347: // to cast from any class to any other class. Technically, using a union dvander@347: // to perform the cast is undefined behaviour (even in C). But we can see if dvander@347: // it is OK by checking that the union is the same size as each of its members. dvander@347: // horrible_cast<> should only be used for compiler-specific workarounds. dvander@347: // Usage is identical to reinterpret_cast<>. dvander@347: dvander@347: // This union is declared outside the horrible_cast because BCC 5.5.1 dvander@347: // can't inline a function with a nested class, and gives a warning. dvander@347: template dvander@347: union horrible_union{ dvander@347: OutputClass out; dvander@347: InputClass in; dvander@347: }; dvander@347: dvander@347: template dvander@347: inline OutputClass horrible_cast(const InputClass input){ dvander@347: horrible_union u; dvander@347: // Cause a compile-time error if in, out and u are not the same size. dvander@347: // If the compile fails here, it means the compiler has peculiar dvander@347: // unions which would prevent the cast from working. dvander@347: typedef int ERROR_CantUseHorrible_cast[sizeof(InputClass)==sizeof(u) dvander@347: && sizeof(InputClass)==sizeof(OutputClass) ? 1 : -1]; dvander@347: u.in = input; dvander@347: return u.out; dvander@347: } dvander@347: dvander@347: //////////////////////////////////////////////////////////////////////////////// dvander@347: // Workarounds dvander@347: // dvander@347: //////////////////////////////////////////////////////////////////////////////// dvander@347: dvander@347: // Backwards compatibility: This macro used to be necessary in the virtual inheritance dvander@347: // case for Intel and Microsoft. Now it just forward-declares the class. dvander@347: #define FASTDELEGATEDECLARE(CLASSNAME) class CLASSNAME; dvander@347: dvander@347: // Prevent use of the static function hack with the DOS medium model. dvander@347: #ifdef __MEDIUM__ dvander@347: #undef FASTDELEGATE_USESTATICFUNCTIONHACK dvander@347: #endif dvander@347: dvander@347: // DefaultVoid - a workaround for 'void' templates in VC6. dvander@347: // dvander@347: // (1) VC6 and earlier do not allow 'void' as a default template argument. dvander@347: // (2) They also doesn't allow you to return 'void' from a function. dvander@347: // dvander@347: // Workaround for (1): Declare a dummy type 'DefaultVoid' which we use dvander@347: // when we'd like to use 'void'. We convert it into 'void' and back dvander@347: // using the templates DefaultVoidToVoid<> and VoidToDefaultVoid<>. dvander@347: // Workaround for (2): On VC6, the code for calling a void function is dvander@347: // identical to the code for calling a non-void function in which the dvander@347: // return value is never used, provided the return value is returned dvander@347: // in the EAX register, rather than on the stack. dvander@347: // This is true for most fundamental types such as int, enum, void *. dvander@347: // Const void * is the safest option since it doesn't participate dvander@347: // in any automatic conversions. But on a 16-bit compiler it might dvander@347: // cause extra code to be generated, so we disable it for all compilers dvander@347: // except for VC6 (and VC5). dvander@347: #ifdef FASTDLGT_VC6 dvander@347: // VC6 workaround dvander@347: typedef const void * DefaultVoid; dvander@347: #else dvander@347: // On any other compiler, just use a normal void. dvander@347: typedef void DefaultVoid; dvander@347: #endif dvander@347: dvander@347: // Translate from 'DefaultVoid' to 'void'. dvander@347: // Everything else is unchanged dvander@347: template dvander@347: struct DefaultVoidToVoid { typedef T type; }; dvander@347: dvander@347: template <> dvander@347: struct DefaultVoidToVoid { typedef void type; }; dvander@347: dvander@347: // Translate from 'void' into 'DefaultVoid' dvander@347: // Everything else is unchanged dvander@347: template dvander@347: struct VoidToDefaultVoid { typedef T type; }; dvander@347: dvander@347: template <> dvander@347: struct VoidToDefaultVoid { typedef DefaultVoid type; }; dvander@347: dvander@347: dvander@347: dvander@347: //////////////////////////////////////////////////////////////////////////////// dvander@347: // Fast Delegates, part 1: dvander@347: // dvander@347: // Conversion of member function pointer to a standard form dvander@347: // dvander@347: //////////////////////////////////////////////////////////////////////////////// dvander@347: dvander@347: // GenericClass is a fake class, ONLY used to provide a type. dvander@347: // It is vitally important that it is never defined, so that the compiler doesn't dvander@347: // think it can optimize the invocation. For example, Borland generates simpler dvander@347: // code if it knows the class only uses single inheritance. dvander@347: dvander@347: // Compilers using Microsoft's structure need to be treated as a special case. dvander@347: #ifdef FASTDLGT_MICROSOFT_MFP dvander@347: dvander@347: #ifdef FASTDLGT_HASINHERITANCE_KEYWORDS dvander@347: // For Microsoft and Intel, we want to ensure that it's the most efficient type of MFP dvander@347: // (4 bytes), even when the /vmg option is used. Declaring an empty class dvander@347: // would give 16 byte pointers in this case.... dvander@347: class __single_inheritance GenericClass; dvander@347: #endif dvander@347: // ...but for Codeplay, an empty class *always* gives 4 byte pointers. dvander@347: // If compiled with the /clr option ("managed C++"), the JIT compiler thinks dvander@347: // it needs to load GenericClass before it can call any of its functions, dvander@347: // (compiles OK but crashes at runtime!), so we need to declare an dvander@347: // empty class to make it happy. dvander@347: // Codeplay and VC4 can't cope with the unknown_inheritance case either. dvander@347: class GenericClass {}; dvander@347: #else dvander@347: class GenericClass; dvander@347: #endif dvander@347: dvander@347: // The size of a single inheritance member function pointer. dvander@347: const int SINGLE_MEMFUNCPTR_SIZE = sizeof(void (GenericClass::*)()); dvander@347: dvander@347: // SimplifyMemFunc< >::Convert() dvander@347: // dvander@347: // A template function that converts an arbitrary member function pointer into the dvander@347: // simplest possible form of member function pointer, using a supplied 'this' pointer. dvander@347: // According to the standard, this can be done legally with reinterpret_cast<>. dvander@347: // For (non-standard) compilers which use member function pointers which vary in size dvander@347: // depending on the class, we need to use knowledge of the internal structure of a dvander@347: // member function pointer, as used by the compiler. Template specialization is used dvander@347: // to distinguish between the sizes. Because some compilers don't support partial dvander@347: // template specialisation, I use full specialisation of a wrapper struct. dvander@347: dvander@347: // general case -- don't know how to convert it. Force a compile failure dvander@347: template dvander@347: struct SimplifyMemFunc { dvander@347: template dvander@347: inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind, dvander@347: GenericMemFuncType &bound_func) { dvander@347: // Unsupported member function type -- force a compile failure. dvander@347: // (it's illegal to have a array with negative size). dvander@347: typedef char ERROR_Unsupported_member_function_pointer_on_this_compiler[N-100]; dvander@347: return 0; dvander@347: } dvander@347: }; dvander@347: dvander@347: // For compilers where all member func ptrs are the same size, everything goes here. dvander@347: // For non-standard compilers, only single_inheritance classes go here. dvander@347: template <> dvander@347: struct SimplifyMemFunc { dvander@347: template dvander@347: inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind, dvander@347: GenericMemFuncType &bound_func) { dvander@347: #if defined __DMC__ dvander@347: // Digital Mars doesn't allow you to cast between abitrary PMF's, dvander@347: // even though the standard says you can. The 32-bit compiler lets you dvander@347: // static_cast through an int, but the DOS compiler doesn't. dvander@347: bound_func = horrible_cast(function_to_bind); dvander@347: #else dvander@347: bound_func = reinterpret_cast(function_to_bind); dvander@347: #endif dvander@347: return reinterpret_cast(pthis); dvander@347: } dvander@347: }; dvander@347: dvander@347: //////////////////////////////////////////////////////////////////////////////// dvander@347: // Fast Delegates, part 1b: dvander@347: // dvander@347: // Workarounds for Microsoft and Intel dvander@347: // dvander@347: //////////////////////////////////////////////////////////////////////////////// dvander@347: dvander@347: dvander@347: // Compilers with member function pointers which violate the standard (MSVC, Intel, Codeplay), dvander@347: // need to be treated as a special case. dvander@347: #ifdef FASTDLGT_MICROSOFT_MFP dvander@347: dvander@347: // We use unions to perform horrible_casts. I would like to use #pragma pack(push, 1) dvander@347: // at the start of each function for extra safety, but VC6 seems to ICE dvander@347: // intermittently if you do this inside a template. dvander@347: dvander@347: // __multiple_inheritance classes go here dvander@347: // Nasty hack for Microsoft and Intel (IA32 and Itanium) dvander@347: template<> dvander@347: struct SimplifyMemFunc< SINGLE_MEMFUNCPTR_SIZE + sizeof(int) > { dvander@347: template dvander@347: inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind, dvander@347: GenericMemFuncType &bound_func) { dvander@347: // We need to use a horrible_cast to do this conversion. dvander@347: // In MSVC, a multiple inheritance member pointer is internally defined as: dvander@347: union { dvander@347: XFuncType func; dvander@347: struct { dvander@347: GenericMemFuncType funcaddress; // points to the actual member function dvander@347: int delta; // #BYTES to be added to the 'this' pointer dvander@347: }s; dvander@347: } u; dvander@347: // Check that the horrible_cast will work dvander@347: typedef int ERROR_CantUsehorrible_cast[sizeof(function_to_bind)==sizeof(u.s)? 1 : -1]; dvander@347: u.func = function_to_bind; dvander@347: bound_func = u.s.funcaddress; dvander@347: return reinterpret_cast(reinterpret_cast(pthis) + u.s.delta); dvander@347: } dvander@347: }; dvander@347: dvander@347: // virtual inheritance is a real nuisance. It's inefficient and complicated. dvander@347: // On MSVC and Intel, there isn't enough information in the pointer itself to dvander@347: // enable conversion to a closure pointer. Earlier versions of this code didn't dvander@347: // work for all cases, and generated a compile-time error instead. dvander@347: // But a very clever hack invented by John M. Dlugosz solves this problem. dvander@347: // My code is somewhat different to his: I have no asm code, and I make no dvander@347: // assumptions about the calling convention that is used. dvander@347: dvander@347: // In VC++ and ICL, a virtual_inheritance member pointer dvander@347: // is internally defined as: dvander@347: struct MicrosoftVirtualMFP { dvander@347: void (GenericClass::*codeptr)(); // points to the actual member function dvander@347: int delta; // #bytes to be added to the 'this' pointer dvander@347: int vtable_index; // or 0 if no virtual inheritance dvander@347: }; dvander@347: // The CRUCIAL feature of Microsoft/Intel MFPs which we exploit is that the dvander@347: // m_codeptr member is *always* called, regardless of the values of the other dvander@347: // members. (This is *not* true for other compilers, eg GCC, which obtain the dvander@347: // function address from the vtable if a virtual function is being called). dvander@347: // Dlugosz's trick is to make the codeptr point to a probe function which dvander@347: // returns the 'this' pointer that was used. dvander@347: dvander@347: // Define a generic class that uses virtual inheritance. dvander@347: // It has a trival member function that returns the value of the 'this' pointer. dvander@347: struct GenericVirtualClass : virtual public GenericClass dvander@347: { dvander@347: typedef GenericVirtualClass * (GenericVirtualClass::*ProbePtrType)(); dvander@347: GenericVirtualClass * GetThis() { return this; } dvander@347: }; dvander@347: dvander@347: // __virtual_inheritance classes go here dvander@347: template <> dvander@347: struct SimplifyMemFunc dvander@347: { dvander@347: dvander@347: template dvander@347: inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind, dvander@347: GenericMemFuncType &bound_func) { dvander@347: union { dvander@347: XFuncType func; dvander@347: GenericClass* (X::*ProbeFunc)(); dvander@347: MicrosoftVirtualMFP s; dvander@347: } u; dvander@347: u.func = function_to_bind; dvander@347: bound_func = reinterpret_cast(u.s.codeptr); dvander@347: union { dvander@347: GenericVirtualClass::ProbePtrType virtfunc; dvander@347: MicrosoftVirtualMFP s; dvander@347: } u2; dvander@347: // Check that the horrible_cast<>s will work dvander@347: typedef int ERROR_CantUsehorrible_cast[sizeof(function_to_bind)==sizeof(u.s) dvander@347: && sizeof(function_to_bind)==sizeof(u.ProbeFunc) dvander@347: && sizeof(u2.virtfunc)==sizeof(u2.s) ? 1 : -1]; dvander@347: // Unfortunately, taking the address of a MF prevents it from being inlined, so dvander@347: // this next line can't be completely optimised away by the compiler. dvander@347: u2.virtfunc = &GenericVirtualClass::GetThis; dvander@347: u.s.codeptr = u2.s.codeptr; dvander@347: return (pthis->*u.ProbeFunc)(); dvander@347: } dvander@347: }; dvander@347: dvander@347: #if (_MSC_VER <1300) dvander@347: dvander@347: // Nasty hack for Microsoft Visual C++ 6.0 dvander@347: // unknown_inheritance classes go here dvander@347: // There is a compiler bug in MSVC6 which generates incorrect code in this case!! dvander@347: template <> dvander@347: struct SimplifyMemFunc dvander@347: { dvander@347: template dvander@347: inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind, dvander@347: GenericMemFuncType &bound_func) { dvander@347: // There is an apalling but obscure compiler bug in MSVC6 and earlier: dvander@347: // vtable_index and 'vtordisp' are always set to 0 in the dvander@347: // unknown_inheritance case! dvander@347: // This means that an incorrect function could be called!!! dvander@347: // Compiling with the /vmg option leads to potentially incorrect code. dvander@347: // This is probably the reason that the IDE has a user interface for specifying dvander@347: // the /vmg option, but it is disabled - you can only specify /vmg on dvander@347: // the command line. In VC1.5 and earlier, the compiler would ICE if it ever dvander@347: // encountered this situation. dvander@347: // It is OK to use the /vmg option if /vmm or /vms is specified. dvander@347: dvander@347: // Fortunately, the wrong function is only called in very obscure cases. dvander@347: // It only occurs when a derived class overrides a virtual function declared dvander@347: // in a virtual base class, and the member function dvander@347: // points to the *Derived* version of that function. The problem can be dvander@347: // completely averted in 100% of cases by using the *Base class* for the dvander@347: // member fpointer. Ie, if you use the base class as an interface, you'll dvander@347: // stay out of trouble. dvander@347: // Occasionally, you might want to point directly to a derived class function dvander@347: // that isn't an override of a base class. In this case, both vtable_index dvander@347: // and 'vtordisp' are zero, but a virtual_inheritance pointer will be generated. dvander@347: // We can generate correct code in this case. To prevent an incorrect call from dvander@347: // ever being made, on MSVC6 we generate a warning, and call a function to dvander@347: // make the program crash instantly. dvander@347: typedef char ERROR_VC6CompilerBug[-100]; dvander@347: return 0; dvander@347: } dvander@347: }; dvander@347: dvander@347: dvander@347: #else dvander@347: dvander@347: // Nasty hack for Microsoft and Intel (IA32 and Itanium) dvander@347: // unknown_inheritance classes go here dvander@347: // This is probably the ugliest bit of code I've ever written. Look at the casts! dvander@347: // There is a compiler bug in MSVC6 which prevents it from using this code. dvander@347: template <> dvander@347: struct SimplifyMemFunc dvander@347: { dvander@347: template dvander@347: inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind, dvander@347: GenericMemFuncType &bound_func) { dvander@347: // The member function pointer is 16 bytes long. We can't use a normal cast, but dvander@347: // we can use a union to do the conversion. dvander@347: union { dvander@347: XFuncType func; dvander@347: // In VC++ and ICL, an unknown_inheritance member pointer dvander@347: // is internally defined as: dvander@347: struct { dvander@347: GenericMemFuncType m_funcaddress; // points to the actual member function dvander@347: int delta; // #bytes to be added to the 'this' pointer dvander@347: int vtordisp; // #bytes to add to 'this' to find the vtable dvander@347: int vtable_index; // or 0 if no virtual inheritance dvander@347: } s; dvander@347: } u; dvander@347: // Check that the horrible_cast will work dvander@347: typedef int ERROR_CantUsehorrible_cast[sizeof(XFuncType)==sizeof(u.s)? 1 : -1]; dvander@347: u.func = function_to_bind; dvander@347: bound_func = u.s.funcaddress; dvander@347: int virtual_delta = 0; dvander@347: if (u.s.vtable_index) { // Virtual inheritance is used dvander@347: // First, get to the vtable. dvander@347: // It is 'vtordisp' bytes from the start of the class. dvander@347: const int * vtable = *reinterpret_cast( dvander@347: reinterpret_cast(pthis) + u.s.vtordisp ); dvander@347: dvander@347: // 'vtable_index' tells us where in the table we should be looking. dvander@347: virtual_delta = u.s.vtordisp + *reinterpret_cast( dvander@347: reinterpret_cast(vtable) + u.s.vtable_index); dvander@347: } dvander@347: // The int at 'virtual_delta' gives us the amount to add to 'this'. dvander@347: // Finally we can add the three components together. Phew! dvander@347: return reinterpret_cast( dvander@347: reinterpret_cast(pthis) + u.s.delta + virtual_delta); dvander@347: }; dvander@347: }; dvander@347: #endif // MSVC 7 and greater dvander@347: dvander@347: #endif // MS/Intel hacks dvander@347: dvander@347: } // namespace detail dvander@347: dvander@347: //////////////////////////////////////////////////////////////////////////////// dvander@347: // Fast Delegates, part 2: dvander@347: // dvander@347: // Define the delegate storage, and cope with static functions dvander@347: // dvander@347: //////////////////////////////////////////////////////////////////////////////// dvander@347: dvander@347: // DelegateMemento -- an opaque structure which can hold an arbitary delegate. dvander@347: // It knows nothing about the calling convention or number of arguments used by dvander@347: // the function pointed to. dvander@347: // It supplies comparison operators so that it can be stored in STL collections. dvander@347: // It cannot be set to anything other than null, nor invoked directly: dvander@347: // it must be converted to a specific delegate. dvander@347: dvander@347: // Implementation: dvander@347: // There are two possible implementations: the Safe method and the Evil method. dvander@347: // DelegateMemento - Safe version dvander@347: // dvander@347: // This implementation is standard-compliant, but a bit tricky. dvander@347: // A static function pointer is stored inside the class. dvander@347: // Here are the valid values: dvander@347: // +-- Static pointer --+--pThis --+-- pMemFunc-+-- Meaning------+ dvander@347: // | 0 | 0 | 0 | Empty | dvander@347: // | !=0 |(dontcare)| Invoker | Static function| dvander@347: // | 0 | !=0 | !=0* | Method call | dvander@347: // +--------------------+----------+------------+----------------+ dvander@347: // * For Metrowerks, this can be 0. (first virtual function in a dvander@347: // single_inheritance class). dvander@347: // When stored stored inside a specific delegate, the 'dontcare' entries are replaced dvander@347: // with a reference to the delegate itself. This complicates the = and == operators dvander@347: // for the delegate class. dvander@347: dvander@347: // DelegateMemento - Evil version dvander@347: // dvander@347: // For compilers where data pointers are at least as big as code pointers, it is dvander@347: // possible to store the function pointer in the this pointer, using another dvander@347: // horrible_cast. In this case the DelegateMemento implementation is simple: dvander@347: // +--pThis --+-- pMemFunc-+-- Meaning---------------------+ dvander@347: // | 0 | 0 | Empty | dvander@347: // | !=0 | !=0* | Static function or method call| dvander@347: // +----------+------------+-------------------------------+ dvander@347: // * For Metrowerks, this can be 0. (first virtual function in a dvander@347: // single_inheritance class). dvander@347: // Note that the Sun C++ and MSVC documentation explicitly state that they dvander@347: // support static_cast between void * and function pointers. dvander@347: dvander@347: class DelegateMemento { dvander@347: protected: dvander@347: // the data is protected, not private, because many dvander@347: // compilers have problems with template friends. dvander@347: typedef void (detail::GenericClass::*GenericMemFuncType)(); // arbitrary MFP. dvander@347: detail::GenericClass *m_pthis; dvander@347: GenericMemFuncType m_pFunction; dvander@347: dvander@347: #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK) dvander@347: typedef void (*GenericFuncPtr)(); // arbitrary code pointer dvander@347: GenericFuncPtr m_pStaticFunction; dvander@347: #endif dvander@347: dvander@347: public: dvander@347: #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK) dvander@347: DelegateMemento() : m_pthis(0), m_pFunction(0), m_pStaticFunction(0) {}; dvander@347: void clear() { dvander@347: m_pthis=0; m_pFunction=0; m_pStaticFunction=0; dvander@347: } dvander@347: #else dvander@347: DelegateMemento() : m_pthis(0), m_pFunction(0) {}; dvander@347: void clear() { m_pthis=0; m_pFunction=0; } dvander@347: #endif dvander@347: public: dvander@347: #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK) dvander@347: inline bool IsEqual (const DelegateMemento &x) const{ dvander@347: // We have to cope with the static function pointers as a special case dvander@347: if (m_pFunction!=x.m_pFunction) return false; dvander@347: // the static function ptrs must either both be equal, or both be 0. dvander@347: if (m_pStaticFunction!=x.m_pStaticFunction) return false; dvander@347: if (m_pStaticFunction!=0) return m_pthis==x.m_pthis; dvander@347: else return true; dvander@347: } dvander@347: #else // Evil Method dvander@347: inline bool IsEqual (const DelegateMemento &x) const{ dvander@347: return m_pthis==x.m_pthis && m_pFunction==x.m_pFunction; dvander@347: } dvander@347: #endif dvander@347: // Provide a strict weak ordering for DelegateMementos. dvander@347: inline bool IsLess(const DelegateMemento &right) const { dvander@347: // deal with static function pointers first dvander@347: #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK) dvander@347: if (m_pStaticFunction !=0 || right.m_pStaticFunction!=0) dvander@347: return m_pStaticFunction < right.m_pStaticFunction; dvander@347: #endif dvander@347: if (m_pthis !=right.m_pthis) return m_pthis < right.m_pthis; dvander@347: // There are no ordering operators for member function pointers, dvander@347: // but we can fake one by comparing each byte. The resulting ordering is dvander@347: // arbitrary (and compiler-dependent), but it permits storage in ordered STL containers. dvander@347: return memcmp(&m_pFunction, &right.m_pFunction, sizeof(m_pFunction)) < 0; dvander@347: dvander@347: } dvander@347: // BUGFIX (Mar 2005): dvander@347: // We can't just compare m_pFunction because on Metrowerks, dvander@347: // m_pFunction can be zero even if the delegate is not empty! dvander@347: inline bool operator ! () const // Is it bound to anything? dvander@347: { return m_pthis==0 && m_pFunction==0; } dvander@347: inline bool empty() const // Is it bound to anything? dvander@347: { return m_pthis==0 && m_pFunction==0; } dvander@347: public: dvander@347: DelegateMemento & operator = (const DelegateMemento &right) { dvander@347: SetMementoFrom(right); dvander@347: return *this; dvander@347: } dvander@347: inline bool operator <(const DelegateMemento &right) { dvander@347: return IsLess(right); dvander@347: } dvander@347: inline bool operator >(const DelegateMemento &right) { dvander@347: return right.IsLess(*this); dvander@347: } dvander@347: DelegateMemento (const DelegateMemento &right) : dvander@347: m_pthis(right.m_pthis), m_pFunction(right.m_pFunction) dvander@347: #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK) dvander@347: , m_pStaticFunction (right.m_pStaticFunction) dvander@347: #endif dvander@347: {} dvander@347: protected: dvander@347: void SetMementoFrom(const DelegateMemento &right) { dvander@347: m_pFunction = right.m_pFunction; dvander@347: m_pthis = right.m_pthis; dvander@347: #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK) dvander@347: m_pStaticFunction = right.m_pStaticFunction; dvander@347: #endif dvander@347: } dvander@347: }; dvander@347: dvander@347: dvander@347: // ClosurePtr<> dvander@347: // dvander@347: // A private wrapper class that adds function signatures to DelegateMemento. dvander@347: // It's the class that does most of the actual work. dvander@347: // The signatures are specified by: dvander@347: // GenericMemFunc: must be a type of GenericClass member function pointer. dvander@347: // StaticFuncPtr: must be a type of function pointer with the same signature dvander@347: // as GenericMemFunc. dvander@347: // UnvoidStaticFuncPtr: is the same as StaticFuncPtr, except on VC6 dvander@347: // where it never returns void (returns DefaultVoid instead). dvander@347: dvander@347: // An outer class, FastDelegateN<>, handles the invoking and creates the dvander@347: // necessary typedefs. dvander@347: // This class does everything else. dvander@347: dvander@347: namespace detail { dvander@347: dvander@347: template < class GenericMemFunc, class StaticFuncPtr, class UnvoidStaticFuncPtr> dvander@347: class ClosurePtr : public DelegateMemento { dvander@347: public: dvander@347: // These functions are for setting the delegate to a member function. dvander@347: dvander@347: // Here's the clever bit: we convert an arbitrary member function into a dvander@347: // standard form. XMemFunc should be a member function of class X, but I can't dvander@347: // enforce that here. It needs to be enforced by the wrapper class. dvander@347: template < class X, class XMemFunc > dvander@347: inline void bindmemfunc(X *pthis, XMemFunc function_to_bind ) { dvander@347: m_pthis = SimplifyMemFunc< sizeof(function_to_bind) > dvander@347: ::Convert(pthis, function_to_bind, m_pFunction); dvander@347: #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK) dvander@347: m_pStaticFunction = 0; dvander@347: #endif dvander@347: } dvander@347: // For const member functions, we only need a const class pointer. dvander@347: // Since we know that the member function is const, it's safe to dvander@347: // remove the const qualifier from the 'this' pointer with a const_cast. dvander@347: // VC6 has problems if we just overload 'bindmemfunc', so we give it a different name. dvander@347: template < class X, class XMemFunc> dvander@347: inline void bindconstmemfunc(const X *pthis, XMemFunc function_to_bind) { dvander@347: m_pthis= SimplifyMemFunc< sizeof(function_to_bind) > dvander@347: ::Convert(const_cast(pthis), function_to_bind, m_pFunction); dvander@347: #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK) dvander@347: m_pStaticFunction = 0; dvander@347: #endif dvander@347: } dvander@347: #ifdef FASTDELEGATE_GCC_BUG_8271 // At present, GCC doesn't recognize constness of MFPs in templates dvander@347: template < class X, class XMemFunc> dvander@347: inline void bindmemfunc(const X *pthis, XMemFunc function_to_bind) { dvander@347: bindconstmemfunc(pthis, function_to_bind); dvander@347: #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK) dvander@347: m_pStaticFunction = 0; dvander@347: #endif dvander@347: } dvander@347: #endif dvander@347: // These functions are required for invoking the stored function dvander@347: inline GenericClass *GetClosureThis() const { return m_pthis; } dvander@347: inline GenericMemFunc GetClosureMemPtr() const { return reinterpret_cast(m_pFunction); } dvander@347: dvander@347: // There are a few ways of dealing with static function pointers. dvander@347: // There's a standard-compliant, but tricky method. dvander@347: // There's also a straightforward hack, that won't work on DOS compilers using the dvander@347: // medium memory model. It's so evil that I can't recommend it, but I've dvander@347: // implemented it anyway because it produces very nice asm code. dvander@347: dvander@347: #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK) dvander@347: dvander@347: // ClosurePtr<> - Safe version dvander@347: // dvander@347: // This implementation is standard-compliant, but a bit tricky. dvander@347: // I store the function pointer inside the class, and the delegate then dvander@347: // points to itself. Whenever the delegate is copied, these self-references dvander@347: // must be transformed, and this complicates the = and == operators. dvander@347: public: dvander@347: // The next two functions are for operator ==, =, and the copy constructor. dvander@347: // We may need to convert the m_pthis pointers, so that dvander@347: // they remain as self-references. dvander@347: template< class DerivedClass > dvander@347: inline void CopyFrom (DerivedClass *pParent, const DelegateMemento &x) { dvander@347: SetMementoFrom(x); dvander@347: if (m_pStaticFunction!=0) { dvander@347: // transform self references... dvander@347: m_pthis=reinterpret_cast(pParent); dvander@347: } dvander@347: } dvander@347: // For static functions, the 'static_function_invoker' class in the parent dvander@347: // will be called. The parent then needs to call GetStaticFunction() to find out dvander@347: // the actual function to invoke. dvander@347: template < class DerivedClass, class ParentInvokerSig > dvander@347: inline void bindstaticfunc(DerivedClass *pParent, ParentInvokerSig static_function_invoker, dvander@347: StaticFuncPtr function_to_bind ) { dvander@347: if (function_to_bind==0) { // cope with assignment to 0 dvander@347: m_pFunction=0; dvander@347: } else { dvander@347: bindmemfunc(pParent, static_function_invoker); dvander@347: } dvander@347: m_pStaticFunction=reinterpret_cast(function_to_bind); dvander@347: } dvander@347: inline UnvoidStaticFuncPtr GetStaticFunction() const { dvander@347: return reinterpret_cast(m_pStaticFunction); dvander@347: } dvander@347: #else dvander@347: dvander@347: // ClosurePtr<> - Evil version dvander@347: // dvander@347: // For compilers where data pointers are at least as big as code pointers, it is dvander@347: // possible to store the function pointer in the this pointer, using another dvander@347: // horrible_cast. Invocation isn't any faster, but it saves 4 bytes, and dvander@347: // speeds up comparison and assignment. If C++ provided direct language support dvander@347: // for delegates, they would produce asm code that was almost identical to this. dvander@347: // Note that the Sun C++ and MSVC documentation explicitly state that they dvander@347: // support static_cast between void * and function pointers. dvander@347: dvander@347: template< class DerivedClass > dvander@347: inline void CopyFrom (DerivedClass *pParent, const DelegateMemento &right) { dvander@347: SetMementoFrom(right); dvander@347: } dvander@347: // For static functions, the 'static_function_invoker' class in the parent dvander@347: // will be called. The parent then needs to call GetStaticFunction() to find out dvander@347: // the actual function to invoke. dvander@347: // ******** EVIL, EVIL CODE! ******* dvander@347: template < class DerivedClass, class ParentInvokerSig> dvander@347: inline void bindstaticfunc(DerivedClass *pParent, ParentInvokerSig static_function_invoker, dvander@347: StaticFuncPtr function_to_bind) { dvander@347: if (function_to_bind==0) { // cope with assignment to 0 dvander@347: m_pFunction=0; dvander@347: } else { dvander@347: // We'll be ignoring the 'this' pointer, but we need to make sure we pass dvander@347: // a valid value to bindmemfunc(). dvander@347: bindmemfunc(pParent, static_function_invoker); dvander@347: } dvander@347: dvander@347: // WARNING! Evil hack. We store the function in the 'this' pointer! dvander@347: // Ensure that there's a compilation failure if function pointers dvander@347: // and data pointers have different sizes. dvander@347: // If you get this error, you need to #undef FASTDELEGATE_USESTATICFUNCTIONHACK. dvander@347: typedef int ERROR_CantUseEvilMethod[sizeof(GenericClass *)==sizeof(function_to_bind) ? 1 : -1]; dvander@347: m_pthis = horrible_cast(function_to_bind); dvander@347: // MSVC, SunC++ and DMC accept the following (non-standard) code: dvander@347: // m_pthis = static_cast(static_cast(function_to_bind)); dvander@347: // BCC32, Comeau and DMC accept this method. MSVC7.1 needs __int64 instead of long dvander@347: // m_pthis = reinterpret_cast(reinterpret_cast(function_to_bind)); dvander@347: } dvander@347: // ******** EVIL, EVIL CODE! ******* dvander@347: // This function will be called with an invalid 'this' pointer!! dvander@347: // We're just returning the 'this' pointer, converted into dvander@347: // a function pointer! dvander@347: inline UnvoidStaticFuncPtr GetStaticFunction() const { dvander@347: // Ensure that there's a compilation failure if function pointers dvander@347: // and data pointers have different sizes. dvander@347: // If you get this error, you need to #undef FASTDELEGATE_USESTATICFUNCTIONHACK. dvander@347: typedef int ERROR_CantUseEvilMethod[sizeof(UnvoidStaticFuncPtr)==sizeof(this) ? 1 : -1]; dvander@347: return horrible_cast(this); dvander@347: } dvander@347: #endif // !defined(FASTDELEGATE_USESTATICFUNCTIONHACK) dvander@347: dvander@347: // Does the closure contain this static function? dvander@347: inline bool IsEqualToStaticFuncPtr(StaticFuncPtr funcptr){ dvander@347: if (funcptr==0) return empty(); dvander@347: // For the Evil method, if it doesn't actually contain a static function, this will return an arbitrary dvander@347: // value that is not equal to any valid function pointer. dvander@347: else return funcptr==reinterpret_cast(GetStaticFunction()); dvander@347: } dvander@347: }; dvander@347: dvander@347: dvander@347: } // namespace detail dvander@347: dvander@347: //////////////////////////////////////////////////////////////////////////////// dvander@347: // Fast Delegates, part 3: dvander@347: // dvander@347: // Wrapper classes to ensure type safety dvander@347: // dvander@347: //////////////////////////////////////////////////////////////////////////////// dvander@347: dvander@347: dvander@347: // Once we have the member function conversion templates, it's easy to make the dvander@347: // wrapper classes. So that they will work with as many compilers as possible, dvander@347: // the classes are of the form dvander@347: // FastDelegate3 dvander@347: // They can cope with any combination of parameters. The max number of parameters dvander@347: // allowed is 8, but it is trivial to increase this limit. dvander@347: // Note that we need to treat const member functions seperately. dvander@347: // All this class does is to enforce type safety, and invoke the delegate with dvander@347: // the correct list of parameters. dvander@347: dvander@347: // Because of the weird rule about the class of derived member function pointers, dvander@347: // you sometimes need to apply a downcast to the 'this' pointer. dvander@347: // This is the reason for the use of "implicit_cast(pthis)" in the code below. dvander@347: // If CDerivedClass is derived from CBaseClass, but doesn't override SimpleVirtualFunction, dvander@347: // without this trick you'd need to write: dvander@347: // MyDelegate(static_cast(&d), &CDerivedClass::SimpleVirtualFunction); dvander@347: // but with the trick you can write dvander@347: // MyDelegate(&d, &CDerivedClass::SimpleVirtualFunction); dvander@347: dvander@347: // RetType is the type the compiler uses in compiling the template. For VC6, dvander@347: // it cannot be void. DesiredRetType is the real type which is returned from dvander@347: // all of the functions. It can be void. dvander@347: dvander@347: // Implicit conversion to "bool" is achieved using the safe_bool idiom, dvander@347: // using member data pointers (MDP). This allows "if (dg)..." syntax dvander@347: // Because some compilers (eg codeplay) don't have a unique value for a zero dvander@347: // MDP, an extra padding member is added to the SafeBool struct. dvander@347: // Some compilers (eg VC6) won't implicitly convert from 0 to an MDP, so dvander@347: // in that case the static function constructor is not made explicit; this dvander@347: // allows "if (dg==0) ..." to compile. dvander@347: dvander@347: @VARARGS dvander@347: template<@CLASSARGS, class RetType=detail::DefaultVoid> dvander@347: class FastDelegate@NUM { dvander@347: private: dvander@347: typedef typename detail::DefaultVoidToVoid::type DesiredRetType; dvander@347: typedef DesiredRetType (*StaticFunctionPtr)(@FUNCARGS); dvander@347: typedef RetType (*UnvoidStaticFunctionPtr)(@FUNCARGS); dvander@347: typedef RetType (detail::GenericClass::*GenericMemFn)(@FUNCARGS); dvander@347: typedef detail::ClosurePtr ClosureType; dvander@347: ClosureType m_Closure; dvander@347: public: dvander@347: // Typedefs to aid generic programming dvander@347: typedef FastDelegate@NUM type; dvander@347: dvander@347: // Construction and comparison functions dvander@347: FastDelegate@NUM() { clear(); } dvander@347: FastDelegate@NUM(const FastDelegate@NUM &x) { dvander@347: m_Closure.CopyFrom(this, x.m_Closure); } dvander@347: void operator = (const FastDelegate@NUM &x) { dvander@347: m_Closure.CopyFrom(this, x.m_Closure); } dvander@347: bool operator ==(const FastDelegate@NUM &x) const { dvander@347: return m_Closure.IsEqual(x.m_Closure); } dvander@347: bool operator !=(const FastDelegate@NUM &x) const { dvander@347: return !m_Closure.IsEqual(x.m_Closure); } dvander@347: bool operator <(const FastDelegate@NUM &x) const { dvander@347: return m_Closure.IsLess(x.m_Closure); } dvander@347: bool operator >(const FastDelegate@NUM &x) const { dvander@347: return x.m_Closure.IsLess(m_Closure); } dvander@347: // Binding to non-const member functions dvander@347: template < class X, class Y > dvander@347: FastDelegate@NUM(Y *pthis, DesiredRetType (X::* function_to_bind)(@FUNCARGS) ) { dvander@347: m_Closure.bindmemfunc(detail::implicit_cast(pthis), function_to_bind); } dvander@347: template < class X, class Y > dvander@347: inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(@FUNCARGS)) { dvander@347: m_Closure.bindmemfunc(detail::implicit_cast(pthis), function_to_bind); } dvander@347: // Binding to const member functions. dvander@347: template < class X, class Y > dvander@347: FastDelegate@NUM(const Y *pthis, DesiredRetType (X::* function_to_bind)(@FUNCARGS) const) { dvander@347: m_Closure.bindconstmemfunc(detail::implicit_cast(pthis), function_to_bind); } dvander@347: template < class X, class Y > dvander@347: inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(@FUNCARGS) const) { dvander@347: m_Closure.bindconstmemfunc(detail::implicit_cast(pthis), function_to_bind); } dvander@347: // Static functions. We convert them into a member function call. dvander@347: // This constructor also provides implicit conversion dvander@347: FastDelegate@NUM(DesiredRetType (*function_to_bind)(@FUNCARGS) ) { dvander@347: bind(function_to_bind); } dvander@347: // for efficiency, prevent creation of a temporary dvander@347: void operator = (DesiredRetType (*function_to_bind)(@FUNCARGS) ) { dvander@347: bind(function_to_bind); } dvander@347: inline void bind(DesiredRetType (*function_to_bind)(@FUNCARGS)) { dvander@347: m_Closure.bindstaticfunc(this, &FastDelegate@NUM::InvokeStaticFunction, dvander@347: function_to_bind); } dvander@347: // Invoke the delegate dvander@347: RetType operator() (@FUNCARGS) const { dvander@347: return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(@INVOKEARGS); } dvander@347: // Implicit conversion to "bool" using the safe_bool idiom dvander@347: private: dvander@347: typedef struct SafeBoolStruct { dvander@347: int a_data_pointer_to_this_is_0_on_buggy_compilers; dvander@347: StaticFunctionPtr m_nonzero; dvander@347: } UselessTypedef; dvander@347: typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type; dvander@347: public: dvander@347: operator unspecified_bool_type() const { dvander@347: return empty()? 0: &SafeBoolStruct::m_nonzero; dvander@347: } dvander@347: // necessary to allow ==0 to work despite the safe_bool idiom dvander@347: inline bool operator==(StaticFunctionPtr funcptr) { dvander@347: return m_Closure.IsEqualToStaticFuncPtr(funcptr); } dvander@347: inline bool operator!=(StaticFunctionPtr funcptr) { dvander@347: return !m_Closure.IsEqualToStaticFuncPtr(funcptr); } dvander@347: inline bool operator ! () const { // Is it bound to anything? dvander@347: return !m_Closure; } dvander@347: inline bool empty() const { dvander@347: return !m_Closure; } dvander@347: void clear() { m_Closure.clear();} dvander@347: // Conversion to and from the DelegateMemento storage class dvander@347: const DelegateMemento & GetMemento() { return m_Closure; } dvander@347: void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); } dvander@347: dvander@347: private: // Invoker for static functions dvander@347: RetType InvokeStaticFunction(@FUNCARGS) const { dvander@347: return (*(m_Closure.GetStaticFunction()))(@INVOKEARGS); } dvander@347: }; dvander@347: dvander@347: @ENDVAR dvander@347: dvander@347: //////////////////////////////////////////////////////////////////////////////// dvander@347: // Fast Delegates, part 4: dvander@347: // dvander@347: // FastDelegate<> class (Original author: Jody Hagins) dvander@347: // Allows boost::function style syntax like: dvander@347: // FastDelegate< double (int, long) > dvander@347: // instead of: dvander@347: // FastDelegate2< int, long, double > dvander@347: // dvander@347: //////////////////////////////////////////////////////////////////////////////// dvander@347: dvander@347: #ifdef FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX dvander@347: dvander@347: // Declare FastDelegate as a class template. It will be specialized dvander@347: // later for all number of arguments. dvander@347: template dvander@347: class FastDelegate; dvander@347: dvander@347: @VARARGS dvander@347: // Specialization to allow use of dvander@347: // FastDelegate< R ( @SELARGS ) > dvander@347: // instead of dvander@347: // FastDelegate@NUM < @SELARGS, R > dvander@347: template dvander@347: class FastDelegate< R ( @SELARGS ) > dvander@347: // Inherit from FastDelegate@NUM so that it can be treated just like a FastDelegate@NUM dvander@347: : public FastDelegate@NUM < @SELARGS, R > dvander@347: { dvander@347: public: dvander@347: // Make using the base type a bit easier via typedef. dvander@347: typedef FastDelegate@NUM < @SELARGS, R > BaseType; dvander@347: dvander@347: // Allow users access to the specific type of this delegate. dvander@347: typedef FastDelegate SelfType; dvander@347: dvander@347: // Mimic the base class constructors. dvander@347: FastDelegate() : BaseType() { } dvander@347: dvander@347: template < class X, class Y > dvander@347: FastDelegate(Y * pthis, dvander@347: R (X::* function_to_bind)( @FUNCARGS )) dvander@347: : BaseType(pthis, function_to_bind) { } dvander@347: dvander@347: template < class X, class Y > dvander@347: FastDelegate(const Y *pthis, dvander@347: R (X::* function_to_bind)( @FUNCARGS ) const) dvander@347: : BaseType(pthis, function_to_bind) dvander@347: { } dvander@347: dvander@347: FastDelegate(R (*function_to_bind)( @FUNCARGS )) dvander@347: : BaseType(function_to_bind) { } dvander@347: void operator = (const BaseType &x) { dvander@347: *static_cast(this) = x; } dvander@347: }; dvander@347: dvander@347: @ENDVAR dvander@347: dvander@347: #endif //FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX dvander@347: dvander@347: //////////////////////////////////////////////////////////////////////////////// dvander@347: // Fast Delegates, part 5: dvander@347: // dvander@347: // MakeDelegate() helper function dvander@347: // dvander@347: // MakeDelegate(&x, &X::func) returns a fastdelegate of the type dvander@347: // necessary for calling x.func() with the correct number of arguments. dvander@347: // This makes it possible to eliminate many typedefs from user code. dvander@347: // dvander@347: //////////////////////////////////////////////////////////////////////////////// dvander@347: dvander@347: // Also declare overloads of a MakeDelegate() global function to dvander@347: // reduce the need for typedefs. dvander@347: // We need seperate overloads for const and non-const member functions. dvander@347: // Also, because of the weird rule about the class of derived member function pointers, dvander@347: // implicit downcasts may need to be applied later to the 'this' pointer. dvander@347: // That's why two classes (X and Y) appear in the definitions. Y must be implicitly dvander@347: // castable to X. dvander@347: dvander@347: // Workaround for VC6. VC6 needs void return types converted into DefaultVoid. dvander@347: // GCC 3.2 and later won't compile this unless it's preceded by 'typename', dvander@347: // but VC6 doesn't allow 'typename' in this context. dvander@347: // So, I have to use a macro. dvander@347: dvander@347: #ifdef FASTDLGT_VC6 dvander@347: #define FASTDLGT_RETTYPE detail::VoidToDefaultVoid::type dvander@347: #else dvander@347: #define FASTDLGT_RETTYPE RetType dvander@347: #endif dvander@347: dvander@347: @VARARGS dvander@347: template dvander@347: FastDelegate@NUM<@SELARGS, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(@FUNCARGS)) { dvander@347: return FastDelegate@NUM<@SELARGS, FASTDLGT_RETTYPE>(x, func); dvander@347: } dvander@347: dvander@347: template dvander@347: FastDelegate@NUM<@SELARGS, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(@FUNCARGS) const) { dvander@347: return FastDelegate@NUM<@SELARGS, FASTDLGT_RETTYPE>(x, func); dvander@347: } dvander@347: dvander@347: template <@CLASSARGS, class RetType> dvander@347: FastDelegate@NUM<@SELARGS, FASTDLGT_RETTYPE> MakeDelegate(RetType (*func)(@FUNCARGS)) { dvander@347: return FastDelegate@NUM<@SELARGS, FASTDLGT_RETTYPE>(func); dvander@347: } dvander@347: dvander@347: @ENDVAR dvander@347: dvander@347: // clean up after ourselves... dvander@347: #undef FASTDLGT_RETTYPE dvander@347: dvander@347: } // namespace fastdelegate dvander@347: dvander@347: #endif // !defined(FASTDELEGATE_H)