core/interfaces/IShareSys.h
author David Anderson <dvander@alliedmods.net>
Sat Nov 11 11:10:24 2006 +0000 (2006-11-11)
changeset 177 d29ade52d953
parent 140 2c25d4131b38
child 211 4c77142eaf36
permissions -rw-r--r--
Fleshed out forward system and extensively documented its design considerations
Implemented and did basic tests on new IPluginFunction type
Split function types into a separate file
     1 #ifndef _INCLUDE_SOURCEMOD_IFACE_SHARE_SYS_H_
     2 #define _INCLUDE_SOURCEMOD_IFACE_SHARE_SYS_H_
     3 
     4 namespace SourceMod
     5 {
     6 	/**
     7 	 * @brief Defines the base functionality required by a shared interface.
     8 	 */
     9 	class SMInterface
    10 	{
    11 	public:
    12 		/**
    13 		 * @brief Must return an integer defining the interface's version.
    14 		 */
    15 		virtual unsigned int GetInterfaceVersion() =0;
    16 
    17 		/**
    18 		 * @brief Must return a string defining the interface's unique name.
    19 		 */
    20 		virtual const char *GetInterfaceName() =0;
    21 
    22 		/**
    23 		 * @brief Must return whether the requested version number is backwards comaptible.
    24 		 * Note: This can be overridden for breaking changes or custom versioning.
    25 		 * 
    26 		 * @param version		Version number to compare against.
    27 		 * @return				True if compatible, false otherwise.
    28 		 */
    29 		virtual bool IsVersionCompatible(unsigned int version)
    30 		{
    31 			if (version > GetInterfaceVersion())
    32 			{
    33 				return false;
    34 			}
    35 
    36 			return true;
    37 		}
    38 	};
    39 
    40 	enum UnloadableParentType
    41 	{
    42 		ParentType_Module,
    43 		ParentType_Plugin
    44 	};
    45 
    46 	/**
    47 	 * @brief Denotes a top-level unloadable object.
    48 	 */
    49 	class IUnloadableParent
    50 	{
    51 	public:
    52 		virtual UnloadableParentType GetParentType() =0;
    53 
    54 		virtual void *GetParentToken() =0;
    55 	
    56 		/**
    57 		 * @brief Called when an interface this object has requested is removed.
    58 		 *
    59 		 * @param pIface		Interface being removed.
    60 		 */
    61 		virtual void OnInterfaceUnlink(SMInterface *pIface) =0;
    62 	protected:
    63 		void *m_parent_token;
    64 	};
    65 
    66 	/**
    67 	 * @brief Listens for unlinked objects.
    68 	 */
    69 	class IUnlinkListener
    70 	{
    71 	public:
    72 		/**
    73 		 * @brief Called when a parent object is unloaded.
    74 		 *
    75 		 * @param parent		The parent object which is dying.
    76 		 */
    77 		virtual void OnParentUnlink(IUnloadableParent *parent)
    78 		{
    79 		}
    80 	};
    81 
    82 	/**
    83 	 * @brief Tracks dependencies and fires dependency listeners.
    84 	 */
    85 	class IShareSys
    86 	{
    87 	public:
    88 		/**
    89 		 * @brief Adds an interface to the global interface system
    90 		 *
    91 		 * @param iface			Interface pointer (must be unique).
    92 		 * @param parent		Parent unloadable token given to the module/interface.
    93 		 */
    94 		virtual bool AddInterface(SMInterface *iface, IUnloadableParent *parent) =0;
    95 
    96 		/**
    97 		 * @brief Requests an interface from the global interface system.
    98 		 * If found, the interface's internal reference count will be increased.
    99 		 *
   100 		 * @param iface_name	Interface name.
   101 		 * @param iface_vers	Interface version to attempt to match.
   102 		 * @param me			Object requesting this interface, in order to track dependencies.
   103 		 * @param pIface		Pointer to store the return value in.
   104 		 */
   105 		virtual bool RequestInterface(const char *iface_name, 
   106 										unsigned int iface_vers, 
   107 										IUnloadableParent *me,
   108 										void **pIface) =0;
   109 
   110 		/**
   111 		 * @brief Unloads an interface.
   112 		 *
   113 		 * @param iface			Interface pointer.
   114 		 * @param parent		Security token, trivial measure to prevent accidental unloads.
   115 		 *                      This token must match the one used with AddInterface().
   116 		 */
   117 		virtual void RemoveInterface(SMInterface *iface, IUnloadableParent *parent) =0;
   118 
   119 		/**
   120 		 * @brief Adds an unlink listener.
   121 		 *
   122 		 * @param pListener		Listener pointer.
   123 		 */
   124 		virtual void AddUnlinkListener(IUnlinkListener *pListener) =0;
   125 
   126 		/**
   127 		 * @brief Removes an unlink listener.
   128 		 *
   129 		 * @param pListener		Listener pointer.
   130 		 */
   131 		virtual void RemoveUnlinkListener(IUnlinkListener *pListener) =0;
   132 	};
   133 };
   134 
   135 #endif //_INCLUDE_SOURCEMOD_IFACE_SHARE_SYS_H_