public/extensions/IBinTools.h
author David Anderson <dvander@alliedmods.net>
Sat Jun 16 01:34:41 2007 +0000 (2007-06-16)
changeset 942 5a930535b6d0
parent 739 d22243bb5864
child 1226 478550a4d5ec
permissions -rw-r--r--
added ICallWrapper destruction
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod, Copyright (C) 2004-2007 AlliedModders LLC. 
     5  * All rights reserved.
     6  * ===============================================================
     7  *
     8  *  This file is part of the SourceMod/SourcePawn SDK.  This file may only be 
     9  * used or modified under the Terms and Conditions of its License Agreement, 
    10  * which is found in public/licenses/LICENSE.txt.  As of this notice, derivative 
    11  * works must be licensed under the GNU General Public License (version 2 or 
    12  * greater).  A copy of the GPL is included under public/licenses/GPL.txt.
    13  * 
    14  * To view the latest information, see: http://www.sourcemod.net/license.php
    15  *
    16  * Version: $Id$
    17  */
    18 
    19 #ifndef _INCLUDE_SMEXT_BINTOOLS_H_
    20 #define _INCLUDE_SMEXT_BINTOOLS_H_
    21 
    22 #include <IShareSys.h>
    23 
    24 #define SMINTERFACE_BINTOOLS_NAME		"IBinTools"
    25 #define SMINTERFACE_BINTOOLS_VERSION	2
    26 
    27 /**
    28  * @brief Function calling encoding utilities
    29  * @file IBinTools.h
    30  */
    31 
    32 namespace SourceMod
    33 {
    34 	/**
    35 	 * @brief Supported calling conventions
    36 	 */
    37 	enum CallConvention
    38 	{
    39 		CallConv_ThisCall,		/**< This call (object pointer required) */
    40 		CallConv_Cdecl,			/**< Standard C call */
    41 	};
    42 
    43 	/**
    44 	 * @brief Describes how a parameter should be passed
    45 	 */
    46 	enum PassType
    47 	{
    48 		PassType_Basic,			/**< Plain old register data (pointers, integers) */
    49 		PassType_Float,			/**< Floating point data */
    50 		PassType_Object,		/**< Object or structure */
    51 	};
    52 
    53 	#define PASSFLAG_BYVAL		(1<<0)		/**< Passing by value */
    54 	#define PASSFLAG_BYREF		(1<<1)		/**< Passing by reference */
    55 	#define PASSFLAG_ODTOR		(1<<2)		/**< Object has a destructor */
    56 	#define PASSFLAG_OCTOR		(1<<3)		/**< Object has a constructor */
    57 	#define PASSFLAG_OASSIGNOP	(1<<4)		/**< Object has an assignment operator */
    58 
    59 	/**
    60 	 * @brief Parameter passing information
    61 	 */
    62 	struct PassInfo
    63 	{
    64 		PassType type;			/**< PassType value */
    65 		unsigned int flags;		/**< Pass/return flags */
    66 		size_t size;			/**< Size of the data being passed */
    67 	};
    68 
    69 	/**
    70 	 * @brief Parameter encoding information
    71 	 */
    72 	struct PassEncode
    73 	{
    74 		PassInfo info;			/**< Parameter information */
    75 		size_t offset;			/**< Offset into the virtual stack */
    76 	};
    77 
    78 	/**
    79 	 * @brief Wraps a C/C++ call.
    80 	 */
    81 	class ICallWrapper
    82 	{
    83 	public:
    84 		/**
    85 		 * @brief Returns the calling convention.
    86 		 *
    87 		 * @return				CallConvention value.
    88 		 */
    89 		virtual CallConvention GetCallConvention() =0;
    90 
    91 		/**
    92 		 * @brief Returns parameter info.
    93 		 *
    94 		 * @param num			Parameter number to get (starting from 0).
    95 		 * @return				A PassInfo pointer.
    96 		 */
    97 		virtual const PassEncode *GetParamInfo(unsigned int num) =0;
    98 
    99 		/**
   100 		 * @brief Returns return type info.
   101 		 *
   102 		 * @return				A PassInfo pointer.
   103 		 */
   104 		virtual const PassInfo *GetReturnInfo() =0;
   105 
   106 		/**
   107 		 * @brief Returns the number of parameters.
   108 		 *
   109 		 * @return				Number of parameters.
   110 		 */
   111 		virtual unsigned int GetParamCount() =0;
   112 
   113 		/**
   114 		 * @brief Execute the contained function.
   115 		 *
   116 		 * @param vParamStack	A blob of memory containing stack data.
   117 		 * @param retBuffer		Buffer to store return value.
   118 		 */
   119 		virtual void Execute(void *vParamStack, void *retBuffer) =0;
   120 
   121 		/**
   122 		 * @brief Destroys all resources used by this object.
   123 		 */
   124 		virtual void Destroy() =0;
   125 	};
   126 
   127 	/**
   128 	 * @brief Binary tools interface.
   129 	 */
   130 	class IBinTools : public SMInterface
   131 	{
   132 	public:
   133 		virtual const char *GetInterfaceName()
   134 		{
   135 			return SMINTERFACE_BINTOOLS_NAME;
   136 		}
   137 		virtual unsigned int GetInterfaceVersion()
   138 		{
   139 			return SMINTERFACE_BINTOOLS_VERSION;
   140 		}
   141 	public:
   142 		/**
   143 		 * @brief Creates a call decoder.
   144 		 *
   145 		 * Note: CallConv_ThisCall requires an implicit first parameter
   146 		 * of PassType_Basic / PASSFLAG_BYVAL / sizeof(void *).  However,
   147 		 * this should only be given to the Execute() function, and never
   148 		 * listed in the paramInfo array.
   149 		 *
   150 		 * @param address			Address to use as a call.
   151 		 * @param cv				Calling convention.
   152 		 * @param retInfo			Return type information, or NULL for void.
   153 		 * @param paramInfo			Array of parameters.
   154 		 * @param numParams			Number of parameters in the array.
   155 		 * @return					A new ICallWrapper function.
   156 		 */
   157 		virtual ICallWrapper *CreateCall(void *address,
   158 											CallConvention cv,
   159 											const PassInfo *retInfo,
   160 											const PassInfo paramInfo[],
   161 											unsigned int numParams) =0;
   162 
   163 		/**
   164 		 * @brief Creates a vtable call decoder.
   165 		 *
   166 		 * Note: CallConv_ThisCall requires an implicit first parameter
   167 		 * of PassType_Basic / PASSFLAG_BYVAL / sizeof(void *).  However,
   168 		 * this should only be given to the Execute() function, and never
   169 		 * listed in the paramInfo array.
   170 		 *
   171 		 * @param vtblIdx			Index into the virtual table.
   172 		 * @param vtblOffs			Offset of the virtual table.
   173 		 * @param thisOffs			Offset of the this pointer of the virtual table.
   174 		 * @param retInfo			Return type information, or NULL for void.
   175 		 * @param paramInfo			Array of parameters.
   176 		 * @param numParams			Number of parameters in the array.
   177 		 * @return					A new ICallWrapper function.
   178 		 */
   179 		virtual ICallWrapper *CreateVCall(unsigned int vtblIdx,
   180 											unsigned int vtblOffs,
   181 											unsigned int thisOffs,
   182 											const PassInfo *retInfo,
   183 											const PassInfo paramInfo[],
   184 											unsigned int numParams) =0;
   185 	};
   186 }
   187 
   188 #endif //_INCLUDE_SMEXT_BINTOOLS_H_