sourcehook/test/testhookmangen.cpp
branchhookman_autogen
changeset 437 89cf494f6088
parent 436 41f21d77c3c1
child 438 bafcc3c3538f
     1.1 --- a/sourcehook/test/testhookmangen.cpp	Thu Nov 01 17:29:39 2007 +0000
     1.2 +++ b/sourcehook/test/testhookmangen.cpp	Fri Nov 02 00:53:32 2007 +0000
     1.3 @@ -41,7 +41,7 @@
     1.4  	};
     1.5  
     1.6  	template <int MYSIZE>
     1.7 -	std::ostream& operator <<(std::ostream &os, const POD<MYSIZE> &obj)
     1.8 +		std::ostream& operator <<(std::ostream &os, const POD<MYSIZE> &obj)
     1.9  	{
    1.10  		os << "Some POD!";
    1.11  		return os;
    1.12 @@ -50,6 +50,7 @@
    1.13  	MAKE_STATE_1(State_ObjOCtor_Called, int /*MYSIZE*/);
    1.14  	MAKE_STATE_1(State_ObjCCtor_Called, int /*MYSIZE*/);
    1.15  	MAKE_STATE_1(State_ObjODtor_Called, int /*MYSIZE*/);
    1.16 +	MAKE_STATE_1(State_ObjAssignOp_Called, int /*MYSIZE*/);
    1.17  
    1.18  	template <int MYSIZE>
    1.19  	struct Object
    1.20 @@ -82,6 +83,16 @@
    1.21  				ADD_STATE(State_ObjODtor_Called(MYSIZE));
    1.22  		}
    1.23  
    1.24 +		Object & operator = (const Object &other)
    1.25 +		{
    1.26 +			if (!g_Inside_LeafFunc)
    1.27 +				ADD_STATE(State_ObjAssignOp_Called(MYSIZE));
    1.28 +
    1.29 +			memcpy(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE);
    1.30 +
    1.31 +			return *this;
    1.32 +		}
    1.33 +
    1.34  		bool operator==(const Object<MYSIZE> &other)
    1.35  		{
    1.36  			return memcmp(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE) == 0;
    1.37 @@ -95,6 +106,51 @@
    1.38  		return os;
    1.39  	}
    1.40  
    1.41 +	// Because of some weird bug in MSVC < 1400
    1.42 +#define MAKE_PODRET(size) \
    1.43 +	struct PodRet##size \
    1.44 +	{ \
    1.45 +		POD<size> actPod; \
    1.46 +		bool operator==(const PodRet##size &other) { return actPod == other.actPod; } \
    1.47 +		bool operator==(char other) { return actPod == other; } \
    1.48 +	}; \
    1.49 +	std::ostream& operator <<(std::ostream &os, const PodRet##size &obj) \
    1.50 +	{ \
    1.51 +		os << obj.actPod; \
    1.52 +		return os; \
    1.53 +	} \
    1.54 +	template <> struct MakeRet< PodRet##size > \
    1.55 +	{ \
    1.56 +		static PodRet##size Do(int a) \
    1.57 +		{ \
    1.58 +			PodRet##size x; \
    1.59 +			memset(reinterpret_cast<void*>(x.actPod.x), a, size); \
    1.60 +			return x; \
    1.61 +		} \
    1.62 +	};
    1.63 +
    1.64 +#define MAKE_OBJRET(size) \
    1.65 +	struct ObjRet##size \
    1.66 +	{ \
    1.67 +		Object<size> actObj; \
    1.68 +		bool operator==(const ObjRet##size &other) { return actObj == other.actObj; } \
    1.69 +		bool operator==(char other) { return actObj == other; } \
    1.70 +	}; \
    1.71 +	std::ostream& operator <<(std::ostream &os, const ObjRet##size &obj) \
    1.72 +	{ \
    1.73 +		os << obj.actObj; \
    1.74 +		return os; \
    1.75 +	} \
    1.76 +	template <> struct MakeRet< ObjRet##size > \
    1.77 +	{ \
    1.78 +		static ObjRet##size Do(int a) \
    1.79 +		{ \
    1.80 +			ObjRet##size x; \
    1.81 +			memset(reinterpret_cast<void*>(x.actObj.x), a, size); \
    1.82 +			return x; \
    1.83 +		} \
    1.84 +	};
    1.85 +
    1.86  	#include "testhookmangen.h"
    1.87  
    1.88  	StateList g_States;
    1.89 @@ -279,29 +335,43 @@
    1.90  	THGM_SETUP_RI(105, double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
    1.91  
    1.92  	// pod 1-13
    1.93 -	THGM_MAKE_TEST1(106, POD<1>, int);
    1.94 +	MAKE_PODRET(1);
    1.95 +	THGM_MAKE_TEST1(106, PodRet1, int);
    1.96  	THGM_SETUP_PI1(106,
    1.97  		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
    1.98  		);
    1.99 -	THGM_SETUP_RI(106, POD<1>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   1.100 +	THGM_SETUP_RI(106, PodRet1, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   1.101  
   1.102 -	THGM_MAKE_TEST1(107, POD<4>, int);
   1.103 +	MAKE_PODRET(4);
   1.104 +	THGM_MAKE_TEST1(107, PodRet4, int);
   1.105  	THGM_SETUP_PI1(107,
   1.106  		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   1.107  		);
   1.108 -	THGM_SETUP_RI(107, POD<4>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   1.109 +	THGM_SETUP_RI(107, PodRet4, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   1.110  
   1.111 -	THGM_MAKE_TEST1(108, POD<8>, int);
   1.112 +	MAKE_PODRET(8);
   1.113 +	THGM_MAKE_TEST1(108, PodRet8, int);
   1.114  	THGM_SETUP_PI1(108,
   1.115  		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   1.116  		);
   1.117 -	THGM_SETUP_RI(108, POD<8>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   1.118 +	THGM_SETUP_RI(108, PodRet8, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   1.119  
   1.120 -	THGM_MAKE_TEST1(109, POD<13>, int);
   1.121 +	MAKE_PODRET(13);
   1.122 +	THGM_MAKE_TEST1(109, PodRet13, int);
   1.123  	THGM_SETUP_PI1(109,
   1.124  		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   1.125  		);
   1.126 -	THGM_SETUP_RI(109, POD<13>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   1.127 +	THGM_SETUP_RI(109, PodRet13, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   1.128 +
   1.129 +	MAKE_OBJRET(13);
   1.130 +	THGM_MAKE_TEST1(110, ObjRet13, int);
   1.131 +	THGM_SETUP_PI1(110,
   1.132 +		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   1.133 +		);
   1.134 +	THGM_SETUP_RI(110, ObjRet13, SourceHook::PassInfo::PassType_Object,
   1.135 +		SourceHook::PassInfo::PassFlag_ByVal | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor |
   1.136 +		SourceHook::PassInfo::PassFlag_CCtor | SourceHook::PassInfo::PassFlag_AssignOp);
   1.137 +
   1.138  }
   1.139  
   1.140  bool TestHookManGen(std::string &error)
   1.141 @@ -502,11 +572,196 @@
   1.142  	THGM_DO_TEST(108, (5));
   1.143  	THGM_DO_TEST(109, (5));
   1.144  
   1.145 +	// Test110: Special: constructors/destructors on return
   1.146 +	PtrBuf_Clear();
   1.147 +	ObjRet13 obj13;
   1.148 +
   1.149 +	CHECK_STATES((&g_States,
   1.150 +		new State_ObjOCtor_Called(13),
   1.151 +		NULL), "Test110 Part0");
   1.152 +
   1.153 +	setuppi_110();
   1.154 +	setupri_110();
   1.155 +
   1.156 +	g_Genc110 = new SourceHook::Impl::GenContext(&protoinfo_110, 0, 0, g_SHPtr);
   1.157 +	g_Genc_ad110.set(g_Genc110);
   1.158 +	SourceHook::HookManagerPubFunc myhookman110 = g_Genc110->Generate();
   1.159 +	int hook1_110, hook2_110, hook3_110, hook4_110;
   1.160 +
   1.161 +	TestClass110 *pTest110 = new TestClass110;
   1.162 +	CAutoPtrDestruction<TestClass110> apd110(pTest110);
   1.163 +
   1.164 +	/* no hooks - no hooks */
   1.165 +	PtrBuf_Clear();
   1.166 +	obj13 = pTest110->Func(5);
   1.167 +
   1.168 +	g_Inside_LeafFunc = true;
   1.169 +	CHECK_STATES((&g_States,
   1.170 +		new State_Func110(pTest110, ParamState_m110 (5)),
   1.171 +		new State_ObjOCtor_Called(13),			// MakeRet: Construction of x
   1.172 +		new State_ObjCCtor_Called(13),			// Return from MakeRet	-> construct temporary object in our stack
   1.173 +		new State_ObjODtor_Called(13),			// MakeRet: Destruction of x
   1.174 +
   1.175 +		new State_ObjAssignOp_Called(13),		// assign: obj13 = temporary object in our stack
   1.176 +		new State_ObjODtor_Called(13),			// Func110: destruction of temporary object
   1.177 +		NULL), "Test" "110" " Part1");
   1.178 +	g_Inside_LeafFunc = false;
   1.179 +
   1.180 +	/* hook1 - no hooks */
   1.181 +	THGM_ADD_HOOK(110, 1);
   1.182 +
   1.183 +	PtrBuf_Clear();
   1.184 +	obj13 = pTest110->Func(5);
   1.185 +	g_Inside_LeafFunc = true;
   1.186 +	CHECK_STATES((&g_States,
   1.187 +		// HookFunc: construct orig_ret/override_ret/plugin_ret objects
   1.188 +		new State_ObjOCtor_Called(13),
   1.189 +		new State_ObjOCtor_Called(13),
   1.190 +		new State_ObjOCtor_Called(13),
   1.191 +
   1.192 +		// Calling delegate
   1.193 +			new State_Deleg1_110(pTest110, 0, ParamState_m110 (5)),
   1.194 +
   1.195 +			new State_ObjOCtor_Called(13),	// MakeRet: Construction of x
   1.196 +			new State_ObjCCtor_Called(13),	// Return from MakeRet	-> construct temporary object in HookFunc's stack
   1.197 +			new State_ObjODtor_Called(13),	// MakeRet: Destruction of x
   1.198 +
   1.199 +		// back in hookfunc
   1.200 +		new State_ObjAssignOp_Called(13),	// assign: plugin_ret = temporary object in HookFunc's stack
   1.201 +		new State_ObjODtor_Called(13),		// destruction of temporary object in HookFunc's stack
   1.202 +
   1.203 +		// Calling orig function Func110
   1.204 +			new State_Func110(pTest110, ParamState_m110 (5)),
   1.205 +			new State_ObjOCtor_Called(13),	// MakeRet: Construction of x
   1.206 +			new State_ObjCCtor_Called(13),	// Return from MakeRet	-> construct temporary object in HookFunc's stack
   1.207 +			new State_ObjODtor_Called(13),	// MakeRet: Destruction of x
   1.208 +
   1.209 +		// back in hookfunc
   1.210 +		new State_ObjAssignOp_Called(13),	// assign: orig_ret = temporary object in HookFunc's stack
   1.211 +		new State_ObjODtor_Called(13),		// destruction of temporary object in HookFunc's stack
   1.212 +
   1.213 +		// hookfunc is returning:
   1.214 +		new State_ObjCCtor_Called(13),		// copy to temp object in our stack
   1.215 +
   1.216 +		// hookfunc cleans up its stack -> destroys plugin_ret/override_ret/orig_ret
   1.217 +		new State_ObjODtor_Called(13),
   1.218 +		new State_ObjODtor_Called(13),
   1.219 +		new State_ObjODtor_Called(13),
   1.220 +		
   1.221 +		// we are in our function: assign 
   1.222 +		new State_ObjAssignOp_Called(13),	// assign: obj13 = temporary object in our stack
   1.223 +		new State_ObjODtor_Called(13),		// Func110: destruction of temporary object
   1.224 +
   1.225 +		NULL), "Test" "11" " Part2");
   1.226 +	
   1.227 +	CHECK_COND(obj13 == 0, "Test" "11" " Part 2.1");
   1.228 +	g_Inside_LeafFunc = false;
   1.229 +
   1.230 +	THGM_REMOVE_HOOK(11, 1);
   1.231 +
   1.232 +	/* hook1, hook2 - hook3, hook4 */
   1.233 +	THGM_ADD_HOOK(110, 1);
   1.234 +	THGM_ADD_HOOK(110, 2);
   1.235 +	THGM_ADD_HOOK(110, 3);
   1.236 +	THGM_ADD_HOOK(110, 4);
   1.237 +
   1.238 +	PtrBuf_Clear();
   1.239 +	obj13 = pTest110->Func(5);
   1.240 +	g_Inside_LeafFunc = true;
   1.241 +	CHECK_STATES((&g_States,
   1.242 +		// HookFunc: construct orig_ret/override_ret/plugin_ret objects
   1.243 +		new State_ObjOCtor_Called(13),
   1.244 +		new State_ObjOCtor_Called(13),
   1.245 +		new State_ObjOCtor_Called(13),
   1.246 +
   1.247 +		// Calling delegate1
   1.248 +			new State_Deleg1_110(pTest110, 0, ParamState_m110 (5)),
   1.249 +
   1.250 +			new State_ObjOCtor_Called(13),	// MakeRet: Construction of x
   1.251 +			new State_ObjCCtor_Called(13),	// Return from MakeRet	-> construct temporary object in HookFunc's stack
   1.252 +			new State_ObjODtor_Called(13),	// MakeRet: Destruction of x
   1.253 +
   1.254 +		// back in hookfunc
   1.255 +		new State_ObjAssignOp_Called(13),	// assign: plugin_ret = temporary object in HookFunc's stack
   1.256 +		new State_ObjODtor_Called(13),		// destruction of temporary object in HookFunc's stack
   1.257 +
   1.258 +
   1.259 +		// Calling delegate2
   1.260 +			new State_Deleg2_110(pTest110, 1, ParamState_m110 (5)),
   1.261 +
   1.262 +			new State_ObjOCtor_Called(13),	// MakeRet: Construction of x
   1.263 +			new State_ObjCCtor_Called(13),	// Return from MakeRet	-> construct temporary object in HookFunc's stack
   1.264 +			new State_ObjODtor_Called(13),	// MakeRet: Destruction of x
   1.265 +
   1.266 +		// back in hookfunc
   1.267 +		new State_ObjAssignOp_Called(13),	// assign: plugin_ret = temporary object in HookFunc's stack
   1.268 +		new State_ObjODtor_Called(13),		// destruction of temporary object in HookFunc's stack
   1.269 +
   1.270 +		// hookfunc finds out that the hook wanted to SUPERCEDE --> copy to override_Ret
   1.271 +		new State_ObjAssignOp_Called(13),
   1.272 +
   1.273 +		// SUPERCEDE -> orig function is not called
   1.274 +		// instead: orig_ret = override_ret
   1.275 +		new State_ObjAssignOp_Called(13),
   1.276 +
   1.277 +		// Calling delegate3
   1.278 +			new State_Deleg3_110(pTest110, 2, ParamState_m110 (5)),
   1.279 +
   1.280 +			new State_ObjOCtor_Called(13),	// MakeRet: Construction of x
   1.281 +			new State_ObjCCtor_Called(13),	// Return from MakeRet	-> construct temporary object in HookFunc's stack
   1.282 +			new State_ObjODtor_Called(13),	// MakeRet: Destruction of x
   1.283 +
   1.284 +		// back in hookfunc
   1.285 +		new State_ObjAssignOp_Called(13),	// assign: plugin_ret = temporary object in HookFunc's stack
   1.286 +		new State_ObjODtor_Called(13),		// destruction of temporary object in HookFunc's stack
   1.287 +
   1.288 +		// Calling delegate4
   1.289 +			new State_Deleg4_110(pTest110, 3, ParamState_m110 (5)),
   1.290 +
   1.291 +			new State_ObjOCtor_Called(13),	// MakeRet: Construction of x
   1.292 +			new State_ObjCCtor_Called(13),	// Return from MakeRet	-> construct temporary object in HookFunc's stack
   1.293 +			new State_ObjODtor_Called(13),	// MakeRet: Destruction of x
   1.294 +
   1.295 +		// back in hookfunc
   1.296 +		new State_ObjAssignOp_Called(13),	// assign: plugin_ret = temporary object in HookFunc's stack
   1.297 +		new State_ObjODtor_Called(13),		// destruction of temporary object in HookFunc's stack
   1.298 +
   1.299 +		// hookfunc finds out that the hook wanted to SUPERCEDE --> copy to override_Ret (yes really, we overwrite the old value!)
   1.300 +		new State_ObjAssignOp_Called(13),
   1.301 +
   1.302 +		// hookfunc is returning:
   1.303 +		new State_ObjCCtor_Called(13),		// copy to temp object in our stack
   1.304 +
   1.305 +		// hookfunc cleans up its stack -> destroys plugin_ret/override_ret/orig_ret
   1.306 +		new State_ObjODtor_Called(13),
   1.307 +		new State_ObjODtor_Called(13),
   1.308 +		new State_ObjODtor_Called(13),
   1.309 +
   1.310 +		// we are in our function: assign 
   1.311 +		new State_ObjAssignOp_Called(13),	// assign: obj13 = temporary object in our stack
   1.312 +		new State_ObjODtor_Called(13),		// Func110: destruction of temporary object
   1.313 +
   1.314 +		NULL), "Test" "11" " Part3");
   1.315 +
   1.316 +	CHECK_COND(obj13 == 4, "Test" "11" " Part 3.1");
   1.317 +	g_Inside_LeafFunc = false;
   1.318 +
   1.319 +	THGM_REMOVE_HOOK(110, 1);
   1.320 +	THGM_REMOVE_HOOK(110, 2);
   1.321 +	THGM_REMOVE_HOOK(110, 3);
   1.322 +	THGM_REMOVE_HOOK(110, 4);
   1.323 +
   1.324  	// Shutdown now!
   1.325  	// If we don't SH will auto-shutdown _after_ genc's destructor is called
   1.326  	//  -> crash
   1.327  
   1.328  	Test_CompleteShutdown(g_SHPtr);
   1.329 +
   1.330 +	CHECK_COND(sizeof(PodRet1) == 1, "WTF!");
   1.331 +	CHECK_COND(sizeof(PodRet4) == 4, "WTF!");
   1.332 +	CHECK_COND(sizeof(PodRet8) == 8, "WTF!");
   1.333 +	CHECK_COND(sizeof(PodRet13) == 13, "WTF!");
   1.334 +
   1.335  	return true;
   1.336  }
   1.337