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.

114 lines
4.1 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 "soid.h"
  12. #include "sharedobject.h"
  13. namespace GCSDK
  14. {
  15. //----------------------------------------------------------------------------
  16. // Purpose: The part of a shared object cache that handles all objects of a
  17. // single type.
  18. //----------------------------------------------------------------------------
  19. class CSharedObjectTypeCache
  20. {
  21. public:
  22. CSharedObjectTypeCache( int nTypeID );
  23. virtual ~CSharedObjectTypeCache();
  24. int GetTypeID() const { return m_nTypeID; }
  25. uint32 GetCount() const { return m_vecObjects.Count(); }
  26. CSharedObject *GetObject( uint32 nObj ) { return m_vecObjects[nObj]; }
  27. const CSharedObject *GetObject( uint32 nObj ) const { return m_vecObjects[nObj]; }
  28. virtual bool AddObject( CSharedObject *pObject );
  29. virtual bool AddObjectClean( CSharedObject *pObject );
  30. virtual CSharedObject *RemoveObject( const CSharedObject & soIndex );
  31. virtual void RemoveAllObjectsWithoutDeleting();
  32. virtual void EnsureCapacity( uint32 nItems );
  33. CSharedObject *FindSharedObject( const CSharedObject & soIndex );
  34. const CSharedObject *FindSharedObject( const CSharedObject & soIndex ) const;
  35. virtual void Dump() const;
  36. protected:
  37. CSharedObject *RemoveObjectByIndex( uint32 nObj );
  38. bool HasElement( CSharedObject *pSO ) const { return m_vecObjects.HasElement( pSO ); }
  39. private:
  40. int FindSharedObjectIndex( const CSharedObject & soIndex ) const;
  41. CSharedObjectVec m_vecObjects;
  42. int m_nTypeID;
  43. };
  44. //----------------------------------------------------------------------------
  45. // Purpose: A cache of a bunch of shared objects of different types. This class
  46. // is shared between clients, gameservers, and the GC and is
  47. // responsible for sending messages from the GC to cause object
  48. // creation/destruction/updating on the clients/gameservers.
  49. //----------------------------------------------------------------------------
  50. class CSharedObjectCache
  51. {
  52. public:
  53. CSharedObjectCache();
  54. virtual ~CSharedObjectCache();
  55. virtual SOID_t GetOwner() const = 0;
  56. virtual bool AddObject( CSharedObject *pSharedObject );
  57. virtual bool AddObjectClean( CSharedObject *pSharedObject );
  58. virtual CSharedObject *RemoveObject( const CSharedObject & soIndex );
  59. virtual bool RemoveAllObjectsWithoutDeleting();
  60. //called to find the type cache for the specified class ID. This will return NULL if one does not exist
  61. const CSharedObjectTypeCache *FindBaseTypeCache( int nClassID ) const;
  62. //called to find the type cache for the specified class ID. This will return NULL if one does not exist
  63. CSharedObjectTypeCache *FindBaseTypeCache( int nClassID );
  64. //called to create the specified class ID. If one exists, this is the same as find, otherwise one will be constructed
  65. CSharedObjectTypeCache *CreateBaseTypeCache( int nClassID );
  66. CSharedObject *FindSharedObject( const CSharedObject & soIndex );
  67. const CSharedObject *FindSharedObject( const CSharedObject & soIndex ) const;
  68. void SetVersion( uint64 ulVersion ) { m_ulVersion = ulVersion; }
  69. uint64 GetVersion() const { return m_ulVersion; }
  70. virtual void MarkDirty() {}
  71. virtual void Dump() const;
  72. protected:
  73. virtual CSharedObjectTypeCache *AllocateTypeCache( int nClassID ) const = 0;
  74. CSharedObjectTypeCache *GetTypeCacheByIndex( int nIndex ) { return m_CacheObjects.IsValidIndex( nIndex ) ? m_CacheObjects.Element( nIndex ) : NULL; }
  75. int GetTypeCacheCount() const { return m_CacheObjects.Count(); }
  76. int FirstTypeCacheIndex() const { return m_CacheObjects.Count() > 0 ? 0 : m_CacheObjects.InvalidIndex(); }
  77. int NextTypeCacheIndex( int iCurrent ) const { return ( iCurrent + 1 < m_CacheObjects.Count() ) ? iCurrent + 1 : m_CacheObjects.InvalidIndex(); }
  78. int InvalidTypeCacheIndex() const { return m_CacheObjects.InvalidIndex(); }
  79. uint64 m_ulVersion;
  80. private:
  81. CUtlVector< CSharedObjectTypeCache * > m_CacheObjects;
  82. };
  83. } // namespace GCSDK
  84. #endif //SHAREDOBJECTCACHE_H