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