public/IHandleSys.h
author David Anderson <dvander@alliedmods.net>
Mon May 26 07:52:27 2008 +0000 (2008-05-26)
changeset 2088 c84cd855ca10
parent 1952 b7aa2b626807
child 2243 b9cb189640a0
permissions -rw-r--r--
committed two forgotten files and updated the linux makefile
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod
ds@1952
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     6
 * =============================================================================
ds@1952
     7
 *
ds@1952
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    10
 * Free Software Foundation.
ds@1952
    11
 * 
ds@1952
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    15
 * details.
ds@1952
    16
 *
ds@1952
    17
 * You should have received a copy of the GNU General Public License along with
ds@1952
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    19
 *
ds@1952
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    28
 *
ds@1952
    29
 * Version: $Id$
ds@1952
    30
 */
ds@1952
    31
ds@1952
    32
#ifndef _INCLUDE_SOURCEMOD_HANDLESYSTEM_INTERFACE_H_
ds@1952
    33
#define _INCLUDE_SOURCEMOD_HANDLESYSTEM_INTERFACE_H_
ds@1952
    34
ds@1952
    35
/**
ds@1952
    36
 * @file IHandleSys.h
ds@1952
    37
 * @brief Defines the interface for creating, reading, and removing Handles.
ds@1952
    38
 * 
ds@1952
    39
 *  The Handle system abstracts generic pointers into typed objects represented by 
ds@1952
    40
 * 32bit codes.  This is extremely useful for verifying data integrity and cross-platform
ds@1952
    41
 * support in SourcePawn scripts.  When a Plugin unloads, all its Handles are freed, ensuring 
ds@1952
    42
 * that no memory leaks are present,  They have reference counts and thus can be duplicated, 
ds@1952
    43
 * or cloned, and are safe to pass between Plugins even if one is unloaded.
ds@1952
    44
 *
ds@1952
    45
 *  Handles are created with a given type (custom types may be created).  They can have
ds@1952
    46
 * per-Identity permissions for deletion, reading, and cloning.  They also support generic
ds@1952
    47
 * operations.  For example, deleting a Handle will call that type's destructor on the generic
ds@1952
    48
 * pointer, making cleanup easier for users and eliminating memory leaks.
ds@1952
    49
 */
ds@1952
    50
ds@1952
    51
#include <IShareSys.h>
ds@1952
    52
#include <sp_vm_types.h>
ds@1952
    53
ds@1952
    54
#define SMINTERFACE_HANDLESYSTEM_NAME			"IHandleSys"
ds@1952
    55
#define SMINTERFACE_HANDLESYSTEM_VERSION		3
ds@1952
    56
ds@1952
    57
/** Specifies no Identity */
ds@1952
    58
#define DEFAULT_IDENTITY			NULL
ds@1952
    59
/** Specifies no Type.  This is invalid for everything but reading a Handle. */
ds@1952
    60
#define NO_HANDLE_TYPE				0
ds@1952
    61
/** Specifies an invalid/NULL Handle */
ds@1952
    62
#define BAD_HANDLE					0
ds@1952
    63
ds@1952
    64
