sourcehook/test/testref.cpp
author Scott Ehlert <ds@alliedmods.net>
Fri Aug 10 23:43:16 2007 +0000 (2007-08-10)
changeset 346 d0868271663a
parent 328 3a04ef79edf1
child 380 f9e21a2ac558
permissions -rw-r--r--
Reformatted output of meta list command
     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 	ADD_STATE(State_Result(pHello->Func(base)));
   108 	ADD_STATE(State_Result(pHello->Func(der)));
   109 	ADD_STATE(State_Result(pHello->Func(der2)));
   110 	ADD_STATE(State_Result(pHello->Func(der3)));
   111 
   112 	CHECK_STATES((&g_States,
   113 		new State_Result(0),
   114 		new State_Result(10),
   115 		new State_Result(11),
   116 		new State_Result(12),
   117 		NULL), "Part 1");
   118 
   119 	ADD_STATE(State_Result(SH_CALL(pHello, &CHello::Func)(base)));
   120 	ADD_STATE(State_Result(SH_CALL(pHello, &CHello::Func)(der)));
   121 	ADD_STATE(State_Result(SH_CALL(pHello, &CHello::Func)(der2)));
   122 	ADD_STATE(State_Result(SH_CALL(pHello, &CHello::Func)(der3)));
   123 
   124 	CHECK_STATES((&g_States,
   125 		new State_Result(0),
   126 		new State_Result(10),
   127 		new State_Result(11),
   128 		new State_Result(12),
   129 		NULL), "Part 2");
   130 
   131 	SH_ADD_HOOK(CHello, Func, &hello, SH_MEMBER(&hook, &CHook::Func), false);
   132 
   133 	ADD_STATE(State_Result(pHello->Func(base)));
   134 	ADD_STATE(State_Result(pHello->Func(der)));
   135 	ADD_STATE(State_Result(pHello->Func(der2)));
   136 	ADD_STATE(State_Result(pHello->Func(der3)));
   137 
   138 	CHECK_STATES((&g_States,
   139 		new State_Result_InHook(0),
   140 		new State_Result(20),
   141 		new State_Result_InHook(10),
   142 		new State_Result(20),
   143 		new State_Result_InHook(11),
   144 		new State_Result(20),
   145 		new State_Result_InHook(12),
   146 		new State_Result(20),
   147 		NULL), "Part 3");
   148 
   149 	ADD_STATE(State_Result(SH_CALL(pHello, &CHello::Func)(base)));
   150 	ADD_STATE(State_Result(SH_CALL(pHello, &CHello::Func)(der)));
   151 	ADD_STATE(State_Result(SH_CALL(pHello, &CHello::Func)(der2)));
   152 	ADD_STATE(State_Result(SH_CALL(pHello, &CHello::Func)(der3)));
   153 
   154 	CHECK_STATES((&g_States,
   155 		new State_Result(0),
   156 		new State_Result(10),
   157 		new State_Result(11),
   158 		new State_Result(12),
   159 		NULL), "Part 4");
   160 
   161    	return true;
   162 }