core/systems/ForwardSys.h
author David Anderson <dvander@alliedmods.net>
Fri Feb 16 03:47:10 2007 +0000 (2007-02-16)
changeset 501 e209a7296936
parent 464 765517f654a5
child 526 bcb82491f9df
permissions -rw-r--r--
removed this potential feature, not useful enough
     1 /**
     2  * ===============================================================
     3  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     4  * ===============================================================
     5  *
     6  * This file is not open source and may not be copied without explicit
     7  * written permission of AlliedModders LLC.  This file may not be redistributed 
     8  * in whole or significant part.
     9  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    10  *
    11  * Version: $Id$
    12  */
    13 
    14 #ifndef _INCLUDE_SOURCEMOD_FORWARDSYSTEM_H_
    15 #define _INCLUDE_SOURCEMOD_FORWARDSYSTEM_H_
    16 
    17 #include <IForwardSys.h>
    18 #include <IPluginSys.h>
    19 #include "sm_globals.h"
    20 #include <sh_list.h>
    21 #include <sh_stack.h>
    22 #include "sourcemod.h"
    23 
    24 using namespace SourceHook;
    25 
    26 typedef List<IPluginFunction *>::iterator FuncIter;
    27 
    28 //:TODO: a global name max define for sourcepawn, should mirror compiler's sNAMEMAX
    29 #define FORWARDS_NAME_MAX		64
    30 
    31 struct ByrefInfo
    32 {
    33 	unsigned int cells;
    34 	cell_t *orig_addr;
    35 	int flags;
    36 	int sz_flags;
    37 };
    38 
    39 struct FwdParamInfo
    40 {
    41 	cell_t val;
    42 	ByrefInfo byref;
    43 	ParamType pushedas;
    44 };
    45 
    46 class CForward : public IChangeableForward
    47 {
    48 public: //ICallable
    49 	virtual int PushCell(cell_t cell);
    50 	virtual int PushCellByRef(cell_t *cell, int flags);
    51 	virtual int PushFloat(float number);
    52 	virtual int PushFloatByRef(float *number, int flags);
    53 	virtual int PushArray(cell_t *inarray, unsigned int cells, cell_t **phys_addr, int flags);
    54 	virtual int PushString(const char *string);
    55 	virtual int PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags);
    56 	virtual void Cancel();
    57 public: //IForward
    58 	virtual const char *GetForwardName() const;
    59 	virtual unsigned int GetFunctionCount() const;
    60 	virtual ExecType GetExecType() const;
    61 	virtual int Execute(cell_t *result, IForwardFilter *filter);
    62 public: //IChangeableForward
    63 	virtual bool RemoveFunction(IPluginFunction *func);
    64 	virtual unsigned int RemoveFunctionsOfPlugin(IPlugin *plugin);
    65 	virtual bool AddFunction(IPluginFunction *func);
    66 	virtual bool AddFunction(IPluginContext *ctx, funcid_t index);
    67 public:
    68 	static CForward *CreateForward(const char *name, 
    69 								   ExecType et, 
    70 								   unsigned int num_params, 
    71 								   ParamType *types, 
    72 								   va_list ap);
    73 	void PushPausedFunctions(IPlugin *plugin);
    74 	void PopPausedFunctions(IPlugin *plugin);
    75 private:
    76 	void _Int_PushArray(cell_t *inarray, unsigned int cells, int flags);
    77 	void _Int_PushString(cell_t *inarray, unsigned int cells, int sz_flags, int cp_flags);
    78 	inline int SetError(int err)
    79 	{
    80 		m_errstate = err;
    81 		return err;
    82 	}
    83 protected:
    84 	/* :TODO: I want a caching list type here.
    85 	 * Destroying these things and using new/delete for their members feels bad.
    86 	 */
    87 	mutable List<IPluginFunction *> m_functions;
    88 	List<IPluginFunction *> m_paused;
    89 
    90 	/* Type and name information */
    91 	FwdParamInfo m_params[SP_MAX_EXEC_PARAMS];
    92 	ParamType m_types[SP_MAX_EXEC_PARAMS];
    93 	char m_name[FORWARDS_NAME_MAX+1];
    94 	unsigned int m_numparams;
    95 	unsigned int m_varargs;
    96 	ExecType m_ExecType;
    97 
    98 	/* State information */
    99 	unsigned int m_curparam;
   100 	int m_errstate;
   101 };
   102 
   103 class CForwardManager : 
   104 	public IForwardManager,
   105 	public IPluginsListener,
   106 	public SMGlobalClass
   107 {
   108 	friend class CForward;
   109 public: //IForwardManager
   110 	IForward *CreateForward(const char *name, 
   111 		ExecType et, 
   112 		unsigned int num_params, 
   113 		ParamType *types, 
   114 		...);
   115 	IChangeableForward *CreateForwardEx(const char *name, 
   116 		ExecType et, 
   117 		int num_params, 
   118 		ParamType *types, 
   119 		...);
   120 	IForward *FindForward(const char *name, IChangeableForward **ifchng);
   121 	void ReleaseForward(IForward *forward);
   122 public: //IPluginsListener
   123 	void OnPluginLoaded(IPlugin *plugin);
   124 	void OnPluginUnloaded(IPlugin *plugin);
   125 	void OnPluginPauseChange(IPlugin *plugin, bool paused);
   126 public: //SMGlobalClass
   127 	void OnSourceModAllInitialized();
   128 	void OnSourceModShutdown();
   129 protected:
   130 	CForward *ForwardMake();
   131 	void ForwardFree(CForward *fwd);
   132 private:
   133 	CStack<CForward *> m_FreeForwards;
   134 	List<CForward *> m_managed;
   135 	List<CForward *> m_unmanaged;
   136 };
   137 
   138 extern CForwardManager g_Forwards;
   139 
   140 #endif //_INCLUDE_SOURCEMOD_FORWARDSYSTEM_H_