Team Fortress 2 Source Code as on 22/4/2020
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.

166 lines
5.8 KiB

  1. //====== Copyright �, Valve Corporation, All rights reserved. =================
  2. //
  3. // Purpose: Defines a directory for all the GC processes for an app
  4. //
  5. //=============================================================================
  6. #ifndef DIRECTORY_H
  7. #define DIRECTORY_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "tier1/utlvector.h"
  12. #include "tier1/utlsortvector.h"
  13. #include "tier1/utlmap.h"
  14. #include "tier1/keyvalues.h"
  15. #include "gamecoordinator/igamecoordinator.h"
  16. namespace GCSDK
  17. {
  18. class CGCDirProcess;
  19. //-----------------------------------------------------------------------------
  20. // Purpose: Entry for a GC instance
  21. //-----------------------------------------------------------------------------
  22. class CGCDirTypeInstance
  23. {
  24. public:
  25. CGCDirTypeInstance( const char* pszTypeName, uint32 nType, uint32 nInstance, const KeyValues *pKVConfig, const CGCDirProcess* pProcess );
  26. ~CGCDirTypeInstance();
  27. uint32 GetType() const;
  28. const char* GetTypeName() const;
  29. uint32 GetInstance() const;
  30. KeyValues * GetConfig() const;
  31. const CGCDirProcess* GetProcess() const;
  32. private:
  33. int32 m_nType;
  34. uint32 m_nInstance;
  35. KeyValues * m_pConfig;
  36. CUtlConstString m_sTypeName;
  37. const CGCDirProcess* m_pProcess;
  38. };
  39. //-----------------------------------------------------------------------------
  40. // Purpose: Entry for a physical machine hosting one or more GC instances
  41. //-----------------------------------------------------------------------------
  42. class CGCDirProcess
  43. {
  44. public:
  45. CGCDirProcess( uint32 nDirIndex, const char *pchProcessName, const char* pchProcessType, const KeyValues *pKVConfig );
  46. ~CGCDirProcess( );
  47. //determines if this process has any GC's of the specified type
  48. bool HasInstanceOfType( uint32 nType ) const;
  49. uint32 GetGCDirIndex() const;
  50. const char * GetName() const;
  51. const char * GetProcessType() const;
  52. uint32 GetTypeInstanceCount() const;
  53. KeyValues * GetConfig() const;
  54. const CGCDirTypeInstance * GetTypeInstance( uint32 nGCIndex ) const;
  55. //gets the list of unique types contained within this process as IDs (note order does not match)
  56. const CUtlSortVector< uint32 >& GetUniqueTypeList() const;
  57. private:
  58. friend class CDirectory;
  59. void AddGC( CGCDirTypeInstance *pGC );
  60. uint32 m_iDirGC;
  61. CUtlConstString m_sName;
  62. CUtlConstString m_sType;
  63. KeyValues * m_pConfig;
  64. CUtlVector<CGCDirTypeInstance *> m_vecGCs;
  65. //a bit mask of which types exist on this GC
  66. uint64 m_nTypeMask;
  67. //optimized list for fast searching of which types this process has
  68. CUtlSortVector< uint32 > m_vecUniqueTypeList;
  69. };
  70. //-----------------------------------------------------------------------------
  71. // Purpose: Defines what kind of GCs exist for this app in the current universe,
  72. // the types that exist, and what machines they run on
  73. //-----------------------------------------------------------------------------
  74. class CDirectory
  75. {
  76. public:
  77. typedef CGCBase *(*GCFactory_t)( const CGCDirProcess *pGCDirProcess );
  78. static const int s_nInvalidGCType = -1;
  79. CDirectory();
  80. ~CDirectory();
  81. bool BInit( KeyValues *pKVDirectory );
  82. void RegisterGCType( int32 nTypeID, const char *pchName );
  83. uint32 GetProcessCount() const;
  84. const CGCDirProcess *GetProcess( int32 nIndex ) const;
  85. uint32 GetGCTypeInstanceCount() const;
  86. const CGCDirTypeInstance *GetGCTypeInstance( uint32 nTypeInst ) const;
  87. int32 GetGCCountForType( int32 nTypeID ) const;
  88. const CGCDirTypeInstance *GetGCInstanceForType( int32 nTypeID, int32 nInstance ) const;
  89. const char *GetNameForGCType( int32 nTypeID ) const;
  90. int32 GetGCTypeForName( const char *pchName ) const;
  91. void RegisterProcessTypeFactory( const char* pszProcessType, GCFactory_t pfnFactory );
  92. GCFactory_t GetFactoryForProcessType( const char* pszProcessType ) const;
  93. //given a GC type, and an instance number, this will return the GC directory index used for routing. If the instance number
  94. //is greater than what is available, it will be modulo'd into the acceptable range. If no match for that type can be found, -1 will
  95. //be returned
  96. int32 GetGCDirIndexForInstance( int32 nTypeID, uint32 nInstanceIndex ) const;
  97. //given a GC type, this will add all of the GC indices associated with that type onto the provided vector
  98. void GetGCDirIndicesForType( int32 nTypeID, CUtlVector< uint32 >& nIndices ) const;
  99. private:
  100. bool m_bInitialized;
  101. CUtlVector<CGCDirProcess *> m_vecProcesses;
  102. CUtlVector<CGCDirTypeInstance *> m_vecTypeInstances;
  103. CUtlMap< int32, CCopyableUtlVector< CGCDirTypeInstance *> > m_mapGCsByType;
  104. struct RegisteredGCType_t
  105. {
  106. CUtlConstString m_strName;
  107. };
  108. CUtlMap< int32, RegisteredGCType_t > m_mapRegisteredGCTypes;
  109. CUtlDict< int32 > m_dictRegisteredGCNameToType;
  110. CUtlDict< GCFactory_t > m_dictProcessTypeToFactory;
  111. };
  112. // Gets the global directory singleton
  113. extern CDirectory *GDirectory();
  114. //the maximum value that a GC type can use. This is to enable bit masks to encode type information primarily (also don't allow the high bit to avoid SQL signed issues)
  115. #define GC_TYPE_MAX_NUMBER 62
  116. // Registration macro for GC types
  117. #define REG_GC_TYPE( typeNum, typeName ) \
  118. static class CRegGC_##typeName \
  119. { \
  120. public: \
  121. CRegGC_##typeName() { \
  122. COMPILE_TIME_ASSERT( typeNum <= GC_TYPE_MAX_NUMBER ); \
  123. GDirectory()->RegisterGCType( ( typeNum ), ( #typeName ) ); } \
  124. } g_RegGC_##typeName;
  125. #define REG_GC_PROCESS_TYPE( gcClass, typeName ) \
  126. static class CRegProcessType_##typeName \
  127. { \
  128. public: \
  129. static CGCBase *Factory( const CGCDirProcess *pDirGC ) { return new gcClass( pDirGC ); } \
  130. CRegProcessType_##typeName() { GDirectory()->RegisterProcessTypeFactory( ( #typeName ), &Factory ); } \
  131. } g_RegProcessType_##typeName;
  132. } // namespace GCSDK
  133. #endif // DIRECTORY_H