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.

136 lines
4.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Base class for objects that are kept in synch between client and server
  4. //
  5. //=============================================================================
  6. #ifndef SHAREDOBJECTCACHE_H
  7. #define SHAREDOBJECTCACHE_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "sharedobject.h"
  12. namespace GCSDK
  13. {
  14. //----------------------------------------------------------------------------
  15. // Purpose: The part of a shared object cache that handles all objects of a
  16. // single type.
  17. //----------------------------------------------------------------------------
  18. class CSharedObjectTypeCache
  19. {
  20. public:
  21. CSharedObjectTypeCache( int nTypeID );
  22. virtual ~CSharedObjectTypeCache();
  23. int GetTypeID() const { return m_nTypeID; }
  24. uint32 GetCount() const { return m_vecObjects.Count(); }
  25. CSharedObject *GetObject( uint32 nObj ) { return m_vecObjects[nObj]; }
  26. const CSharedObject *GetObject( uint32 nObj ) const { return m_vecObjects[nObj]; }
  27. virtual bool AddObject( CSharedObject *pObject );
  28. virtual bool AddObjectClean( CSharedObject *pObject );
  29. virtual CSharedObject *RemoveObject( const CSharedObject & soIndex );
  30. CSharedObject *RemoveObjectByIndex( uint32 nObj );
  31. void DestroyAllObjects();
  32. void RemoveAllObjectsWithoutDeleting();
  33. virtual void EnsureCapacity( uint32 nItems );
  34. CSharedObject *FindSharedObject( const CSharedObject & soIndex );
  35. virtual void Dump() const;
  36. #ifdef DBGFLAG_VALIDATE
  37. virtual void Validate( CValidator &validator, const char *pchName );
  38. #endif
  39. private:
  40. int FindSharedObjectIndex( const CSharedObject & soIndex ) const;
  41. void AddObjectInternal( CSharedObject *pObject );
  42. CSharedObjectVec m_vecObjects;
  43. int m_nTypeID;
  44. };
  45. //----------------------------------------------------------------------------
  46. // Purpose: A cache of a bunch of shared objects of different types. This class
  47. // is shared between clients, gameservers, and the GC and is
  48. // responsible for sending messages from the GC to cause object
  49. // creation/destruction/updating on the clients/gameservers.
  50. //----------------------------------------------------------------------------
  51. class CSharedObjectCache
  52. {
  53. public:
  54. CSharedObjectCache();
  55. virtual ~CSharedObjectCache();
  56. virtual const CSteamID & GetOwner() const = 0;
  57. bool AddObject( CSharedObject *pSharedObject );
  58. bool BDestroyObject( const CSharedObject & soIndex, bool bRemoveFromDatabase );
  59. CSharedObject *RemoveObject( const CSharedObject & soIndex );
  60. void RemoveAllObjectsWithoutDeleting();
  61. //called to find the type cache for the specified class ID. This will return NULL if one does not exist
  62. CSharedObjectTypeCache *FindBaseTypeCache( int nClassID ) const;
  63. //called to create the specified class ID. If one exists, this is the same as find, otherwise one will be constructed
  64. CSharedObjectTypeCache *CreateBaseTypeCache( int nClassID );
  65. CSharedObject *FindSharedObject( const CSharedObject & soIndex );
  66. template < class T >
  67. T *FindTypedSharedObject( const CSharedObject &soIndex )
  68. {
  69. return assert_cast<T *>( FindSharedObject( soIndex ) );
  70. }
  71. // returns various singleton objects
  72. template< typename SOClass_t >
  73. SOClass_t *GetSingleton() const
  74. {
  75. CSharedObjectTypeCache *pTypeCache = FindBaseTypeCache( SOClass_t::k_nTypeID );
  76. if ( pTypeCache )
  77. {
  78. AssertMsg2( pTypeCache->GetCount() == 0 || pTypeCache->GetCount() == 1, "GetSingleton() called on type %u that has invalid number of items %u.", SOClass_t::k_nTypeID, pTypeCache->GetCount() );
  79. if ( pTypeCache->GetCount() == 1 )
  80. {
  81. return (SOClass_t *)pTypeCache->GetObject( 0 );
  82. }
  83. }
  84. return NULL;
  85. }
  86. void SetVersion( uint64 ulVersion ) { m_ulVersion = ulVersion; }
  87. uint64 GetVersion() const { return m_ulVersion; }
  88. virtual void MarkDirty() {}
  89. virtual void Dump() const;
  90. #ifdef DBGFLAG_VALIDATE
  91. virtual void Validate( CValidator &validator, const char *pchName );
  92. #endif
  93. protected:
  94. virtual CSharedObjectTypeCache *AllocateTypeCache( int nClassID ) const = 0;
  95. CSharedObjectTypeCache *GetTypeCacheByIndex( int nIndex ) { return m_mapObjects.IsValidIndex( nIndex ) ? m_mapObjects.Element( nIndex ) : NULL; }
  96. int GetTypeCacheCount() const { return m_mapObjects.MaxElement(); }
  97. int FirstTypeCacheIndex() { return m_mapObjects.FirstInorder(); }
  98. int NextTypeCacheIndex( int iCurrent ) { return m_mapObjects.NextInorder( iCurrent ); }
  99. int InvalidTypeCacheIndex() { return m_mapObjects.InvalidIndex(); }
  100. uint64 m_ulVersion;
  101. private:
  102. CUtlMap<int, CSharedObjectTypeCache *> m_mapObjects;
  103. };
  104. } // namespace GCSDK
  105. #endif //SHAREDOBJECTCACHE_H