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