core/interfaces/IPluginFunction.h
author David Anderson <dvander@alliedmods.net>
Sun Nov 12 09:51:39 2006 +0000 (2006-11-12)
changeset 183 78488edd8873
parent 181 1e1570518051
child 185 5e0987d38ee0
permissions -rw-r--r--
rewrote forward API to use cached parameter pushing instead of immediate pushing
removed copy back cruft since it's no longer needed
removed PushCells() from API requirements, not needed
adjusted documentation and added TODO list to ForwardSys.cpp
various internal improvements
     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 SM_FUNCFLAG_COPYBACK		(1<<0)		/* Copy an array/reference back after call */
     9 
    10 	/**
    11 	 * @brief Represents what a function needs to implement in order to be callable.
    12 	 */
    13 	class ICallable
    14 	{
    15 	public:
    16 		/**
    17 		 * @brief Pushes a cell onto the current call.
    18 		 *
    19 		 * @param cell		Parameter value to push.
    20 		 * @return			Error code, if any.
    21 		 */
    22 		virtual int PushCell(cell_t cell) =0;
    23 
    24 		/**
    25 		 * @brief Pushes a cell by reference onto the current call.
    26 		 * NOTE: On Execute, the pointer passed will be modified if copyback is enabled.
    27 		 * NOTE: By reference parameters are cached and thus are not read until execution.
    28 		 *		 This means you cannot push a pointer, change it, and push it again and expect
    29 		 *       two different values to come out.
    30 		 *
    31 		 * @param cell		Address containing parameter value to push.
    32 		 * @param flags		Copy-back flags.
    33 		 * @return			Error code, if any.
    34 		 */
    35 		virtual int PushCellByRef(cell_t *cell, int flags) =0;
    36 
    37 		/**
    38 		 * @brief Pushes a float onto the current call.
    39 		 *
    40 		 * @param float		Parameter value to push.
    41 		 * @return			Error code, if any.
    42 		 */
    43 		virtual int PushFloat(float number) =0;
    44 
    45 		/**
    46 		 * @brief Pushes a float onto the current call by reference.
    47 		 * NOTE: On Execute, the pointer passed will be modified if copyback is enabled.
    48 		 * NOTE: By reference parameters are cached and thus are not read until execution.
    49 		 *		 This means you cannot push a pointer, change it, and push it again and expect
    50 		 *       two different values to come out.
    51 		 *
    52 		 * @param float		Parameter value to push.
    53 		 & @param flags		Copy-back flags.
    54 		 * @return			Error code, if any.
    55 		 */
    56 		virtual int PushFloatByRef(float *number, int flags) =0;
    57 
    58 		/**
    59 		 * @brief Pushes an array of cells onto the current call.  
    60 		 * NOTE: On Execute, the pointer passed will be modified if non-NULL and copy-back
    61 		 * is enabled.
    62 		 * NOTE: By reference parameters are cached and thus are not read until execution.
    63 		 *		 This means you cannot push a pointer, change it, and push it again and expect
    64 		 *       two different values to come out.
    65 		 *
    66 		 * @param array		Array to copy, NULL if no initial array should be copied.
    67 		 * @param cells		Number of cells to allocate and optionally read from the input array.
    68 		 * @param phys_addr	Optional return address for physical array, if one was made.
    69 		 * @param flags		Whether or not changes should be copied back to the input array.
    70 		 * @return			Error code, if any.
    71 		 */
    72 		virtual int PushArray(cell_t *inarray, 
    73 								unsigned int cells, 
    74 								cell_t **phys_addr, 
    75 								int flags=0) =0;
    76 
    77 		/**
    78 		 * @brief Pushes a string onto the current call.
    79 		 *
    80 		 * @param string	String to push.
    81 		 * @return			Error code, if any.
    82 		 */
    83 		virtual int PushString(const char *string) =0;
    84 
    85 		/**
    86 		 * @brief Pushes a string onto the current call.
    87 		 * NOTE: On Execute, the pointer passed will be modified if copy-back is enabled.
    88 		 *
    89 		 * @param string	String to push.
    90 		 * @param flags		Copy-back flags.
    91 		 * @return			Error code, if any.
    92 		 */
    93 		virtual int PushStringEx(char *string, int flags) =0;
    94 
    95 		/**
    96 		 * @brief Cancels a function call that is being pushed but not yet executed.
    97 		 * This can be used be reset for CallFunction() use.
    98 		 */
    99 		virtual void Cancel() =0;
   100 	};
   101 
   102 	/**
   103 	 * @brief Encapsulates a function call in a plugin.
   104 	 * NOTE: Function calls must be atomic to one execution context.
   105 	 * NOTE: This object should not be deleted.  It lives for the lifetime of the plugin.
   106 	 */
   107 	class IPluginFunction : public ICallable
   108 	{
   109 	public:
   110 		/**
   111 		 * @brief Executes the forward, resets the pushed parameter list, and performs any copybacks.
   112 		 *
   113 		 * @param result		Pointer to store return value in.
   114 		 * @param reader		Copy-back listener.  NULL to specify 
   115 		 * @return				Error code, if any.
   116 		 */
   117 		virtual int Execute(cell_t *result) =0;
   118 
   119 		/**
   120 		 * @brief Executes the function with the given parameter array.
   121 		 * Parameters are read in forward order (i.e. index 0 is parameter #1)
   122 		 * NOTE: You will get an error if you attempt to use CallFunction() with
   123 		 * previously pushed parameters.
   124 		 *
   125 		 * @param param			Array of cell parameters.
   126 		 * @param num_params	Number of parameters to push.
   127 		 * @param result		Pointer to store result of function on return.
   128 		 * @return				SourcePawn error code (if any).
   129 		 */
   130 		virtual int CallFunction(const cell_t *params, unsigned int num_params, cell_t *result) =0;
   131 
   132 		/**
   133 		 * @brief Returns which plugin this function belongs to.
   134 		 *
   135 		 * @return				IPlugin pointer to parent plugin.
   136 		 */
   137 		virtual IPlugin *GetParentPlugin() =0;
   138 
   139 		/**
   140 		 * @brief Returns the physical address of a by-reference parameter.
   141 		 *
   142 		 * @param				Parameter index to read (beginning at 0).
   143 		 * @return				Address, or NULL if invalid parameter specified.
   144 		 */
   145 		virtual cell_t *GetAddressOfPushedParam(unsigned int param) =0;
   146 	};
   147 };
   148 
   149 #endif //_INCLUDE_SOURCEMOD_PLUGINFUNCTION_INTERFACE_H_