sourcehook/test/test2.cpp
author Pavol Marko <pavolmarko@gmail.com>
Sun May 01 12:36:48 2005 +0000 (2005-05-01)
changeset 48 a52dba4f8ed9
parent 46 079c2393469e
child 49 ec95d7681407
permissions -rw-r--r--
linux/gcc/32bit compatibility
     1 #include <string>
     2 #include "sourcehook_impl.h"
     3 #include "testevents.h"
     4 
     5 namespace
     6 {
     7 	StateList g_States;
     8 	SourceHook::ISourceHook *g_SHPtr;
     9 	SourceHook::Plugin g_PLID;
    10 
    11 	struct State_EatYams_Called : State
    12 	{
    13 		int m_Overload;
    14 		State_EatYams_Called(int ovrld) : m_Overload(ovrld)
    15 		{
    16 		}
    17 		bool IsEqual(State *other)
    18 		{
    19 			State_EatYams_Called *other2 = dynamic_cast<State_EatYams_Called*>(other);
    20 			if (!other2)
    21 				return false;
    22 			return other2->m_Overload == m_Overload;
    23 		}
    24 	};
    25 
    26 	struct State_EatYams_Handler_Called : State
    27 	{
    28 		int m_Overload;
    29 		State_EatYams_Handler_Called(int ovrld) : m_Overload(ovrld)
    30 		{
    31 		}
    32 		bool IsEqual(State *other)
    33 		{
    34 			State_EatYams_Handler_Called *other2 = dynamic_cast<State_EatYams_Handler_Called*>(other);
    35 			if (!other2)
    36 				return false;
    37 			return other2->m_Overload == m_Overload;
    38 		}
    39 	};
    40 
    41 
    42 	// TEST1
    43 	// Basic tests
    44 	// Hooking and callclass
    45 	class IGaben
    46 	{
    47 	public:
    48 		virtual void EatYams()
    49 		{
    50 			ADD_STATE(State_EatYams_Called(0));
    51 		}
    52 		virtual bool EatYams(const char *location) const
    53 		{
    54 			ADD_STATE(State_EatYams_Called(1));
    55 			return true;
    56 		}
    57 	};
    58 
    59 	SH_DECL_HOOK0_void(IGaben, EatYams, SH_NOATTRIB, 0);
    60 	SH_DECL_HOOK1(IGaben, EatYams, const, 1, bool, const char *);
    61 }
    62 
    63 void EatYams0_Handler()
    64 {
    65 	ADD_STATE(State_EatYams_Handler_Called(0));
    66 }
    67 
    68 bool EatYams1_Handler(const char *loc)
    69 {
    70 	ADD_STATE(State_EatYams_Handler_Called(1));
    71 	return true;
    72 }
    73 
    74 bool TestVafmtAndOverload(std::string &error)
    75 {
    76 	SourceHook::CSourceHookImpl g_SHImpl;
    77 	g_SHPtr = &g_SHImpl;
    78 	g_PLID = 1337;	
    79 
    80 	IGaben gabgab;
    81 	IGaben *pGab = &gabgab;
    82 
    83 	SourceHook::CallClass<IGaben> *cc = SH_GET_CALLCLASS(pGab);
    84 
    85 	SH_CALL(cc, static_cast<void (IGaben::*)()>(&IGaben::EatYams))();
    86 	SH_CALL(cc, static_cast<bool (IGaben::*)(const char *) const>(&IGaben::EatYams))("Here!");
    87 
    88 	SH_ADD_HOOK(IGaben, EatYams, pGab, EatYams0_Handler, false);
    89 	SH_ADD_HOOK(IGaben, EatYams, pGab, EatYams1_Handler, false);
    90 
    91 	pGab->EatYams();
    92 	pGab->EatYams("Here!");
    93 
    94 	SH_REMOVE_HOOK(IGaben, EatYams, pGab, EatYams0_Handler, false);
    95 	SH_REMOVE_HOOK(IGaben, EatYams, pGab, EatYams1_Handler, false);
    96 
    97 	CHECK_STATES((&g_States, 
    98 		new State_EatYams_Called(0),
    99 		new State_EatYams_Called(1),
   100 		new State_EatYams_Handler_Called(0),
   101 		new State_EatYams_Called(0),
   102 		new State_EatYams_Handler_Called(1),
   103 		new State_EatYams_Called(1),
   104 		NULL),"Part 1");
   105 
   106 	SH_RELEASE_CALLCLASS(cc);
   107 
   108 	return true;
   109 }
   110