1.1 --- a/sourcehook/test/test2.cpp Sun May 01 12:36:48 2005 +0000
1.2 +++ b/sourcehook/test/test2.cpp Sun May 01 14:30:52 2005 +0000
1.3 @@ -1,47 +1,22 @@
1.4 #include <string>
1.5 #include "sourcehook_impl.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 - struct State_EatYams_Called : State
1.18 - {
1.19 - int m_Overload;
1.20 - State_EatYams_Called(int ovrld) : m_Overload(ovrld)
1.21 - {
1.22 - }
1.23 - bool IsEqual(State *other)
1.24 - {
1.25 - State_EatYams_Called *other2 = dynamic_cast<State_EatYams_Called*>(other);
1.26 - if (!other2)
1.27 - return false;
1.28 - return other2->m_Overload == m_Overload;
1.29 - }
1.30 - };
1.31 + MAKE_STATE_1(State_EatYams_Called, int);
1.32 + MAKE_STATE_1(State_EatYams_Handler_Called, int);
1.33 + MAKE_STATE_2(State_Vafmt_Called, int, std::string);
1.34 + MAKE_STATE_2(State_Vafmt_PreHandler_Called, int, std::string);
1.35 + MAKE_STATE_2(State_Vafmt_PostHandler_Called, int, std::string);
1.36
1.37 - struct State_EatYams_Handler_Called : State
1.38 - {
1.39 - int m_Overload;
1.40 - State_EatYams_Handler_Called(int ovrld) : m_Overload(ovrld)
1.41 - {
1.42 - }
1.43 - bool IsEqual(State *other)
1.44 - {
1.45 - State_EatYams_Handler_Called *other2 = dynamic_cast<State_EatYams_Handler_Called*>(other);
1.46 - if (!other2)
1.47 - return false;
1.48 - return other2->m_Overload == m_Overload;
1.49 - }
1.50 - };
1.51 -
1.52 -
1.53 - // TEST1
1.54 - // Basic tests
1.55 - // Hooking and callclass
1.56 class IGaben
1.57 {
1.58 public:
1.59 @@ -54,22 +29,63 @@
1.60 ADD_STATE(State_EatYams_Called(1));
1.61 return true;
1.62 }
1.63 + virtual void Vafmt1(bool param1, int x, const char *fmt, ...)
1.64 + {
1.65 + va_list ap;
1.66 + va_start(ap, fmt);
1.67 + char buffer[512];
1.68 + vsprintf(buffer, fmt, ap);
1.69 + va_end(ap);
1.70 + ADD_STATE(State_Vafmt_Called(1, std::string(buffer)));
1.71 + }
1.72 + virtual float Vafmt2(const char *fmt, ...)
1.73 + {
1.74 + va_list ap;
1.75 + va_start(ap, fmt);
1.76 + char buffer[512];
1.77 + vsprintf(buffer, fmt, ap);
1.78 + va_end(ap);
1.79 + ADD_STATE(State_Vafmt_Called(2, std::string(buffer)));
1.80 + return 0.0f;
1.81 + }
1.82 };
1.83
1.84 SH_DECL_HOOK0_void(IGaben, EatYams, SH_NOATTRIB, 0);
1.85 SH_DECL_HOOK1(IGaben, EatYams, const, 1, bool, const char *);
1.86 + SH_DECL_HOOK2_void_vafmt(IGaben, Vafmt1, SH_NOATTRIB, 0, bool, int);
1.87 + SH_DECL_HOOK0_vafmt(IGaben, Vafmt2, SH_NOATTRIB, 0, float);
1.88 +
1.89 + void EatYams0_Handler()
1.90 + {
1.91 + ADD_STATE(State_EatYams_Handler_Called(0));
1.92 + }
1.93 +
1.94 + bool EatYams1_Handler(const char *loc)
1.95 + {
1.96 + ADD_STATE(State_EatYams_Handler_Called(1));
1.97 + return true;
1.98 + }
1.99 +
1.100 + void Vafmt1_PreHandler(bool param1, int x, const char *in)
1.101 + {
1.102 + ADD_STATE(State_Vafmt_PreHandler_Called(1, std::string(in)));
1.103 + }
1.104 + void Vafmt1_PostHandler(bool param1, int x, const char *in)
1.105 + {
1.106 + ADD_STATE(State_Vafmt_PostHandler_Called(1, std::string(in)));
1.107 + }
1.108 + float Vafmt2_PreHandler(const char *in)
1.109 + {
1.110 + ADD_STATE(State_Vafmt_PreHandler_Called(2, std::string(in)));
1.111 + return 0.0f;
1.112 + }
1.113 + float Vafmt2_PostHandler(const char *in)
1.114 + {
1.115 + ADD_STATE(State_Vafmt_PostHandler_Called(2, std::string(in)));
1.116 + return 0.0f;
1.117 + }
1.118 }
1.119
1.120 -void EatYams0_Handler()
1.121 -{
1.122 - ADD_STATE(State_EatYams_Handler_Called(0));
1.123 -}
1.124 -
1.125 -bool EatYams1_Handler(const char *loc)
1.126 -{
1.127 - ADD_STATE(State_EatYams_Handler_Called(1));
1.128 - return true;
1.129 -}
1.130
1.131 bool TestVafmtAndOverload(std::string &error)
1.132 {
1.133 @@ -82,6 +98,7 @@
1.134
1.135 SourceHook::CallClass<IGaben> *cc = SH_GET_CALLCLASS(pGab);
1.136
1.137 + // Part 1
1.138 SH_CALL(cc, static_cast<void (IGaben::*)()>(&IGaben::EatYams))();
1.139 SH_CALL(cc, static_cast<bool (IGaben::*)(const char *) const>(&IGaben::EatYams))("Here!");
1.140
1.141 @@ -103,6 +120,52 @@
1.142 new State_EatYams_Called(1),
1.143 NULL),"Part 1");
1.144
1.145 + // Part 2
1.146 + pGab->Vafmt1(true, 55, "Hello %s%d%s", "BA", 1, "L");
1.147 + SH_CALL(cc, &IGaben::Vafmt1)(true, 55, "Hello %s%d%s", "BA", 1, "L");
1.148 + pGab->Vafmt2("Hello %s%d%s", "BA", 1, "LOPAN");
1.149 + SH_CALL(cc, &IGaben::Vafmt2)("Hello %s%d%s", "BA", 1, "LOPAN");
1.150 +
1.151 + CHECK_STATES((&g_States,
1.152 + new State_Vafmt_Called(1, "Hello BA1L"),
1.153 + new State_Vafmt_Called(1, "Hello BA1L"),
1.154 + new State_Vafmt_Called(2, "Hello BA1LOPAN"),
1.155 + new State_Vafmt_Called(2, "Hello BA1LOPAN"),
1.156 + NULL), "Part 2");
1.157 +
1.158 + // Part 3
1.159 + SH_ADD_HOOK(IGaben, Vafmt1, pGab, Vafmt1_PreHandler, false);
1.160 + SH_ADD_HOOK(IGaben, Vafmt1, pGab, Vafmt1_PostHandler, true);
1.161 + SH_ADD_HOOK(IGaben, Vafmt2, pGab, Vafmt2_PreHandler, false);
1.162 + SH_ADD_HOOK(IGaben, Vafmt2, pGab, Vafmt2_PostHandler, true);
1.163 +
1.164 + pGab->Vafmt1(true, 55, "Hello %s%d%s", "BA", 1, "L");
1.165 + pGab->Vafmt2("Hello %s%d%s", "BA", 1, "LOPAN");
1.166 +
1.167 + CHECK_STATES((&g_States,
1.168 + new State_Vafmt_PreHandler_Called(1, std::string("Hello BA1L")),
1.169 + new State_Vafmt_Called(1, std::string("Hello BA1L")),
1.170 + new State_Vafmt_PostHandler_Called(1, std::string("Hello BA1L")),
1.171 +
1.172 + new State_Vafmt_PreHandler_Called(2, std::string("Hello BA1LOPAN")),
1.173 + new State_Vafmt_Called(2, std::string("Hello BA1LOPAN")),
1.174 + new State_Vafmt_PostHandler_Called(2, std::string("Hello BA1LOPAN")),
1.175 + NULL), "Part 3");
1.176 +
1.177 + // Part 4
1.178 + SH_REMOVE_HOOK(IGaben, Vafmt1, pGab, Vafmt1_PreHandler, false);
1.179 + SH_REMOVE_HOOK(IGaben, Vafmt1, pGab, Vafmt1_PostHandler, true);
1.180 + SH_REMOVE_HOOK(IGaben, Vafmt2, pGab, Vafmt2_PreHandler, false);
1.181 + SH_REMOVE_HOOK(IGaben, Vafmt2, pGab, Vafmt2_PostHandler, true);
1.182 +
1.183 + pGab->Vafmt1(true, 55, "Hello %s%d%s", "BA", 1, "L");
1.184 + pGab->Vafmt2("Hello %s%d%s", "BA", 1, "LOPAN");
1.185 +
1.186 + CHECK_STATES((&g_States,
1.187 + new State_Vafmt_Called(1, "Hello BA1L"),
1.188 + new State_Vafmt_Called(2, "Hello BA1LOPAN"),
1.189 + NULL), "Part 4");
1.190 +
1.191 SH_RELEASE_CALLCLASS(cc);
1.192
1.193 return true;