core/sourcehook/test/testhookmangen.hxx
author Scott Ehlert <ds@alliedmods.net>
Mon Nov 17 00:57:17 2008 -0600 (2008-11-17)
changeset 557 0a8ad2b12708
parent 539 db914129ae01
permissions -rw-r--r--
Added Windows support for loader, core, and core-legacy
     1 struct CAutoReleaseHookMan
     2 {
     3 	SourceHook::HookManagerPubFunc m_Ptr;
     4 	CAutoReleaseHookMan(SourceHook::HookManagerPubFunc ptr) : m_Ptr(ptr)
     5 	{
     6 	}
     7 	~CAutoReleaseHookMan()
     8 	{
     9 		g_HMAGPtr->ReleaseHookMan(m_Ptr);
    10 	}
    11 };
    12 
    13 // Strip &
    14 template <class T> struct StripRef
    15 {
    16 	typedef T type;
    17 };
    18 
    19 template <class T> struct StripRef<T&>
    20 {
    21 	typedef T type;
    22 };
    23 
    24 // Address of constructor/destructor
    25 // (using wrappers)
    26 template <class T>
    27 class Ctor_Thunk
    28 {
    29 public:
    30 	void NormalConstructor()
    31 	{
    32 		new(this) T;
    33 	}
    34 
    35 	void CopyConstructor(const T &other)
    36 	{
    37 		new(this) T(other);
    38 	}
    39 
    40 	void Destructor()
    41 	{
    42 		reinterpret_cast<T*>(this)->~T();
    43 	}
    44 
    45 	const T& AssignOp(const T &other)
    46 	{
    47 		return (*reinterpret_cast<T*>(this) = other);
    48 	}
    49 };
    50 
    51 
    52 template <class T>
    53 void *FindFuncAddr(T mfp)
    54 {
    55 	union
    56 	{
    57 		T a;
    58 		void *b;
    59 	} u;
    60 	u.a = mfp;
    61 	return u.b;
    62 }
    63 
    64 // Reference carrier
    65 template <class T> struct MyRefCarrier
    66 {
    67 	typedef T type;
    68 };
    69 
    70 template <class T> struct MyRefCarrier<T&>
    71 {
    72 	class type
    73 	{
    74 		T *m_StoredRef;
    75 	public:
    76 		type() : m_StoredRef(NULL)
    77 		{
    78 		}
    79 		type(T& ref) : m_StoredRef(&ref)
    80 		{
    81 		}
    82 
    83 		T& operator= (T& ref)
    84 		{
    85 			m_StoredRef = &ref;
    86 			return ref;
    87 		}
    88 
    89 		operator T& () const
    90 		{
    91 			return *m_StoredRef;
    92 		}
    93 		
    94 		bool operator== (const typename MyRefCarrier<T&>::type &other)
    95 		{
    96 			return m_StoredRef == other.m_StoredRef;
    97 		}
    98 		
    99 		friend std::ostream& operator <<(std::ostream &os,const typename MyRefCarrier<T&>::type &obj)
   100 		{
   101 			os << *obj.m_StoredRef;
   102 			return os;
   103 		}
   104 	};
   105 };
   106 
   107 #if SH_COMP==SH_COMP_GCC
   108 #define NO_OPTIMIZE __attribute__((noinline))
   109 #else
   110 #define NO_OPTIMIZE
   111 #endif
   112 
   113 // Return value maker
   114 template <class T>
   115 struct MakeRet
   116 {
   117 	static T Do(int a)
   118 	{
   119 		return a;
   120 	}
   121 };
   122 
   123 template <int SIZE>
   124 struct MakeRet< POD<SIZE> >
   125 {
   126 	static POD<SIZE> Do(int a)
   127 	{
   128 		POD<SIZE> x;
   129 		memset(reinterpret_cast<void*>(x.x), a, SIZE);
   130 		return x;
   131 	}
   132 };
   133 
   134 // Stores parameter status
   135 template <class T>
   136 bool EqualToMyFmtString(T sth)
   137 {
   138 	return false;
   139 }
   140 
   141 bool EqualToMyFmtString(std::string &sth)
   142 {
   143 	if (sth == "Hello %s%d%s")
   144 		sth = "Hello BA1L!";
   145 	return true;
   146 }
   147 
   148 
   149 @[$1,0,$a:
   150 
   151 template<int dummy@[$2,1,$1:, class p$2@]>
   152 struct ParamState$1
   153 {
   154 	@[$2,1,$1:typename MyRefCarrier<p$2>::type m_$2; @]
   155 	
   156 	bool operator==(const ParamState$1<dummy@[$2,1,$1:, p$2@]> &other)
   157 	{
   158 		return true
   159 			@[$2,1,$1: && m_$1 == other.m_$1@]
   160 			;
   161 	}
   162 	ParamState$1(@[$2,1,$1:p$2 a$2, @]...) @[$1!=0::@] @[$2,1,$1|, :m_$2(a$2)@]
   163 	{
   164 		@[$2,1,$1:
   165 		EqualToMyFmtString(m_$2);
   166 		@]
   167 	}
   168 	
   169 	ParamState$1<dummy@[$2,1,$1:, p$2@]> & operator() (int incrsteps)
   170 	{
   171 		@[$1!=0:int i;@]
   172 		@[$2,1,$1:
   173 		for (i = 0; i < incrsteps; ++i)
   174 			Increment<typename StripRef< p$2 >::type >::Incr(m_$2);
   175 		@]
   176 		
   177 		return *this;
   178 	}
   179 };
   180 
   181 @[$1!=0:template<@[$2,1,$1|, :class p$2@]>@]
   182 std::ostream& operator <<(std::ostream &os,const ParamState$1<0@[$2,1,$1:, p$2@]> &obj)
   183 {
   184 	@[$1!=0:os@] @[$2,1,$1:<< obj.m_$2@];
   185 	return os;
   186 }
   187 	
   188 @]
   189 
   190 #define CAT2(a, b) a##b
   191 #define CAT3(a, b, c) a##b##c
   192 #define CAT4(a, b, c, d) a##b##c##d
   193 
   194 // hook1: pre ignore
   195 // hook2: pre supercede
   196 // hook3: post ignore
   197 // hook4: post supercede
   198 
   199 
   200 @[$1,0,$a:
   201 
   202 #define THGM_MAKE_TEST$1_void(id@[$2,1,$1:, param$2@]) \
   203 	struct TestClass##id; \
   204 	typedef ParamState$1<0@[$2,1,$1:, param$2@] > ParamState_m##id; \
   205 	MAKE_STATE_2(State_Func##id, TestClass##id* /*thisptr*/, ParamState_m##id ); \
   206 	MAKE_STATE_4(State_Deleg_##id, int /*delegnumber*/, TestClass##id* /*ifptr*/, int /*deleg thisptr*/, ParamState_m##id ); \
   207 	\
   208 	struct TestClass##id \
   209 	{ \
   210 		static bool ms_DoRecall; \
   211 		\
   212 		virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
   213 		{ \
   214 			g_Inside_LeafFunc = true; \
   215 			ADD_STATE(State_Func##id(this, ParamState_m##id(@[$2,1,$1|, :p$2@]))); \
   216 			g_Inside_LeafFunc = false; \
   217 		} \
   218 		\
   219 		struct Delegate : public MyDelegate \
   220 		{ \
   221 			int m_DelegNumber; \
   222 			Delegate(int num) : m_DelegNumber(num) { } \
   223 			\
   224 			virtual void Call(@[$2,1,$1|, :param$2 p$2@]) \
   225 			{ \
   226 				g_Inside_LeafFunc = true; \
   227 				ADD_STATE(State_Deleg_##id(m_DelegNumber, META_IFACEPTR(TestClass##id), PtrBuf(this), ParamState_m##id(@[$2,1,$1|, :p$2@]))); \
   228 				g_Inside_LeafFunc = false; \
   229 				if (ms_DoRecall) \
   230 				{ \
   231 					@[$2,1,$1:Increment<StripRef< param$2 >::type>::Incr(p$2);@] \
   232 					RETURN_META_NEWPARAMS((m_DelegNumber & 1) ? MRES_IGNORED : MRES_SUPERCEDE, &TestClass##id::Func, (@[$2,1,$1|, :p$2@])); \
   233 				} \
   234 				else \
   235 					RETURN_META((m_DelegNumber & 1) ? MRES_IGNORED : MRES_SUPERCEDE); \
   236 			} \
   237 		}; \
   238 	}; \
   239 	\
   240 	bool TestClass##id::ms_DoRecall = false; \
   241 	SourceHook::CProtoInfoBuilder protoinfo_##id(SourceHook::ProtoInfo::CallConv_ThisCall);
   242 
   243 
   244 #define THGM_MAKE_TEST$1(id, ret_type@[$2,1,$1:, param$2@]) \
   245 	struct TestClass##id; \
   246 	typedef ret_type RetType##id; \
   247 	typedef ParamState$1<0@[$2,1,$1:, param$2@] > ParamState_m##id; \
   248 	MAKE_STATE_2(State_Func##id, TestClass##id* /*thisptr*/, ParamState_m##id ); \
   249 	MAKE_STATE_4(State_Deleg_##id, int /*delegnumber*/, TestClass##id* /*ifptr*/, int /*deleg thisptr*/, ParamState_m##id ); \
   250 	\
   251 	struct TestClass##id \
   252 	{ \
   253 		static bool ms_DoRecall; \
   254 		\
   255 		virtual ret_type Func(@[$2,1,$1|, :param$2 p$2@]) \
   256 		{ \
   257 			g_Inside_LeafFunc = true; \
   258 			ADD_STATE(State_Func##id(this, ParamState_m##id(@[$2,1,$1|, :p$2@]))); \
   259 			g_Inside_LeafFunc = false; \
   260 			\
   261 			return MakeRet< ret_type >::Do(0); \
   262 		} \
   263 		\
   264 		struct Delegate : public MyDelegate \
   265 		{ \
   266 			int m_DelegNumber; \
   267 			Delegate(int num) : m_DelegNumber(num) { } \
   268 			\
   269 			virtual ret_type Call(@[$2,1,$1|, :param$2 p$2@]) \
   270 			{ \
   271 				g_Inside_LeafFunc = true; \
   272 				ADD_STATE(State_Deleg_##id(m_DelegNumber, META_IFACEPTR(TestClass##id), PtrBuf(this), ParamState_m##id(@[$2,1,$1|, :p$2@]))); \
   273 				g_Inside_LeafFunc = false; \
   274 				if (ms_DoRecall) \
   275 				{ \
   276 					@[$2,1,$1:Increment<StripRef< param$2 >::type>::Incr(p$2);@] \
   277 					RETURN_META_VALUE_NEWPARAMS((m_DelegNumber & 1) ? MRES_IGNORED : MRES_SUPERCEDE, MakeRet< ret_type >::Do(m_DelegNumber), &TestClass##id::Func, (@[$2,1,$1|, :p$2@])); \
   278 				} \
   279 				else \
   280 					RETURN_META_VALUE((m_DelegNumber & 1) ? MRES_IGNORED : MRES_SUPERCEDE, MakeRet< ret_type >::Do(m_DelegNumber)); \
   281 			} \
   282 		}; \
   283 	}; \
   284 	\
   285 	bool TestClass##id::ms_DoRecall = false; \
   286 	SourceHook::CProtoInfoBuilder protoinfo_##id(SourceHook::ProtoInfo::CallConv_ThisCall);
   287 	
   288 #define THGM_MAKE_TEST$1_vafmt_void(id@[$2,1,$1:, param$2@]) \
   289 	struct TestClass##id; \
   290 	typedef ParamState@($1+1)<0@[$2,1,$1:, param$2@], std::string > ParamState_m##id; \
   291 	MAKE_STATE_2(State_Func##id, TestClass##id* /*thisptr*/, ParamState_m##id ); \
   292 	MAKE_STATE_4(State_Deleg_##id, int /*delegnumber*/, TestClass##id* /*ifptr*/, int /*deleg thisptr*/, ParamState_m##id ); \
   293 	\
   294 	struct TestClass##id \
   295 	{ \
   296 		static bool ms_DoRecall; \
   297 		\
   298 		virtual void Func(@[$2,1,$1:param$2 p$2, @]const char *fmt, ...) \
   299 		{ \
   300 			g_Inside_LeafFunc = true; \
   301 			\
   302 			char buf[9999]; \
   303 			va_list ap; \
   304 			va_start(ap, fmt); \
   305 			vsnprintf(buf, 9998, fmt, ap); \
   306 			buf[9998] = 0; \
   307 			va_end(ap); \
   308 			\
   309 			ADD_STATE(State_Func##id(this, ParamState_m##id(@[$2,1,$1:p$2, @]std::string(buf)))); \
   310 			g_Inside_LeafFunc = false; \
   311 		} \
   312 		\
   313 		struct Delegate : public MyDelegate \
   314 		{ \
   315 			int m_DelegNumber; \
   316 			Delegate(int num) : m_DelegNumber(num) { } \
   317 			\
   318 			virtual void Call(@[$2,1,$1:param$2 p$2, @]const char *buf) \
   319 			{ \
   320 				g_Inside_LeafFunc = true; \
   321 				ADD_STATE(State_Deleg_##id(m_DelegNumber, META_IFACEPTR(TestClass##id), PtrBuf(this), ParamState_m##id(@[$2,1,$1:p$2, @]buf))); \
   322 				g_Inside_LeafFunc = false; \
   323 				if (ms_DoRecall) \
   324 				{ \
   325 					@[$2,1,$1:Increment<StripRef< param$2 >::type>::Incr(p$2);@] \
   326 					RETURN_META_NEWPARAMS((m_DelegNumber & 1) ? MRES_IGNORED : MRES_SUPERCEDE, &TestClass##id::Func, (@[$2,1,$1:p$2, @]"%s!", buf)); \
   327 				} \
   328 				else \
   329 					RETURN_META((m_DelegNumber & 1) ? MRES_IGNORED : MRES_SUPERCEDE); \
   330 			} \
   331 		}; \
   332 	}; \
   333 	\
   334 	bool TestClass##id::ms_DoRecall = false; \
   335 	SourceHook::CProtoInfoBuilder protoinfo_##id(SourceHook::ProtoInfo::CallConv_ThisCall | SourceHook::ProtoInfo::CallConv_HasVafmt);
   336 
   337 #define THGM_MAKE_TEST$1_vafmt(id, ret_type@[$2,1,$1:, param$2@]) \
   338 	struct TestClass##id; \
   339 	typedef ret_type RetType##id; \
   340 	typedef ParamState@($1+1)<0@[$2,1,$1:, param$2@], std::string > ParamState_m##id; \
   341 	MAKE_STATE_2(State_Func##id, TestClass##id* /*thisptr*/, ParamState_m##id ); \
   342 	MAKE_STATE_4(State_Deleg_##id, int /*delegnumber*/, TestClass##id* /*ifptr*/, int /*deleg thisptr*/, ParamState_m##id ); \
   343 	\
   344 	struct TestClass##id \
   345 	{ \
   346 		static bool ms_DoRecall; \
   347 		\
   348 		virtual ret_type Func(@[$2,1,$1:param$2 p$2, @]const char *fmt, ...) \
   349 		{ \
   350 			g_Inside_LeafFunc = true; \
   351 			\
   352 			char buf[9999]; \
   353 			va_list ap; \
   354 			va_start(ap, fmt); \
   355 			vsnprintf(buf, 9998, fmt, ap); \
   356 			buf[9998] = 0; \
   357 			va_end(ap); \
   358 			\
   359 			ADD_STATE(State_Func##id(this, ParamState_m##id(@[$2,1,$1:p$2, @]std::string(buf)))); \
   360 			\
   361 			return MakeRet< ret_type >::Do(0); \
   362 		} \
   363 		\
   364 		struct Delegate : public MyDelegate \
   365 		{ \
   366 			int m_DelegNumber; \
   367 			Delegate(int num) : m_DelegNumber(num) { } \
   368 			\
   369 			virtual ret_type Call(@[$2,1,$1:param$2 p$2, @]const char *buf) \
   370 			{ \
   371 				g_Inside_LeafFunc = true; \
   372 				ADD_STATE(State_Deleg_##id(m_DelegNumber, META_IFACEPTR(TestClass##id), PtrBuf(this), ParamState_m##id(@[$2,1,$1:p$2, @]buf))); \
   373 				g_Inside_LeafFunc = false; \
   374 				if (ms_DoRecall) \
   375 				{ \
   376 					@[$2,1,$1:Increment<StripRef< param$2 >::type>::Incr(p$2);@] \
   377 					RETURN_META_VALUE_NEWPARAMS((m_DelegNumber & 1) ? MRES_IGNORED : MRES_SUPERCEDE, MakeRet< ret_type >::Do(m_DelegNumber), &TestClass##id::Func, (@[$2,1,$1:p$2, @]"%s!", buf)); \
   378 				} \
   379 				else \
   380 					RETURN_META_VALUE((m_DelegNumber & 1) ? MRES_IGNORED : MRES_SUPERCEDE, MakeRet< ret_type >::Do(m_DelegNumber)); \
   381 			} \
   382 		}; \
   383 	}; \
   384 	\
   385 	bool TestClass##id::ms_DoRecall = false; \
   386 	SourceHook::CProtoInfoBuilder protoinfo_##id(SourceHook::ProtoInfo::CallConv_ThisCall | SourceHook::ProtoInfo::CallConv_HasVafmt);
   387 
   388 #define THGM_SETUP_PI$1(id@[$2,1,$1:, p$2_type, p$2_passtype, p$2_flags@]) \
   389 	void setuppi_##id() \
   390 	{ \
   391 		@[$2,1,$1: \
   392 		protoinfo_##id.AddParam(sizeof(p$2_type), p$2_passtype, p$2_flags, \
   393 			(p$2_flags & SourceHook::PassInfo::PassFlag_OCtor) ? FindFuncAddr(&Ctor_Thunk<StripRef< p$2_type >::type>::NormalConstructor) : NULL, \
   394 			(p$2_flags & SourceHook::PassInfo::PassFlag_CCtor) ? FindFuncAddr(&Ctor_Thunk<StripRef< p$2_type >::type>::CopyConstructor) : NULL, \
   395 			(p$2_flags & SourceHook::PassInfo::PassFlag_ODtor) ? FindFuncAddr(&Ctor_Thunk<StripRef< p$2_type >::type>::Destructor) : NULL, \
   396 			(p$2_flags & SourceHook::PassInfo::PassFlag_AssignOp) ? FindFuncAddr(&Ctor_Thunk<StripRef< p$2_type >::type>::AssignOp) : NULL \
   397 			); \
   398 		@] \
   399 	}
   400 	
   401 @]
   402 
   403 #define THGM_SETUP_RI(id, ret_type, ret_passtype, ret_flags) \
   404 	void setupri_##id() \
   405 	{ \
   406 		protoinfo_##id.SetReturnType(sizeof(ret_type), ret_passtype, ret_flags, \
   407 			(ret_flags & SourceHook::PassInfo::PassFlag_OCtor) ? FindFuncAddr(&Ctor_Thunk<StripRef< ret_type >::type>::NormalConstructor) : NULL, \
   408 			(ret_flags & SourceHook::PassInfo::PassFlag_CCtor) ? FindFuncAddr(&Ctor_Thunk<StripRef< ret_type >::type>::CopyConstructor) : NULL, \
   409 			(ret_flags & SourceHook::PassInfo::PassFlag_ODtor) ? FindFuncAddr(&Ctor_Thunk<StripRef< ret_type >::type>::Destructor) : NULL, \
   410 			(ret_flags & SourceHook::PassInfo::PassFlag_AssignOp) ? FindFuncAddr(&Ctor_Thunk<StripRef< ret_type >::type>::AssignOp) : NULL \
   411 			); \
   412 	}
   413 	
   414 #define THGM_ADD_HOOK(id, num) \
   415 	CAT4(hook, num, _, id) = g_SHPtr->AddHook(g_PLID, SourceHook::ISourceHook::Hook_Normal, reinterpret_cast<void*>(pTest##id), \
   416 		0, myhookman##id, PtrBufPtr(new TestClass##id::Delegate(num)), num >= 3);
   417 
   418 #define THGM_REMOVE_HOOK(id, num) \
   419 	g_SHPtr->RemoveHookByID(CAT4(hook, num, _, id));
   420 
   421 #define THGM_CALLS_void(id, call_params) \
   422 	pTest##id->Func call_params; \
   423 	SH_CALL(pTest##id, &TestClass##id::Func) call_params;
   424 
   425 #define THGM_DO_TEST_void(id, call_params) \
   426 	setuppi_##id(); \
   427 	SourceHook::HookManagerPubFunc myhookman##id = g_HMAGPtr->MakeHookMan(protoinfo_##id, 0, 0); \
   428 	CAutoReleaseHookMan arhm_##id(myhookman##id); \
   429 	int hook1_##id, hook2_##id, hook3_##id, hook4_##id; \
   430 	\
   431 	TestClass##id::ms_DoRecall = false; \
   432 	TestClass##id *pTest##id = new TestClass##id; \
   433 	CAutoPtrDestruction<TestClass##id> apd##id(pTest##id); \
   434 	\
   435 			/* no hooks - no hooks */ \
   436 	PtrBuf_Clear(); \
   437 	THGM_CALLS_void(id, call_params); \
   438 	CHECK_STATES((&g_States, \
   439 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   440 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   441 		NULL), "Test" #id " Part1"); \
   442 	\
   443 			/* hook1 - hook3 */ \
   444 	PtrBuf_Clear(); \
   445 	THGM_ADD_HOOK(id, 1); \
   446 	THGM_ADD_HOOK(id, 3); \
   447 	THGM_CALLS_void(id, call_params); \
   448 	CHECK_STATES((&g_States, \
   449 		new State_Deleg_##id(1, pTest##id, 0, ParamState_m##id call_params), \
   450 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   451 		new State_Deleg_##id(3, pTest##id, 1, ParamState_m##id call_params), \
   452 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   453 		NULL), "Test" #id " Part4"); \
   454 	THGM_REMOVE_HOOK(id, 1); \
   455 	THGM_REMOVE_HOOK(id, 3); \
   456 	\
   457 			/* hook1, hook2 - hook3 */ \
   458 	PtrBuf_Clear(); \
   459 	THGM_ADD_HOOK(id, 1); \
   460 	THGM_ADD_HOOK(id, 2); \
   461 	THGM_ADD_HOOK(id, 3); \
   462 	THGM_CALLS_void(id, call_params); \
   463 	CHECK_STATES((&g_States, \
   464 		new State_Deleg_##id(1, pTest##id, 0, ParamState_m##id call_params), \
   465 		new State_Deleg_##id(2, pTest##id, 1, ParamState_m##id call_params), \
   466 		new State_Deleg_##id(3, pTest##id, 2, ParamState_m##id call_params), \
   467 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   468 		NULL), "Test" #id " Part5"); \
   469 			/* hook1, hook2 - hook3, hook4 */ \
   470 	THGM_ADD_HOOK(id, 4); \
   471 	THGM_CALLS_void(id, call_params); \
   472 	CHECK_STATES((&g_States, \
   473 		new State_Deleg_##id(1, pTest##id, 0, ParamState_m##id call_params), \
   474 		new State_Deleg_##id(2, pTest##id, 1, ParamState_m##id call_params), \
   475 		new State_Deleg_##id(3, pTest##id, 2, ParamState_m##id call_params), \
   476 		new State_Deleg_##id(4, pTest##id, 3, ParamState_m##id call_params), \
   477 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   478 		NULL), "Test" #id " Part6"); \
   479 		\
   480 			/* hook1 - hook3, hook4, with recalls! */ \
   481 		\
   482 	TestClass##id::ms_DoRecall = true; \
   483 	THGM_REMOVE_HOOK(id, 2); \
   484 	THGM_CALLS_void(id, call_params); \
   485 	CHECK_STATES((&g_States, \
   486 		new State_Deleg_##id(1, pTest##id, 0, ParamState_m##id call_params(0)), \
   487 		new State_Func##id(pTest##id, ParamState_m##id call_params(1)), \
   488 		new State_Deleg_##id(3, pTest##id, 2, ParamState_m##id call_params(1)), \
   489 		new State_Deleg_##id(4, pTest##id, 3, ParamState_m##id call_params(2)), \
   490 		/* sh_call one */ \
   491 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   492 		NULL), "Test" #id " Part7"); \
   493 	THGM_REMOVE_HOOK(id, 1); \
   494 	THGM_REMOVE_HOOK(id, 3); \
   495 	THGM_REMOVE_HOOK(id, 4);
   496 
   497 template<class T>
   498 T ComparableRef(T x)
   499 {
   500 	return x;
   501 }
   502 
   503 template <class T>
   504 T* ComparableRef(T& x)
   505 {
   506 	return &x;
   507 }
   508 
   509 #define THGM_CALLS(id, call_params, exp_ret_norm, exp_ret_shcall, err) \
   510 	CHECK_COND(ComparableRef<RetType##id>(pTest##id->Func call_params) == ComparableRef<RetType##id>(MakeRet< RetType##id >::Do(exp_ret_norm)), err " /retcallnorm"); \
   511 	CHECK_COND(ComparableRef<RetType##id>(SH_CALL(pTest##id, &TestClass##id::Func) call_params) == ComparableRef<RetType##id>(MakeRet< RetType##id >::Do(exp_ret_shcall)), err " /retcallshcall");
   512 
   513 #define THGM_DO_TEST(id, call_params) \
   514 	setuppi_##id(); \
   515 	setupri_##id(); \
   516 	SourceHook::HookManagerPubFunc myhookman##id = g_HMAGPtr->MakeHookMan(protoinfo_##id, 0, 0); \
   517 	CAutoReleaseHookMan arhm_##id(myhookman##id); \
   518 	int hook1_##id, hook2_##id, hook3_##id, hook4_##id; \
   519 	\
   520 	TestClass##id::ms_DoRecall = false; \
   521 	TestClass##id *pTest##id = new TestClass##id; \
   522 	CAutoPtrDestruction<TestClass##id> apd##id(pTest##id); \
   523 	\
   524 			/* no hooks - no hooks */ \
   525 	PtrBuf_Clear(); \
   526 	THGM_CALLS(id, call_params, 0, 0, "Part1"); \
   527 	CHECK_STATES((&g_States, \
   528 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   529 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   530 		NULL), "Test" #id " Part1"); \
   531 	\
   532 			/* hook1 - hook3 */ \
   533 	PtrBuf_Clear(); \
   534 	THGM_ADD_HOOK(id, 1); \
   535 	THGM_ADD_HOOK(id, 3); \
   536 	THGM_CALLS(id, call_params, 0, 0, "Part4"); \
   537 	CHECK_STATES((&g_States, \
   538 		new State_Deleg_##id(1, pTest##id, 0, ParamState_m##id call_params), \
   539 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   540 		new State_Deleg_##id(3, pTest##id, 1, ParamState_m##id call_params), \
   541 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   542 		NULL), "Test" #id " Part4"); \
   543 	THGM_REMOVE_HOOK(id, 1); \
   544 	THGM_REMOVE_HOOK(id, 3); \
   545 	\
   546 			/* hook1, hook2 - hook3 */ \
   547 	PtrBuf_Clear(); \
   548 	THGM_ADD_HOOK(id, 1); \
   549 	THGM_ADD_HOOK(id, 2); \
   550 	THGM_ADD_HOOK(id, 3); \
   551 	THGM_CALLS(id, call_params, 2, 0, "Part5"); \
   552 	CHECK_STATES((&g_States, \
   553 		new State_Deleg_##id(1, pTest##id, 0, ParamState_m##id call_params), \
   554 		new State_Deleg_##id(2, pTest##id, 1, ParamState_m##id call_params), \
   555 		new State_Deleg_##id(3, pTest##id, 2, ParamState_m##id call_params), \
   556 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   557 		NULL), "Test" #id " Part5"); \
   558 			/* hook1, hook2 - hook3, hook4 */ \
   559 	THGM_ADD_HOOK(id, 4); \
   560 	THGM_CALLS(id, call_params, 4, 0, "Part6"); \
   561 	CHECK_STATES((&g_States, \
   562 		new State_Deleg_##id(1, pTest##id, 0, ParamState_m##id call_params), \
   563 		new State_Deleg_##id(2, pTest##id, 1, ParamState_m##id call_params), \
   564 		new State_Deleg_##id(3, pTest##id, 2, ParamState_m##id call_params), \
   565 		new State_Deleg_##id(4, pTest##id, 3, ParamState_m##id call_params), \
   566 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   567 		NULL), "Test" #id " Part6"); \
   568 		\
   569 			/* hook1 - hook3, hook4, with recalls! */ \
   570 		\
   571 	TestClass##id::ms_DoRecall = true; \
   572 	THGM_REMOVE_HOOK(id, 2); \
   573 	THGM_CALLS(id, call_params, 4, 0, "Part7"); \
   574 	CHECK_STATES((&g_States, \
   575 		new State_Deleg_##id(1, pTest##id, 0, ParamState_m##id call_params(0)), \
   576 		new State_Func##id(pTest##id, ParamState_m##id call_params(1)), \
   577 		new State_Deleg_##id(3, pTest##id, 2, ParamState_m##id call_params(1)), \
   578 		new State_Deleg_##id(4, pTest##id, 3, ParamState_m##id call_params(2)), \
   579 		/* sh_call one */ \
   580 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   581 		NULL), "Test" #id " Part7"); \
   582 	THGM_REMOVE_HOOK(id, 1); \
   583 	THGM_REMOVE_HOOK(id, 3); \
   584 	THGM_REMOVE_HOOK(id, 4);