namespace SourceMod
ds@1952
    65
{
ds@1952
    66
	/**
ds@1952
    67
	 * @brief Represents a Handle Type ID.
ds@1952
    68
	 */
ds@1952
    69
	typedef unsigned int HandleType_t;
ds@1952
    70
ds@1952
    71
	/**
ds@1952
    72
	 * @brief Represents a Handle ID.
ds@1952
    73
	 */
ds@1952
    74
	typedef unsigned int Handle_t;
ds@1952
    75
ds@1952
    76
ds@1952
    77
	/*
ds@1952
    78
	 * About type checking:
ds@1952
    79
	 * Types can be inherited - a Parent type ("Supertype") can have child types.
ds@1952
    80
	 * When accessing handles, type checking is done.  This table shows how this is resolved:
ds@1952
    81
	 *
ds@1952
    82
	 * HANDLE		CHECK		->  RESULT
ds@1952
    83
	 * ------		-----			------
ds@1952
    84
	 * Parent		Parent			Success
ds@1952
    85
	 * Parent		Child			Fail
ds@1952
    86
	 * Child		Parent			Success
ds@1952
    87
	 * Child		Child			Success
ds@1952
    88
	 */
ds@1952
    89
ds@1952
    90
	/**
ds@1952
    91
	 * @brief Lists the possible handle error codes.
ds@1952
    92
	 */
ds@1952
    93
	enum HandleError
ds@1952
    94
	{
ds@1952
    95
		HandleError_None = 0,		/**< No error */
ds@1952
    96
		HandleError_Changed,		/**< The handle has been freed and reassigned */
ds@1952
    97
		HandleError_Type,			/**< The handle has a different type registered */
ds@1952
    98
		HandleError_Freed,			/**< The handle has been freed */
ds@1952
    99
		HandleError_Index,			/**< generic internal indexing error */
ds@1952
   100
		HandleError_Access,			/**< No access permitted to free this handle */
ds@1952
   101
		HandleError_Limit,			/**< The limited number of handles has been reached */
ds@1952
   102
		HandleError_Identity,		/**< The identity token was not usable */
ds@1952
   103
		HandleError_Owner,			/**< Owners do not match for this operation */
ds@1952
   104
		HandleError_Version,		/**< Unrecognized security structure version */
ds@1952
   105
		HandleError_Parameter,		/**< An invalid parameter was passed */
ds@1952
   106
		HandleError_NoInherit,		/**< This type cannot be inherited */
ds@1952
   107
	};
ds@1952
   108
ds@1952
   109
	/**
ds@1952
   110
	 * @brief Lists access rights specific to a type.
ds@1952
   111
	 */
ds@1952
   112
	enum HTypeAccessRight
ds@1952
   113
	{
ds@1952
   114
		HTypeAccess_Create = 0,		/**< Handles of this type can be created (DEFAULT=false) */
ds@1952
   115
		HTypeAccess_Inherit,		/**< Sub-types can inherit this type (DEFAULT=false) */
ds@1952
   116
		/* -------------- */
ds@1952
   117
		HTypeAccess_TOTAL,			/**< Total number of type access rights */
ds@1952
   118
	};
ds@1952
   119
ds@1952
   120
	/**
ds@1952
   121
	 * @brief Lists access rights specific to a Handle.  
ds@1952
   122
	 * 
ds@1952
   123
	 * These rights are exclusive. For example, you do not need "read" access to delete or clone.
ds@1952
   124
	 */
ds@1952
   125
	enum HandleAccessRight
ds@1952
   126
	{
ds@1952
   127
		HandleAccess_Read,			/**< Can be read (DEFAULT=ident only) */
ds@1952
   128
		HandleAccess_Delete,		/**< Can be deleted (DEFAULT=owner only) */
ds@1952
   129
		HandleAccess_Clone,			/**< Can be cloned (DEFAULT=any) */
ds@1952
   130
		/* ------------- */
ds@1952
   131
		HandleAccess_TOTAL,			/**< Total number of access rights */
ds@1952
   132
	};
ds@1952
   133
ds@1952
   134
	/** Access is restricted to the identity */
ds@1952
   135
	#define HANDLE_RESTRICT_IDENTITY	(1<<0)	
ds@1952
   136
	/** Access is restricted to the owner */
ds@1952
   137
	#define HANDLE_RESTRICT_OWNER		(1<<1)
ds@1952
   138
ds@1952
   139
	/**
ds@1952
   140
	 * @brief This is used to define per-type access rights.
ds@1952
   141
	 */
ds@1952
   142
	struct TypeAccess
ds@1952
   143
	{
ds@1952
   144
		/** Constructor */
ds@1952
   145
		TypeAccess()
ds@1952
   146
		{
ds@1952
   147
			hsVersion = SMINTERFACE_HANDLESYSTEM_VERSION;
ds@1952
   148
		}
ds@1952
   149
		unsigned int hsVersion;			/**< Handle API version */
ds@1952
   150
		IdentityToken_t *ident;			/**< Identity owning this type */
ds@1952
   151
		bool access[HTypeAccess_TOTAL];	/**< Access array */
ds@1952
   152
	};
ds@1952
   153
ds@1952
   154
	/**
ds@1952
   155
	 * @brief This is used to define per-Handle access rights.
ds@1952
   156
	 */
ds@1952
   157
	struct HandleAccess
ds@1952
   158
	{
ds@1952
   159
		/** Constructor */
ds@1952
   160
		HandleAccess()
ds@1952
   161
		{
ds@1952
   162
			hsVersion = SMINTERFACE_HANDLESYSTEM_VERSION;
ds@1952
   163
		}
ds@1952
   164
		unsigned int hsVersion;						/**< Handle API version */
ds@1952
   165
		unsigned int access[HandleAccess_TOTAL];	/**< Access array */
ds@1952
   166
	};
ds@1952
   167
ds@1952
   168
	/**
ds@1952
   169
	 * @brief This pair of tokens is used for identification.
ds@1952
   170
	 */
ds@1952
   171
	struct HandleSecurity
ds@1952
   172
	{
ds@1952
   173
		HandleSecurity()
ds@1952
   174
		{
ds@1952
   175
		}
ds@1952
   176
		HandleSecurity(IdentityToken_t *owner, IdentityToken_t *identity)
ds@1952
   177
			: pOwner(owner), pIdentity(identity)
ds@1952
   178
		{
ds@1952
   179
		}
ds@1952
   180
		IdentityToken_t *pOwner;			/**< Owner of the Handle */
ds@1952
   181
		IdentityToken_t *pIdentity;			/**< Owner of the Type */
ds@1952
   182
	};
ds@1952
   183
ds@1952
   184
	/**
ds@1952
   185
	 * @brief Hooks type-specific Handle operations.
ds@1952
   186
	 */
ds@1952
   187
	class IHandleTypeDispatch
ds@1952
   188
	{
ds@1952
   189
	public:
ds@1952
   190
		/** Returns the Handle API version */
ds@1952
   191
		virtual unsigned int GetDispatchVersion()
ds@1952
   192
		{
ds@1952
   193
			return SMINTERFACE_HANDLESYSTEM_VERSION;
ds@1952
   194
		}
ds@1952
   195
	public:
ds@1952
   196
		/**
ds@1952
   197
		 * @brief Called when destroying a handle.  Must be implemented.
ds@1952
   198
		 *
ds@1952
   199
		 * @param type		Handle type.
ds@1952
   200
		 * @param object	Handle internal object.
ds@1952
   201
		 */
ds@1952
   202
		virtual void OnHandleDestroy(HandleType_t type, void *object) =0;
ds@1952
   203
ds@1952
   204
		/**
ds@1952
   205
		 * @brief Called to get the size of a handle's memory usage in bytes.
ds@1952
   206
		 * Implementation is optional.
ds@1952
   207
		 *
ds@1952
   208
		 * @param type		Handle type.
ds@1952
   209
		 * @param object	Handle internal object.
ds@1952
   210
		 * @param pSize		Pointer to store the approximate memory usage in bytes.
ds@1952
   211
		 * @return			True on success, false if not implemented.
ds@1952
   212
		 */
ds@1952
   213
		virtual bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
ds@1952
   214
		{
ds@1952
   215
			return false;
ds@1952
   216
		}
ds@1952
   217
	};
ds@1952
   218
ds@1952
   219
	/**
ds@1952
   220
	 * @brief Provides functions for managing Handles.
ds@1952
   221
	 */
ds@1952
   222
	class IHandleSys : public SMInterface
ds@1952
   223
	{
ds@1952
   224
	public:
ds@1952
   225
		virtual unsigned int GetInterfaceVersion()
ds@1952
   226
		{
ds@1952
   227
			return SMINTERFACE_HANDLESYSTEM_VERSION;
ds@1952
   228
		}
ds@1952
   229
		virtual const char *GetInterfaceName()
ds@1952
   230
		{
ds@1952
   231
			return SMINTERFACE_HANDLESYSTEM_NAME;
ds@1952
   232
		}
ds@1952
   233
	public:
ds@1952
   234
		/**
ds@1952
   235
		 * @brief Creates a new Handle type.
ds@1952
   236
		 * NOTE: Currently, a child type may not have its own children.
ds@1952
   237
		 * NOTE: Handle names must be unique if not private.
ds@1952
   238
		 *
ds@1952
   239
		 * @param name		Name of handle type (NULL or "" to be anonymous)
ds@1952
   240
		 * @param dispatch	Pointer to a valid IHandleTypeDispatch object.
ds@1952
   241
		 * @param parent	Parent handle to inherit from, 0 for none.
ds@1952
   242
		 * @param typeAccess	Pointer to a TypeAccess object, NULL to use default 
ds@1952
   243
		 *						or inherited permissions.  Pointer can be temporary.
ds@1952
   244
		 * @param hndlAccess	Pointer to a HandleAccess object to define default
ds@1952
   245
		 *						default permissions on each Handle.  NULL to use default
ds@1952
   246
		 *						permissions.
ds@1952
   247
		 * @param ident		Security token for any permissions.  If typeAccess is NULL, this 
ds@1952
   248
		 *					becomes the owning identity.
ds@1952
   249
		 * @param err		Optional pointer to store an error code.
ds@1952
   250
		 * @return			A new HandleType_t unique ID, or 0 on failure.
ds@1952
   251
		 */
ds@1952
   252
		virtual HandleType_t CreateType(const char *name,
ds@1952
   253
										  IHandleTypeDispatch *dispatch,
ds@1952
   254
										  HandleType_t parent,
ds@1952
   255
										  const TypeAccess *typeAccess,
ds@1952
   256
										  const HandleAccess *hndlAccess,
ds@1952
   257
										  IdentityToken_t *ident,
ds@1952
   258
										  HandleError *err) =0;
ds@1952
   259
ds@1952
   260
		/**
ds@1952
   261
		 * @brief Removes a handle type.
ds@1952
   262
		 * NOTE: This removes all child types.
ds@1952
   263
		 *
ds@1952
   264
		 * @param type		Type chain to remove.
ds@1952
   265
		 * @param ident		Identity token.  Removal fails if the token does not match.
ds@1952
   266
		 * @return			True on success, false on failure.
ds@1952
   267
		 */
ds@1952
   268
		virtual bool RemoveType(HandleType_t type, IdentityToken_t *ident) =0;
ds@1952
   269
ds@1952
   270
		/**
ds@1952
   271
		 * @brief Finds a handle type by name.
ds@1952
   272
		 *
ds@1952
   273
		 * @param name		Name of handle type to find (anonymous not allowed).
ds@1952
   274
		 * @param type		Address to store found handle in (if not found, undefined).
ds@1952
   275
		 * @return			True if found, false otherwise.
ds@1952
   276
		 */
ds@1952
   277
		virtual bool FindHandleType(const char *name, HandleType_t *type) =0;
ds@1952
   278
ds@1952
   279
		/**
ds@1952
   280
		 * @brief Creates a new handle.
ds@1952
   281
		 * 
ds@1952
   282
		 * @param type		Type to use on the handle.
ds@1952
   283
		 * @param object	Object to bind to the handle.
ds@1952
   284
		 * @param owner		Owner of the new Handle (may be NULL).
ds@1952
   285
		 * @param ident		Identity for type access if needed (may be NULL).
ds@1952
   286
		 * @param err		Optional pointer to store an error code.
ds@1952
   287
		 * @return			A new Handle_t, or 0 on failure.
ds@1952
   288
		 */
ds@1952
   289
		virtual Handle_t CreateHandle(HandleType_t type, 
ds@1952
   290
									  void *object,
ds@1952
   291
									  IdentityToken_t *owner,
ds@1952
   292
									  IdentityToken_t *ident,
ds@1952
   293
									  HandleError *err) =0;
ds@1952
   294
ds@1952
   295
		/**
ds@1952
   296
		 * @brief Frees the memory associated with a handle and calls any destructors.
ds@1952
   297
		 * NOTE: This function will decrement the internal reference counter.  It will
ds@1952
   298
		 * only perform any further action if the counter hits 0.
ds@1952
   299
		 *
ds@1952
   300
		 * @param handle	Handle_t identifier to destroy.
ds@1952
   301
		 * @param pSecurity	Security information struct (may be NULL).
ds@1952
   302
		 * @return			A HandleError error code.
ds@1952
   303
		 */
ds@1952
   304
		virtual HandleError FreeHandle(Handle_t handle, const HandleSecurity *pSecurity) =0;
ds@1952
   305
ds@1952
   306
		/**
ds@1952
   307
		 * @brief Clones a handle by adding to its internal reference count.  Its data,
ds@1952
   308
		 * type, and security permissions remain the same.
ds@1952
   309
		 *
ds@1952
   310
		 * @param handle	Handle to duplicate.  Any non-free handle target is valid.
ds@1952
   311
		 * @param newhandle	Stores the duplicated handle in the pointer (must not be NULL).
ds@1952
   312
		 * @param newOwner	New owner of cloned handle.
ds@1952
   313
		 * @param pSecurity	Security information struct (may be NULL).
ds@1952
   314
		 * @return			A HandleError error code.
ds@1952
   315
		 */
ds@1952
   316
		virtual HandleError CloneHandle(Handle_t handle, 
ds@1952
   317
										Handle_t *newhandle, 
ds@1952
   318
										IdentityToken_t *newOwner,
ds@1952
   319
										const HandleSecurity *pSecurity) =0;
ds@1952
   320
ds@1952
   321
		/**
ds@1952
   322
		 * @brief Retrieves the contents of a handle.
ds@1952
   323
		 *
ds@1952
   324
		 * @param handle	Handle_t from which to retrieve contents.
ds@1952
   325
		 * @param type		Expected type to read as.  0 ignores typing rules.
ds@1952
   326
		 * @param pSecurity	Security information struct (may be NULL).
ds@1952
   327
		 * @param object	Optional address to store object in.
ds@1952
   328
		 * @return			HandleError error code.
ds@1952
   329
		 */
ds@1952
   330
		virtual HandleError ReadHandle(Handle_t handle, 
ds@1952
   331
									   HandleType_t type, 
ds@1952
   332
									   const HandleSecurity *pSecurity,
ds@1952
   333
									   void **object) =0;
ds@1952
   334
ds@1952
   335
		/**
ds@1952
   336
		 * @brief Sets access permissions on one or more structures.
ds@1952
   337
		 *
ds@1952
   338
		 * @param pTypeAccess	Optional TypeAccess buffer to initialize with the default values.
ds@1952
   339
		 * @param pHandleAccess	Optional HandleAccess buffer to initialize with the default values.
ds@1952
   340
		 * @return				True on success, false if version is unsupported.
ds@1952
   341
		 */
ds@1952
   342
		virtual bool InitAccessDefaults(TypeAccess *pTypeAccess, HandleAccess *pHandleAccess) =0;
ds@1952
   343
ds@1952
   344
		/**
ds@1952
   345
		 * @brief Creates a new handle.
ds@1952
   346
		 * 
ds@1952
   347
		 * @param type		Type to use on the handle.
ds@1952
   348
		 * @param object	Object to bind to the handle.
ds@1952
   349
		 * @param pSec		Security pointer; pOwner is written as the owner,
ds@1952
   350
		 *					pIdent is used as the parent identity for authorization.
ds@1952
   351
		 * @param pAccess	Access right descriptor for the Handle; NULL for type defaults.
ds@1952
   352
		 * @param err		Optional pointer to store an error code.
ds@1952
   353
		 * @return			A new Handle_t, or 0 on failure.
ds@1952
   354
		 */
ds@1952
   355
		virtual Handle_t CreateHandleEx(HandleType_t type, 
ds@1952
   356
										void *object,
ds@1952
   357
										const HandleSecurity *pSec,
ds@1952
   358
										const HandleAccess *pAccess,
ds@1952
   359
										HandleError *err) =0;
ds@1952
   360
	};
ds@1952
   361
}
ds@1952
   362
ds@1952
   363
#endif //_INCLUDE_SOURCEMOD_HANDLESYSTEM_INTERFACE_H_