sourcehook/generate/sourcehook.hxx
author Pavol Marko <pavolmarko@gmail.com>
Wed Aug 09 07:25:48 2006 +0000 (2006-08-09)
changeset 234 f7526d407359
parent 233 6d19421c552c
child 238 c27105949293
permissions -rwxr-xr-x
fixed location of testrefret.cpp and added forgotten typename keywords
     1 /* ======== SourceHook ========
     2 * Copyright (C) 2004-2005 Metamod:Source Development Team
     3 * No warranties of any kind
     4 *
     5 * License: zlib/libpng
     6 *
     7 * Author(s): Pavol "PM OnoTo" Marko
     8 * ============================
     9 */
    10 
    11 /**
    12 *	@file sourcehook.h
    13 *	@brief Contains the public SourceHook API
    14 */
    15 
    16 #ifndef __SOURCEHOOK_H__
    17 #define __SOURCEHOOK_H__
    18 
    19 // Interface revisions:
    20 //  1 - Initial revision
    21 //  2 - Changed to virtual functions for iterators and all queries
    22 //  3 - Added "hook loop status variable"
    23 //  4 - Reentrant
    24 #define SH_IFACE_VERSION 4
    25 #define SH_IMPL_VERSION 3
    26 
    27 // Hookman version:
    28 //  1 - Support for recalls, performance optimisations
    29 #define SH_HOOKMAN_VERSION 1
    30 
    31 // The value of SH_GLOB_SHPTR has to be a pointer to SourceHook::ISourceHook
    32 // It's used in various macros
    33 #ifndef SH_GLOB_SHPTR
    34 #define SH_GLOB_SHPTR g_SHPtr
    35 #endif
    36 
    37 // Used to identify the current plugin
    38 #ifndef SH_GLOB_PLUGPTR
    39 #define SH_GLOB_PLUGPTR g_PLID
    40 #endif
    41 
    42 #ifdef SH_DEBUG
    43 # include <stdio.h>
    44 # include <stdlib.h>
    45 
    46 # define SH_ASSERT(x, info) \
    47 	do { \
    48 		if (!(x)) \
    49 		{ \
    50 			printf("SOURCEHOOK DEBUG ASSERTION FAILED: %s:%u(%s): %s: ", __FILE__, __LINE__, __FUNCTION__, #x); \
    51 			printf info; \
    52 			putchar('\n'); \
    53 			abort(); \
    54 		} \
    55 	} while(0)
    56 
    57 #else
    58 # define SH_ASSERT(x, info)
    59 #endif
    60 
    61 // System
    62 #define SH_SYS_WIN32 1
    63 #define SH_SYS_LINUX 2
    64 
    65 #ifdef _WIN32
    66 # define SH_SYS SH_SYS_WIN32
    67 #elif defined __linux__
    68 # define SH_SYS SH_SYS_LINUX
    69 #else
    70 # error Unsupported system
    71 #endif
    72 
    73 // Compiler
    74 #define SH_COMP_GCC 1
    75 #define SH_COMP_MSVC 2
    76 
    77 #ifdef _MSC_VER
    78 # define SH_COMP SH_COMP_MSVC
    79 #elif defined __GNUC__
    80 # define SH_COMP SH_COMP_GCC
    81 #else
    82 # error Unsupported compiler
    83 #endif
    84 
    85 #if SH_COMP==SH_COMP_MSVC
    86 # define vsnprintf _vsnprintf
    87 #endif
    88 
    89 #if SH_SYS != SH_SYS_WIN32
    90 # include <unistd.h>
    91 #endif
    92 
    93 #define SH_PTRSIZE sizeof(void*)
    94 
    95 #include "FastDelegate.h"
    96 #include "sh_memfuncinfo.h"
    97 
    98 // Good old metamod!
    99 
   100 // Flags returned by a plugin's api function.
   101 // NOTE: order is crucial, as greater/less comparisons are made.
   102 enum META_RES
   103 {
   104 	MRES_IGNORED=0,		// plugin didn't take any action
   105 	MRES_HANDLED,		// plugin did something, but real function should still be called
   106 	MRES_OVERRIDE,		// call real function, but use my return value
   107 	MRES_SUPERCEDE		// skip real function; use my return value
   108 };
   109 
   110 
   111 namespace SourceHook
   112 {
   113 	/**
   114 	*	@brief	Specifies the size (in bytes) for the internal buffer of vafmt(printf-like) function handlers
   115 	*/
   116 	const int STRBUF_LEN=4096;
   117 
   118 	/**
   119 	*	@brief	An empty class. No inheritance used. Used for original-function-call hacks
   120 	*/
   121 	class EmptyClass
   122 	{
   123 	};
   124 
   125 	/**
   126 	*	@brief Implicit cast.
   127 	*/
   128 	template <class In, class Out>
   129 		inline Out implicit_cast(In input)
   130 		{
   131 			return input;
   132 		}
   133 
   134 	/**
   135 	*	@brief A plugin typedef
   136 	*
   137 	*	SourceHook doesn't really care what this is. As long as the ==, != and = operators work on it and every
   138 	*	plugin has a unique identifier, everything is ok.
   139 	*	It should also be possible to set it to 0.
   140 	*/
   141 	typedef int Plugin;
   142 
   143 	struct ProtoInfo
   144 	{
   145 		ProtoInfo(int rtsz, int nop, const int *p) : beginningNull(0), retTypeSize(rtsz), numOfParams(nop), params(p)
   146 		{
   147 		}
   148 		int beginningNull;		//!< To distinguish from old protos (which never begin with 0)
   149 		int retTypeSize;		//!< 0 if void
   150 		int numOfParams;		//!< number of parameters
   151 		const int *params;		//!< params[0]=0 (or -1 for vararg), params[1]=size of first param, ...
   152 	};
   153 
   154 	/**
   155 	*	@brief Specifies the actions for hook managers
   156 	*/
   157 	enum HookManagerAction
   158 	{
   159 		HA_GetInfo = 0,			//!< Store info about the hook manager
   160 		HA_Register,			//!< Save the IHookManagerInfo pointer for future reference
   161 		HA_Unregister			//!< Clear the saved pointer
   162 	};
   163 
   164 	struct IHookManagerInfo;
   165 
   166 	/**
   167 	*	@brief Pointer to hook manager interface function
   168 	*
   169 	*	A "hook manager" is a the only thing that knows the actual protoype of the function at compile time.
   170 	*
   171 	*   @param ha What the hook manager should do
   172 	*	@param hi A pointer to IHookManagerInfo
   173 	*/
   174 	typedef int (*HookManagerPubFunc)(HookManagerAction ha, IHookManagerInfo *hi);
   175 
   176 	class ISHDelegate
   177 	{
   178 	public:
   179 		virtual void DeleteThis() = 0;				// Ugly, I know
   180 		virtual bool IsEqual(ISHDelegate *other) = 0;
   181 	};
   182 
   183 	template <class T> class CSHDelegate : public ISHDelegate
   184 	{
   185 		T m_Deleg;
   186 	public:
   187 		CSHDelegate(T deleg) : m_Deleg(deleg)
   188 		{
   189 		}
   190 
   191 		CSHDelegate(const CSHDelegate &other) : m_Deleg(other.m_Deleg)
   192 		{
   193 		}
   194 
   195 		void DeleteThis()
   196 		{
   197 			delete this;
   198 		}
   199 
   200 		bool IsEqual(ISHDelegate *other)
   201 		{
   202 			return static_cast<CSHDelegate<T>* >(other)->GetDeleg() == GetDeleg();
   203 		}
   204 
   205 		T &GetDeleg()
   206 		{
   207 			return m_Deleg;
   208 		}
   209 	};
   210 
   211 	struct IHookList
   212 	{
   213 		struct IIter
   214 		{
   215 			virtual bool End() = 0;
   216 			virtual void Next() = 0;
   217 			virtual ISHDelegate *Handler() = 0;
   218 			virtual int ThisPtrOffs() = 0;
   219 		};
   220 		virtual IIter *GetIter() = 0;
   221 		virtual void ReleaseIter(IIter *pIter) = 0;
   222 	};
   223 
   224 	struct IIface
   225 	{
   226 		virtual void *GetPtr() = 0;
   227 		virtual IHookList *GetPreHooks() = 0;
   228 		virtual IHookList *GetPostHooks() = 0;
   229 	};
   230 
   231 	struct IVfnPtr
   232 	{
   233 		virtual void *GetVfnPtr() = 0;
   234 		virtual void *GetOrigEntry() = 0;
   235 
   236 		virtual IIface *FindIface(void *ptr) = 0;
   237 	};
   238 
   239 	struct IHookManagerInfo
   240 	{
   241 		virtual IVfnPtr *FindVfnPtr(void *vfnptr) = 0;
   242 
   243 		virtual void SetInfo(int vtbloffs, int vtblidx, const char *proto) = 0;
   244 		virtual void SetHookfuncVfnptr(void *hookfunc_vfnptr) = 0;
   245 
   246 		// Added 23.12.2005 (yup! I'm coding RIGHT BEFORE CHRISTMAS!)
   247 		// If the hookman doesn't set this, it defaults 0
   248 		// SourceHook prefers hookmans with higher version numbers
   249 		virtual void SetVersion(int version) = 0;
   250 	};
   251 
   252 	class AutoHookIter
   253 	{
   254 		IHookList *m_pList;
   255 		IHookList::IIter *m_pIter;
   256 	public:
   257 		AutoHookIter(IHookList *pList)
   258 			: m_pList(pList), m_pIter(pList->GetIter())
   259 		{
   260 		}
   261 
   262 		~AutoHookIter()
   263 		{
   264 			if (m_pList)
   265 				m_pList->ReleaseIter(m_pIter);
   266 		}
   267 
   268 		bool End()
   269 		{
   270 			return m_pIter->End();
   271 		}
   272 
   273 		void Next()
   274 		{
   275 			m_pIter->Next();
   276 		}
   277 
   278 		ISHDelegate *Handler()
   279 		{
   280 			return m_pIter->Handler();
   281 		}
   282 
   283 		int ThisPtrOffs()
   284 		{
   285 			return m_pIter->ThisPtrOffs();
   286 		}
   287 
   288 		void SetToZero()
   289 		{
   290 			m_pList = 0;
   291 		}
   292 	};
   293 
   294 	template<class B> struct CallClass
   295 	{
   296 		virtual B *GetThisPtr() = 0;
   297 		virtual void *GetOrigFunc(int vtbloffs, int vtblidx) = 0;
   298 	};
   299 
   300 	typedef CallClass<void> GenericCallClass;
   301 	typedef CallClass<EmptyClass> ManualCallClass;
   302 
   303 	// 09.08.2008 (6 AM, I just woke up, the others are still sleeping so i finally can use this notebook !!)
   304 	// - Today is an important day.
   305 	// I'm adding support for functions which return references.
   306 
   307 	// How it works:
   308 	//  SH_SETUPCALLS doesn't use plain rettype to store the temporary return values (plugin ret, orig ret,
   309 	//  override ret) anymore; instead, it uses SourceHook::ReferenceCarrier<rettype>::type
   310 	//  this is typedefed to the original rettype normally, but if the rettype is a reference, it's a special class
   311 	//  which stores the reference as a pointer, and implements constructors, operator= and a conversion operator.
   312 	//  special cases were needed for getoverrideret / getorigret; these are implemented through the
   313 	//  SourceHook::MacroRefHelpers structs.
   314 	//  Furthermore, SetOverrideRet had to be changed a bit; see SourceHook::OverrideFunctor somewhere down in this file.
   315 	template <class T> struct ReferenceCarrier
   316 	{
   317 		typedef T type;
   318 	};
   319 
   320 	template <class T> struct ReferenceCarrier<T&>
   321 	{
   322 		class type
   323 		{
   324 			T *m_StoredRef;
   325 		public:
   326 			type() : m_StoredRef(NULL)
   327 			{
   328 			}
   329 			type(T& ref) : m_StoredRef(&ref)
   330 			{
   331 			}
   332 
   333 			T& operator= (T& ref)
   334 			{
   335 				m_StoredRef = &ref;
   336 				return ref;
   337 			}
   338 
   339 			operator T& () const
   340 			{
   341 				return *m_StoredRef;
   342 			}
   343 		};
   344 	};
   345 
   346 	/**
   347 	*	@brief The main SourceHook interface
   348 	*/
   349 	class ISourceHook
   350 	{
   351 	public:
   352 		/**
   353 		*	@brief Return interface version
   354 		*/
   355 		virtual int GetIfaceVersion() = 0;
   356 
   357 		/**
   358 		*	@brief Return implementation version
   359 		*/
   360 		virtual int GetImplVersion() = 0;
   361 
   362 		/**
   363 		*	@brief Add a hook.
   364 		*
   365 		*	@return True if the function succeeded, false otherwise
   366 		*
   367 		*	@param plug The unique identifier of the plugin that calls this function
   368 		*	@param iface The interface pointer
   369 		*	@param ifacesize The size of the class iface points to
   370 		*	@param myHookMan A hook manager function that should be capable of handling the function
   371 		*	@param handler A pointer to a FastDelegate containing the hook handler
   372 		*	@param post Set to true if you want a post handler
   373 		*/
   374 		virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
   375 			ISHDelegate *handler, bool post) = 0;
   376 
   377 		/**
   378 		*	@brief Removes a hook.
   379 		*
   380 		*	@return True if the function succeeded, false otherwise
   381 		*
   382 		*	@param plug The unique identifier of the plugin that calls this function
   383 		*	@param iface The interface pointer
   384 		*	@param myHookMan A hook manager function that should be capable of handling the function
   385 		*	@param handler A pointer to a FastDelegate containing the hook handler
   386 		*	@param post Set to true if you want a post handler
   387 		*/
   388 		virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
   389 			ISHDelegate *handler, bool post) = 0;
   390 
   391 		/**
   392 		*	@brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
   393 		*
   394 		*	@param plug The unique identifier of the plugin in question
   395 		*/
   396 		virtual bool IsPluginInUse(Plugin plug) = 0;
   397 
   398 		/**
   399 		*	@brief Return a pointer to a callclass. Generate a new one if required.
   400 		*
   401 		*	@param iface The interface pointer
   402 		*	@param size Size of the class instance
   403 		*/
   404 		virtual GenericCallClass *GetCallClass(void *iface, size_t size) = 0;
   405 
   406 		/**
   407 		*	@brief Release a callclass
   408 		*
   409 		*	@param ptr Pointer to the callclass
   410 		*/
   411 		virtual void ReleaseCallClass(GenericCallClass *ptr) = 0;
   412 
   413 		virtual void SetRes(META_RES res) = 0;				//!< Sets the meta result
   414 		virtual META_RES GetPrevRes() = 0;					//!< Gets the meta result of the
   415 															//!<  previously calledhandler
   416 		virtual META_RES GetStatus() = 0;					//!< Gets the highest meta result
   417 		virtual const void *GetOrigRet() = 0;				//!< Gets the original result.
   418 															//!<  If not in post function, undefined
   419 		virtual const void *GetOverrideRet() = 0;			//!< Gets the override result.
   420 															//!<  If none is specified, NULL
   421 		virtual void *GetIfacePtr() = 0;					//!< Gets the interface pointer
   422 		//////////////////////////////////////////////////////////////////////////
   423 		// For hook managers
   424 		virtual void HookLoopBegin(IIface *pIface) = 0;			//!< Should be called when a hook loop begins
   425 		virtual void HookLoopEnd() = 0;							//!< Should be called when a hook loop exits
   426 		virtual void SetCurResPtr(META_RES *mres) = 0;			//!< Sets pointer to the current meta result
   427 		virtual void SetPrevResPtr(META_RES *mres) = 0;			//!< Sets pointer to previous meta result
   428 		virtual void SetStatusPtr(META_RES *mres) = 0;			//!< Sets pointer to the status variable
   429 		virtual void SetIfacePtrPtr(void **pp) = 0;				//!< Sets pointer to the interface this pointer
   430 		virtual void SetOrigRetPtr(const void *ptr) = 0;		//!< Sets the original return pointer
   431 		virtual void SetOverrideRetPtr(void *ptr) = 0;			//!< Sets the override result pointer
   432 		virtual bool ShouldContinue() = 0;						//!< Returns false if the hook loop should exit
   433 
   434 		/**
   435 		*	@brief Remove a hook manager. Auto-removes all hooks attached to it from plugin plug.
   436 		*
   437 		*	@param plug The owner of the hook manager
   438 		*   @param pubFunc The hook manager's info function
   439 		*/
   440 		virtual void RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc) = 0;
   441 
   442 		virtual void DoRecall() = 0;							//!< Initiates a recall sequence
   443 		/*
   444 			HOW RECALLS WORK:
   445 			
   446 			The problem:
   447 				Users want the ability to change parameters of the called function
   448 				from inside their handler.
   449 			The solution:
   450 				1) Mark as "recall"
   451 				2) Recall the function
   452 				3) => SH's hook func gets called:
   453 				  4) The first iterator points at the first hook the last hookfunc didn't execute yet
   454 				  5) does all iteration and returns normally
   455 		        6) The user's handler returns immediately
   456 				7) The hook func returns immediately as well
   457 
   458 				Also note that the recalled hookfunc starts with the status the recalling hookfunc
   459 				ended with. The last handler (doing the recall) is also able to specify its own
   460 				META_RES.
   461 		*/
   462 
   463 		virtual void *GetOverrideRetPtr() = 0;		//!< Returns the pointer set by SetOverrideRetPtr
   464 
   465 		/**
   466 		*	@brief Set up the hook loop. Equivalent to calling:
   467 		*	SetStatusPtr, SetPrevResPtr, SetCurResPtr, SetIfacePtrPtr, SetOrigRetPtr, Get/SetOverrideRetPtr
   468 		*
   469 		*	@param statusPtr		pointer to status variable
   470 		*	@param prevResPtr		pointer to previous result variable
   471 		*	@param curResPtr		pointer to current result variable
   472 		*	@param ifacePtrPtr		pointer to interface this pointer variable
   473 		*	@param origRetPr		pointer to original return value variable. NULL for void funcs
   474 		*	@param overrideRetPtr	pointer to override return value variable. NULL for void funcs
   475 		*	
   476 		*	@return Override Return Pointer the hookfunc should use (may differ from overrideRetPtr
   477 		*		when the hook func is being called as part of a recall
   478 		*/
   479 		virtual void *SetupHookLoop(META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr,
   480 			void **ifacePtrPtr, const void *origRetPtr, void *overrideRetPtr) = 0;
   481 
   482 		//!< 
   483 	};
   484 
   485 	// For META_RESULT_ORIG_RET and META_RESULT_OVERRIDE_RET:
   486 	//  These have to be able to return references. If T is a reference, the pointers returned
   487 	//  from the SH_GLOB_SHPTR are pointers to instances of ReferenceCarrier<T>::type.
   488 	template <class T> struct MacroRefHelpers
   489 	{
   490 		inline static const T* GetOrigRet(ISourceHook *shptr)
   491 		{
   492 			return reinterpret_cast<const T*>(shptr->GetOrigRet());
   493 		}
   494 		inline static const T* GetOverrideRet(ISourceHook *shptr)
   495 		{
   496 			return reinterpret_cast<const T*>(shptr->GetOverrideRet());
   497 		}
   498 	};
   499 
   500 	template <class T> struct MacroRefHelpers<T&>
   501 	{
   502 		inline static T* GetOrigRet(ISourceHook *shptr)
   503 		{
   504 			T &ref = *reinterpret_cast<const typename ReferenceCarrier<T&>::type *>(shptr->GetOrigRet());
   505 			return &ref;
   506 		}
   507 		inline static T* GetOverrideRet(ISourceHook *shptr)
   508 		{
   509 			T &ref = *reinterpret_cast<const typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRet());
   510 			return &ref;
   511 		}
   512 	};
   513 }
   514 
   515 /************************************************************************/
   516 /* High level interface                                                 */
   517 /************************************************************************/
   518 #define META_RESULT_STATUS					SH_GLOB_SHPTR->GetStatus()
   519 #define META_RESULT_PREVIOUS				SH_GLOB_SHPTR->GetPrevRes()
   520 #define META_RESULT_ORIG_RET(type)			*SourceHook::MacroRefHelpers<type>::GetOrigRet(SH_GLOB_SHPTR)
   521 #define META_RESULT_OVERRIDE_RET(type)		*SourceHook::MacroRefHelpers<type>::GetOverrideRet(SH_GLOB_SHPTR)
   522 #define META_IFACEPTR(type)					reinterpret_cast<type*>(SH_GLOB_SHPTR->GetIfacePtr())
   523 
   524 #define SET_META_RESULT(result)				SH_GLOB_SHPTR->SetRes(result)
   525 #define RETURN_META(result)					do { SET_META_RESULT(result); return; } while(0)
   526 #define RETURN_META_VALUE(result, value)	do { SET_META_RESULT(result); return (value); } while(0)
   527 
   528 // If a hook on a function which returns a reference does not want to specify a return value,
   529 // it can use this macro.
   530 //   ONLY USE THIS WITH MRES_IGNORED AND MRES_HANDLED !!!
   531 #define RETURN_META_NOREF(result, rettype)	do { SET_META_RESULT(result); return reinterpret_cast<rettype>(*SH_GLOB_SHPTR); } while(0)
   532 
   533 // only call these from the hook handlers directly!
   534 // :TODO: enforce it ?
   535 
   536 // Why take a memfuncptr instead of iface and func when we have to deduce the iface anyway now?
   537 // Well, without it, there'd be no way to specify which overloaded version we want in _VALUE
   538 
   539 // SourceHook::SetOverrideRet is defined later.
   540 #define RETURN_META_NEWPARAMS(result, memfuncptr, newparams) \
   541 	do { \
   542 		SET_META_RESULT(result); \
   543 		SH_GLOB_SHPTR->DoRecall(); \
   544 		(SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams; \
   545 		RETURN_META(MRES_SUPERCEDE); \
   546 	} while (0)
   547 
   548 #define RETURN_META_VALUE_NEWPARAMS(result, value, memfuncptr, newparams) \
   549 	do { \
   550 		SET_META_RESULT(result); \
   551 		SH_GLOB_SHPTR->DoRecall(); \
   552 		if ((result) >= MRES_OVERRIDE) \
   553 		{ \
   554 			/* meh, set the override result here because we don't get a chance to return */ \
   555 			/* before continuing the hook loop through the recall */ \
   556 			SourceHook::SetOverrideResult(memfuncptr)(SH_GLOB_SHPTR, value); \
   557 		} \
   558 		RETURN_META_VALUE(MRES_SUPERCEDE, \
   559 			(SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams); \
   560 	} while (0)
   561 
   562 // :TODO: thisptroffs in MNEWPARAMS ??
   563 
   564 #if SH_COMP == SH_COMP_MSVC
   565 
   566 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
   567 	union \
   568 	{ \
   569 		SH_MFHCls(hookname)::ECMFP mfp; \
   570 		void *addr; \
   571 	} u; \
   572 	SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
   573 	u.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
   574 		SH_MFHCls(hookname)::ms_MFI.vtblindex];
   575 
   576 #elif SH_COMP == SH_COMP_GCC
   577 
   578 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
   579 	union \
   580 	{ \
   581 		SH_MFHCls(hookname)::ECMFP mfp; \
   582 		struct \
   583 		{ \
   584 			void *addr; \
   585 			intptr_t adjustor; \
   586 		} s; \
   587 	} u; \
   588 	SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
   589 	u.s.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
   590 		SH_MFHCls(hookname)::ms_MFI.vtblindex]; \
   591 	u.s.adjustor = 0;
   592 
   593 #endif
   594 
   595 #define RETURN_META_MNEWPARAMS(result, hookname, newparams) \
   596 	do { \
   597 		SET_META_RESULT(result); \
   598 		SH_GLOB_SHPTR->DoRecall(); \
   599 		SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
   600 		(thisptr->*(u.mfp)) newparams; \
   601 		RETURN_META(MRES_SUPERCEDE); \
   602 	} while (0)
   603 
   604 #define RETURN_META_VALUE_MNEWPARAMS(result, value, hookname, newparams) \
   605 	do { \
   606 		SET_META_RESULT(result); \
   607 		SH_GLOB_SHPTR->DoRecall(); \
   608 		if ((result) >= MRES_OVERRIDE) \
   609 		{ \
   610 			/* see RETURN_META_VALUE_NEWPARAMS */ \
   611 			SourceHook::SetOverrideResult<SH_MFHCls(hookname)::RetType>(SH_GLOB_SHPTR, value); \
   612 		} \
   613 		SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
   614 		RETURN_META_VALUE(MRES_SUPERCEDE, (thisptr->*(u.mfp)) newparams); \
   615 	} while (0)
   616 
   617 /**
   618 *	@brief Get/generate callclass for an interface pointer
   619 *
   620 *	@param ifaceptr The interface pointer
   621 */
   622 template<class ifacetype>
   623 inline SourceHook::CallClass<ifacetype> *SH_GET_CALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr)
   624 {
   625 	return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
   626 		shptr->GetCallClass(reinterpret_cast<void*>(ptr), sizeof(ifacetype)));
   627 }
   628 
   629 template<class ifacetype>
   630 inline SourceHook::CallClass<ifacetype> *SH_GET_MCALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr, int ifacesize)
   631 {
   632 	return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
   633 		shptr->GetCallClass(reinterpret_cast<void*>(ptr), ifacesize));
   634 }
   635 
   636 template<class ifacetype>
   637 inline void SH_RELEASE_CALLCLASS_R(SourceHook::ISourceHook *shptr, SourceHook::CallClass<ifacetype> *ptr)
   638 {
   639 	shptr->ReleaseCallClass(reinterpret_cast<SourceHook::GenericCallClass*>(ptr));
   640 }
   641 
   642 #define SH_MANUALHOOK_RECONFIGURE(hookname, pvtblindex, pvtbloffs, pthisptroffs) \
   643 	do { \
   644 		SH_GLOB_SHPTR->RemoveHookManager(SH_GLOB_PLUGPTR, SH_MFHCls(hookname)::HookManPubFunc); \
   645 		SH_MFHCls(hookname)::ms_MFI.thisptroffs = pthisptroffs; \
   646 		SH_MFHCls(hookname)::ms_MFI.vtblindex = pvtblindex; \
   647 		SH_MFHCls(hookname)::ms_MFI.vtbloffs = pvtbloffs; \
   648 	} while (0)
   649 
   650 #define SH_GET_CALLCLASS(ptr) SH_GET_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
   651 #define SH_GET_MCALLCLASS(ptr, size) SH_GET_MCALLCLASS_R(SH_GLOB_SHPTR, reinterpret_cast<SourceHook::EmptyClass*>(ptr), size)
   652 #define SH_RELEASE_CALLCLASS(ptr) SH_RELEASE_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
   653 
   654 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   655 	__SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   656 	post, handler)
   657 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
   658 	SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
   659 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
   660 	SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
   661 
   662 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   663 	__SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   664 	post, handler)
   665 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
   666 	SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
   667 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
   668 	SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
   669 
   670 
   671 #define SH_ADD_MANUALHOOK(hookname, ifaceptr, handler, post) \
   672 	__SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
   673 #define SH_ADD_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
   674 	SH_ADD_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler), post)
   675 #define SH_ADD_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
   676 	SH_ADD_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
   677 
   678 #define SH_REMOVE_MANUALHOOK(hookname, ifaceptr, handler, post) \
   679 	__SourceHook_FHMRemove##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
   680 #define SH_REMOVE_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
   681 	SH_REMOVE_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler), post)
   682 #define SH_REMOVE_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
   683 	SH_REMOVE_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
   684 
   685 
   686 #define SH_NOATTRIB
   687 
   688 
   689 
   690 
   691 #if SH_COMP == SH_COMP_MSVC
   692 # define SH_SETUP_MFP(mfp) \
   693 	reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry;
   694 #elif SH_COMP == SH_COMP_GCC
   695 # define SH_SETUP_MFP(mfp) \
   696 	reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry; \
   697 	reinterpret_cast<void**>(&mfp)[1] = 0;
   698 #else
   699 # error Not supported yet.
   700 #endif
   701 
   702 //////////////////////////////////////////////////////////////////////////
   703 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
   704 #define SH_MFHCls(hookname) __SourceHook_MFHCls_##hookname
   705 
   706 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
   707 	SH_FHCls(ifacetype,ifacefunc,overload)() \
   708 	{ \
   709 		GetFuncInfo(funcptr, ms_MFI); \
   710 	} \
   711 	\
   712 	static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
   713 	{ \
   714 		using namespace ::SourceHook; \
   715 		GetFuncInfo(funcptr, ms_MFI); \
   716 		/* Verify interface version */ \
   717 		if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
   718 			return 1; \
   719 		\
   720 		if (action == HA_GetInfo) \
   721 		{ \
   722 			param->SetVersion(SH_HOOKMAN_VERSION); \
   723 			param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
   724 				reinterpret_cast<const char*>(&ms_Proto)); \
   725 			\
   726 			MemFuncInfo mfi; \
   727 			GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
   728 			param->SetHookfuncVfnptr( \
   729 				reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
   730 			return 0; \
   731 		} \
   732 		else if (action == HA_Register) \
   733 		{ \
   734 			ms_HI = param; \
   735 			return 0; \
   736 		} \
   737 		else if (action == HA_Unregister) \
   738 		{ \
   739 			ms_HI = NULL; \
   740 			return 0; \
   741 		} \
   742 		else \
   743 			return 1; \
   744 	}
   745 
   746 // It has to be possible to use the macros in namespaces
   747 // -> So we need to access and extend the global SourceHook namespace
   748 // We use a namespace alias for this
   749 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
   750 	struct SH_FHCls(ifacetype,ifacefunc,overload) \
   751 	{ \
   752 		static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
   753 		static ::SourceHook::MemFuncInfo ms_MFI; \
   754 		static ::SourceHook::IHookManagerInfo *ms_HI; \
   755 		static ::SourceHook::ProtoInfo ms_Proto; \
   756 		SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
   757 
   758 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, funcptr) \
   759 	}; \
   760 	SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
   761 	::SourceHook::MemFuncInfo SH_FHCls(ifacetype,ifacefunc,overload)::ms_MFI; \
   762 	::SourceHook::IHookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
   763 	bool __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
   764 		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   765 	{ \
   766 		using namespace ::SourceHook; \
   767 		MemFuncInfo mfi; \
   768 		GetFuncInfo(funcptr, mfi); \
   769 		if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
   770 			return false; /* No non-virtual functions / virtual inheritance supported */ \
   771 		\
   772 		return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
   773 			SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
   774 			new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
   775 	} \
   776 	bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
   777 		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   778 	{ \
   779 		using namespace ::SourceHook; \
   780 		MemFuncInfo mfi; \
   781 		GetFuncInfo(funcptr, mfi); \
   782 		if (mfi.thisptroffs < 0) \
   783 			return false; /* No virtual inheritance supported */ \
   784 		\
   785 		CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
   786 		return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
   787 			SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
   788 	} \
   789 
   790 #define SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
   791 	struct SH_MFHCls(hookname) \
   792 	{ \
   793 		static SH_MFHCls(hookname) ms_Inst; \
   794 		static ::SourceHook::MemFuncInfo ms_MFI; \
   795 		static ::SourceHook::IHookManagerInfo *ms_HI; \
   796 		static ::SourceHook::ProtoInfo ms_Proto; \
   797 		\
   798 		SH_MFHCls(hookname)() \
   799 		{ \
   800 			ms_MFI.isVirtual = true; \
   801 			ms_MFI.thisptroffs = pthisptroffs; \
   802 			ms_MFI.vtblindex = pvtblidx; \
   803 			ms_MFI.vtbloffs = pvtbloffs; \
   804 		} \
   805 		static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
   806 		{ \
   807 			using namespace ::SourceHook; \
   808 			/* we don't set ms_MFI here because manual hookmans can be reconfigured */ \
   809 			/* :FIXME: possible problem: someone adding a hook from a constructor of a global entity */ \
   810 			/* which is construced before SH_MFHCls(hookname)() gets called? */ \
   811 			/* Verify interface version */ \
   812 			if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
   813 				return 1; \
   814 			\
   815 			if (action == HA_GetInfo) \
   816 			{ \
   817 				param->SetVersion(SH_HOOKMAN_VERSION); \
   818 				param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
   819 					reinterpret_cast<const char*>(&ms_Proto)); \
   820 				\
   821 				MemFuncInfo mfi; \
   822 				GetFuncInfo(&SH_MFHCls(hookname)::Func, mfi); \
   823 				param->SetHookfuncVfnptr( \
   824 					reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
   825 				return 0; \
   826 			} \
   827 			else if (action == HA_Register) \
   828 			{ \
   829 				ms_HI = param; \
   830 				return 0; \
   831 			} \
   832 			else if (action == HA_Unregister) \
   833 			{ \
   834 				ms_HI = NULL; \
   835 				return 0; \
   836 			} \
   837 			else \
   838 				return 1; \
   839 		}
   840 
   841 #define SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
   842 	}; \
   843 	SH_MFHCls(hookname) SH_MFHCls(hookname)::ms_Inst; \
   844 	::SourceHook::MemFuncInfo SH_MFHCls(hookname)::ms_MFI; \
   845 	::SourceHook::IHookManagerInfo *SH_MFHCls(hookname)::ms_HI; \
   846 	bool __SourceHook_FHMAdd##hookname(void *iface, bool post, \
   847 		SH_MFHCls(hookname)::FD handler) \
   848 	{ \
   849 		return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
   850 			SH_MFHCls(hookname)::HookManPubFunc, \
   851 			new ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD>(handler), post); \
   852 	} \
   853 	bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
   854 		SH_MFHCls(hookname)::FD handler) \
   855 	{ \
   856 		::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD> tmp(handler); \
   857 		return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
   858 			SH_MFHCls(hookname)::HookManPubFunc, &tmp, post); \
   859 	} \
   860 
   861 
   862 #define SH_SETUPCALLS(rettype, paramtypes, params) \
   863 	/* 1) Find the vfnptr */ \
   864 	using namespace ::SourceHook; \
   865 	void *ourvfnptr = reinterpret_cast<void*>( \
   866 		*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
   867 	IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
   868 	SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
   869 	\
   870 	void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
   871 	/* ... and the iface */ \
   872 	IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
   873 	if (!ifinfo) \
   874 	{ \
   875 		/* The iface info was not found. Redirect the call to the original function. */ \
   876 		rettype (EmptyClass::*mfp)paramtypes; \
   877 		SH_SETUP_MFP(mfp); \
   878 		return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   879 	} \
   880 	/* 2) Declare some vars and set it up */ \
   881 	SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
   882 	IHookList *prelist = ifinfo->GetPreHooks(); \
   883 	IHookList *postlist = ifinfo->GetPostHooks(); \
   884 	META_RES status = MRES_IGNORED; \
   885 	META_RES prev_res; \
   886 	META_RES cur_res; \
   887 	typedef ReferenceCarrier<rettype>::type my_rettype; \
   888 	my_rettype orig_ret; \
   889 	my_rettype override_ret; \
   890 	my_rettype plugin_ret; \
   891 	void* ifptr; \
   892 	my_rettype *pOverrideRet = reinterpret_cast<my_rettype*>(SH_GLOB_SHPTR->SetupHookLoop( \
   893 		&status, &prev_res, &cur_res, &ifptr, &orig_ret, &override_ret));
   894 
   895 #define SH_CALL_HOOKS(post, params) \
   896 	if (SH_GLOB_SHPTR->ShouldContinue()) \
   897 	{ \
   898 		prev_res = MRES_IGNORED; \
   899 		for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
   900 		{ \
   901 			cur_res = MRES_IGNORED; \
   902 			ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
   903 			plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
   904 			prev_res = cur_res; \
   905 			if (cur_res > status) \
   906 				status = cur_res; \
   907 			if (cur_res >= MRES_OVERRIDE) \
   908 				*pOverrideRet = plugin_ret; \
   909 			if (!SH_GLOB_SHPTR->ShouldContinue()) \
   910 			{ \
   911 				iter.SetToZero(); \
   912 				break; \
   913 			} \
   914 		} \
   915 	}
   916 
   917 #define SH_CALL_ORIG(rettype, paramtypes, params) \
   918 	if (status != MRES_SUPERCEDE) \
   919 	{ \
   920 		rettype (EmptyClass::*mfp)paramtypes; \
   921 		SH_SETUP_MFP(mfp); \
   922 		orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   923 	} \
   924 	else \
   925 		orig_ret = override_ret; \
   926 
   927 #define SH_RETURN() \
   928 	SH_GLOB_SHPTR->HookLoopEnd(); \
   929 	return status >= MRES_OVERRIDE ? *pOverrideRet : orig_ret;
   930 
   931 #define SH_HANDLEFUNC(paramtypes, params, rettype) \
   932 	SH_SETUPCALLS(rettype, paramtypes, params) \
   933 	SH_CALL_HOOKS(pre, params) \
   934 	SH_CALL_ORIG(rettype, paramtypes, params) \
   935 	SH_CALL_HOOKS(post, params) \
   936 	SH_RETURN()
   937 
   938 //////////////////////////////////////////////////////////////////////////
   939 #define SH_SETUPCALLS_void(paramtypes, params) \
   940 	/* 1) Find the vfnptr */ \
   941 	using namespace ::SourceHook; \
   942 	void *ourvfnptr = reinterpret_cast<void*>( \
   943 		*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
   944 	IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
   945 	SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
   946 	\
   947 	void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
   948 	/* ... and the iface */ \
   949 	IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
   950 	if (!ifinfo) \
   951 	{ \
   952 		/* The iface info was not found. Redirect the call to the original function. */ \
   953 		void (EmptyClass::*mfp)paramtypes; \
   954 		SH_SETUP_MFP(mfp); \
   955 		(reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   956 		return; \
   957 	} \
   958 	/* 2) Declare some vars and set it up */ \
   959 	SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
   960 	IHookList *prelist = ifinfo->GetPreHooks(); \
   961 	IHookList *postlist = ifinfo->GetPostHooks(); \
   962 	META_RES status = MRES_IGNORED; \
   963 	META_RES prev_res; \
   964 	META_RES cur_res; \
   965 	void* ifptr; \
   966 	SH_GLOB_SHPTR->SetupHookLoop(&status, &prev_res, &cur_res, &ifptr, NULL, NULL); \
   967 
   968 #define SH_CALL_HOOKS_void(post, params) \
   969 	if (SH_GLOB_SHPTR->ShouldContinue()) \
   970 	{ \
   971 		prev_res = MRES_IGNORED; \
   972 		for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
   973 		{ \
   974 			cur_res = MRES_IGNORED; \
   975 			ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
   976 			reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
   977 			prev_res = cur_res; \
   978 			if (cur_res > status) \
   979 				status = cur_res; \
   980 			if (!SH_GLOB_SHPTR->ShouldContinue()) \
   981 			{ \
   982 				iter.SetToZero(); \
   983 				break; \
   984 			} \
   985 		} \
   986 	}
   987 
   988 #define SH_CALL_ORIG_void(paramtypes, params) \
   989 	if (status != MRES_SUPERCEDE) \
   990 	{ \
   991 		void (EmptyClass::*mfp)paramtypes; \
   992 		SH_SETUP_MFP(mfp); \
   993 		(reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   994 	}
   995 
   996 #define SH_RETURN_void() \
   997 	SH_GLOB_SHPTR->HookLoopEnd();
   998 
   999 #define SH_HANDLEFUNC_void(paramtypes, params) \
  1000 	SH_SETUPCALLS_void(paramtypes, params) \
  1001 	SH_CALL_HOOKS_void(pre, params) \
  1002 	SH_CALL_ORIG_void(paramtypes, params) \
  1003 	SH_CALL_HOOKS_void(post, params) \
  1004 	SH_RETURN_void()
  1005 
  1006 
  1007 // Special vafmt handlers
  1008 // :TODO: what
  1009 #define SH_HANDLEFUNC_vafmt(paramtypes, params_orig, params_plug, rettype) \
  1010 	SH_SETUPCALLS(rettype, paramtypes, params_orig) \
  1011 	SH_CALL_HOOKS(pre, params_plug) \
  1012 	SH_CALL_ORIG(rettype, paramtypes, params_orig) \
  1013 	SH_CALL_HOOKS(post, params_plug) \
  1014 	SH_RETURN()
  1015 
  1016 #define SH_HANDLEFUNC_void_vafmt(paramtypes, params_orig, params_plug) \
  1017 	SH_SETUPCALLS_void(paramtypes, params_orig) \
  1018 	SH_CALL_HOOKS_void(pre, params_plug) \
  1019 	SH_CALL_ORIG_void(paramtypes, params_orig) \
  1020 	SH_CALL_HOOKS_void(post, params_plug) \
  1021 	SH_RETURN_void()
  1022 
  1023 //////////////////////////////////////////////////////////////////////////
  1024 
  1025 // :FIXME:
  1026 //  sizeof on references returns the size of the datatype, NOT the pointer size or something
  1027 //  -> one should probably flag references in __SourceHook_ParamSizes_* !
  1028 //		or simply assume that their size is sizeof(void*)=SH_PTRSIZE... could be doable through a simple template
  1029 
  1030 @[$1,0,$a:
  1031 // ********* Support for $1 arguments *********
  1032 #define SH_DECL_HOOK$1(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
  1033 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
  1034 		(&ifacetype::ifacefunc))) \
  1035 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
  1036 		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
  1037 		{ SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
  1038 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
  1039 		(static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
  1040 	\
  1041 	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
  1042 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
  1043 		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
  1044 
  1045 #define SH_DECL_HOOK$1_void(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
  1046 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
  1047 		(&ifacetype::ifacefunc))) \
  1048 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
  1049 		virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
  1050 		{ SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
  1051 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
  1052 		(static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
  1053 	\
  1054 	\
  1055 	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
  1056 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
  1057 		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
  1058 
  1059 #define SH_DECL_HOOK$1_vafmt(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
  1060 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
  1061 		(&ifacetype::ifacefunc))) \
  1062 		typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, rettype> FD; \
  1063 		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
  1064 		{ \
  1065 			char buf[::SourceHook::STRBUF_LEN]; \
  1066 			va_list ap; \
  1067 			va_start(ap, fmt); \
  1068 			vsnprintf(buf, sizeof(buf), fmt, ap); \
  1069 			va_end(ap); \
  1070 			SH_HANDLEFUNC_vafmt((@[$2,1,$1|, :param$2@]@[$1!=0:, @]...), (@[$2,1,$1|, :p$2@]@[$1!=0:, @]"%s", buf), (@[$2,1,$1|, :p$2@]@[$1!=0:, @]buf), rettype); \
  1071 		} \
  1072 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
  1073 		(static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
  1074 	\
  1075 	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@[$2,1,$1:, sizeof(param$2)@] }; \
  1076 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
  1077 		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
  1078 
  1079 #define SH_DECL_HOOK$1_void_vafmt(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
  1080 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
  1081 		(&ifacetype::ifacefunc))) \
  1082 		typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *> FD; \
  1083 		virtual void Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
  1084 		{ \
  1085 			char buf[::SourceHook::STRBUF_LEN]; \
  1086 			va_list ap; \
  1087 			va_start(ap, fmt); \
  1088 			vsnprintf(buf, sizeof(buf), fmt, ap); \
  1089 			va_end(ap); \
  1090 			SH_HANDLEFUNC_void_vafmt((@[$2,1,$1|, :param$2@]@[$1!=0:, @]...), (@[$2,1,$1|, :p$2@]@[$1!=0:, @]"%s", buf), (@[$2,1,$1|, :p$2@]@[$1!=0:, @]buf)); \
  1091 		} \
  1092 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
  1093 		(static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
  1094 	\
  1095 	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@[$2,1,$1:, sizeof(param$2)@] }; \
  1096 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
  1097 		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
  1098 
  1099 #define SH_DECL_MANUALHOOK$1(hookname, vtblidx, vtbloffs, thisptroffs, rettype@[$2,1,$1:, param$2@]) \
  1100 	SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1101 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
  1102 		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
  1103 		{ SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
  1104 		typedef rettype(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
  1105 		typedef rettype RetType; \
  1106 	SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1107 	\
  1108 	const int __SourceHook_ParamSizesM_##hookname[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
  1109 	::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(sizeof(rettype), \
  1110 		$1, __SourceHook_ParamSizesM_##hookname); \
  1111 
  1112 #define SH_DECL_MANUALHOOK$1_void(hookname, vtblidx, vtbloffs, thisptroffs@[$2,1,$1:, param$2@]) \
  1113 	SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1114 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
  1115 		virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
  1116 		{ SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
  1117 		typedef void(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
  1118 	SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1119 	\
  1120 	const int __SourceHook_ParamSizesM_##hookname[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
  1121 	::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(0, \
  1122 		$1, __SourceHook_ParamSizesM_##hookname); \
  1123 
  1124 @]
  1125 
  1126 
  1127 //////////////////////////////////////////////////////////////////////////
  1128 // SH_CALL
  1129 
  1130 #if SH_COMP == SH_COMP_MSVC
  1131 
  1132 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
  1133 { \
  1134 	using namespace ::SourceHook; \
  1135 	MemFuncInfo mfi; \
  1136 	GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
  1137 	void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
  1138 	if (!origfunc) \
  1139 		return (m_CC->GetThisPtr()->*m_MFP)call; \
  1140 	\
  1141 	/* It's hooked. Call the original function. */ \
  1142 	union \
  1143 	{ \
  1144 		RetType(EmptyClass::*mfpnew)prms; \
  1145 		void *addr; \
  1146 	} u; \
  1147 	u.addr = origfunc; \
  1148 	\
  1149 	void *adjustedthisptr = reinterpret_cast<void*>(reinterpret_cast<char*>(m_CC->GetThisPtr()) + mfi.thisptroffs); \
  1150 	return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \
  1151 }
  1152 
  1153 # define SH_MAKE_MEXECUTABLECLASS_OB(call, prms) \
  1154 { \
  1155 	using namespace ::SourceHook; \
  1156 	char *adjustedthisptr = reinterpret_cast<char*>(m_CC->GetThisPtr()) + m_ThisPtrOffs; \
  1157 	union \
  1158 	{ \
  1159 		RetType(EmptyClass::*mfpnew)prms; \
  1160 		void *addr; \
  1161 	} u; \
  1162 	u.addr = m_CC->GetOrigFunc(m_ThisPtrOffs + m_VtblOffs, m_VtblIdx); \
  1163 	if (!u.addr) \
  1164 		u.addr = (*reinterpret_cast<void***>(adjustedthisptr + m_VtblOffs))[m_VtblIdx]; \
  1165 	\
  1166 	return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \
  1167 }
  1168 
  1169 #elif SH_COMP == SH_COMP_GCC
  1170 
  1171 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
  1172 { \
  1173 	using namespace ::SourceHook; \
  1174 	MemFuncInfo mfi; \
  1175 	GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
  1176 	void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
  1177 	if (!origfunc) \
  1178 		return (m_CC->GetThisPtr()->*m_MFP)call; \
  1179 	\
  1180 	/* It's hooked. Call the original function. */ \
  1181 	union \
  1182 	{ \
  1183 		RetType(EmptyClass::*mfpnew)prms; \
  1184 		struct \
  1185 		{ \
  1186 			void *addr; \
  1187 			intptr_t adjustor; \
  1188 		} s; \
  1189 	} u; \
  1190 	u.s.addr = origfunc; \
  1191 	u.s.adjustor = mfi.thisptroffs; \
  1192 	\
  1193 	return (reinterpret_cast<EmptyClass*>(m_CC->GetThisPtr())->*u.mfpnew)call; \
  1194 }
  1195 
  1196 # define SH_MAKE_MEXECUTABLECLASS_OB(call, prms) \
  1197 { \
  1198 	using namespace ::SourceHook; \
  1199 	char *thisptr = reinterpret_cast<char*>(m_CC->GetThisPtr()); \
  1200 	union \
  1201 	{ \
  1202 		RetType(EmptyClass::*mfpnew)prms; \
  1203 		struct { \
  1204 			void *addr; \
  1205 			intptr_t adjustor; \
  1206 		} s; \
  1207 	} u; \
  1208 	u.s.addr = m_CC->GetOrigFunc(m_ThisPtrOffs + m_VtblOffs, m_VtblIdx); \
  1209 	if (!u.s.addr) \
  1210 		u.s.addr = (*reinterpret_cast<void***>(thisptr + m_ThisPtrOffs + m_VtblOffs))[m_VtblIdx]; \
  1211 	\
  1212 	u.s.adjustor = m_ThisPtrOffs; \
  1213 	return (reinterpret_cast<EmptyClass*>(thisptr)->*u.mfpnew)call; \
  1214 }
  1215 
  1216 #endif
  1217 
  1218 namespace SourceHook
  1219 {
  1220 @[$1,0,$a:
  1221 	// Support for $1 arguments
  1222 	template<class CCType, class MFPType, class RetType@[$2,1,$1:, class Param$2@]> class ExecutableClass$1
  1223 	{
  1224 	   CCType *m_CC;
  1225 	   MFPType m_MFP;
  1226 	public:
  1227 	   ExecutableClass$1(CCType *cc, MFPType mfp) : m_CC(cc), m_MFP(mfp) { }
  1228 	
  1229 	   RetType operator()(@[$2,1,$1|, :Param$2 p$2@]) const
  1230 	      SH_MAKE_EXECUTABLECLASS_OB((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
  1231 	         
  1232 	   @[$2,$1+1,$a:
  1233 	   template <@[$3,$1+1,$2|, :class Param$3@]> RetType operator()(@[$3,1,$2|, :Param$3 p$3@]) const
  1234 	      SH_MAKE_EXECUTABLECLASS_OB((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
  1235 	   @]
  1236 	};
  1237 
  1238 	template <class RetType@[$2,1,$1:, class Param$2@]> class MExecutableClass$1
  1239 	{
  1240 		ManualCallClass *m_CC;
  1241 		int m_ThisPtrOffs;
  1242 		int m_VtblIdx;
  1243 		int m_VtblOffs;
  1244 	public:
  1245 		MExecutableClass$1(ManualCallClass *cc, int vtbloffs, int vtblidx, int thisptroffs) : m_CC(cc),
  1246 			m_ThisPtrOffs(thisptroffs), m_VtblIdx(vtblidx), m_VtblOffs(vtbloffs) { }
  1247 
  1248 		RetType operator()(@[$2,1,$1|, :Param$2 p$2@]) const
  1249 			SH_MAKE_MEXECUTABLECLASS_OB((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
  1250 
  1251 		@[$2,$1+1,$a:
  1252 		template <@[$3,$1+1,$2|, :class Param$3@]> RetType operator()(@[$3,1,$2|, :Param$3 p$3@]) const
  1253 			SH_MAKE_MEXECUTABLECLASS_OB((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
  1254 		@]
  1255 	};
  1256 @]
  1257 
  1258 }
  1259 
  1260 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
  1261 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
  1262 // The only purpose of the mfp2 parameter is to extract the return type
  1263 
  1264 @[$1,0,$a:
  1265 // Support for $1 arguments
  1266 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1267 SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>
  1268 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]))
  1269 {
  1270 	return SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp);
  1271 }
  1272 
  1273 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1274 SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>
  1275 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@])const)
  1276 {
  1277 	return SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp);
  1278 }
  1279 
  1280 template <class X, class RetType@[$2,1,$1:, class Param$2@]>
  1281 SourceHook::MExecutableClass$1<RetType@[$2,1,$1:, Param$2@]>
  1282 SH_MCALL2(SourceHook::ManualCallClass *ptr, RetType(X::*mfp)(@[$2,1,$1|, :Param$2@]), int vtblidx, int vtbloffs, int thisptroffs)
  1283 {
  1284 	return SourceHook::MExecutableClass$1<RetType@[$2,1,$1:, Param$2@]>(ptr, vtbloffs, vtblidx, thisptroffs);
  1285 }
  1286 @]
  1287 
  1288 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
  1289 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
  1290 
  1291 @[$1,0,$a:
  1292 // Support for $1 arguments
  1293 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1294 SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>
  1295 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...))
  1296 {
  1297 	return SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp);
  1298 }
  1299 
  1300 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1301 SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>
  1302 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...)const)
  1303 {
  1304 	return SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp);
  1305 }
  1306 
  1307 @]
  1308 
  1309 #endif
  1310 
  1311 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp))
  1312 #define SH_MCALL(ptr, mhookname) SH_MCALL2((ptr), SH_MFHCls(mhookname)::ECMFP(), SH_MFHCls(mhookname)::ms_MFI.vtblindex, \
  1313 	SH_MFHCls(mhookname)::ms_MFI.vtbloffs, SH_MFHCls(mhookname)::ms_MFI.thisptroffs)
  1314 
  1315 #undef SH_MAKE_EXECUTABLECLASS_OB
  1316 
  1317 //////////////////////////////////////////////////////////////////////////
  1318 // SetOverrideRet and RecallGetIface for recalls
  1319 // These take a ISourceHook pointer instead of using SH_GLOB_SHPTR directly
  1320 // The reason is that the user may want to redefine SH_GLOB_SHPTR - then the macros
  1321 // (META_RETURN_VALUE_NEWPARAMS) should obey the new pointer.
  1322 
  1323 namespace SourceHook
  1324 {
  1325 	template <class RetType>
  1326 	void SetOverrideResult(ISourceHook *shptr, const RetType res)
  1327 	{
  1328 		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
  1329 	}
  1330 
  1331 	// SetOverrideResult used to be implemented like this:
  1332 	//  SetOverrideResult(shptr, memfuncptr, return);
  1333 	//  normally the compiler can deduce the return type from memfuncptr, but (at least msvc8) failed when it was a reference
  1334 	//  (it thought it was ambigous - the ref and non-ref type)
  1335 	//  so now SetOverrideResult(memfuncptr) deduces the ret type, and returns a functor which does the work
  1336 	//  new syntax: SetOverrideResult(memfuncptr)(shptr, return)
  1337 	// This also allows us to create a special version for references which respects ReferenceCarrier.
  1338 
  1339 	template <class T> struct OverrideFunctor
  1340 	{
  1341 		void operator()(ISourceHook *shptr, T res)
  1342 		{
  1343 			*reinterpret_cast<T*>(shptr->GetOverrideRetPtr()) = res;
  1344 		}
  1345 	};
  1346 	template <class T> struct OverrideFunctor<T&>
  1347 	{
  1348 		void operator()(ISourceHook *shptr, T &res)
  1349 		{
  1350 			// overrideretptr points to ReferenceCarrier<T&>
  1351 			*reinterpret_cast<typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRetPtr()) = res;
  1352 		}
  1353 	};
  1354 @[$1,0,$a:
  1355 	template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
  1356 	OverrideFunctor<RetType> SetOverrideResult(RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
  1357 	{
  1358 		return OverrideFunctor<RetType>();
  1359 	}
  1360 
  1361 	template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
  1362 	Iface *RecallGetIface(ISourceHook *shptr, RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
  1363 	{
  1364 		return reinterpret_cast<Iface*>(shptr->GetIfacePtr());
  1365 	}
  1366 @]
  1367 }
  1368 
  1369 #endif
  1370 	// The pope is dead. -> :(