Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
8.7 KiB

  1. //========= Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. // This header defines the interface convention used in the valve engine.
  9. // To make an interface and expose it:
  10. // 1. The interface must be ALL pure virtuals, and have no data members.
  11. // 2. Define a name for it.
  12. // 3. In its implementation file, use EXPOSE_INTERFACE or EXPOSE_SINGLE_INTERFACE.
  13. // Versioning
  14. // There are two versioning cases that are handled by this:
  15. // 1. You add functions to the end of an interface, so it is binary compatible with the previous interface. In this case,
  16. // you need two EXPOSE_INTERFACEs: one to expose your class as the old interface and one to expose it as the new interface.
  17. // 2. You update an interface so it's not compatible anymore (but you still want to be able to expose the old interface
  18. // for legacy code). In this case, you need to make a new version name for your new interface, and make a wrapper interface and
  19. // expose it for the old interface.
  20. // Static Linking:
  21. // Must mimic unique seperate class 'InterfaceReg' constructors per subsystem.
  22. // Each subsystem can then import and export interfaces as expected.
  23. // This is achieved through unique namespacing 'InterfaceReg' via symbol _SUBSYSTEM.
  24. // Static Linking also needs to generate unique symbols per interface so as to
  25. // provide a 'stitching' method whereby these interface symbols can be referenced
  26. // via the lib's primary module (usually the lib's interface exposure)
  27. // therby stitching all of that lib's code/data together for eventual final exe link inclusion.
  28. #ifndef INTERFACE_H
  29. #define INTERFACE_H
  30. #ifdef _WIN32
  31. #pragma once
  32. #endif
  33. // TODO: move interface.cpp into tier0 library.
  34. // Need to include platform.h in case _PS3 and other tokens are not yet defined
  35. #include "tier0/platform.h"
  36. #if defined( POSIX ) && !defined( _PS3 )
  37. #include <dlfcn.h> // dlopen,dlclose, et al
  38. #include <unistd.h>
  39. #define GetProcAddress dlsym
  40. #ifdef _snprintf
  41. #undef _snprintf
  42. #endif
  43. #define _snprintf snprintf
  44. #endif // POSIX && !_PS3
  45. // All interfaces derive from this.
  46. class IBaseInterface
  47. {
  48. public:
  49. virtual ~IBaseInterface() {}
  50. };
  51. #if !defined( _X360 )
  52. #define CREATEINTERFACE_PROCNAME "CreateInterface"
  53. #else
  54. // x360 only allows ordinal exports, .def files export "CreateInterface" at 1
  55. #define CREATEINTERFACE_PROCNAME ((const char*)1)
  56. #endif
  57. typedef void* (*CreateInterfaceFn)(const char *pName, int *pReturnCode);
  58. typedef void* (*InstantiateInterfaceFn)();
  59. // Used internally to register classes.
  60. class InterfaceReg
  61. {
  62. public:
  63. InterfaceReg(InstantiateInterfaceFn fn, const char *pName);
  64. public:
  65. InstantiateInterfaceFn m_CreateFn;
  66. const char *m_pName;
  67. InterfaceReg *m_pNext; // For the global list.
  68. };
  69. // Use this to expose an interface that can have multiple instances.
  70. // e.g.:
  71. // EXPOSE_INTERFACE( CInterfaceImp, IInterface, "MyInterface001" )
  72. // This will expose a class called CInterfaceImp that implements IInterface (a pure class)
  73. // clients can receive a pointer to this class by calling CreateInterface( "MyInterface001" )
  74. //
  75. // In practice, the shared header file defines the interface (IInterface) and version name ("MyInterface001")
  76. // so that each component can use these names/vtables to communicate
  77. //
  78. // A single class can support multiple interfaces through multiple inheritance
  79. //
  80. // Use this if you want to write the factory function.
  81. #if !defined(_STATIC_LINKED) || !defined(_SUBSYSTEM)
  82. #define EXPOSE_INTERFACE_FN(functionName, interfaceName, versionName) \
  83. static InterfaceReg __g_Create##interfaceName##_reg(functionName, versionName);
  84. #else
  85. #define EXPOSE_INTERFACE_FN(functionName, interfaceName, versionName) \
  86. namespace _SUBSYSTEM \
  87. { \
  88. static InterfaceReg __g_Create##interfaceName##_reg(functionName, versionName); \
  89. }
  90. #endif
  91. #if !defined(_STATIC_LINKED) || !defined(_SUBSYSTEM)
  92. #define EXPOSE_INTERFACE(className, interfaceName, versionName) \
  93. static void* __Create##className##_interface() {return static_cast<interfaceName *>( new className );} \
  94. static InterfaceReg __g_Create##className##_reg(__Create##className##_interface, versionName );
  95. #else
  96. #define EXPOSE_INTERFACE(className, interfaceName, versionName) \
  97. namespace _SUBSYSTEM \
  98. { \
  99. static void* __Create##className##_interface() {return static_cast<interfaceName *>( new className );} \
  100. static InterfaceReg __g_Create##className##_reg(__Create##className##_interface, versionName ); \
  101. }
  102. #endif
  103. // Use this to expose a singleton interface with a global variable you've created.
  104. #if !defined(_STATIC_LINKED) || !defined(_SUBSYSTEM)
  105. #define EXPOSE_SINGLE_INTERFACE_GLOBALVAR(className, interfaceName, versionName, globalVarName) \
  106. static void* __Create##className##interfaceName##_interface() {return static_cast<interfaceName *>( &globalVarName );} \
  107. static InterfaceReg __g_Create##className##interfaceName##_reg(__Create##className##interfaceName##_interface, versionName);
  108. #else
  109. #define EXPOSE_SINGLE_INTERFACE_GLOBALVAR(className, interfaceName, versionName, globalVarName) \
  110. namespace _SUBSYSTEM \
  111. { \
  112. static void* __Create##className##interfaceName##_interface() {return static_cast<interfaceName *>( &globalVarName );} \
  113. static InterfaceReg __g_Create##className##interfaceName##_reg(__Create##className##interfaceName##_interface, versionName); \
  114. }
  115. #endif
  116. // Use this to expose a singleton interface. This creates the global variable for you automatically.
  117. #if !defined(_STATIC_LINKED) || !defined(_SUBSYSTEM)
  118. #define EXPOSE_SINGLE_INTERFACE(className, interfaceName, versionName) \
  119. static className __g_##className##_singleton; \
  120. EXPOSE_SINGLE_INTERFACE_GLOBALVAR(className, interfaceName, versionName, __g_##className##_singleton)
  121. #else
  122. #define EXPOSE_SINGLE_INTERFACE(className, interfaceName, versionName) \
  123. namespace _SUBSYSTEM \
  124. { \
  125. static className __g_##className##_singleton; \
  126. } \
  127. EXPOSE_SINGLE_INTERFACE_GLOBALVAR(className, interfaceName, versionName, __g_##className##_singleton)
  128. #endif
  129. // load/unload components
  130. class CSysModule;
  131. // interface return status
  132. enum
  133. {
  134. IFACE_OK = 0,
  135. IFACE_FAILED
  136. };
  137. //-----------------------------------------------------------------------------
  138. // This function is automatically exported and allows you to access any interfaces exposed with the above macros.
  139. // if pReturnCode is set, it will return one of the following values (IFACE_OK, IFACE_FAILED)
  140. // extend this for other error conditions/code
  141. //-----------------------------------------------------------------------------
  142. DLL_EXPORT void* CreateInterface(const char *pName, int *pReturnCode);
  143. #if defined( _X360 )
  144. DLL_EXPORT void *CreateInterfaceThunk( const char *pName, int *pReturnCode );
  145. #endif
  146. //-----------------------------------------------------------------------------
  147. // UNDONE: This is obsolete, use the module load/unload/get instead!!!
  148. //-----------------------------------------------------------------------------
  149. extern CreateInterfaceFn Sys_GetFactory( CSysModule *pModule );
  150. extern CreateInterfaceFn Sys_GetFactory( const char *pModuleName );
  151. extern CreateInterfaceFn Sys_GetFactoryThis( void );
  152. //-----------------------------------------------------------------------------
  153. // Load & Unload should be called in exactly one place for each module
  154. // The factory for that module should be passed on to dependent components for
  155. // proper versioning.
  156. //-----------------------------------------------------------------------------
  157. extern CSysModule *Sys_LoadModule( const char *pModuleName );
  158. extern void Sys_UnloadModule( CSysModule *pModule );
  159. // Determines if current process is running with any debug modules
  160. extern bool Sys_RunningWithDebugModules();
  161. // This is a helper function to load a module, get its factory, and get a specific interface.
  162. // You are expected to free all of these things.
  163. // Returns false and cleans up if any of the steps fail.
  164. bool Sys_LoadInterface(
  165. const char *pModuleName,
  166. const char *pInterfaceVersionName,
  167. CSysModule **pOutModule,
  168. void **pOutInterface );
  169. bool Sys_IsDebuggerPresent();
  170. //-----------------------------------------------------------------------------
  171. // Purpose: Place this as a singleton at module scope (e.g.) and use it to get the factory from the specified module name.
  172. //
  173. // When the singleton goes out of scope (.dll unload if at module scope),
  174. // then it'll call Sys_UnloadModule on the module so that the refcount is decremented
  175. // and the .dll actually can unload from memory.
  176. //-----------------------------------------------------------------------------
  177. class CDllDemandLoader
  178. {
  179. public:
  180. CDllDemandLoader( char const *pchModuleName );
  181. virtual ~CDllDemandLoader();
  182. CreateInterfaceFn GetFactory();
  183. void Unload();
  184. private:
  185. char const *m_pchModuleName;
  186. CSysModule *m_hModule;
  187. bool m_bLoadAttempted;
  188. };
  189. #endif