sourcehook/generate/FastDelegate.hxx
changeset 134 a9e04855f582
parent 117 e322da4af2d2
child 294 0d87bc8606f8
     1.1 --- a/sourcehook/generate/FastDelegate.hxx	Fri Oct 14 17:45:36 2005 +0000
     1.2 +++ b/sourcehook/generate/FastDelegate.hxx	Fri Oct 14 20:57:43 2005 +0000
     1.3 @@ -5,7 +5,7 @@
     1.4  //						- Don Clugston, Mar 2004.
     1.5  //		Major contributions were made by Jody Hagins.
     1.6  // History:
     1.7 -// 24-Apr-04 1.0  Submitted to CodeProject.
     1.8 +// 24-Apr-04 1.0  * Submitted to CodeProject.
     1.9  // 28-Apr-04 1.1  * Prevent most unsafe uses of evil static function hack.
    1.10  //				  * Improved syntax for horrible_cast (thanks Paul Bludov).
    1.11  //				  * Tested on Metrowerks MWCC and Intel ICL (IA32)
    1.12 @@ -30,14 +30,20 @@
    1.13  //				  * Less use of macros. Error messages should be more comprehensible.
    1.14  //				  * Added include guards
    1.15  //				  * Added FastDelegate::empty() to test if invocation is safe (Thanks Neville Franks).
    1.16 -//				  * Now tested on VS 2005 Beta, PGI C++
    1.17 +//				  * Now tested on VS 2005 Express Beta, PGI C++
    1.18  // 24-Dec-04 1.4  * Added DelegateMemento, to allow collections of disparate delegates.
    1.19  //                * <,>,<=,>= comparison operators to allow storage in ordered containers.
    1.20  //				  * Substantial reduction of code size, especially the 'Closure' class.
    1.21  //				  * Standardised all the compiler-specific workarounds.
    1.22  //                * MFP conversion now works for CodePlay (but not yet supported in the full code).
    1.23 -//                * Now compiles without warnings on _any_ compiler, including BCC 5.5.1
    1.24 +//                * Now compiles without warnings on _any_ supported compiler, including BCC 5.5.1
    1.25  //				  * New syntax: FastDelegate< int (char *, double) >.
    1.26 +// 14-Feb-05 1.4.1* Now treats =0 as equivalent to .clear(), ==0 as equivalent to .empty(). (Thanks elfric).
    1.27 +//				  * Now tested on Intel ICL for AMD64, VS2005 Beta for AMD64 and Itanium.
    1.28 +// 30-Mar-05 1.5  * Safebool idiom: "if (dg)" is now equivalent to "if (!dg.empty())"
    1.29 +//				  * Fully supported by CodePlay VectorC
    1.30 +//                * Bugfix for Metrowerks: empty() was buggy because a valid MFP can be 0 on MWCC!
    1.31 +//                * More optimal assignment,== and != operators for static function pointers.
    1.32  
    1.33  #ifndef FASTDELEGATE_H
    1.34  #define FASTDELEGATE_H
    1.35 @@ -531,8 +537,10 @@
    1.36  // +-- Static pointer --+--pThis --+-- pMemFunc-+-- Meaning------+
    1.37  // |   0				|  0       |   0        | Empty          |
    1.38  // |   !=0              |(dontcare)|  Invoker   | Static function|
    1.39 -// |   0                |  !=0     |  !=0       | Method call    |
    1.40 +// |   0                |  !=0     |  !=0*      | Method call    |
    1.41  // +--------------------+----------+------------+----------------+
    1.42 +//  * For Metrowerks, this can be 0. (first virtual function in a 
    1.43 +//       single_inheritance class).
    1.44  // When stored stored inside a specific delegate, the 'dontcare' entries are replaced
    1.45  // with a reference to the delegate itself. This complicates the = and == operators
    1.46  // for the delegate class.
    1.47 @@ -544,9 +552,11 @@
    1.48  // horrible_cast. In this case the DelegateMemento implementation is simple:
    1.49  // +--pThis --+-- pMemFunc-+-- Meaning---------------------+
    1.50  // |    0     |  0         | Empty                         |
    1.51 -// |  !=0     |  !=0       | Static function or method call|
    1.52 +// |  !=0     |  !=0*      | Static function or method call|
    1.53  // +----------+------------+-------------------------------+
    1.54 -// Note that the Sun C++ and MSVC documentation explicitly state that they
    1.55 +//  * For Metrowerks, this can be 0. (first virtual function in a 
    1.56 +//       single_inheritance class).
    1.57 +// Note that the Sun C++ and MSVC documentation explicitly state that they 
    1.58  // support static_cast between void * and function pointers.
    1.59  
    1.60  class DelegateMemento {
    1.61 @@ -570,9 +580,7 @@
    1.62  	}
    1.63  #else
    1.64  	DelegateMemento() : m_pthis(0), m_pFunction(0) {};
    1.65 -	void clear() {
    1.66 -		m_pthis=0; m_pFunction=0;
    1.67 -	}
    1.68 +	void clear() {	m_pthis=0; m_pFunction=0;	}
    1.69  #endif
    1.70  public:
    1.71  #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
    1.72 @@ -603,8 +611,13 @@
    1.73  		return memcmp(&m_pFunction, &right.m_pFunction, sizeof(m_pFunction)) < 0;
    1.74  
    1.75  	}
    1.76 +	// BUGFIX (Mar 2005):
    1.77 +	// We can't just compare m_pFunction because on Metrowerks,
    1.78 +	// m_pFunction can be zero even if the delegate is not empty!
    1.79  	inline bool operator ! () const		// Is it bound to anything?
    1.80 -	{ return m_pFunction==0; }
    1.81 +	{ return m_pthis==0 && m_pFunction==0; }
    1.82 +	inline bool empty() const		// Is it bound to anything?
    1.83 +	{ return m_pthis==0 && m_pFunction==0; }
    1.84  public:
    1.85  	DelegateMemento & operator = (const DelegateMemento &right)  {
    1.86  		SetMementoFrom(right);
    1.87 @@ -723,7 +736,11 @@
    1.88  	template < class DerivedClass, class ParentInvokerSig >
    1.89  	inline void bindstaticfunc(DerivedClass *pParent, ParentInvokerSig static_function_invoker,
    1.90  				StaticFuncPtr function_to_bind ) {
    1.91 -		bindmemfunc(pParent, static_function_invoker);
    1.92 +		if (function_to_bind==0) { // cope with assignment to 0
    1.93 +			m_pFunction=0;
    1.94 +		} else {
    1.95 +			bindmemfunc(pParent, static_function_invoker);
    1.96 +        }
    1.97  		m_pStaticFunction=reinterpret_cast<GenericFuncPtr>(function_to_bind);
    1.98  	}
    1.99  	inline UnvoidStaticFuncPtr GetStaticFunction() const {
   1.100 @@ -752,9 +769,13 @@
   1.101  	template < 	class DerivedClass, class ParentInvokerSig>
   1.102  	inline void bindstaticfunc(DerivedClass *pParent, ParentInvokerSig static_function_invoker,
   1.103  				StaticFuncPtr function_to_bind) {
   1.104 -		// We'll be ignoring the 'this' pointer, but we need to make sure we pass
   1.105 -		// a valid value to bindmemfunc().
   1.106 -		bindmemfunc(pParent, static_function_invoker);
   1.107 +		if (function_to_bind==0) { // cope with assignment to 0
   1.108 +			m_pFunction=0;
   1.109 +		} else { 
   1.110 +		   // We'll be ignoring the 'this' pointer, but we need to make sure we pass
   1.111 +		   // a valid value to bindmemfunc().
   1.112 +			bindmemfunc(pParent, static_function_invoker);
   1.113 +        }
   1.114  
   1.115  		// WARNING! Evil hack. We store the function in the 'this' pointer!
   1.116  		// Ensure that there's a compilation failure if function pointers
   1.117 @@ -780,6 +801,13 @@
   1.118  	}
   1.119  #endif // !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
   1.120  
   1.121 +	// Does the closure contain this static function?
   1.122 +	inline bool IsEqualToStaticFuncPtr(StaticFuncPtr funcptr){
   1.123 +		if (funcptr==0) return empty();
   1.124 +	// For the Evil method, if it doesn't actually contain a static function, this will return an arbitrary
   1.125 +	// value that is not equal to any valid function pointer.
   1.126 +		else return funcptr==reinterpret_cast<StaticFuncPtr>(GetStaticFunction());
   1.127 +	}
   1.128  };
   1.129  
   1.130  
   1.131 @@ -816,6 +844,14 @@
   1.132  // it cannot be void. DesiredRetType is the real type which is returned from
   1.133  // all of the functions. It can be void.
   1.134  
   1.135 +// Implicit conversion to "bool" is achieved using the safe_bool idiom,
   1.136 +// using member data pointers (MDP). This allows "if (dg)..." syntax
   1.137 +// Because some compilers (eg codeplay) don't have a unique value for a zero
   1.138 +// MDP, an extra padding member is added to the SafeBool struct.
   1.139 +// Some compilers (eg VC6) won't implicitly convert from 0 to an MDP, so
   1.140 +// in that case the static function constructor is not made explicit; this
   1.141 +// allows "if (dg==0) ..." to compile.
   1.142 +
   1.143  @VARARGS
   1.144  template<@CLASSARGS, class RetType=detail::DefaultVoid>
   1.145  class FastDelegate@NUM {
   1.146 @@ -824,7 +860,8 @@
   1.147  	typedef DesiredRetType (*StaticFunctionPtr)(@FUNCARGS);
   1.148  	typedef RetType (*UnvoidStaticFunctionPtr)(@FUNCARGS);
   1.149  	typedef RetType (detail::GenericClass::*GenericMemFn)(@FUNCARGS);
   1.150 -	detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> m_Closure;
   1.151 +	typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
   1.152 +	ClosureType m_Closure;
   1.153  public:
   1.154  	// Typedefs to aid generic programming
   1.155  	typedef FastDelegate@NUM type;
   1.156 @@ -858,15 +895,34 @@
   1.157  	inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(@FUNCARGS) const) {
   1.158  		m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind);	}
   1.159  	// Static functions. We convert them into a member function call.
   1.160 -	// Note that this also provides a conversion from static functions.
   1.161 +	// This constructor also provides implicit conversion
   1.162  	FastDelegate@NUM(DesiredRetType (*function_to_bind)(@FUNCARGS) ) {
   1.163  		bind(function_to_bind);	}
   1.164 +	// for efficiency, prevent creation of a temporary
   1.165 +	void operator = (DesiredRetType (*function_to_bind)(@FUNCARGS) ) {
   1.166 +		bind(function_to_bind);	}
   1.167  	inline void bind(DesiredRetType (*function_to_bind)(@FUNCARGS)) {
   1.168  		m_Closure.bindstaticfunc(this, &FastDelegate@NUM::InvokeStaticFunction,
   1.169  			function_to_bind); }
   1.170 -	RetType operator() (@FUNCARGS) const { // Invoke the delegate
   1.171 -		// this next line is the only one that violates the standard
   1.172 +	// Invoke the delegate
   1.173 +	RetType operator() (@FUNCARGS) const {
   1.174  	return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(@INVOKEARGS); }
   1.175 +	// Implicit conversion to "bool" using the safe_bool idiom
   1.176 +private:
   1.177 +	typedef struct SafeBoolStruct {
   1.178 +		int a_data_pointer_to_this_is_0_on_buggy_compilers;
   1.179 +		StaticFunctionPtr m_nonzero;
   1.180 +	} UselessTypedef;
   1.181 +    typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type;
   1.182 +public:
   1.183 +	operator unspecified_bool_type() const {
   1.184 +        return empty()? 0: &SafeBoolStruct::m_nonzero;
   1.185 +    }
   1.186 +	// necessary to allow ==0 to work despite the safe_bool idiom
   1.187 +	inline bool operator==(StaticFunctionPtr funcptr) {
   1.188 +		return m_Closure.IsEqualToStaticFuncPtr(funcptr);	}
   1.189 +	inline bool operator!=(StaticFunctionPtr funcptr) { 
   1.190 +		return !m_Closure.IsEqualToStaticFuncPtr(funcptr);    }
   1.191  	inline bool operator ! () const	{	// Is it bound to anything?
   1.192  			return !m_Closure; }
   1.193  	inline bool empty() const	{
   1.194 @@ -908,8 +964,7 @@
   1.195  // FastDelegate@NUM < @SELARGS, R >
   1.196  template<typename R, @CLASSARGS>
   1.197  class FastDelegate< R ( @SELARGS ) >
   1.198 -  // Inherit from FastDelegate@NUM so that it can be treated just
   1.199 -  // like a FastDelegate@NUM
   1.200 +  // Inherit from FastDelegate@NUM so that it can be treated just like a FastDelegate@NUM
   1.201    : public FastDelegate@NUM < @SELARGS, R >
   1.202  {
   1.203  public: