sourcehook/test/test3.cpp
author David Anderson <dvander@alliedmods.net>
Fri Sep 28 21:40:58 2007 +0000 (2007-09-28)
branchsourcemm-1.6.0
changeset 361 f34718303c8f
parent 347 e82968c21606
permissions -rw-r--r--
removed old providers location
replaced sourcehook with v4.5
     1 #include <string>
     2 #include "sourcehook_test.h"
     3 #include "testevents.h"
     4 
     5 // TEST3
     6 // Tests with inheritance / thisptroffsets
     7 
     8 namespace
     9 {
    10 	StateList g_States;
    11 	SourceHook::ISourceHook *g_SHPtr;
    12 	SourceHook::Plugin g_PLID;
    13 
    14 	MAKE_STATE_1(State_Func1_Called, void*);			// param1: This pointer
    15 	MAKE_STATE_1(State_Func2_Called, void*);			// param1: This pointer
    16 	MAKE_STATE_1(State_Func3_Called, void*);			// param1: This pointer
    17 
    18 	MAKE_STATE_1(State_Func1H_Called, void*);
    19 	MAKE_STATE_1(State_Func2H_Called, void*);
    20 	MAKE_STATE_1(State_Func3H_Called, void*);
    21 
    22 	class Base1
    23 	{
    24 		int a;
    25 	public:
    26 		virtual void Func1()
    27 		{
    28 			ADD_STATE(State_Func1_Called(reinterpret_cast<void*>(this)));
    29 		}
    30 	};
    31 	class Base2
    32 	{
    33 		int b;
    34 	public:
    35 		virtual void Func2()
    36 		{
    37 			ADD_STATE(State_Func2_Called(reinterpret_cast<void*>(this)));
    38 		}
    39 	};
    40 	class Derived : public Base1, public Base2
    41 	{
    42 		int c;
    43 	public:
    44 		virtual void Func3()
    45 		{
    46 			ADD_STATE(State_Func3_Called(reinterpret_cast<void*>(this)));
    47 		}
    48 	};
    49 
    50 	class DerivedDerived : public Derived { };
    51 
    52 	SH_DECL_HOOK0_void(Derived, Func1, SH_NOATTRIB, 0);
    53 	SH_DECL_HOOK0_void(Derived, Func2, SH_NOATTRIB, 0);
    54 	SH_DECL_HOOK0_void(Derived, Func3, SH_NOATTRIB, 0);
    55 	SH_DECL_HOOK0_void(Base1, Func1, SH_NOATTRIB, 0);
    56 	SH_DECL_HOOK0_void(Base2, Func2, SH_NOATTRIB, 0);
    57 
    58 	void Handler_Func1()
    59 	{
    60 		ADD_STATE(State_Func1H_Called(META_IFACEPTR(void)));
    61 	}
    62 	void Handler_Func2()
    63 	{
    64 		ADD_STATE(State_Func2H_Called(META_IFACEPTR(void)));
    65 	}
    66 	void Handler_Func3()
    67 	{
    68 		ADD_STATE(State_Func3H_Called(META_IFACEPTR(void)));
    69 	}
    70 }
    71 
    72 bool TestThisPtrOffs(std::string &error)
    73 {
    74 	GET_SHPTR(g_SHPtr);
    75 	g_PLID = 1337;
    76 
    77 	DerivedDerived inst;
    78 	Derived *pD = &inst;
    79 	Base1 *pB1 = pD;
    80 	Base2 *pB2 = pD;
    81 
    82 	// It should be:
    83 	// pB1 = pD
    84 	// pB2 > pB1
    85 
    86 	// 1)
    87 	//  Get a callclass for pD
    88 	//  Verify whether the this pointers are correct
    89 	//  Also call them normally to make sure that we aren't messing it up ;)
    90 
    91 	SH_CALL(pD, &Derived::Func1)();
    92 	SH_CALL(pD, &Derived::Func2)();
    93 	SH_CALL(pD, &Derived::Func3)();
    94 	pD->Func1();
    95 	pD->Func2();
    96 	pD->Func3();
    97 
    98 	CHECK_STATES((&g_States,
    99 		new State_Func1_Called(pB1),
   100 		new State_Func2_Called(pB2),
   101 		new State_Func3_Called(pD),
   102 		new State_Func1_Called(pB1),
   103 		new State_Func2_Called(pB2),
   104 		new State_Func3_Called(pD),
   105 		NULL), "Part 1");
   106 
   107 	SH_CALL(pD, &Base1::Func1)();
   108 	SH_CALL(pD, &Base2::Func2)();
   109 
   110 	CHECK_STATES((&g_States,
   111 		new State_Func1_Called(pB1),
   112 		new State_Func2_Called(pB2),
   113 		NULL), "Part 1.1");
   114 
   115 	// 2)
   116 	//   Get callclasses for the other ones and verify it as well
   117 
   118 	SH_CALL(pB1, &Base1::Func1)();
   119 	SH_CALL(pB2, &Base2::Func2)();
   120 
   121 	CHECK_STATES((&g_States,
   122 		new State_Func1_Called(pB1),
   123 		new State_Func2_Called(pB2),
   124 		NULL), "Part 2");
   125 
   126 
   127 	// 3) Add hooks on them (referring to them through pD1 / Derived)
   128 	//   Check whether the hooks are called with the correct this pointer
   129 
   130 	SH_ADD_HOOK(Derived, Func1, pD, SH_STATIC(Handler_Func1), false);
   131 	SH_ADD_HOOK(Derived, Func2, pD, SH_STATIC(Handler_Func2), false);
   132 	SH_ADD_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
   133 
   134 	pD->Func1();
   135 	pD->Func2();
   136 	pD->Func3();
   137 	pB1->Func1();
   138 	pB2->Func2();
   139 
   140 	// The handlers should always be called with the pointer to Derived
   141 	CHECK_STATES((&g_States,
   142 		new State_Func1H_Called(pD),
   143 		new State_Func1_Called(pB1),
   144 		new State_Func2H_Called(pD),
   145 		new State_Func2_Called(pB2),
   146 		new State_Func3H_Called(pD),
   147 		new State_Func3_Called(pD),
   148 		new State_Func1H_Called(pD),
   149 		new State_Func1_Called(pB1),
   150 		new State_Func2H_Called(pD),
   151 		new State_Func2_Called(pB2),
   152 		NULL), "Part 3");
   153 
   154 	SH_REMOVE_HOOK(Derived, Func1, pD, SH_STATIC(Handler_Func1), false);
   155 	SH_REMOVE_HOOK(Derived, Func2, pD, SH_STATIC(Handler_Func2), false);
   156 	SH_REMOVE_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
   157 
   158 	// 4)
   159 	//   Now add the hooks on Base1 and Base2 and check again
   160 
   161 	// Note that the new implicit_cast should convert the pD to Base1*/Base2* :)
   162 	SH_ADD_HOOK(Base1, Func1, pD, SH_STATIC(Handler_Func1), false);
   163 	SH_ADD_HOOK(Base2, Func2, pD, SH_STATIC(Handler_Func2), false);
   164 	SH_ADD_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
   165 
   166 	pD->Func1();
   167 	pD->Func2();
   168 	pD->Func3();
   169 	pB1->Func1();
   170 	pB2->Func2();
   171 
   172 	// This time, the handlers for Func1 should be called with pB1 and the handlers
   173 	// for Func2 should be called with pB2
   174 	CHECK_STATES((&g_States,
   175 		new State_Func1H_Called(pB1),
   176 		new State_Func1_Called(pB1),
   177 		new State_Func2H_Called(pB2),
   178 		new State_Func2_Called(pB2),
   179 		new State_Func3H_Called(pD),
   180 		new State_Func3_Called(pD),
   181 		new State_Func1H_Called(pB1),
   182 		new State_Func1_Called(pB1),
   183 		new State_Func2H_Called(pB2),
   184 		new State_Func2_Called(pB2),
   185 		NULL), "Part 4");
   186 
   187 	SH_REMOVE_HOOK(Base1, Func1, pD, SH_STATIC(Handler_Func1), false);
   188 	SH_REMOVE_HOOK(Base2, Func2, pD, SH_STATIC(Handler_Func2), false);
   189 	SH_REMOVE_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
   190 
   191 
   192 	// 5)
   193 	//   Add some hooks, and use callclasses
   194 
   195 	// 5.1) First off, add all of them on pD
   196 	SH_ADD_HOOK(Derived, Func1, pD, SH_STATIC(Handler_Func1), false);
   197 	SH_ADD_HOOK(Derived, Func2, pD, SH_STATIC(Handler_Func2), false);
   198 	SH_ADD_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
   199 
   200 	pD->Func1();
   201 	pD->Func2();
   202 	pD->Func3();
   203 
   204 	CHECK_STATES((&g_States,
   205 		new State_Func1H_Called(pD),
   206 		new State_Func1_Called(pB1),
   207 		new State_Func2H_Called(pD),
   208 		new State_Func2_Called(pB2),
   209 		new State_Func3H_Called(pD),
   210 		new State_Func3_Called(pD),
   211 		NULL), "Part 5.1");
   212 
   213 	SH_CALL(pD, &Derived::Func1)();
   214 	SH_CALL(pD, &Derived::Func2)();
   215 	SH_CALL(pD, &Derived::Func3)();
   216 	SH_CALL(pB1, &Base1::Func1)();
   217 	SH_CALL(pB2, &Base2::Func2)();
   218 
   219 	CHECK_STATES((&g_States,
   220 		new State_Func1_Called(pB1),
   221 		new State_Func2_Called(pB2),
   222 		new State_Func3_Called(pD),
   223 		new State_Func1_Called(pB1),
   224 		new State_Func2_Called(pB2),
   225 		NULL), "Part 5.2");
   226 
   227 	SH_REMOVE_HOOK(Derived, Func1, pD, SH_STATIC(Handler_Func1), false);
   228 	SH_REMOVE_HOOK(Derived, Func2, pD, SH_STATIC(Handler_Func2), false);
   229 	SH_REMOVE_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
   230 
   231 	return true;
   232 }