sourcehook/test/test2.cpp
author Pavol Marko <pavolmarko@gmail.com>
Sat Nov 17 09:53:58 2007 +0000 (2007-11-17)
branchhookman_autogen
changeset 461 2fa049f36721
parent 421 573b9a092a5f
child 453 223f67807196
permissions -rw-r--r--
New hookfunc local vars code; first step to vafmt support
     1 #include <string>
     2 #include "sourcehook_test.h"
     3 #include "testevents.h"
     4 #include <stdarg.h>
     5 
     6 // TEST2
     7 // Vafmt and Overloaded functions
     8 namespace
     9 {
    10 	StateList g_States;
    11 	SourceHook::ISourceHook *g_SHPtr;
    12 	SourceHook::Plugin g_PLID;
    13 
    14 	MAKE_STATE_1(State_EatYams_Called, int);
    15 	MAKE_STATE_1(State_EatYams_Handler_Called, int);
    16 	MAKE_STATE_2(State_Vafmt_Called, int, std::string);
    17 	MAKE_STATE_2(State_Vafmt_PreHandler_Called, int, std::string);
    18 	MAKE_STATE_2(State_Vafmt_PostHandler_Called, int, std::string);
    19 
    20 	class IGaben
    21 	{
    22 	public:
    23 		virtual void EatYams()
    24 		{
    25 			ADD_STATE(State_EatYams_Called(0));
    26 		}
    27 		virtual bool EatYams(const char *location) const
    28 		{
    29 			ADD_STATE(State_EatYams_Called(1));
    30 			return true;
    31 		}
    32 		virtual void Vafmt1(bool param1, int x, const char *fmt, ...)
    33 		{
    34 			va_list ap;
    35 			va_start(ap, fmt);
    36 			char buffer[512];
    37 			vsprintf(buffer, fmt, ap);
    38 			va_end(ap);
    39 			ADD_STATE(State_Vafmt_Called(1, std::string(buffer)));
    40 		}
    41 		virtual float Vafmt2(const char *fmt, ...)
    42 		{
    43 			va_list ap;
    44 			va_start(ap, fmt);
    45 			char buffer[512];
    46 			vsprintf(buffer, fmt, ap);
    47 			va_end(ap);
    48 			ADD_STATE(State_Vafmt_Called(2, std::string(buffer)));
    49 			return 0.0f;
    50 		}
    51 	};
    52 
    53 	SH_DECL_HOOK0_void(IGaben, EatYams, SH_NOATTRIB, 0);
    54 	SH_DECL_HOOK1(IGaben, EatYams, const, 1, bool, const char *);
    55 	SH_DECL_HOOK2_void_vafmt(IGaben, Vafmt1, SH_NOATTRIB, 0, bool, int);
    56 	SH_DECL_HOOK0_vafmt(IGaben, Vafmt2, SH_NOATTRIB, 0, float);
    57 
    58 	SH_DECL_EXTERN2_void_vafmt(IGaben, Vafmt1, SH_NOATTRIB, 0, bool, int);
    59 	SH_DECL_EXTERN0_vafmt(IGaben, Vafmt2, SH_NOATTRIB, 0, float);
    60 
    61 	void EatYams0_Handler()
    62 	{
    63 		ADD_STATE(State_EatYams_Handler_Called(0));
    64 	}
    65 
    66 	bool EatYams1_Handler(const char *loc)
    67 	{
    68 		ADD_STATE(State_EatYams_Handler_Called(1));
    69 		return true;
    70 	}
    71 
    72 	void Vafmt1_PreHandler(bool param1, int x, const char *in)
    73 	{
    74 		ADD_STATE(State_Vafmt_PreHandler_Called(1, std::string(in)));
    75 	}
    76 	void Vafmt1_PostHandler(bool param1, int x, const char *in)
    77 	{
    78 		ADD_STATE(State_Vafmt_PostHandler_Called(1, std::string(in)));
    79 	}
    80 	float Vafmt2_PreHandler(const char *in)
    81 	{
    82 		ADD_STATE(State_Vafmt_PreHandler_Called(2, std::string(in)));
    83 		return 0.0f;
    84 	}
    85 	float Vafmt2_PostHandler(const char *in)
    86 	{
    87 		ADD_STATE(State_Vafmt_PostHandler_Called(2, std::string(in)));
    88 		return 0.0f;
    89 	}
    90 
    91 	IGaben *MyInstanceFactory()
    92 	{
    93 		return new IGaben;
    94 	}
    95 }
    96 
    97 
    98 bool TestVafmtAndOverload(std::string &error)
    99 {
   100 	int h1, h2, h3, h4;
   101 	GET_SHPTR(g_SHPtr);
   102 	g_PLID = 1337;	
   103 
   104 	IGaben *pGab = MyInstanceFactory();
   105 	CAutoPtrDestruction<IGaben> apd(pGab);
   106 
   107 	// Part 1
   108 	SH_CALL(pGab, static_cast<void (IGaben::*)()>(&IGaben::EatYams))();
   109 	SH_CALL(pGab, static_cast<bool (IGaben::*)(const char *) const>(&IGaben::EatYams))("Here!");
   110 
   111 	h1 = SH_ADD_HOOK(IGaben, EatYams, pGab, EatYams0_Handler, false);
   112 	h2 = SH_ADD_HOOK(IGaben, EatYams, pGab, EatYams1_Handler, false);
   113 
   114 	pGab->EatYams();
   115 	pGab->EatYams("Here!");
   116 
   117 	SH_REMOVE_HOOK_ID(h1);
   118 	SH_REMOVE_HOOK_ID(h2);
   119 
   120 	CHECK_STATES((&g_States, 
   121 		new State_EatYams_Called(0),
   122 		new State_EatYams_Called(1),
   123 		new State_EatYams_Handler_Called(0),
   124 		new State_EatYams_Called(0),
   125 		new State_EatYams_Handler_Called(1),
   126 		new State_EatYams_Called(1),
   127 		NULL),"Part 1");
   128 
   129 	// Part 2
   130 	pGab->Vafmt1(true, 55, "Hello %s%d%s", "BA", 1, "L");
   131 	SH_CALL(pGab, &IGaben::Vafmt1)(true, 55, "Hello %s%d%s", "BA", 1, "L");
   132 	pGab->Vafmt2("Hello %s%d%s", "BA", 1, "LOPAN");
   133 	SH_CALL(pGab, &IGaben::Vafmt2)("Hello %s%d%s", "BA", 1, "LOPAN");
   134 
   135 	CHECK_STATES((&g_States,
   136 		new State_Vafmt_Called(1, "Hello BA1L"),
   137 		new State_Vafmt_Called(1, "Hello BA1L"),
   138 		new State_Vafmt_Called(2, "Hello BA1LOPAN"),
   139 		new State_Vafmt_Called(2, "Hello BA1LOPAN"),
   140 		NULL), "Part 2");
   141 
   142 	// Part 3
   143 	h1 = SH_ADD_HOOK(IGaben, Vafmt1, pGab, Vafmt1_PreHandler, false);
   144 	h2 = SH_ADD_HOOK(IGaben, Vafmt1, pGab, Vafmt1_PostHandler, true);
   145 	h3 = SH_ADD_HOOK(IGaben, Vafmt2, pGab, Vafmt2_PreHandler, false);
   146 	h4 = SH_ADD_HOOK(IGaben, Vafmt2, pGab, Vafmt2_PostHandler, true);
   147 
   148 	pGab->Vafmt1(true, 55, "Hello %s%d%s", "BA", 1, "L");
   149 	pGab->Vafmt2("Hello %s%d%s", "BA", 1, "LOPAN");
   150 
   151 	CHECK_STATES((&g_States,
   152 		new State_Vafmt_PreHandler_Called(1, std::string("Hello BA1L")),
   153 		new State_Vafmt_Called(1, std::string("Hello BA1L")),
   154 		new State_Vafmt_PostHandler_Called(1, std::string("Hello BA1L")),
   155 
   156 		new State_Vafmt_PreHandler_Called(2, std::string("Hello BA1LOPAN")),
   157 		new State_Vafmt_Called(2, std::string("Hello BA1LOPAN")),
   158 		new State_Vafmt_PostHandler_Called(2, std::string("Hello BA1LOPAN")),
   159 		NULL), "Part 3");
   160 
   161 	// Part 4
   162 	SH_REMOVE_HOOK_ID(h1);
   163 	SH_REMOVE_HOOK_ID(h2);
   164 	SH_REMOVE_HOOK_ID(h3);
   165 	SH_REMOVE_HOOK_ID(h4);
   166 
   167 	pGab->Vafmt1(true, 55, "Hello %s%d%s", "BA", 1, "L");
   168 	pGab->Vafmt2("Hello %s%d%s", "BA", 1, "LOPAN");
   169 
   170 	CHECK_STATES((&g_States,
   171 		new State_Vafmt_Called(1, "Hello BA1L"),
   172 		new State_Vafmt_Called(2, "Hello BA1LOPAN"),
   173 		NULL), "Part 4");
   174 
   175 	return true;
   176 }