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.

127 lines
4.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Shared object based on a CBaseRecord subclass
  4. //
  5. //=============================================================================
  6. #ifndef SCHEMASHAREDOBJECT_H
  7. #define SCHEMASHAREDOBJECT_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #ifndef GC
  12. #error "CSchemaSharedObject is only intended for use on GC-only shared objects. It shouldn't be included on the client"
  13. #endif
  14. #if defined( DEBUG )
  15. #include "gcbase.h"
  16. #endif
  17. namespace GCSDK
  18. {
  19. //----------------------------------------------------------------------------
  20. // Purpose: Contains helper functions to deal with passed in CRecordInfo,
  21. // so that a CRecordInfo can be built on the fly
  22. //----------------------------------------------------------------------------
  23. class CSchemaSharedObjectHelper
  24. {
  25. public:
  26. static bool BYieldingAddInsertToTransaction( CSQLAccess & sqlAccess, CRecordBase *pRecordBase );
  27. static bool BYieldingAddWriteToTransaction( CSQLAccess & sqlAccess, CRecordBase *pRecordBase, const CColumnSet & csDatabaseDirty );
  28. static bool BYieldingAddRemoveToTransaction( CSQLAccess & sqlAccess, CRecordBase *pRecordBase );
  29. static bool BYieldingAddToDatabase( CRecordBase *pRecordBase );
  30. static bool BYieldingReadFromDatabase( CRecordBase *pRecordBase );
  31. static void Dump( const CRecordBase *pRecordBase );
  32. };
  33. //----------------------------------------------------------------------------
  34. // Purpose: Base class for CSchemaSharedObject. This is where all the actual
  35. // code lives.
  36. //----------------------------------------------------------------------------
  37. class CSchemaSharedObjectBase : public CSharedObject
  38. {
  39. public:
  40. typedef CSharedObject BaseClass;
  41. CSchemaSharedObjectBase( CRecordInfo *pRecordInfo ) : BaseClass() {}
  42. virtual bool BYieldingAddInsertToTransaction( CSQLAccess & sqlAccess );
  43. virtual bool BYieldingAddWriteToTransaction( CSQLAccess & sqlAccess, const CUtlVector< int > &fields );
  44. virtual bool BYieldingAddRemoveToTransaction( CSQLAccess & sqlAccess );
  45. virtual bool BYieldingAddToDatabase();
  46. virtual bool BYieldingReadFromDatabase() ;
  47. // schema shared objects are GC-only and are never sent to clients.
  48. virtual bool BIsNetworked() const { return false; }
  49. virtual bool BParseFromMessage( const CUtlBuffer & buffer ) { return false; }
  50. virtual bool BParseFromMessage( const std::string &buffer ) { return false; }
  51. virtual bool BAddToMessage( CUtlBuffer & bufOutput ) const { return false; }
  52. virtual bool BAddToMessage( std::string *pBuffer ) const { return false; }
  53. virtual bool BAddDestroyToMessage( CUtlBuffer & bufDestroy ) const { return false; }
  54. virtual bool BAddDestroyToMessage( std::string *pBuffer ) const { return false; }
  55. virtual bool BUpdateFromNetwork( const CSharedObject & objUpdate ) { return false; }
  56. virtual bool BIsKeyLess( const CSharedObject & soRHS ) const;
  57. virtual void Copy( const CSharedObject & soRHS );
  58. virtual void Dump() const;
  59. // virtual bool BIsNetworkDirty() const { return false; }
  60. // virtual void MakeNetworkClean() {}
  61. const CRecordInfo * GetRecordInfo() const;
  62. #ifdef DBGFLAG_VALIDATE
  63. virtual void Validate( CValidator &validator, const char *pchName );
  64. #endif
  65. protected:
  66. virtual CRecordBase *GetPObject() = 0;
  67. const CRecordBase *GetPObject() const { return const_cast<CSchemaSharedObjectBase *>(this)->GetPObject(); }
  68. CColumnSet GetDatabaseDirtyColumnSet( const CUtlVector< int > &fields ) const;
  69. };
  70. //----------------------------------------------------------------------------
  71. // Purpose: Template for making a shared object that uses a specific Schema
  72. // class for its data store.
  73. //----------------------------------------------------------------------------
  74. template< typename SchObject_t, int nTypeID >
  75. class CSchemaSharedObject : public CSchemaSharedObjectBase
  76. {
  77. public:
  78. CSchemaSharedObject()
  79. : CSchemaSharedObjectBase( GCSDK::GSchemaFull().GetSchema( SchObject_t::k_iTable ).GetRecordInfo() )
  80. {
  81. }
  82. ~CSchemaSharedObject()
  83. {
  84. #ifdef DEBUG
  85. // Ensure this SO is not in any cache, or we have an error. Note we must provide the type
  86. //since we are in a destructor and it is unsafe to call virtual functions
  87. Assert( !GGCBase()->IsSOCached( this, nTypeID ) );
  88. #endif
  89. }
  90. virtual int GetTypeID() const { return nTypeID; }
  91. SchObject_t & Obj() { return m_schObject; }
  92. const SchObject_t & Obj() const { return m_schObject; }
  93. typedef SchObject_t SchObjectType_t;
  94. public:
  95. const static int k_nTypeID = nTypeID;
  96. const static int k_iFieldMax = SchObject_t::k_iFieldMax;
  97. protected:
  98. CRecordBase *GetPObject() { return &m_schObject; }
  99. private:
  100. SchObject_t m_schObject;
  101. };
  102. } // namespace GCSDK
  103. #endif //SCHEMASHAREDOBJECT_H