core/systems/ForwardSys.h
author David Anderson <dvander@alliedmods.net>
Wed Jan 17 03:01:38 2007 +0000 (2007-01-17)
changeset 304 fa485d2f25ed
parent 211 4c77142eaf36
child 327 e244337a9b38
permissions -rw-r--r--
Added extension loading/unloading
Extended SDK for interface sharing
Completed Metamod extension support
     1 #ifndef _INCLUDE_SOURCEMOD_FORWARDSYSTEM_H_
     2 #define _INCLUDE_SOURCEMOD_FORWARDSYSTEM_H_
     3 
     4 #include <IForwardSys.h>
     5 #include <IPluginSys.h>
     6 #include "sm_globals.h"
     7 #include <sh_list.h>
     8 #include <sh_stack.h>
     9 #include "sourcemod.h"
    10 
    11 using namespace SourceHook;
    12 
    13 typedef List<IPluginFunction *>::iterator FuncIter;
    14 
    15 //:TODO: a global name max define for sourcepawn, should mirror compiler's sNAMEMAX
    16 #define FORWARDS_NAME_MAX		64
    17 
    18 struct ByrefInfo
    19 {
    20 	unsigned int cells;
    21 	cell_t *orig_addr;
    22 	int flags;
    23 	int sz_flags;
    24 };
    25 
    26 struct FwdParamInfo
    27 {
    28 	cell_t val;
    29 	ByrefInfo byref;
    30 	ParamType pushedas;
    31 };
    32 
    33 class CForward : public IChangeableForward
    34 {
    35 public: //ICallable
    36 	virtual int PushCell(cell_t cell);
    37 	virtual int PushCellByRef(cell_t *cell, int flags);
    38 	virtual int PushFloat(float number);
    39 	virtual int PushFloatByRef(float *number, int flags);
    40 	virtual int PushArray(cell_t *inarray, unsigned int cells, cell_t **phys_addr, int flags);
    41 	virtual int PushString(const char *string);
    42 	virtual int PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags);
    43 	virtual void Cancel();
    44 public: //IForward
    45 	virtual const char *GetForwardName();
    46 	virtual unsigned int GetFunctionCount();
    47 	virtual ExecType GetExecType();
    48 	virtual int Execute(cell_t *result, IForwardFilter *filter);
    49 public: //IChangeableForward
    50 	virtual bool RemoveFunction(IPluginFunction *func);
    51 	virtual unsigned int RemoveFunctionsOfPlugin(IPlugin *plugin);
    52 	virtual bool AddFunction(IPluginFunction *func);
    53 	virtual bool AddFunction(sp_context_t *ctx, funcid_t index);
    54 public:
    55 	static CForward *CreateForward(const char *name, 
    56 								   ExecType et, 
    57 								   unsigned int num_params, 
    58 								   ParamType *types, 
    59 								   va_list ap);
    60 private:
    61 	void _Int_PushArray(cell_t *inarray, unsigned int cells, int flags);
    62 	void _Int_PushString(cell_t *inarray, unsigned int cells, int sz_flags, int cp_flags);
    63 	inline int SetError(int err)
    64 	{
    65 		m_errstate = err;
    66 		return err;
    67 	}
    68 protected:
    69 	/* :TODO: I want a caching list type here.
    70 	 * Destroying these things and using new/delete for their members feels bad.
    71 	 */
    72 	List<IPluginFunction *> m_functions;
    73 
    74 	/* Type and name information */
    75 	FwdParamInfo m_params[SP_MAX_EXEC_PARAMS];
    76 	ParamType m_types[SP_MAX_EXEC_PARAMS];
    77 	char m_name[FORWARDS_NAME_MAX+1];
    78 	unsigned int m_numparams;
    79 	unsigned int m_varargs;
    80 	ExecType m_ExecType;
    81 
    82 	/* State information */
    83 	unsigned int m_curparam;
    84 	int m_errstate;
    85 };
    86 
    87 class CForwardManager : 
    88 	public IForwardManager,
    89 	public IPluginsListener,
    90 	public SMGlobalClass
    91 {
    92 	friend class CForward;
    93 public: //IForwardManager
    94 	virtual IForward *CreateForward(const char *name, 
    95 		ExecType et, 
    96 		unsigned int num_params, 
    97 		ParamType *types, 
    98 		...);
    99 	virtual IChangeableForward *CreateForwardEx(const char *name, 
   100 		ExecType et, 
   101 		int num_params, 
   102 		ParamType *types, 
   103 		...);
   104 	virtual IForward *FindForward(const char *name, IChangeableForward **ifchng);
   105 	virtual void ReleaseForward(IForward *forward);
   106 public: //IPluginsListener
   107 	virtual void OnPluginLoaded(IPlugin *plugin);
   108 	virtual void OnPluginUnloaded(IPlugin *plugin);
   109 public: //SMGlobalClass
   110 	virtual void OnSourceModAllInitialized();
   111 	virtual void OnSourceModShutdown();
   112 protected:
   113 	CForward *ForwardMake();
   114 	void ForwardFree(CForward *fwd);
   115 private:
   116 	CStack<CForward *> m_FreeForwards;
   117 	List<CForward *> m_managed;
   118 	List<CForward *> m_unmanaged;
   119 };
   120 
   121 extern CForwardManager g_Forwards;
   122 
   123 #endif //_INCLUDE_SOURCEMOD_FORWARDSYSTEM_H_