1.1 --- a/sourcehook/test/test2.cpp Thu Oct 04 19:46:28 2007 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,172 +0,0 @@
1.4 -#include <string>
1.5 -#include "sourcehook_test.h"
1.6 -#include "testevents.h"
1.7 -#include <stdarg.h>
1.8 -
1.9 -// TEST2
1.10 -// Vafmt and Overloaded functions
1.11 -namespace
1.12 -{
1.13 - StateList g_States;
1.14 - SourceHook::ISourceHook *g_SHPtr;
1.15 - SourceHook::Plugin g_PLID;
1.16 -
1.17 - MAKE_STATE_1(State_EatYams_Called, int);
1.18 - MAKE_STATE_1(State_EatYams_Handler_Called, int);
1.19 - MAKE_STATE_2(State_Vafmt_Called, int, std::string);
1.20 - MAKE_STATE_2(State_Vafmt_PreHandler_Called, int, std::string);
1.21 - MAKE_STATE_2(State_Vafmt_PostHandler_Called, int, std::string);
1.22 -
1.23 - class IGaben
1.24 - {
1.25 - public:
1.26 - virtual void EatYams()
1.27 - {
1.28 - ADD_STATE(State_EatYams_Called(0));
1.29 - }
1.30 - virtual bool EatYams(const char *location) const
1.31 - {
1.32 - ADD_STATE(State_EatYams_Called(1));
1.33 - return true;
1.34 - }
1.35 - virtual void Vafmt1(bool param1, int x, const char *fmt, ...)
1.36 - {
1.37 - va_list ap;
1.38 - va_start(ap, fmt);
1.39 - char buffer[512];
1.40 - vsprintf(buffer, fmt, ap);
1.41 - va_end(ap);
1.42 - ADD_STATE(State_Vafmt_Called(1, std::string(buffer)));
1.43 - }
1.44 - virtual float Vafmt2(const char *fmt, ...)
1.45 - {
1.46 - va_list ap;
1.47 - va_start(ap, fmt);
1.48 - char buffer[512];
1.49 - vsprintf(buffer, fmt, ap);
1.50 - va_end(ap);
1.51 - ADD_STATE(State_Vafmt_Called(2, std::string(buffer)));
1.52 - return 0.0f;
1.53 - }
1.54 - };
1.55 -
1.56 - // GCC's optimizer is too good. I had to add this in order to make it execute a virtual table lookup!
1.57 - class Whatever : public IGaben
1.58 - {
1.59 - };
1.60 -
1.61 - SH_DECL_HOOK0_void(IGaben, EatYams, SH_NOATTRIB, 0);
1.62 - SH_DECL_HOOK1(IGaben, EatYams, const, 1, bool, const char *);
1.63 - SH_DECL_HOOK2_void_vafmt(IGaben, Vafmt1, SH_NOATTRIB, 0, bool, int);
1.64 - SH_DECL_HOOK0_vafmt(IGaben, Vafmt2, SH_NOATTRIB, 0, float);
1.65 -
1.66 - void EatYams0_Handler()
1.67 - {
1.68 - ADD_STATE(State_EatYams_Handler_Called(0));
1.69 - }
1.70 -
1.71 - bool EatYams1_Handler(const char *loc)
1.72 - {
1.73 - ADD_STATE(State_EatYams_Handler_Called(1));
1.74 - return true;
1.75 - }
1.76 -
1.77 - void Vafmt1_PreHandler(bool param1, int x, const char *in)
1.78 - {
1.79 - ADD_STATE(State_Vafmt_PreHandler_Called(1, std::string(in)));
1.80 - }
1.81 - void Vafmt1_PostHandler(bool param1, int x, const char *in)
1.82 - {
1.83 - ADD_STATE(State_Vafmt_PostHandler_Called(1, std::string(in)));
1.84 - }
1.85 - float Vafmt2_PreHandler(const char *in)
1.86 - {
1.87 - ADD_STATE(State_Vafmt_PreHandler_Called(2, std::string(in)));
1.88 - return 0.0f;
1.89 - }
1.90 - float Vafmt2_PostHandler(const char *in)
1.91 - {
1.92 - ADD_STATE(State_Vafmt_PostHandler_Called(2, std::string(in)));
1.93 - return 0.0f;
1.94 - }
1.95 -}
1.96 -
1.97 -
1.98 -bool TestVafmtAndOverload(std::string &error)
1.99 -{
1.100 - GET_SHPTR(g_SHPtr);
1.101 - g_PLID = 1337;
1.102 -
1.103 - Whatever gabgab;
1.104 - IGaben *pGab = &gabgab;
1.105 -
1.106 - // Part 1
1.107 - SH_CALL(pGab, static_cast<void (IGaben::*)()>(&IGaben::EatYams))();
1.108 - SH_CALL(pGab, static_cast<bool (IGaben::*)(const char *) const>(&IGaben::EatYams))("Here!");
1.109 -
1.110 - SH_ADD_HOOK(IGaben, EatYams, pGab, EatYams0_Handler, false);
1.111 - SH_ADD_HOOK(IGaben, EatYams, pGab, EatYams1_Handler, false);
1.112 -
1.113 - pGab->EatYams();
1.114 - pGab->EatYams("Here!");
1.115 -
1.116 - SH_REMOVE_HOOK(IGaben, EatYams, pGab, EatYams0_Handler, false);
1.117 - SH_REMOVE_HOOK(IGaben, EatYams, pGab, EatYams1_Handler, false);
1.118 -
1.119 - CHECK_STATES((&g_States,
1.120 - new State_EatYams_Called(0),
1.121 - new State_EatYams_Called(1),
1.122 - new State_EatYams_Handler_Called(0),
1.123 - new State_EatYams_Called(0),
1.124 - new State_EatYams_Handler_Called(1),
1.125 - new State_EatYams_Called(1),
1.126 - NULL),"Part 1");
1.127 -
1.128 - // Part 2
1.129 - pGab->Vafmt1(true, 55, "Hello %s%d%s", "BA", 1, "L");
1.130 - SH_CALL(pGab, &IGaben::Vafmt1)(true, 55, "Hello %s%d%s", "BA", 1, "L");
1.131 - pGab->Vafmt2("Hello %s%d%s", "BA", 1, "LOPAN");
1.132 - SH_CALL(pGab, &IGaben::Vafmt2)("Hello %s%d%s", "BA", 1, "LOPAN");
1.133 -
1.134 - CHECK_STATES((&g_States,
1.135 - new State_Vafmt_Called(1, "Hello BA1L"),
1.136 - new State_Vafmt_Called(1, "Hello BA1L"),
1.137 - new State_Vafmt_Called(2, "Hello BA1LOPAN"),
1.138 - new State_Vafmt_Called(2, "Hello BA1LOPAN"),
1.139 - NULL), "Part 2");
1.140 -
1.141 - // Part 3
1.142 - SH_ADD_HOOK(IGaben, Vafmt1, pGab, Vafmt1_PreHandler, false);
1.143 - SH_ADD_HOOK(IGaben, Vafmt1, pGab, Vafmt1_PostHandler, true);
1.144 - SH_ADD_HOOK(IGaben, Vafmt2, pGab, Vafmt2_PreHandler, false);
1.145 - SH_ADD_HOOK(IGaben, Vafmt2, pGab, Vafmt2_PostHandler, true);
1.146 -
1.147 - pGab->Vafmt1(true, 55, "Hello %s%d%s", "BA", 1, "L");
1.148 - pGab->Vafmt2("Hello %s%d%s", "BA", 1, "LOPAN");
1.149 -
1.150 - CHECK_STATES((&g_States,
1.151 - new State_Vafmt_PreHandler_Called(1, std::string("Hello BA1L")),
1.152 - new State_Vafmt_Called(1, std::string("Hello BA1L")),
1.153 - new State_Vafmt_PostHandler_Called(1, std::string("Hello BA1L")),
1.154 -
1.155 - new State_Vafmt_PreHandler_Called(2, std::string("Hello BA1LOPAN")),
1.156 - new State_Vafmt_Called(2, std::string("Hello BA1LOPAN")),
1.157 - new State_Vafmt_PostHandler_Called(2, std::string("Hello BA1LOPAN")),
1.158 - NULL), "Part 3");
1.159 -
1.160 - // Part 4
1.161 - SH_REMOVE_HOOK(IGaben, Vafmt1, pGab, Vafmt1_PreHandler, false);
1.162 - SH_REMOVE_HOOK(IGaben, Vafmt1, pGab, Vafmt1_PostHandler, true);
1.163 - SH_REMOVE_HOOK(IGaben, Vafmt2, pGab, Vafmt2_PreHandler, false);
1.164 - SH_REMOVE_HOOK(IGaben, Vafmt2, pGab, Vafmt2_PostHandler, true);
1.165 -
1.166 - pGab->Vafmt1(true, 55, "Hello %s%d%s", "BA", 1, "L");
1.167 - pGab->Vafmt2("Hello %s%d%s", "BA", 1, "LOPAN");
1.168 -
1.169 - CHECK_STATES((&g_States,
1.170 - new State_Vafmt_Called(1, "Hello BA1L"),
1.171 - new State_Vafmt_Called(2, "Hello BA1LOPAN"),
1.172 - NULL), "Part 4");
1.173 -
1.174 - return true;
1.175 -}