sourcehook/test/test2.cpp
author Pavol Marko <pavolmarko@gmail.com>
Sun Oct 14 11:59:13 2007 +0000 (2007-10-14)
changeset 418 109309ff6350
parent 387 093c72464b35
child 421 573b9a092a5f
permissions -rw-r--r--
Removed "trust the prototype" model which could lead to problems. Instead, every CVfnPtr now has a list of hook managers.
-> Removed CHookManagerContainer
Also removed listcat (unnecessary since "V2" interface)
     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 	void EatYams0_Handler()
    59 	{
    60 		ADD_STATE(State_EatYams_Handler_Called(0));
    61 	}
    62 
    63 	bool EatYams1_Handler(const char *loc)
    64 	{
    65 		ADD_STATE(State_EatYams_Handler_Called(1));
    66 		return true;
    67 	}
    68 
    69 	void Vafmt1_PreHandler(bool param1, int x, const char *in)
    70 	{
    71 		ADD_STATE(State_Vafmt_PreHandler_Called(1, std::string(in)));
    72 	}
    73 	void Vafmt1_PostHandler(bool param1, int x, const char *in)
    74 	{
    75 		ADD_STATE(State_Vafmt_PostHandler_Called(1, std::string(in)));
    76 	}
    77 	float Vafmt2_PreHandler(const char *in)
    78 	{
    79 		ADD_STATE(State_Vafmt_PreHandler_Called(2, std::string(in)));
    80 		return 0.0f;
    81 	}
    82 	float Vafmt2_PostHandler(const char *in)
    83 	{
    84 		ADD_STATE(State_Vafmt_PostHandler_Called(2, std::string(in)));
    85 		return 0.0f;
    86 	}
    87 
    88 	IGaben *MyInstanceFactory()
    89 	{
    90 		return new IGaben;
    91 	}
    92 }
    93 
    94 
    95 bool TestVafmtAndOverload(std::string &error)
    96 {
    97 	int h1, h2, h3, h4;
    98 	GET_SHPTR(g_SHPtr);
    99 	g_PLID = 1337;	
   100 
   101 	IGaben *pGab = MyInstanceFactory();
   102 	CAutoPtrDestruction<IGaben> apd(pGab);
   103 
   104 	// Part 1
   105 	SH_CALL(pGab, static_cast<void (IGaben::*)()>(&IGaben::EatYams))();
   106 	SH_CALL(pGab, static_cast<bool (IGaben::*)(const char *) const>(&IGaben::EatYams))("Here!");
   107 
   108 	h1 = SH_ADD_HOOK(IGaben, EatYams, pGab, EatYams0_Handler, false);
   109 	h2 = SH_ADD_HOOK(IGaben, EatYams, pGab, EatYams1_Handler, false);
   110 
   111 	pGab->EatYams();
   112 	pGab->EatYams("Here!");
   113 
   114 	SH_REMOVE_HOOK_ID(h1);
   115 	SH_REMOVE_HOOK_ID(h2);
   116 
   117 	CHECK_STATES((&g_States, 
   118 		new State_EatYams_Called(0),
   119 		new State_EatYams_Called(1),
   120 		new State_EatYams_Handler_Called(0),
   121 		new State_EatYams_Called(0),
   122 		new State_EatYams_Handler_Called(1),
   123 		new State_EatYams_Called(1),
   124 		NULL),"Part 1");
   125 
   126 	// Part 2
   127 	pGab->Vafmt1(true, 55, "Hello %s%d%s", "BA", 1, "L");
   128 	SH_CALL(pGab, &IGaben::Vafmt1)(true, 55, "Hello %s%d%s", "BA", 1, "L");
   129 	pGab->Vafmt2("Hello %s%d%s", "BA", 1, "LOPAN");
   130 	SH_CALL(pGab, &IGaben::Vafmt2)("Hello %s%d%s", "BA", 1, "LOPAN");
   131 
   132 	CHECK_STATES((&g_States,
   133 		new State_Vafmt_Called(1, "Hello BA1L"),
   134 		new State_Vafmt_Called(1, "Hello BA1L"),
   135 		new State_Vafmt_Called(2, "Hello BA1LOPAN"),
   136 		new State_Vafmt_Called(2, "Hello BA1LOPAN"),
   137 		NULL), "Part 2");
   138 
   139 	// Part 3
   140 	h1 = SH_ADD_HOOK(IGaben, Vafmt1, pGab, Vafmt1_PreHandler, false);
   141 	h2 = SH_ADD_HOOK(IGaben, Vafmt1, pGab, Vafmt1_PostHandler, true);
   142 	h3 = SH_ADD_HOOK(IGaben, Vafmt2, pGab, Vafmt2_PreHandler, false);
   143 	h4 = SH_ADD_HOOK(IGaben, Vafmt2, pGab, Vafmt2_PostHandler, true);
   144 
   145 	pGab->Vafmt1(true, 55, "Hello %s%d%s", "BA", 1, "L");
   146 	pGab->Vafmt2("Hello %s%d%s", "BA", 1, "LOPAN");
   147 
   148 	CHECK_STATES((&g_States,
   149 		new State_Vafmt_PreHandler_Called(1, std::string("Hello BA1L")),
   150 		new State_Vafmt_Called(1, std::string("Hello BA1L")),
   151 		new State_Vafmt_PostHandler_Called(1, std::string("Hello BA1L")),
   152 
   153 		new State_Vafmt_PreHandler_Called(2, std::string("Hello BA1LOPAN")),
   154 		new State_Vafmt_Called(2, std::string("Hello BA1LOPAN")),
   155 		new State_Vafmt_PostHandler_Called(2, std::string("Hello BA1LOPAN")),
   156 		NULL), "Part 3");
   157 
   158 	// Part 4
   159 	SH_REMOVE_HOOK_ID(h1);
   160 	SH_REMOVE_HOOK_ID(h2);
   161 	SH_REMOVE_HOOK_ID(h3);
   162 	SH_REMOVE_HOOK_ID(h4);
   163 
   164 	pGab->Vafmt1(true, 55, "Hello %s%d%s", "BA", 1, "L");
   165 	pGab->Vafmt2("Hello %s%d%s", "BA", 1, "LOPAN");
   166 
   167 	CHECK_STATES((&g_States,
   168 		new State_Vafmt_Called(1, "Hello BA1L"),
   169 		new State_Vafmt_Called(2, "Hello BA1LOPAN"),
   170 		NULL), "Part 4");
   171 
   172 	return true;
   173 }