sourcehook/test/testref.cpp
author Pavol Marko <pavolmarko@gmail.com>
Mon Oct 08 14:20:51 2007 +0000 (2007-10-08)
changeset 387 093c72464b35
parent 380 f9e21a2ac558
child 412 43098e572d24
permissions -rw-r--r--
Subtle bug fixed + tests now more resistant against compiler
optimizations
     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 	CHello *MyInstanceFactory()
    92 	{
    93 		return new CHello;
    94 	}
    95 }
    96 
    97 
    98 bool TestRef(std::string &error)
    99 {
   100 	GET_SHPTR(g_SHPtr);
   101 	g_PLID = 1;
   102 
   103 	CBase base;
   104 	CDerived der;
   105 	CDerived2 der2(11);
   106 	CDerived2 der3(12);
   107 	CHello *pHello = MyInstanceFactory();
   108 	CAutoPtrDestruction<CHello> apd(pHello);
   109 	CHook hook;
   110 
   111 	SourceHook::CallClass<CHello> *cc = SH_GET_CALLCLASS(pHello);
   112 
   113 	ADD_STATE(State_Result(pHello->Func(base)));
   114 	ADD_STATE(State_Result(pHello->Func(der)));
   115 	ADD_STATE(State_Result(pHello->Func(der2)));
   116 	ADD_STATE(State_Result(pHello->Func(der3)));
   117 
   118 	CHECK_STATES((&g_States,
   119 		new State_Result(0),
   120 		new State_Result(10),
   121 		new State_Result(11),
   122 		new State_Result(12),
   123 		NULL), "Part 1");
   124 
   125 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(base)));
   126 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(der)));
   127 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(der2)));
   128 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(der3)));
   129 
   130 	CHECK_STATES((&g_States,
   131 		new State_Result(0),
   132 		new State_Result(10),
   133 		new State_Result(11),
   134 		new State_Result(12),
   135 		NULL), "Part 2");
   136 
   137 	SH_ADD_HOOK(CHello, Func, pHello, SH_MEMBER(&hook, &CHook::Func), false);
   138 
   139 	ADD_STATE(State_Result(pHello->Func(base)));
   140 	ADD_STATE(State_Result(pHello->Func(der)));
   141 	ADD_STATE(State_Result(pHello->Func(der2)));
   142 	ADD_STATE(State_Result(pHello->Func(der3)));
   143 
   144 	CHECK_STATES((&g_States,
   145 		new State_Result_InHook(0),
   146 		new State_Result(20),
   147 		new State_Result_InHook(10),
   148 		new State_Result(20),
   149 		new State_Result_InHook(11),
   150 		new State_Result(20),
   151 		new State_Result_InHook(12),
   152 		new State_Result(20),
   153 		NULL), "Part 3");
   154 
   155 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(base)));
   156 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(der)));
   157 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(der2)));
   158 	ADD_STATE(State_Result(SH_CALL(cc, &CHello::Func)(der3)));
   159 
   160 	CHECK_STATES((&g_States,
   161 		new State_Result(0),
   162 		new State_Result(10),
   163 		new State_Result(11),
   164 		new State_Result(12),
   165 		NULL), "Part 4");
   166 
   167    	return true;
   168 }