core/interfaces/IPluginFunction.h
author David Anderson <dvander@alliedmods.net>
Sun Nov 12 01:06:17 2006 +0000 (2006-11-12)
changeset 180 fe406c5f2aeb
parent 177 d29ade52d953
child 181 1e1570518051
permissions -rw-r--r--
Initial import of forward system
     1 #ifndef _INCLUDE_SOURCEMOD_PLUGINFUNCTION_INTERFACE_H_
     2 #define _INCLUDE_SOURCEMOD_PLUGINFUNCTION_INTERFACE_H_
     3 
     4 #include <IPluginSys.h>
     5 
     6 namespace SourceMod
     7 {
     8 	#define SMFUNC_COPYBACK_NONE	(0)			/* Never copy an array back */
     9 	#define SMFUNC_COPYBACK_ONCE	(1<<0)		/* Copy an array back after call */
    10 	#define SMFUNC_COPYBACK_ALWAYS	(1<<1)		/* Copy an array back after subsequent calls (forwards) */
    11 
    12 	/**
    13 	 * @brief Represents what a function needs to implement in order to be callable.
    14 	 */
    15 	class ICallable
    16 	{
    17 	public:
    18 		/**
    19 		 * @brief Pushes a cell onto the current call.
    20 		 *
    21 		 * @param cell		Parameter value to push.
    22 		 * @return			Error code, if any.
    23 		 */
    24 		virtual int PushCell(cell_t cell) =0;
    25 
    26 		/**
    27 		 * @brief Pushes a cell by reference onto the current call.
    28 		 * NOTE: On Execute, the pointer passed will be modified if copyback is enabled.
    29 		 *
    30 		 * @param cell		Address containing parameter value to push.
    31 		 * @param flags		Copy-back flags.
    32 		 * @return			Error code, if any.
    33 		 */
    34 		virtual int PushCellByRef(cell_t *cell, int flags) =0;
    35 
    36 		/**
    37 		 * @brief Pushes a float onto the current call.
    38 		 *
    39 		 * @param float		Parameter value to push.
    40 		 * @return			Error code, if any.
    41 		 */
    42 		virtual int PushFloat(float number) =0;
    43 
    44 		/**
    45 		 * @brief Pushes a float onto the current call by reference.
    46 		 * NOTE: On Execute, the pointer passed will be modified if copyback is enabled.
    47 		 *
    48 		 * @param float		Parameter value to push.
    49 		 & @param flags		Copy-back flags.
    50 		 * @return			Error code, if any.
    51 		 */
    52 		virtual int PushFloatByRef(float *number, int flags) =0;
    53 
    54 		/**
    55 		 * @brief Pushes an array of cells onto the current call, each cell individually.
    56 		 * NOTE: This is essentially a crippled version of PushArray().
    57 		 *
    58 		 * @param array		Array of cells.
    59 		 * @param numcells	Number of cells in array.
    60 		 * @param each		Whether or not to push as an array or individual cells.
    61 		 * @return			Error code, if any.
    62 		 */
    63 		virtual int PushCells(cell_t array[], unsigned int numcells, bool each) =0;
    64 
    65 		/**
    66 		 * @brief Pushes an array of cells onto the current call.  
    67 		 * NOTE: On Execute, the pointer passed will be modified if non-NULL and copy-back
    68 		 * is enabled.
    69 		 *
    70 		 * @param array		Array to copy, NULL if no initial array should be copied.
    71 		 * @param cells		Number of cells to allocate and optionally read from the input array.
    72 		 * @param phys_addr	Optional return address for physical array, if one was made.
    73 		 * @param flags		Whether or not changes should be copied back to the input array.
    74 		 * @return			Error code, if any.
    75 		 */
    76 		virtual int PushArray(cell_t *inarray, 
    77 								unsigned int cells, 
    78 								cell_t **phys_addr, 
    79 								int flags=SMFUNC_COPYBACK_NONE) =0;
    80 
    81 		/**
    82 		 * @brief Pushes a string onto the current call.
    83 		 *
    84 		 * @param string	String to push.
    85 		 * @return			Error code, if any.
    86 		 */
    87 		virtual int PushString(const char *string) =0;
    88 
    89 		/**
    90 		 * @brief Pushes a string onto the current call.
    91 		 * NOTE: On Execute, the pointer passed will be modified if copy-back is enabled.
    92 		 *
    93 		 * @param string	String to push.
    94 		 * @param flags		Copy-back flags.
    95 		 * @return			Error code, if any.
    96 		 */
    97 		virtual int PushStringByRef(char *string, int flags) =0;
    98 
    99 		/**
   100 		 * @brief Cancels a function call that is being pushed but not yet executed.
   101 		 * This can be used be reset for CallFunction() use.
   102 		 */
   103 		virtual void Cancel() =0;
   104 	};
   105 
   106 
   107 	/**
   108 	 * @brief Used for copy-back notification
   109 	 */
   110 	class IFunctionCopybackReader
   111 	{
   112 	public:
   113 		/**
   114 		 * @brief Called before an array is copied back.
   115 		 * 
   116 		 * @param param			Parameter index.
   117 		 * @param cells			Number of cells in the array.
   118 		 * @param source_addr	Source address in the plugin that will be copied.
   119 		 * @param orig_addr		Destination addres in plugin that will be overwritten.
   120 		 * @param flags			Copy flags.
   121 		 * @return				True to copy back, false otherwise.
   122 		 */
   123 		virtual bool OnCopybackArray(unsigned int param, 
   124 								unsigned int cells,
   125 								cell_t *source_addr,
   126 								cell_t *orig_addr,
   127 								int flags) =0;
   128 	};
   129 
   130 	/**
   131 	 * @brief Encapsulates a function call in a plugin.
   132 	 * NOTE: Function calls must be atomic to one execution context.
   133 	 * NOTE: This object should not be deleted.  It lives for the lifetime of the plugin.
   134 	 */
   135 	class IPluginFunction : public ICallable
   136 	{
   137 	public:
   138 		/**
   139 		 * @brief Executes the forward, resets the pushed parameter list, and performs any copybacks.
   140 		 *
   141 		 * @param result		Pointer to store return value in.
   142 		 * @param reader		Copy-back listener.  NULL to specify 
   143 		 * @return				Error code, if any.
   144 		 */
   145 		virtual int Execute(cell_t *result, IFunctionCopybackReader *reader) =0;
   146 
   147 		/**
   148 		 * @brief Executes the function with the given parameter array.
   149 		 * Parameters are read in forward order (i.e. index 0 is parameter #1)
   150 		 * NOTE: You will get an error if you attempt to use CallFunction() with
   151 		 * previously pushed parameters.
   152 		 *
   153 		 * @param param			Array of cell parameters.
   154 		 * @param num_params	Number of parameters to push.
   155 		 * @param result		Pointer to store result of function on return.
   156 		 * @return				SourcePawn error code (if any).
   157 		 */
   158 		virtual int CallFunction(const cell_t *params, unsigned int num_params, cell_t *result) =0;
   159 
   160 		/**
   161 		 * @brief Returns which plugin this function belongs to.
   162 		 *
   163 		 * @return				IPlugin pointer to parent plugin.
   164 		 */
   165 		virtual IPlugin *GetParentPlugin() =0;
   166 
   167 		/**
   168 		 * @brief Returns the physical address of a by-reference parameter.
   169 		 *
   170 		 * @param				Parameter index to read (beginning at 0).
   171 		 * @return				Address, or NULL if invalid parameter specified.
   172 		 */
   173 		virtual cell_t *GetAddressOfPushedParam(unsigned int param) =0;
   174 
   175 #if 0
   176 		/**
   177 		 * @brief Clones the function object.  This can be used to maintain a private copy.
   178 		 * The copy retains no push states and must be freeed with ReleaseCopy().
   179 		 */
   180 		virtual IPluginFunction *Copy() =0;
   181 
   182 		/**
   183 		 * @brief If this object is a clone, this function must be called to destroy it.
   184 		 */
   185 		virtual void ReleaseCopy() =0;
   186 
   187 		/**
   188 		 * @brief Returns original function pointer, if any.
   189 		 * Note: IsCopyOf() will return the original non-copy, even if used
   190 		 * from a copy.
   191 		 *
   192 		 * @return				The original object that this was sourced from.
   193 		 */
   194 		virtual IPluginFunction *IsCopyOf() =0;
   195 #endif
   196 	};
   197 };
   198 
   199 #endif //_INCLUDE_SOURCEMOD_PLUGINFUNCTION_INTERFACE_H_