sourcehook/test/testhookmangen.hxx
author Pavol Marko <pavolmarko@gmail.com>
Sat Nov 17 09:53:58 2007 +0000 (2007-11-17)
branchhookman_autogen
changeset 461 2fa049f36721
parent 458 4a0c5136f5e1
child 463 7a413a63426f
permissions -rw-r--r--
New hookfunc local vars code; first step to vafmt support
     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 @[$1,0,$a:
   136 
   137 template<int dummy@[$2,1,$1:, class p$2@]>
   138 struct ParamState$1
   139 {
   140 	@[$2,1,$1:typename MyRefCarrier<p$2>::type m_$2; @]
   141 	
   142 	bool operator==(const ParamState$1<dummy@[$2,1,$1:, p$2@]> &other)
   143 	{
   144 		return true
   145 			@[$2,1,$1: && m_$1 == other.m_$1@]
   146 			;
   147 	}
   148 	ParamState$1(@[$2,1,$1|, :p$2 a$2@]) @[$1!=0::@] @[$2,1,$1|, :m_$2(a$2)@]
   149 	{
   150 	}
   151 	
   152 	ParamState$1<dummy@[$2,1,$1:, p$2@]> & operator() (int incrsteps)
   153 	{
   154 		@[$1!=0:int i;@]
   155 		@[$2,1,$1:
   156 		for (i = 0; i < incrsteps; ++i)
   157 			Increment<typename StripRef< p$2 >::type >::Incr(m_$2);
   158 		@]
   159 		
   160 		return *this;
   161 	}
   162 };
   163 
   164 @[$1!=0:template<@[$2,1,$1|, :class p$2@]>@]
   165 std::ostream& operator <<(std::ostream &os,const ParamState$1<0@[$2,1,$1:, p$2@]> &obj)
   166 {
   167 	@[$1!=0:os@] @[$2,1,$1:<< obj.m_$2@];
   168 	return os;
   169 }
   170 	
   171 @]
   172 
   173 #define CAT2(a, b) a##b
   174 #define CAT3(a, b, c) a##b##c
   175 #define CAT4(a, b, c, d) a##b##c##d
   176 
   177 // hook1: pre ignore
   178 // hook2: pre supercede
   179 // hook3: post ignore
   180 // hook4: post supercede
   181 
   182 
   183 @[$1,0,$a:
   184 
   185 #define THGM_MAKE_TEST$1_void(id@[$2,1,$1:, param$2@]) \
   186 	struct TestClass##id; \
   187 	typedef ParamState$1<0@[$2,1,$1:, param$2@] > ParamState_m##id; \
   188 	MAKE_STATE_2(State_Func##id, TestClass##id* /*thisptr*/, ParamState_m##id ); \
   189 	MAKE_STATE_3(State_Deleg1_##id, TestClass##id* /*ifptr*/, int /*deleg thisptr*/, ParamState_m##id ); \
   190 	MAKE_STATE_3(State_Deleg2_##id, TestClass##id* /*ifptr*/, int /*deleg thisptr*/, ParamState_m##id ); \
   191 	MAKE_STATE_3(State_Deleg3_##id, TestClass##id* /*ifptr*/, int /*deleg thisptr*/, ParamState_m##id ); \
   192 	MAKE_STATE_3(State_Deleg4_##id, TestClass##id* /*ifptr*/, int /*deleg thisptr*/, ParamState_m##id ); \
   193 	\
   194 	struct TestClass##id \
   195 	{ \
   196 		static bool ms_DoRecall; \
   197 		\
   198 		virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
   199 		{ \
   200 			g_Inside_LeafFunc = true; \
   201 			ADD_STATE(State_Func##id(this, ParamState_m##id(@[$2,1,$1|, :p$2@]))); \
   202 			g_Inside_LeafFunc = false; \
   203 		} \
   204 		\
   205 		struct Delegate1 : public MyDelegate \
   206 		{ \
   207 			virtual void Call(@[$2,1,$1|, :param$2 p$2@]) \
   208 			{ \
   209 				g_Inside_LeafFunc = true; \
   210 				ADD_STATE(State_Deleg1_##id(META_IFACEPTR(TestClass##id), PtrBuf(this), ParamState_m##id(@[$2,1,$1|, :p$2@]))); \
   211 				g_Inside_LeafFunc = false; \
   212 				if (ms_DoRecall) \
   213 				{ \
   214 					@[$2,1,$1:Increment<StripRef< param$2 >::type>::Incr(p$2);@] \
   215 					RETURN_META_NEWPARAMS(MRES_IGNORED, &TestClass##id::Func, (@[$2,1,$1|, :p$2@])); \
   216 				} \
   217 				else \
   218 					RETURN_META(MRES_IGNORED); \
   219 			} \
   220 		}; \
   221 		struct Delegate2 : public MyDelegate \
   222 		{ \
   223 			virtual void Call(@[$2,1,$1|, :param$2 p$2@]) \
   224 			{ \
   225 				g_Inside_LeafFunc = true; \
   226 				ADD_STATE(State_Deleg2_##id(META_IFACEPTR(TestClass##id), PtrBuf(this), ParamState_m##id(@[$2,1,$1|, :p$2@]))); \
   227 				g_Inside_LeafFunc = false; \
   228 				if (ms_DoRecall) \
   229 				{ \
   230 					@[$2,1,$1:Increment<StripRef< param$2 >::type>::Incr(p$2);@] \
   231 					RETURN_META_NEWPARAMS(MRES_SUPERCEDE, &TestClass##id::Func, (@[$2,1,$1|, :p$2@])); \
   232 				} \
   233 				else \
   234 					RETURN_META(MRES_SUPERCEDE); \
   235 			} \
   236 		}; \
   237 		struct Delegate3 : public MyDelegate \
   238 		{ \
   239 			virtual void Call(@[$2,1,$1|, :param$2 p$2@]) \
   240 			{ \
   241 				g_Inside_LeafFunc = true; \
   242 				ADD_STATE(State_Deleg3_##id(META_IFACEPTR(TestClass##id), PtrBuf(this), ParamState_m##id(@[$2,1,$1|, :p$2@]))); \
   243 				g_Inside_LeafFunc = false; \
   244 				if (ms_DoRecall) \
   245 				{ \
   246 					@[$2,1,$1:Increment<StripRef< param$2 >::type>::Incr(p$2);@] \
   247 					RETURN_META_NEWPARAMS(MRES_IGNORED, &TestClass##id::Func, (@[$2,1,$1|, :p$2@])); \
   248 				} \
   249 				else \
   250 					RETURN_META(MRES_IGNORED); \
   251 			} \
   252 		}; \
   253 		struct Delegate4 : public MyDelegate \
   254 		{ \
   255 			virtual void Call(@[$2,1,$1|, :param$2 p$2@]) \
   256 			{ \
   257 				g_Inside_LeafFunc = true; \
   258 				ADD_STATE(State_Deleg4_##id(META_IFACEPTR(TestClass##id), PtrBuf(this), ParamState_m##id(@[$2,1,$1|, :p$2@]))); \
   259 				g_Inside_LeafFunc = false; \
   260 				if (ms_DoRecall) \
   261 				{ \
   262 					@[$2,1,$1:Increment<StripRef< param$2 >::type>::Incr(p$2);@] \
   263 					RETURN_META_NEWPARAMS(MRES_SUPERCEDE, &TestClass##id::Func, (@[$2,1,$1|, :p$2@])); \
   264 				} \
   265 				else \
   266 					RETURN_META(MRES_SUPERCEDE); \
   267 			}; \
   268 		}; \
   269 	}; \
   270 	\
   271 	bool TestClass##id::ms_DoRecall = false; \
   272 	SourceHook::CProtoInfoBuilder protoinfo_##id(SourceHook::ProtoInfo::CallConv_ThisCall);
   273 
   274 
   275 #define THGM_MAKE_TEST$1(id, ret_type@[$2,1,$1:, param$2@]) \
   276 	struct TestClass##id; \
   277 	typedef ret_type RetType##id; \
   278 	typedef ParamState$1<0@[$2,1,$1:, param$2@] > ParamState_m##id; \
   279 	MAKE_STATE_2(State_Func##id, TestClass##id* /*thisptr*/, ParamState_m##id ); \
   280 	MAKE_STATE_3(State_Deleg1_##id, TestClass##id* /*ifptr*/, int /*deleg thisptr*/, ParamState_m##id ); \
   281 	MAKE_STATE_3(State_Deleg2_##id, TestClass##id* /*ifptr*/, int /*deleg thisptr*/, ParamState_m##id ); \
   282 	MAKE_STATE_3(State_Deleg3_##id, TestClass##id* /*ifptr*/, int /*deleg thisptr*/, ParamState_m##id ); \
   283 	MAKE_STATE_3(State_Deleg4_##id, TestClass##id* /*ifptr*/, int /*deleg thisptr*/, ParamState_m##id ); \
   284 	\
   285 	struct TestClass##id \
   286 	{ \
   287 		static bool ms_DoRecall; \
   288 		\
   289 		virtual ret_type Func(@[$2,1,$1|, :param$2 p$2@]) \
   290 		{ \
   291 			g_Inside_LeafFunc = true; \
   292 			ADD_STATE(State_Func##id(this, ParamState_m##id(@[$2,1,$1|, :p$2@]))); \
   293 			g_Inside_LeafFunc = false; \
   294 			\
   295 			return MakeRet< ret_type >::Do(0); \
   296 		} \
   297 		\
   298 		struct Delegate1 : public MyDelegate \
   299 		{ \
   300 			virtual ret_type Call(@[$2,1,$1|, :param$2 p$2@]) \
   301 			{ \
   302 				g_Inside_LeafFunc = true; \
   303 				ADD_STATE(State_Deleg1_##id(META_IFACEPTR(TestClass##id), PtrBuf(this), ParamState_m##id(@[$2,1,$1|, :p$2@]))); \
   304 				g_Inside_LeafFunc = false; \
   305 				if (ms_DoRecall) \
   306 				{ \
   307 					@[$2,1,$1:Increment<StripRef< param$2 >::type>::Incr(p$2);@] \
   308 					RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, MakeRet< ret_type >::Do(1), &TestClass##id::Func, (@[$2,1,$1|, :p$2@])); \
   309 				} \
   310 				else \
   311 					RETURN_META_VALUE(MRES_IGNORED, MakeRet< ret_type >::Do(1)); \
   312 			} \
   313 		}; \
   314 		struct Delegate2 : public MyDelegate \
   315 		{ \
   316 			virtual ret_type Call(@[$2,1,$1|, :param$2 p$2@]) \
   317 			{ \
   318 				g_Inside_LeafFunc = true; \
   319 				ADD_STATE(State_Deleg2_##id(META_IFACEPTR(TestClass##id), PtrBuf(this), ParamState_m##id(@[$2,1,$1|, :p$2@]))); \
   320 				g_Inside_LeafFunc = false; \
   321 				if (ms_DoRecall) \
   322 				{ \
   323 					@[$2,1,$1:Increment<StripRef< param$2 >::type>::Incr(p$2);@] \
   324 					RETURN_META_VALUE_NEWPARAMS(MRES_SUPERCEDE, MakeRet< ret_type >::Do(2), &TestClass##id::Func, (@[$2,1,$1|, :p$2@])); \
   325 				} \
   326 				else \
   327 					RETURN_META_VALUE(MRES_SUPERCEDE, MakeRet< ret_type >::Do(2)); \
   328 			} \
   329 		}; \
   330 		struct Delegate3 : public MyDelegate \
   331 		{ \
   332 			virtual ret_type Call(@[$2,1,$1|, :param$2 p$2@]) \
   333 			{ \
   334 				g_Inside_LeafFunc = true; \
   335 				ADD_STATE(State_Deleg3_##id(META_IFACEPTR(TestClass##id), PtrBuf(this), ParamState_m##id(@[$2,1,$1|, :p$2@]))); \
   336 				g_Inside_LeafFunc = false; \
   337 				if (ms_DoRecall) \
   338 				{ \
   339 					@[$2,1,$1:Increment<StripRef< param$2 >::type>::Incr(p$2);@] \
   340 					RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, MakeRet< ret_type >::Do(3), &TestClass##id::Func, (@[$2,1,$1|, :p$2@])); \
   341 				} \
   342 				else \
   343 					RETURN_META_VALUE(MRES_IGNORED, MakeRet< ret_type >::Do(3)); \
   344 			} \
   345 		}; \
   346 		struct Delegate4 : public MyDelegate \
   347 		{ \
   348 			virtual ret_type Call(@[$2,1,$1|, :param$2 p$2@]) \
   349 			{ \
   350 				g_Inside_LeafFunc = true; \
   351 				ADD_STATE(State_Deleg4_##id(META_IFACEPTR(TestClass##id), PtrBuf(this), ParamState_m##id(@[$2,1,$1|, :p$2@]))); \
   352 				g_Inside_LeafFunc = false; \
   353 				if (ms_DoRecall) \
   354 				{ \
   355 					@[$2,1,$1:Increment<StripRef< param$2 >::type>::Incr(p$2);@] \
   356 					RETURN_META_VALUE_NEWPARAMS(MRES_SUPERCEDE, MakeRet< ret_type >::Do(4), &TestClass##id::Func, (@[$2,1,$1|, :p$2@])); \
   357 				} \
   358 				else \
   359 					RETURN_META_VALUE(MRES_SUPERCEDE, MakeRet< ret_type >::Do(4)); \
   360 			}; \
   361 		}; \
   362 	}; \
   363 	\
   364 	bool TestClass##id::ms_DoRecall = false; \
   365 	SourceHook::CProtoInfoBuilder protoinfo_##id(SourceHook::ProtoInfo::CallConv_ThisCall);
   366 	
   367 #define THGM_SETUP_PI$1(id@[$2,1,$1:, p$2_type, p$2_passtype, p$2_flags@]) \
   368 	void setuppi_##id() \
   369 	{ \
   370 		@[$2,1,$1: \
   371 		protoinfo_##id.AddParam(sizeof(p$2_type), p$2_passtype, p$2_flags, \
   372 			(p$2_flags & SourceHook::PassInfo::PassFlag_OCtor) ? FindFuncAddr(&Ctor_Thunk<StripRef< p$2_type >::type>::NormalConstructor) : NULL, \
   373 			(p$2_flags & SourceHook::PassInfo::PassFlag_CCtor) ? FindFuncAddr(&Ctor_Thunk<StripRef< p$2_type >::type>::CopyConstructor) : NULL, \
   374 			(p$2_flags & SourceHook::PassInfo::PassFlag_ODtor) ? FindFuncAddr(&Ctor_Thunk<StripRef< p$2_type >::type>::Destructor) : NULL, \
   375 			(p$2_flags & SourceHook::PassInfo::PassFlag_AssignOp) ? FindFuncAddr(&Ctor_Thunk<StripRef< p$2_type >::type>::AssignOp) : NULL \
   376 			); \
   377 		@] \
   378 	}
   379 	
   380 @]
   381 
   382 #define THGM_SETUP_RI(id, ret_type, ret_passtype, ret_flags) \
   383 	void setupri_##id() \
   384 	{ \
   385 		protoinfo_##id.SetReturnType(sizeof(ret_type), ret_passtype, ret_flags, \
   386 			(ret_flags & SourceHook::PassInfo::PassFlag_OCtor) ? FindFuncAddr(&Ctor_Thunk<StripRef< ret_type >::type>::NormalConstructor) : NULL, \
   387 			(ret_flags & SourceHook::PassInfo::PassFlag_CCtor) ? FindFuncAddr(&Ctor_Thunk<StripRef< ret_type >::type>::CopyConstructor) : NULL, \
   388 			(ret_flags & SourceHook::PassInfo::PassFlag_ODtor) ? FindFuncAddr(&Ctor_Thunk<StripRef< ret_type >::type>::Destructor) : NULL, \
   389 			(ret_flags & SourceHook::PassInfo::PassFlag_AssignOp) ? FindFuncAddr(&Ctor_Thunk<StripRef< ret_type >::type>::AssignOp) : NULL \
   390 			); \
   391 	}
   392 	
   393 #define THGM_ADD_HOOK(id, num) \
   394 	CAT4(hook, num, _, id) = g_SHPtr->AddHook(g_PLID, SourceHook::ISourceHook::Hook_Normal, reinterpret_cast<void*>(pTest##id), \
   395 		0, myhookman##id, PtrBufPtr(new TestClass##id::Delegate##num), num >= 3);
   396 
   397 #define THGM_REMOVE_HOOK(id, num) \
   398 	g_SHPtr->RemoveHookByID(CAT4(hook, num, _, id));
   399 
   400 #define THGM_CALLS_void(id, call_params) \
   401 	pTest##id->Func call_params; \
   402 	SH_CALL(pTest##id, &TestClass##id::Func) call_params;
   403 
   404 #define THGM_DO_TEST_void(id, call_params) \
   405 	setuppi_##id(); \
   406 	SourceHook::HookManagerPubFunc myhookman##id = g_HMAGPtr->MakeHookMan(protoinfo_##id, 0, 0); \
   407 	CAutoReleaseHookMan arhm_##id(myhookman##id); \
   408 	int hook1_##id, hook2_##id, hook3_##id, hook4_##id; \
   409 	\
   410 	TestClass##id::ms_DoRecall = false; \
   411 	TestClass##id *pTest##id = new TestClass##id; \
   412 	CAutoPtrDestruction<TestClass##id> apd##id(pTest##id); \
   413 	\
   414 			/* no hooks - no hooks */ \
   415 	PtrBuf_Clear(); \
   416 	THGM_CALLS_void(id, call_params); \
   417 	CHECK_STATES((&g_States, \
   418 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   419 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   420 		NULL), "Test" #id " Part1"); \
   421 	\
   422 			/* hook1 - no hooks */ \
   423 	PtrBuf_Clear(); \
   424 	THGM_ADD_HOOK(id, 1); \
   425 	THGM_CALLS_void(id, call_params); \
   426 	CHECK_STATES((&g_States, \
   427 		new State_Deleg1_##id(pTest##id, 0, ParamState_m##id call_params), \
   428 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   429 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   430 		NULL), "Test" #id " Part2"); \
   431 	THGM_REMOVE_HOOK(id, 1); \
   432 	\
   433 			/* no hooks - hook3 */ \
   434 	PtrBuf_Clear(); \
   435 	THGM_ADD_HOOK(id, 3); \
   436 	THGM_CALLS_void(id, call_params); \
   437 	CHECK_STATES((&g_States, \
   438 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   439 		new State_Deleg3_##id(pTest##id, 0, ParamState_m##id call_params), \
   440 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   441 		NULL), "Test" #id " Part3"); \
   442 	THGM_REMOVE_HOOK(id, 3); \
   443 	\
   444 			/* hook1 - hook3 */ \
   445 	PtrBuf_Clear(); \
   446 	THGM_ADD_HOOK(id, 1); \
   447 	THGM_ADD_HOOK(id, 3); \
   448 	THGM_CALLS_void(id, call_params); \
   449 	CHECK_STATES((&g_States, \
   450 		new State_Deleg1_##id(pTest##id, 0, ParamState_m##id call_params), \
   451 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   452 		new State_Deleg3_##id(pTest##id, 1, ParamState_m##id call_params), \
   453 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   454 		NULL), "Test" #id " Part4"); \
   455 	THGM_REMOVE_HOOK(id, 1); \
   456 	THGM_REMOVE_HOOK(id, 3); \
   457 	\
   458 			/* hook1, hook2 - hook3 */ \
   459 	PtrBuf_Clear(); \
   460 	THGM_ADD_HOOK(id, 1); \
   461 	THGM_ADD_HOOK(id, 2); \
   462 	THGM_ADD_HOOK(id, 3); \
   463 	THGM_CALLS_void(id, call_params); \
   464 	CHECK_STATES((&g_States, \
   465 		new State_Deleg1_##id(pTest##id, 0, ParamState_m##id call_params), \
   466 		new State_Deleg2_##id(pTest##id, 1, ParamState_m##id call_params), \
   467 		new State_Deleg3_##id(pTest##id, 2, ParamState_m##id call_params), \
   468 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   469 		NULL), "Test" #id " Part5"); \
   470 	THGM_REMOVE_HOOK(id, 1); \
   471 	THGM_REMOVE_HOOK(id, 2); \
   472 	THGM_REMOVE_HOOK(id, 3); \
   473 			/* hook1, hook2 - hook3, hook4 */ \
   474 	PtrBuf_Clear(); \
   475 	THGM_ADD_HOOK(id, 1); \
   476 	THGM_ADD_HOOK(id, 2); \
   477 	THGM_ADD_HOOK(id, 3); \
   478 	THGM_ADD_HOOK(id, 4); \
   479 	THGM_CALLS_void(id, call_params); \
   480 	CHECK_STATES((&g_States, \
   481 		new State_Deleg1_##id(pTest##id, 0, ParamState_m##id call_params), \
   482 		new State_Deleg2_##id(pTest##id, 1, ParamState_m##id call_params), \
   483 		new State_Deleg3_##id(pTest##id, 2, ParamState_m##id call_params), \
   484 		new State_Deleg4_##id(pTest##id, 3, ParamState_m##id call_params), \
   485 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   486 		NULL), "Test" #id " Part6"); \
   487 		\
   488 			/* hook1 - hook3, hook4, with recalls! */ \
   489 		\
   490 	TestClass##id::ms_DoRecall = true; \
   491 	THGM_REMOVE_HOOK(id, 2); \
   492 	THGM_CALLS_void(id, call_params); \
   493 	CHECK_STATES((&g_States, \
   494 		new State_Deleg1_##id(pTest##id, 0, ParamState_m##id call_params(0)), \
   495 		new State_Func##id(pTest##id, ParamState_m##id call_params(1)), \
   496 		new State_Deleg3_##id(pTest##id, 2, ParamState_m##id call_params(1)), \
   497 		new State_Deleg4_##id(pTest##id, 3, ParamState_m##id call_params(2)), \
   498 		/* sh_call one */ \
   499 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   500 		NULL), "Test" #id " Part7"); \
   501 	THGM_REMOVE_HOOK(id, 1); \
   502 	THGM_REMOVE_HOOK(id, 3); \
   503 	THGM_REMOVE_HOOK(id, 4);
   504 
   505 template<class T>
   506 T ComparableRef(T x)
   507 {
   508 	return x;
   509 }
   510 
   511 template <class T>
   512 T* ComparableRef(T& x)
   513 {
   514 	return &x;
   515 }
   516 
   517 #define THGM_CALLS(id, call_params, exp_ret_norm, exp_ret_shcall, err) \
   518 	CHECK_COND(ComparableRef<RetType##id>(pTest##id->Func call_params) == ComparableRef<RetType##id>(MakeRet< RetType##id >::Do(exp_ret_norm)), err " /retcallnorm"); \
   519 	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");
   520 
   521 #define THGM_DO_TEST(id, call_params) \
   522 	setuppi_##id(); \
   523 	setupri_##id(); \
   524 	SourceHook::HookManagerPubFunc myhookman##id = g_HMAGPtr->MakeHookMan(protoinfo_##id, 0, 0); \
   525 	CAutoReleaseHookMan arhm_##id(myhookman##id); \
   526 	int hook1_##id, hook2_##id, hook3_##id, hook4_##id; \
   527 	\
   528 	TestClass##id::ms_DoRecall = false; \
   529 	TestClass##id *pTest##id = new TestClass##id; \
   530 	CAutoPtrDestruction<TestClass##id> apd##id(pTest##id); \
   531 	\
   532 			/* no hooks - no hooks */ \
   533 	PtrBuf_Clear(); \
   534 	THGM_CALLS(id, call_params, 0, 0, "Part1"); \
   535 	CHECK_STATES((&g_States, \
   536 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   537 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   538 		NULL), "Test" #id " Part1"); \
   539 	\
   540 			/* hook1 - no hooks */ \
   541 	PtrBuf_Clear(); \
   542 	THGM_ADD_HOOK(id, 1); \
   543 	THGM_CALLS(id, call_params, 0, 0, "Part2"); \
   544 	CHECK_STATES((&g_States, \
   545 		new State_Deleg1_##id(pTest##id, 0, ParamState_m##id call_params), \
   546 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   547 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   548 		NULL), "Test" #id " Part2"); \
   549 	THGM_REMOVE_HOOK(id, 1); \
   550 	\
   551 			/* no hooks - hook3 */ \
   552 	PtrBuf_Clear(); \
   553 	THGM_ADD_HOOK(id, 3); \
   554 	THGM_CALLS(id, call_params, 0, 0, "Part3"); \
   555 	CHECK_STATES((&g_States, \
   556 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   557 		new State_Deleg3_##id(pTest##id, 0, ParamState_m##id call_params), \
   558 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   559 		NULL), "Test" #id " Part3"); \
   560 	THGM_REMOVE_HOOK(id, 3); \
   561 	\
   562 			/* hook1 - hook3 */ \
   563 	PtrBuf_Clear(); \
   564 	THGM_ADD_HOOK(id, 1); \
   565 	THGM_ADD_HOOK(id, 3); \
   566 	THGM_CALLS(id, call_params, 0, 0, "Part4"); \
   567 	CHECK_STATES((&g_States, \
   568 		new State_Deleg1_##id(pTest##id, 0, ParamState_m##id call_params), \
   569 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   570 		new State_Deleg3_##id(pTest##id, 1, ParamState_m##id call_params), \
   571 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   572 		NULL), "Test" #id " Part4"); \
   573 	THGM_REMOVE_HOOK(id, 1); \
   574 	THGM_REMOVE_HOOK(id, 3); \
   575 	\
   576 			/* hook1, hook2 - hook3 */ \
   577 	PtrBuf_Clear(); \
   578 	THGM_ADD_HOOK(id, 1); \
   579 	THGM_ADD_HOOK(id, 2); \
   580 	THGM_ADD_HOOK(id, 3); \
   581 	THGM_CALLS(id, call_params, 2, 0, "Part5"); \
   582 	CHECK_STATES((&g_States, \
   583 		new State_Deleg1_##id(pTest##id, 0, ParamState_m##id call_params), \
   584 		new State_Deleg2_##id(pTest##id, 1, ParamState_m##id call_params), \
   585 		new State_Deleg3_##id(pTest##id, 2, ParamState_m##id call_params), \
   586 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   587 		NULL), "Test" #id " Part5"); \
   588 	THGM_REMOVE_HOOK(id, 1); \
   589 	THGM_REMOVE_HOOK(id, 2); \
   590 	THGM_REMOVE_HOOK(id, 3); \
   591 			/* hook1, hook2 - hook3, hook4 */ \
   592 	PtrBuf_Clear(); \
   593 	THGM_ADD_HOOK(id, 1); \
   594 	THGM_ADD_HOOK(id, 2); \
   595 	THGM_ADD_HOOK(id, 3); \
   596 	THGM_ADD_HOOK(id, 4); \
   597 	THGM_CALLS(id, call_params, 4, 0, "Part6"); \
   598 	CHECK_STATES((&g_States, \
   599 		new State_Deleg1_##id(pTest##id, 0, ParamState_m##id call_params), \
   600 		new State_Deleg2_##id(pTest##id, 1, ParamState_m##id call_params), \
   601 		new State_Deleg3_##id(pTest##id, 2, ParamState_m##id call_params), \
   602 		new State_Deleg4_##id(pTest##id, 3, ParamState_m##id call_params), \
   603 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   604 		NULL), "Test" #id " Part6"); \
   605 		\
   606 			/* hook1 - hook3, hook4, with recalls! */ \
   607 		\
   608 	TestClass##id::ms_DoRecall = true; \
   609 	THGM_REMOVE_HOOK(id, 2); \
   610 	THGM_CALLS(id, call_params, 4, 0, "Part7"); \
   611 	CHECK_STATES((&g_States, \
   612 		new State_Deleg1_##id(pTest##id, 0, ParamState_m##id call_params(0)), \
   613 		new State_Func##id(pTest##id, ParamState_m##id call_params(1)), \
   614 		new State_Deleg3_##id(pTest##id, 2, ParamState_m##id call_params(1)), \
   615 		new State_Deleg4_##id(pTest##id, 3, ParamState_m##id call_params(2)), \
   616 		/* sh_call one */ \
   617 		new State_Func##id(pTest##id, ParamState_m##id call_params), \
   618 		NULL), "Test" #id " Part7"); \
   619 	THGM_REMOVE_HOOK(id, 1); \
   620 	THGM_REMOVE_HOOK(id, 3); \
   621 	THGM_REMOVE_HOOK(id, 4);