public/IShareSys.h
author David Anderson <dvander@alliedmods.net>
Wed Sep 26 14:36:08 2007 +0000 (2007-09-26)
changeset 1487 810b3fc562cb
parent 1391 ae2ae57ccccf
child 1572 adbc41dc9cd0
permissions -rw-r--r--
updated sdk for more interfaces
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #ifndef _INCLUDE_SOURCEMOD_IFACE_SHARE_SYS_H_
    33 #define _INCLUDE_SOURCEMOD_IFACE_SHARE_SYS_H_
    34 
    35 /**
    36  * @file IShareSys.h
    37  * @brief Defines the Share System, responsible for shared resources and dependencies.
    38  *
    39  *  The Share System also manages the Identity_t data type, although this is internally
    40  * implemented with the Handle System.
    41  */
    42 
    43 #include <sp_vm_types.h>
    44 
    45 
    46 namespace SourceMod
    47 {
    48 	class IExtension;
    49 	struct IdentityToken_t;
    50 
    51 	/** Forward declaration from IHandleSys.h */
    52 	typedef unsigned int		HandleType_t;
    53 
    54 	/** Forward declaration from IHandleSys.h */
    55 	typedef HandleType_t		IdentityType_t;
    56 
    57 	/**
    58 	 * @brief Defines the base functionality required by a shared interface.
    59 	 */
    60 	class SMInterface
    61 	{
    62 	public:
    63 		/**
    64 		 * @brief Must return an integer defining the interface's version.
    65 		 */
    66 		virtual unsigned int GetInterfaceVersion() =0;
    67 
    68 		/**
    69 		 * @brief Must return a string defining the interface's unique name.
    70 		 */
    71 		virtual const char *GetInterfaceName() =0;
    72 
    73 		/**
    74 		 * @brief Must return whether the requested version number is backwards compatible.
    75 		 * Note: This can be overridden for breaking changes or custom versioning.
    76 		 * 
    77 		 * @param version		Version number to compare against.
    78 		 * @return				True if compatible, false otherwise.
    79 		 */
    80 		virtual bool IsVersionCompatible(unsigned int version)
    81 		{
    82 			if (version > GetInterfaceVersion())
    83 			{
    84 				return false;
    85 			}
    86 
    87 			return true;
    88 		}
    89 	};
    90 
    91 	/**
    92 	 * @brief Tracks dependencies and fires dependency listeners.
    93 	 */
    94 	class IShareSys
    95 	{
    96 	public:
    97 		/**
    98 		 * @brief Adds an interface to the global interface system.
    99 		 *
   100 		 * @param myself		Object adding this interface, in order to track dependencies.
   101 		 * @param iface			Interface pointer (must be unique).
   102 		 * @return				True on success, false otherwise.
   103 		 */
   104 		virtual bool AddInterface(IExtension *myself, SMInterface *iface) =0;
   105 
   106 		/**
   107 		 * @brief Requests an interface from the global interface system.
   108 		 * If found, the interface's internal reference count will be increased.
   109 		 *
   110 		 * @param iface_name	Interface name.
   111 		 * @param iface_vers	Interface version to attempt to match.
   112 		 * @param myself		Object requesting this interface, in order to track dependencies.
   113 		 * @param pIface		Pointer to store the return value in.
   114 		 */
   115 		virtual bool RequestInterface(const char *iface_name, 
   116 										unsigned int iface_vers,
   117 										IExtension *myself,
   118 										SMInterface **pIface) =0;
   119 
   120 		/**
   121 		 * @brief Adds a list of natives to the global native pool, to be 
   122 		 * bound on plugin load.
   123 		 *
   124 		 * Adding natives does not bind them to any loaded plugins; the 
   125 		 * plugins must be reloaded for new natives to take effect.  
   126 		 * 
   127 		 * @param myself		Identity token of parent object.
   128 		 * @param natives		Array of natives to add.  The last entry in 
   129 		 *						the array must be filled with NULLs to 
   130 		 *						terminate the array.  The array must be static 
   131 		 *						as Core will cache the pointer for the 
   132 		 *						lifetime of the extension.
   133 		 */
   134 		virtual void AddNatives(IExtension *myself, const sp_nativeinfo_t *natives) =0;
   135 
   136 		/**
   137 		 * @brief Creates a new identity type.
   138 		 * NOTE: Module authors should never need to use this.  Due to the current implementation,
   139 		 * there is a hardcoded limit of 15 types.  Core uses up a few, so think carefully!
   140 		 *
   141 		 * @param name			String containing type name.  Must not be empty or NULL.
   142 		 * @return				A new HandleType_t identifier, or 0 on failure.
   143 		 */
   144 		virtual IdentityType_t CreateIdentType(const char *name) =0;
   145 
   146 		/**
   147 		 * @brief Finds an identity type by name.
   148 		 * DEFAULT IDENTITY TYPES:
   149 		 *  "PLUGIN"	- An IPlugin object.
   150 		 *  "MODULE"	- An IModule object.
   151 		 *  "CORE"		- An SMGlobalClass or other singleton.
   152 		 * 
   153 		 * @param name			String containing type name to search for.
   154 		 * @return				A HandleType_t identifier if found, 0 otherwise.
   155 		 */
   156 		virtual IdentityType_t FindIdentType(const char *name) =0;
   157 
   158 		/**
   159 		 * @brief Creates a new identity token.  This token is guaranteed to be 
   160 		 * unique amongst all other open identities.
   161 		 *
   162 		 * @param type			Identity type.
   163 		 * @param ptr			Private data pointer (cannot be NULL).
   164 		 * @return				A new IdentityToken_t pointer, or NULL on failure.
   165 		 */
   166 		virtual IdentityToken_t *CreateIdentity(IdentityType_t type, void *ptr) =0;
   167 
   168 		/** 
   169 		 * @brief Destroys an identity type.  Note that this will delete any identities
   170 		 * that are under this type.  
   171 		 *
   172 		 * @param type			Identity type.
   173 		 */
   174 		virtual void DestroyIdentType(IdentityType_t type) =0;
   175 
   176 		/**
   177 		 * @brief Destroys an identity token.  Any handles being owned by this token, or
   178 		 * any handles being 
   179 		 *
   180 		 * @param identity		Identity to remove.
   181 		 */
   182 		virtual void DestroyIdentity(IdentityToken_t *identity) =0;
   183 
   184 		/**
   185 		 * @brief Requires an extension.  This tells SourceMod that without this extension,
   186 		 * your extension should not be loaded.  The name should not include the ".dll" or
   187 		 * the ".so" part of the file name.
   188 		 *
   189 		 * @param myself		IExtension pointer to yourself.
   190 		 * @param filename		File of extension to require.
   191 		 * @param require		Whether or not this extension is a required dependency.
   192 		 * @param autoload		Whether or not to autoload this extension.
   193 		 */
   194 		virtual void AddDependency(IExtension *myself, const char *filename, bool require, bool autoload) =0;
   195 	};
   196 }
   197 
   198 #endif //_INCLUDE_SOURCEMOD_IFACE_SHARE_SYS_H_