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.

138 lines
3.9 KiB

  1. //====== Copyright �, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose: Shared object based on a CBaseRecord subclass
  4. //
  5. //=============================================================================
  6. #ifndef PROTOBUFSHAREDOBJECT_H
  7. #define PROTOBUFSHAREDOBJECT_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "google/protobuf/descriptor.h"
  12. #include "tier1/keyvalues.h"
  13. #if defined( GC ) && defined( DEBUG )
  14. #include "gcbase.h"
  15. #endif
  16. namespace google
  17. {
  18. namespace protobuf
  19. {
  20. class Message;
  21. }
  22. }
  23. namespace GCSDK
  24. {
  25. //----------------------------------------------------------------------------
  26. // Purpose: Base class for CProtoBufSharedObject. This is where all the actual
  27. // code lives.
  28. //----------------------------------------------------------------------------
  29. class CProtoBufSharedObjectBase : public CSharedObject
  30. {
  31. public:
  32. typedef CSharedObject BaseClass;
  33. virtual bool BParseFromMessage( const CUtlBuffer & buffer ) OVERRIDE;
  34. virtual bool BParseFromMessage( const std::string &buffer ) OVERRIDE;
  35. virtual bool BUpdateFromNetwork( const CSharedObject & objUpdate ) OVERRIDE;
  36. virtual bool BAddToMessage( std::string *pBuffer ) const OVERRIDE;
  37. virtual bool BAddDestroyToMessage( std::string *pBuffer ) const OVERRIDE;
  38. virtual bool BIsKeyLess( const CSharedObject & soRHS ) const ;
  39. virtual void Copy( const CSharedObject & soRHS );
  40. virtual void Dump() const OVERRIDE;
  41. #ifdef GC
  42. virtual bool BParseFromMemcached( CUtlBuffer & buffer ) OVERRIDE;
  43. virtual bool BAddToMemcached( CUtlBuffer & bufOutput ) const OVERRIDE;
  44. #endif //GC
  45. // Static helpers
  46. static bool SerializeToBuffer( const ::google::protobuf::Message & msg, CUtlBuffer & bufOutput );
  47. static void Dump( const ::google::protobuf::Message & msg );
  48. protected:
  49. virtual ::google::protobuf::Message *GetPObject() = 0;
  50. const ::google::protobuf::Message *GetPObject() const { return const_cast<CProtoBufSharedObjectBase *>(this)->GetPObject(); }
  51. private:
  52. static ::google::protobuf::Message *BuildDestroyToMessage( const ::google::protobuf::Message & msg );
  53. };
  54. //----------------------------------------------------------------------------
  55. // Purpose: Template for making a shared object that uses a specific protobuf
  56. // message class for its wire protocol and in-memory representation.
  57. //----------------------------------------------------------------------------
  58. template< typename Message_t, int nTypeID >
  59. class CProtoBufSharedObject : public CProtoBufSharedObjectBase
  60. {
  61. public:
  62. ~CProtoBufSharedObject()
  63. {
  64. #if defined( GC ) && defined( DEBUG )
  65. // Ensure this SO is not in any cache, or we have an error. We must provide the type since it is a virutal function otherwise
  66. Assert( !GGCBase()->IsSOCached( this, nTypeID ) );
  67. #endif
  68. }
  69. virtual int GetTypeID() const { return nTypeID; }
  70. Message_t & Obj() { return m_msgObject; }
  71. const Message_t & Obj() const { return m_msgObject; }
  72. typedef Message_t SchObjectType_t;
  73. public:
  74. const static int k_nTypeID = nTypeID;
  75. protected:
  76. ::google::protobuf::Message *GetPObject() { return &m_msgObject; }
  77. private:
  78. Message_t m_msgObject;
  79. };
  80. //----------------------------------------------------------------------------
  81. // Purpose: Special protobuf shared object that caches its serialized form
  82. //----------------------------------------------------------------------------
  83. template< typename Message_t, int nTypeID >
  84. class CProtoBufCachedSharedObject : public CProtoBufSharedObject< Message_t, nTypeID >
  85. {
  86. #ifdef GC
  87. public:
  88. virtual bool BAddToMessage( std::string *pBuffer ) const OVERRIDE
  89. {
  90. UpdateCache();
  91. *pBuffer = m_cachedSerialize;
  92. return true;
  93. }
  94. void ClearCache() // You must call this when your object changes or your object won't update on the client!
  95. {
  96. m_cachedSerialize.clear();
  97. }
  98. private:
  99. void UpdateCache() const
  100. {
  101. if ( m_cachedSerialize.empty() )
  102. {
  103. Obj().SerializeToString( &m_cachedSerialize );
  104. }
  105. }
  106. mutable std::string m_cachedSerialize;
  107. #endif //GC
  108. };
  109. } // GCSDK namespace
  110. #endif //PROTOBUFSHAREDOBJECT_H