From 13d61ec39f89d54b2c998a099a2b51fada97b8b7 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Tue, 14 Nov 2006 08:45:21 +0000 Subject: [PATCH] added implementation to the forward manager --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40191 --- core/interfaces/IForwardSys.h | 11 ++++ core/systems/ForwardSys.cpp | 98 ++++++++++++++++++++++++++++++++++- core/systems/ForwardSys.h | 27 ++++++++++ 3 files changed, 134 insertions(+), 2 deletions(-) diff --git a/core/interfaces/IForwardSys.h b/core/interfaces/IForwardSys.h index e815aee4..d4732899 100644 --- a/core/interfaces/IForwardSys.h +++ b/core/interfaces/IForwardSys.h @@ -98,6 +98,10 @@ namespace SourceMod */ class IForward : public ICallable { + public: + virtual ~IForward() + { + } public: /** * @brief Returns the name of the forward. @@ -271,6 +275,13 @@ namespace SourceMod * @return IForward pointer, or NULL if none found matching the name. */ virtual IForward *FindForward(const char *name, IChangeableForward **ifchng) =0; + + /** + * @brief Frees and destroys a forward object. + * + * @param forward An IForward created by CreateForward() or CreateForwardEx(). + */ + virtual void ReleaseForward(IForward *forward) =0; }; }; diff --git a/core/systems/ForwardSys.cpp b/core/systems/ForwardSys.cpp index 833d5321..ad2a8174 100644 --- a/core/systems/ForwardSys.cpp +++ b/core/systems/ForwardSys.cpp @@ -2,6 +2,8 @@ #include "ForwardSys.h" #include "PluginSys.h" +CForwardManager g_Forwards; + /** * Gensis turns to its source, reduction occurs stepwise although the essence is all one. * End of line. FTL system check. @@ -12,7 +14,7 @@ * NORMAL FUNCTIONS: * X Push cells * X Push cells byref (copyback tested = yes) - * - Push floats (copyback tested = ??) + * - Push floats * - Push floats byref (copyback tested = ??) * - Push arrays (copyback tested = ??) * - Push strings (copyback tested = ??) @@ -26,6 +28,98 @@ * - Push vararg strings (copyback tested = ??) */ +IForward *CForwardManager::CreateForward(const char *name, ExecType et, unsigned int num_params, ParamType *types, ...) +{ + CForward *fwd; + va_list ap; + va_start(ap, types); + + fwd = CForward::CreateForward(name, et, num_params, types, ap); + + va_end(ap); + + return fwd; +} + +IChangeableForward *CForwardManager::CreateForwardEx(const char *name, ExecType et, int num_params, ParamType *types, ...) +{ + CForward *fwd; + va_list ap; + va_start(ap, types); + + fwd = CForward::CreateForward(name, et, num_params, types, ap); + + va_end(ap); + + return fwd; +} + +IForward *CForwardManager::FindForward(const char *name, IChangeableForward **ifchng) +{ + List::iterator iter; + CForward *fwd; + + for (iter=m_managed.begin(); iter!=m_managed.end(); iter++) + { + fwd = (*iter); + if (strcmp(fwd->GetForwardName(), name) == 0) + { + if (ifchng) + { + *ifchng = NULL; + } + return fwd; + } + } + + for (iter=m_unmanaged.begin(); iter!=m_unmanaged.end(); iter++) + { + fwd = (*iter); + if (strcmp(fwd->GetForwardName(), name) == 0) + { + if (ifchng) + { + *ifchng = fwd; + } + return fwd; + } + } + + if (ifchng) + { + *ifchng = NULL; + } + + return NULL; +} + +void CForwardManager::ReleaseForward(IForward *forward) +{ + ForwardFree(static_cast(forward)); +} + +void CForwardManager::ForwardFree(CForward *fwd) +{ + m_FreeForwards.push(fwd); +} + +CForward *CForwardManager::ForwardMake() +{ + CForward *fwd; + if (m_FreeForwards.empty()) + { + fwd = new CForward; + } else { + fwd = m_FreeForwards.front(); + m_FreeForwards.pop(); + } + return fwd; +} + +/************************************* + * ACTUAL FORWARD API IMPLEMENTATION * + *************************************/ + CForward *CForward::CreateForward(const char *name, ExecType et, unsigned int num_params, ParamType *types, va_list ap) { ParamType _types[SP_MAX_EXEC_PARAMS]; @@ -62,7 +156,7 @@ CForward *CForward::CreateForward(const char *name, ExecType et, unsigned int nu return NULL; } - CForward *pForward = new CForward; + CForward *pForward = g_Forwards.ForwardMake(); pForward->m_curparam = 0; pForward->m_ExecType = et; snprintf(pForward->m_name, FORWARDS_NAME_MAX, "%s", name ? name : ""); diff --git a/core/systems/ForwardSys.h b/core/systems/ForwardSys.h index 31a4089d..b286284b 100644 --- a/core/systems/ForwardSys.h +++ b/core/systems/ForwardSys.h @@ -81,4 +81,31 @@ protected: int m_errstate; }; +class CForwardManager : public IForwardManager +{ + friend class CForward; +public: + virtual IForward *CreateForward(const char *name, + ExecType et, + unsigned int num_params, + ParamType *types, + ...); + virtual IChangeableForward *CreateForwardEx(const char *name, + ExecType et, + int num_params, + ParamType *types, + ...); + virtual IForward *FindForward(const char *name, IChangeableForward **ifchng); + virtual void ReleaseForward(IForward *forward); +protected: + CForward *ForwardMake(); + void ForwardFree(CForward *fwd); +private: + CStack m_FreeForwards; + List m_managed; + List m_unmanaged; +}; + +extern CForwardManager g_Forwards; + #endif //_INCLUDE_SOURCEMOD_FORWARDSYSTEM_H_