1.1 --- a/sourcehook/test/testvphooks.cpp Fri May 11 19:21:14 2007 +0000
1.2 +++ b/sourcehook/test/testvphooks.cpp Sat May 12 08:08:01 2007 +0000
1.3 @@ -5,6 +5,7 @@
1.4
1.5 // TEST VP HOOKS
1.6 // Test vfnptr-wide hooks
1.7 +// Also contains a test for removing hooks on deleted instances (by id)
1.8
1.9 namespace
1.10 {
1.11 @@ -121,7 +122,7 @@
1.12 IBase *p_d1i1 = &d1i1;
1.13 IBase *p_d1i2 = &d1i2;
1.14 IBase *p_d2i1 = &d2i1;
1.15 -
1.16 +
1.17 int hook1 = SH_ADD_VPHOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
1.18
1.19 p_d1i1->Func1();
1.20 @@ -138,6 +139,18 @@
1.21 new State_D2_Func1(p_d2i1),
1.22 NULL), "Part 1");
1.23
1.24 + SH_CALL(p_d1i1, &IBase::Func1)();
1.25 + SH_CALL(p_d1i2, &IBase::Func1)();
1.26 + SH_CALL(p_d2i1, &IBase::Func1)();
1.27 +
1.28 + CHECK_STATES((&g_States,
1.29 + new State_D1_Func1(p_d1i1),
1.30 +
1.31 + new State_D1_Func1(p_d1i2),
1.32 +
1.33 + new State_D2_Func1(p_d2i1),
1.34 + NULL), "Part 1.1");
1.35 +
1.36 SH_REMOVE_HOOK_ID(hook1);
1.37
1.38 p_d1i1->Func1();
1.39 @@ -261,6 +274,60 @@
1.40 SH_REMOVE_HOOK_ID(hook2);
1.41 SH_REMOVE_HOOK_ID(hook3);
1.42
1.43 + // Test direct VP hook
1.44 +
1.45 + p_d1i1->Func1();
1.46 + p_d1i2->Func1();
1.47 +
1.48 + // get vtable manually
1.49 + void *pVtable = *reinterpret_cast<void**>(p_d1i1);
1.50 +
1.51 + hook1 = SH_ADD_DVPHOOK(IBase, Func1, pVtable, SH_STATIC(Handler_Func1_Pre), false);
1.52 +
1.53 + p_d1i1->Func1();
1.54 + p_d1i2->Func1();
1.55 +
1.56 + CHECK_STATES((&g_States,
1.57 + new State_D1_Func1(p_d1i1),
1.58 + new State_D1_Func1(p_d1i2),
1.59 +
1.60 + new State_Func1_Pre(p_d1i1),
1.61 + new State_D1_Func1(p_d1i1),
1.62 + new State_Func1_Pre(p_d1i2),
1.63 + new State_D1_Func1(p_d1i2),
1.64 + NULL), "Part 8.1");
1.65 +
1.66 + SH_REMOVE_HOOK_ID(hook1);
1.67 +
1.68 + // Now try manual dvp hooks
1.69 + p_d1i1->Func3(1);
1.70 + p_d1i2->Func3(1);
1.71 +
1.72 + hook1 = SH_ADD_MANUALDVPHOOK(IBase_Func3_Manual, pVtable, SH_STATIC(Handler_Func3_Pre), false);
1.73 +
1.74 + p_d1i1->Func3(1);
1.75 + p_d1i2->Func3(1);
1.76 +
1.77 + CHECK_STATES((&g_States,
1.78 + new State_D1_Func3(p_d1i1, 1),
1.79 + new State_D1_Func3(p_d1i2, 1),
1.80 +
1.81 + new State_Func3_Pre(p_d1i1, 1), // manual vp hook
1.82 + new State_D1_Func3(p_d1i1, 2), // function
1.83 +
1.84 + new State_Func3_Pre(p_d1i2, 1), // normal vp hook
1.85 + new State_D1_Func3(p_d1i2, 2), // function
1.86 +
1.87 + NULL), "Part 8.2");
1.88 +
1.89 +
1.90 + // Test removing normal hooks even though the instance is deleted
1.91 + p_d1i1 = new CDerived1;
1.92 + SH_ADD_HOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
1.93 + delete p_d1i1;
1.94 + // The following line may not crash!
1.95 + SH_REMOVE_HOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
1.96 +
1.97 return true;
1.98 }
1.99