1.1 --- a/sourcehook/generate/sourcehook.hxx Wed Aug 09 02:05:57 2006 +0000
1.2 +++ b/sourcehook/generate/sourcehook.hxx Wed Aug 09 06:46:11 2006 +0000
1.3 @@ -300,6 +300,49 @@
1.4 typedef CallClass<void> GenericCallClass;
1.5 typedef CallClass<EmptyClass> ManualCallClass;
1.6
1.7 + // 09.08.2008 (6 AM, I just woke up, the others are still sleeping so i finally can use this notebook !!)
1.8 + // - Today is an important day.
1.9 + // I'm adding support for functions which return references.
1.10 +
1.11 + // How it works:
1.12 + // SH_SETUPCALLS doesn't use plain rettype to store the temporary return values (plugin ret, orig ret,
1.13 + // override ret) anymore; instead, it uses SourceHook::ReferenceCarrier<rettype>::type
1.14 + // this is typedefed to the original rettype normally, but if the rettype is a reference, it's a special class
1.15 + // which stores the reference as a pointer, and implements constructors, operator= and a conversion operator.
1.16 + // special cases were needed for getoverrideret / getorigret; these are implemented through the
1.17 + // SourceHook::MacroRefHelpers structs.
1.18 + // Furthermore, SetOverrideRet had to be changed a bit; see SourceHook::OverrideFunctor somewhere down in this file.
1.19 + template <class T> struct ReferenceCarrier
1.20 + {
1.21 + typedef T type;
1.22 + };
1.23 +
1.24 + template <class T> struct ReferenceCarrier<T&>
1.25 + {
1.26 + class type
1.27 + {
1.28 + T *m_StoredRef;
1.29 + public:
1.30 + type() : m_StoredRef(NULL)
1.31 + {
1.32 + }
1.33 + type(T& ref) : m_StoredRef(&ref)
1.34 + {
1.35 + }
1.36 +
1.37 + T& operator= (T& ref)
1.38 + {
1.39 + m_StoredRef = &ref;
1.40 + return ref;
1.41 + }
1.42 +
1.43 + operator T& () const
1.44 + {
1.45 + return *m_StoredRef;
1.46 + }
1.47 + };
1.48 + };
1.49 +
1.50 /**
1.51 * @brief The main SourceHook interface
1.52 */
1.53 @@ -438,6 +481,35 @@
1.54
1.55 //!<
1.56 };
1.57 +
1.58 + // For META_RESULT_ORIG_RET and META_RESULT_OVERRIDE_RET:
1.59 + // These have to be able to return references. If T is a reference, the pointers returned
1.60 + // from the SH_GLOB_SHPTR are pointers to instances of ReferenceCarrier<T>::type.
1.61 + template <class T> struct MacroRefHelpers
1.62 + {
1.63 + inline static const T* GetOrigRet(ISourceHook *shptr)
1.64 + {
1.65 + return reinterpret_cast<const T*>(shptr->GetOrigRet());
1.66 + }
1.67 + inline static const T* GetOverrideRet(ISourceHook *shptr)
1.68 + {
1.69 + return reinterpret_cast<const T*>(shptr->GetOverrideRet());
1.70 + }
1.71 + };
1.72 +
1.73 + template <class T> struct MacroRefHelpers<T&>
1.74 + {
1.75 + inline static T* GetOrigRet(ISourceHook *shptr)
1.76 + {
1.77 + T &ref = *reinterpret_cast<const ReferenceCarrier<T&>::type *>(shptr->GetOrigRet());
1.78 + return &ref;
1.79 + }
1.80 + inline static T* GetOverrideRet(ISourceHook *shptr)
1.81 + {
1.82 + T &ref = *reinterpret_cast<const ReferenceCarrier<T&>::type *>(shptr->GetOverrideRet());
1.83 + return &ref;
1.84 + }
1.85 + };
1.86 }
1.87
1.88 /************************************************************************/
1.89 @@ -445,14 +517,18 @@
1.90 /************************************************************************/
1.91 #define META_RESULT_STATUS SH_GLOB_SHPTR->GetStatus()
1.92 #define META_RESULT_PREVIOUS SH_GLOB_SHPTR->GetPrevRes()
1.93 -#define META_RESULT_ORIG_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOrigRet())
1.94 -#define META_RESULT_OVERRIDE_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOverrideRet())
1.95 +#define META_RESULT_ORIG_RET(type) *SourceHook::MacroRefHelpers<type>::GetOrigRet(SH_GLOB_SHPTR)
1.96 +#define META_RESULT_OVERRIDE_RET(type) *SourceHook::MacroRefHelpers<type>::GetOverrideRet(SH_GLOB_SHPTR)
1.97 #define META_IFACEPTR(type) reinterpret_cast<type*>(SH_GLOB_SHPTR->GetIfacePtr())
1.98
1.99 #define SET_META_RESULT(result) SH_GLOB_SHPTR->SetRes(result)
1.100 #define RETURN_META(result) do { SET_META_RESULT(result); return; } while(0)
1.101 #define RETURN_META_VALUE(result, value) do { SET_META_RESULT(result); return (value); } while(0)
1.102
1.103 +// If a hook on a function which returns a reference does not want to specify a return value,
1.104 +// it can use this macro.
1.105 +// ONLY USE THIS WITH MRES_IGNORED AND MRES_HANDLED !!!
1.106 +#define RETURN_META_NOREF(result, rettype) do { SET_META_RESULT(result); return reinterpret_cast<rettype>(*SH_GLOB_SHPTR); } while(0)
1.107
1.108 // only call these from the hook handlers directly!
1.109 // :TODO: enforce it ?
1.110 @@ -477,7 +553,7 @@
1.111 { \
1.112 /* meh, set the override result here because we don't get a chance to return */ \
1.113 /* before continuing the hook loop through the recall */ \
1.114 - SourceHook::SetOverrideResult(SH_GLOB_SHPTR, memfuncptr, value); \
1.115 + SourceHook::SetOverrideResult(memfuncptr)(SH_GLOB_SHPTR, value); \
1.116 } \
1.117 RETURN_META_VALUE(MRES_SUPERCEDE, \
1.118 (SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams); \
1.119 @@ -808,11 +884,12 @@
1.120 META_RES status = MRES_IGNORED; \
1.121 META_RES prev_res; \
1.122 META_RES cur_res; \
1.123 - rettype orig_ret; \
1.124 - rettype override_ret; \
1.125 - rettype plugin_ret; \
1.126 + typedef ReferenceCarrier<rettype>::type my_rettype; \
1.127 + my_rettype orig_ret; \
1.128 + my_rettype override_ret; \
1.129 + my_rettype plugin_ret; \
1.130 void* ifptr; \
1.131 - rettype *pOverrideRet = reinterpret_cast<rettype*>(SH_GLOB_SHPTR->SetupHookLoop( \
1.132 + my_rettype *pOverrideRet = reinterpret_cast<my_rettype*>(SH_GLOB_SHPTR->SetupHookLoop( \
1.133 &status, &prev_res, &cur_res, &ifptr, &orig_ret, &override_ret));
1.134
1.135 #define SH_CALL_HOOKS(post, params) \
1.136 @@ -945,6 +1022,11 @@
1.137
1.138 //////////////////////////////////////////////////////////////////////////
1.139
1.140 +// :FIXME:
1.141 +// sizeof on references returns the size of the datatype, NOT the pointer size or something
1.142 +// -> one should probably flag references in __SourceHook_ParamSizes_* !
1.143 +// or simply assume that their size is sizeof(void*)=SH_PTRSIZE... could be doable through a simple template
1.144 +
1.145 @[$1,0,$a:
1.146 // ********* Support for $1 arguments *********
1.147 #define SH_DECL_HOOK$1(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
1.148 @@ -1246,11 +1328,34 @@
1.149 *reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
1.150 }
1.151
1.152 + // SetOverrideResult used to be implemented like this:
1.153 + // SetOverrideResult(shptr, memfuncptr, return);
1.154 + // normally the compiler can deduce the return type from memfuncptr, but (at least msvc8) failed when it was a reference
1.155 + // (it thought it was ambigous - the ref and non-ref type)
1.156 + // so now SetOverrideResult(memfuncptr) deduces the ret type, and returns a functor which does the work
1.157 + // new syntax: SetOverrideResult(memfuncptr)(shptr, return)
1.158 + // This also allows us to create a special version for references which respects ReferenceCarrier.
1.159 +
1.160 + template <class T> struct OverrideFunctor
1.161 + {
1.162 + void operator()(ISourceHook *shptr, T res)
1.163 + {
1.164 + *reinterpret_cast<T*>(shptr->GetOverrideRetPtr()) = res;
1.165 + }
1.166 + };
1.167 + template <class T> struct OverrideFunctor<T&>
1.168 + {
1.169 + void operator()(ISourceHook *shptr, T &res)
1.170 + {
1.171 + // overrideretptr points to ReferenceCarrier<T&>
1.172 + *reinterpret_cast<ReferenceCarrier<T&>::type *>(shptr->GetOverrideRetPtr()) = res;
1.173 + }
1.174 + };
1.175 @[$1,0,$a:
1.176 template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
1.177 - void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]), const RetType res)
1.178 + OverrideFunctor<RetType> SetOverrideResult(RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
1.179 {
1.180 - *reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
1.181 + return OverrideFunctor<RetType>();
1.182 }
1.183
1.184 template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>