sourcehook/test/testref.cpp
author Pavol Marko <pavolmarko@gmail.com>
Mon May 07 18:35:59 2007 +0000 (2007-05-07)
changeset 324 257f0837a885
parent 196 25d98645f668
child 328 3a04ef79edf1
permissions -rw-r--r--
Added hook ids
Added VP hooks and VP Hook test
New macro syntax (SH_ADD_HOOK + SH_STATIC/SH_MEMBER)
     1 #include "testevents.h"
     2 #include "sourcehook_test.h"
     3 
     4 // References and private copy constructors and stuff like that!
     5 
     6 /*
     7 BAILOPAN, 29.04.2006 21:51 :
     8 onoto-san, interesting error from VS
     9 we have this function:
    10 void GabHaben(ISomething &sth);
    11 
    12 SomethingDerived sth;
    13 and SH_CALL(cc, &WHAT::GabHaben)(sth);
    14 "No copy constructor available for class 'SomethingDerived'"
    15 */
    16 
    17 
    18 namespace
    19 {
    20 	StateList g_States;
    21 	SourceHook::ISourceHook *g_SHPtr;
    22 	SourceHook::Plugin g_PLID;
    23 
    24 	MAKE_STATE_1(State_Result, int);
    25 	MAKE_STATE_1(State_Result_InHook, int);
    26 
    27 	class CBase
    28 	{
    29 		// Private copy constructor!
    30 		CBase(CBase &other)
    31 		{
    32 		}
    33 	public:
    34 		CBase()
    35 		{
    36 		}
    37 
    38 		virtual int Func()
    39 		{
    40 			return 0;
    41 		}
    42 	};
    43 
    44 	class CDerived : public CBase
    45 	{
    46 		virtual int Func()
    47 		{
    48 			return 10;
    49 		}
    50 	};
    51 
    52 	// Complicated derived shit !
    53 	class COtherWeirdBase
    54 	{
    55 		int member;
    56 	};
    57 	class CDerived2 : private COtherWeirdBase, public CBase
    58 	{
    59 		int m_Return;
    60 	public:
    61 		CDerived2(int a) : m_Return(a)
    62 		{
    63 		}
    64 
    65 		virtual int Func()
    66 		{
    67 			return m_Return;
    68 		}
    69 	};
    70 
    71 	class CHello
    72 	{
    73 	public:
    74 		virtual int Func(CBase &sth)
    75 		{
    76 			return sth.Func();
    77 		}
    78 	};
    79 	class CHelloDerived : public CHello { };
    80 
    81 	class CHook
    82 	{
    83 	public:
    84 		virtual int Func(CBase &sth)
    85 		{
    86 			ADD_STATE(State_Result_InHook(sth.Func()));
    87 			RETURN_META_VALUE(MRES_SUPERCEDE, 20);
    88 		}
    89 	};
    90 	SH_DECL_HOOK1(CHello, Func, SH_NOATTRIB, 0, int, CBase&);
    91 }
    92 
    93 
    94 bool TestRef(std::string &error)
    95 {
    96 	GET_SHPTR(g_SHPtr);
    97 	g_PLID = 1;
    98 
    99 	CBase base;
   100 	CDerived der;
   101 	CDerived2 der2(11);
   102 	CDerived2 der3(12);
   103 	CHelloDerived hello;
   104 	CHello *pHello = &hello;
   105 	CHook hook;
   106 
   107 	SourceHook::CallClass<CHello> *cc = SH_GET_CALLCLASS(pHello);
   108 
   109 	ADD_STATE(State_Result(pHello->Func(base)));
   110 	ADD_STATE(State_Result(pHello->Func(der)));
   111 	ADD_STATE(State_Result(pHello->Func(der2)));
   112 	ADD_STATE(State_Result(pHello->Func(der3)));
   113 
   114 	CHECK_STATES((&g_States,
   115 		new State_Result(0),
   116 		new State_Result(10),
   117 		new State_Result(11),
   118 		new State_Result(12),
   119 		NULL), "Part 1");
   120 
   121 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(base)));
   122 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(der)));
   123 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(der2)));
   124 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(der3)));
   125 
   126 	CHECK_STATES((&g_States,
   127 		new State_Result(0),
   128 		new State_Result(10),
   129 		new State_Result(11),
   130 		new State_Result(12),
   131 		NULL), "Part 2");
   132 
   133 	SH_ADD_HOOK(CHello, Func, &hello, SH_MEMBER(&hook, &CHook::Func), false);
   134 
   135 	ADD_STATE(State_Result(pHello->Func(base)));
   136 	ADD_STATE(State_Result(pHello->Func(der)));
   137 	ADD_STATE(State_Result(pHello->Func(der2)));
   138 	ADD_STATE(State_Result(pHello->Func(der3)));
   139 
   140 	CHECK_STATES((&g_States,
   141 		new State_Result_InHook(0),
   142 		new State_Result(20),
   143 		new State_Result_InHook(10),
   144 		new State_Result(20),
   145 		new State_Result_InHook(11),
   146 		new State_Result(20),
   147 		new State_Result_InHook(12),
   148 		new State_Result(20),
   149 		NULL), "Part 3");
   150 
   151 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(base)));
   152 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(der)));
   153 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(der2)));
   154 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(der3)));
   155 
   156 	CHECK_STATES((&g_States,
   157 		new State_Result(0),
   158 		new State_Result(10),
   159 		new State_Result(11),
   160 		new State_Result(12),
   161 		NULL), "Part 4");
   162 
   163    	return true;
   164 }