sourcehook/test/testvphooks.cpp
changeset 324 257f0837a885
child 328 3a04ef79edf1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/sourcehook/test/testvphooks.cpp	Mon May 07 18:35:59 2007 +0000
     1.3 @@ -0,0 +1,266 @@
     1.4 +#include <string>
     1.5 +#include "sourcehook.h"
     1.6 +#include "sourcehook_test.h"
     1.7 +#include "testevents.h"
     1.8 +
     1.9 +// TEST VP HOOKS
    1.10 +// Test vfnptr-wide hooks
    1.11 +
    1.12 +namespace
    1.13 +{
    1.14 +	StateList g_States;
    1.15 +	SourceHook::ISourceHook *g_SHPtr;
    1.16 +	SourceHook::Plugin g_PLID;
    1.17 +
    1.18 +	class IBase;
    1.19 +
    1.20 +	MAKE_STATE_1(State_D1_Func1, IBase *);
    1.21 +	MAKE_STATE_1(State_D2_Func1, IBase *);
    1.22 +	MAKE_STATE_1(State_Func1_Pre, IBase *);
    1.23 +	MAKE_STATE_1(State_Func1_Post, IBase *);
    1.24 +
    1.25 +	MAKE_STATE_1(State_D1_Func2, IBase *);
    1.26 +	MAKE_STATE_1(State_D2_Func2, IBase *);
    1.27 +	MAKE_STATE_1(State_Func2_Pre, IBase *);
    1.28 +	MAKE_STATE_1(State_Func2_Post, IBase *);
    1.29 +
    1.30 +	MAKE_STATE_2(State_D1_Func3, IBase *, int);
    1.31 +	MAKE_STATE_2(State_D2_Func3, IBase *, int);
    1.32 +	MAKE_STATE_2(State_Func3_Pre, IBase *, int);
    1.33 +	MAKE_STATE_2(State_Func3_Post, IBase *, int);
    1.34 +
    1.35 +	class IBase
    1.36 +	{
    1.37 +	public:
    1.38 +		virtual void Func1() = 0;
    1.39 +		virtual void Func2() = 0;
    1.40 +		virtual void Func3(int x) = 0;
    1.41 +	};
    1.42 +
    1.43 +	class CDerived1 : public IBase
    1.44 +	{
    1.45 +	public:
    1.46 +		virtual void Func1()
    1.47 +		{
    1.48 +			ADD_STATE(State_D1_Func1(this));
    1.49 +		}
    1.50 +		virtual void Func2()
    1.51 +		{
    1.52 +			ADD_STATE(State_D1_Func2(this));
    1.53 +		}
    1.54 +		virtual void Func3(int x)
    1.55 +		{
    1.56 +			ADD_STATE(State_D1_Func3(this, x));
    1.57 +		}
    1.58 +	};
    1.59 +
    1.60 +	class CDerived2 : public IBase
    1.61 +	{
    1.62 +	public:
    1.63 +		virtual void Func1()
    1.64 +		{
    1.65 +			ADD_STATE(State_D2_Func1(this));
    1.66 +		}
    1.67 +		virtual void Func2()
    1.68 +		{
    1.69 +			ADD_STATE(State_D2_Func2(this));
    1.70 +		}
    1.71 +		virtual void Func3(int x)
    1.72 +		{
    1.73 +			ADD_STATE(State_D2_Func3(this, x));
    1.74 +		}
    1.75 +	};
    1.76 +
    1.77 +	void Handler_Func1_Pre()
    1.78 +	{
    1.79 +		ADD_STATE(State_Func1_Pre(META_IFACEPTR(IBase)));
    1.80 +	}
    1.81 +	void Handler_Func1_Post()
    1.82 +	{
    1.83 +		ADD_STATE(State_Func1_Post(META_IFACEPTR(IBase)));
    1.84 +	}
    1.85 +	int g_F2_Pre_HookToRemove = 0;
    1.86 +	void Handler_Func2_Pre()
    1.87 +	{
    1.88 +		ADD_STATE(State_Func2_Pre(META_IFACEPTR(IBase)));
    1.89 +		SH_REMOVE_HOOK_ID(g_F2_Pre_HookToRemove);
    1.90 +	}
    1.91 +	void Handler_Func2_Post()
    1.92 +	{
    1.93 +		ADD_STATE(State_Func2_Post(META_IFACEPTR(IBase)));
    1.94 +	}
    1.95 +
    1.96 +
    1.97 +	void Handler_Func3_Pre(int x)
    1.98 +	{
    1.99 +		ADD_STATE(State_Func3_Pre(META_IFACEPTR(IBase), x));
   1.100 +
   1.101 +		RETURN_META_NEWPARAMS(MRES_IGNORED, &IBase::Func3, (x+1));
   1.102 +	}
   1.103 +	void Handler_Func3_Post(int x)
   1.104 +	{
   1.105 +		ADD_STATE(State_Func3_Post(META_IFACEPTR(IBase), x));
   1.106 +	}
   1.107 +
   1.108 +	SH_DECL_HOOK0_void(IBase, Func1, SH_NOATTRIB, 0);
   1.109 +	SH_DECL_HOOK0_void(IBase, Func2, SH_NOATTRIB, 0);
   1.110 +	SH_DECL_HOOK1_void(IBase, Func3, SH_NOATTRIB, 0, int);
   1.111 +
   1.112 +	SH_DECL_MANUALHOOK1_void(IBase_Func3_Manual, 2, 0, 0, int);
   1.113 +}
   1.114 +
   1.115 +bool TestVPHooks(std::string &error)
   1.116 +{
   1.117 +	GET_SHPTR(g_SHPtr);
   1.118 +	g_PLID = 1337;
   1.119 +
   1.120 +	CDerived1 d1i1;
   1.121 +	CDerived1 d1i2;
   1.122 +	CDerived2 d2i1;
   1.123 +
   1.124 +	IBase *p_d1i1 = &d1i1;
   1.125 +	IBase *p_d1i2 = &d1i2;
   1.126 +	IBase *p_d2i1 = &d2i1;
   1.127 +	
   1.128 +	int hook1 = SH_ADD_VPHOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
   1.129 +
   1.130 +	p_d1i1->Func1();
   1.131 +	p_d1i2->Func1();
   1.132 +	p_d2i1->Func1();
   1.133 +
   1.134 +	CHECK_STATES((&g_States,
   1.135 +		new State_Func1_Pre(p_d1i1),
   1.136 +		new State_D1_Func1(p_d1i1),
   1.137 +		
   1.138 +		new State_Func1_Pre(p_d1i2),
   1.139 +		new State_D1_Func1(p_d1i2),
   1.140 +
   1.141 +		new State_D2_Func1(p_d2i1),
   1.142 +		NULL), "Part 1");
   1.143 +
   1.144 +	SH_REMOVE_HOOK_ID(hook1);
   1.145 +
   1.146 +	p_d1i1->Func1();
   1.147 +	p_d1i2->Func1();
   1.148 +	p_d2i1->Func1();
   1.149 +
   1.150 +	CHECK_STATES((&g_States,
   1.151 +		new State_D1_Func1(p_d1i1),
   1.152 +
   1.153 +		new State_D1_Func1(p_d1i2),
   1.154 +
   1.155 +		new State_D2_Func1(p_d2i1),
   1.156 +		NULL), "Part 2");
   1.157 +
   1.158 +
   1.159 +
   1.160 +	// Normal hook, then vp hook
   1.161 +
   1.162 +	int hook2 = SH_ADD_HOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
   1.163 +	hook1 = SH_ADD_VPHOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
   1.164 +
   1.165 +	p_d1i1->Func1();
   1.166 +	p_d1i2->Func1();
   1.167 +	p_d2i1->Func1();
   1.168 +
   1.169 +	CHECK_STATES((&g_States,
   1.170 +		new State_Func1_Pre(p_d1i1),
   1.171 +		new State_Func1_Pre(p_d1i1),
   1.172 +		new State_D1_Func1(p_d1i1),
   1.173 +
   1.174 +		new State_Func1_Pre(p_d1i2),
   1.175 +		new State_D1_Func1(p_d1i2),
   1.176 +
   1.177 +		new State_D2_Func1(p_d2i1),
   1.178 +		NULL), "Part 3");
   1.179 +
   1.180 +	SH_REMOVE_HOOK_ID(hook1);
   1.181 +
   1.182 +	p_d1i1->Func1();
   1.183 +	p_d1i2->Func1();
   1.184 +	p_d2i1->Func1();
   1.185 +
   1.186 +	CHECK_STATES((&g_States,
   1.187 +		new State_Func1_Pre(p_d1i1),
   1.188 +		new State_D1_Func1(p_d1i1),
   1.189 +
   1.190 +		new State_D1_Func1(p_d1i2),
   1.191 +
   1.192 +		new State_D2_Func1(p_d2i1),
   1.193 +		NULL), "Part 4");
   1.194 +
   1.195 +	SH_REMOVE_HOOK_ID(hook2);
   1.196 +
   1.197 +	p_d1i1->Func1();
   1.198 +	p_d1i2->Func1();
   1.199 +	p_d2i1->Func1();
   1.200 +
   1.201 +	CHECK_STATES((&g_States,
   1.202 +		new State_D1_Func1(p_d1i1),
   1.203 +
   1.204 +		new State_D1_Func1(p_d1i2),
   1.205 +
   1.206 +		new State_D2_Func1(p_d2i1),
   1.207 +		NULL), "Part 5");
   1.208 +
   1.209 +	// Test this:
   1.210 +	// Normal hook AND vp hook on Func2
   1.211 +	// Func2's pre handler removes the VP hook. It has to work anyway :)
   1.212 +
   1.213 +	hook1 = SH_ADD_VPHOOK(IBase, Func2, p_d1i1, SH_STATIC(Handler_Func2_Pre), false);
   1.214 +	hook2 = SH_ADD_HOOK(IBase, Func2, p_d1i1, SH_STATIC(Handler_Func2_Pre), false);
   1.215 +
   1.216 +	g_F2_Pre_HookToRemove = hook1;
   1.217 +	p_d1i1->Func2();
   1.218 +	p_d1i1->Func2();
   1.219 +
   1.220 +	CHECK_STATES((&g_States,
   1.221 +		new State_Func2_Pre(p_d1i1),
   1.222 +		new State_Func2_Pre(p_d1i1),
   1.223 +		new State_D1_Func2(p_d1i1),
   1.224 +
   1.225 +		new State_Func2_Pre(p_d1i1),
   1.226 +		new State_D1_Func2(p_d1i1),
   1.227 +
   1.228 +		NULL), "Part 6");
   1.229 +
   1.230 +	SH_REMOVE_HOOK_ID(hook1);
   1.231 +
   1.232 +	// Hook function 3:
   1.233 +	//  Using manualhook, VP
   1.234 +	hook1 = SH_ADD_MANUALVPHOOK(IBase_Func3_Manual, p_d1i1, SH_STATIC(Handler_Func3_Pre), false);
   1.235 +
   1.236 +	//  Normally, VP
   1.237 +	hook2 = SH_ADD_VPHOOK(IBase, Func3, p_d1i1, SH_STATIC(Handler_Func3_Pre), false);
   1.238 +
   1.239 +	// Normally, no VP
   1.240 +	int hook3 = SH_ADD_HOOK(IBase, Func3, p_d1i1, SH_STATIC(Handler_Func3_Pre), false);
   1.241 +
   1.242 +	p_d1i1->Func3(1);
   1.243 +
   1.244 +	CHECK_STATES((&g_States,
   1.245 +		new State_Func3_Pre(p_d1i1, 1),		// manual vp hook
   1.246 +		new State_Func3_Pre(p_d1i1, 2),		// normal vp hook
   1.247 +		new State_Func3_Pre(p_d1i1, 3),		// normal non-vp hook
   1.248 +
   1.249 +		new State_D1_Func3(p_d1i1, 4),		// function
   1.250 +
   1.251 +		NULL), "Part 7.1");
   1.252 +
   1.253 +	p_d1i2->Func3(1);
   1.254 +
   1.255 +	CHECK_STATES((&g_States,
   1.256 +		new State_Func3_Pre(p_d1i2, 1),		// manual vp hook
   1.257 +		new State_Func3_Pre(p_d1i2, 2),		// normal vp hook
   1.258 +
   1.259 +		new State_D1_Func3(p_d1i2, 3),		// function
   1.260 +
   1.261 +		NULL), "Part 7.2");
   1.262 +
   1.263 +	SH_REMOVE_HOOK_ID(hook1);
   1.264 +	SH_REMOVE_HOOK_ID(hook2);
   1.265 +	SH_REMOVE_HOOK_ID(hook3);
   1.266 +
   1.267 +	return true;
   1.268 +}
   1.269 +