sourcehook/test/testref.cpp
author David Anderson <dvander@alliedmods.net>
Tue Sep 18 20:03:47 2007 +0000 (2007-09-18)
branchsourcemm-1.6.0
changeset 347 e82968c21606
child 324 257f0837a885
child 361 f34718303c8f
permissions -rw-r--r--
branched 1.4.2 to 1.6.0
     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 
    80 	class CHook
    81 	{
    82 	public:
    83 		virtual int Func(CBase &sth)
    84 		{
    85 			ADD_STATE(State_Result_InHook(sth.Func()));
    86 			RETURN_META_VALUE(MRES_SUPERCEDE, 20);
    87 		}
    88 	};
    89 	SH_DECL_HOOK1(CHello, Func, SH_NOATTRIB, 0, int, CBase&);
    90 }
    91 
    92 
    93 bool TestRef(std::string &error)
    94 {
    95 	GET_SHPTR(g_SHPtr);
    96 	g_PLID = 1;
    97 
    98 	CBase base;
    99 	CDerived der;
   100 	CDerived2 der2(11);
   101 	CDerived2 der3(12);
   102 	CHello hello;
   103 	CHello *pHello = &hello;
   104 	CHook hook;
   105 
   106 	SourceHook::CallClass<CHello> *cc = SH_GET_CALLCLASS(&hello);
   107 
   108 	ADD_STATE(State_Result(pHello->Func(base)));
   109 	ADD_STATE(State_Result(pHello->Func(der)));
   110 	ADD_STATE(State_Result(pHello->Func(der2)));
   111 	ADD_STATE(State_Result(pHello->Func(der3)));
   112 
   113 	CHECK_STATES((&g_States,
   114 		new State_Result(0),
   115 		new State_Result(10),
   116 		new State_Result(11),
   117 		new State_Result(12),
   118 		NULL), "Part 1");
   119 
   120 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(base)));
   121 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(der)));
   122 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(der2)));
   123 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(der3)));
   124 
   125 	CHECK_STATES((&g_States,
   126 		new State_Result(0),
   127 		new State_Result(10),
   128 		new State_Result(11),
   129 		new State_Result(12),
   130 		NULL), "Part 2");
   131 
   132 	SH_ADD_HOOK_MEMFUNC(CHello, Func, &hello, &hook, &CHook::Func, false);
   133 
   134 	ADD_STATE(State_Result(pHello->Func(base)));
   135 	ADD_STATE(State_Result(pHello->Func(der)));
   136 	ADD_STATE(State_Result(pHello->Func(der2)));
   137 	ADD_STATE(State_Result(pHello->Func(der3)));
   138 
   139 	CHECK_STATES((&g_States,
   140 		new State_Result_InHook(0),
   141 		new State_Result(20),
   142 		new State_Result_InHook(10),
   143 		new State_Result(20),
   144 		new State_Result_InHook(11),
   145 		new State_Result(20),
   146 		new State_Result_InHook(12),
   147 		new State_Result(20),
   148 		NULL), "Part 3");
   149 
   150 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(base)));
   151 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(der)));
   152 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(der2)));
   153 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(der3)));
   154 
   155 	CHECK_STATES((&g_States,
   156 		new State_Result(0),
   157 		new State_Result(10),
   158 		new State_Result(11),
   159 		new State_Result(12),
   160 		NULL), "Part 4");
   161 
   162    	return true;
   163 }