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.

119 lines
3.7 KiB

  1. //====== Copyright �, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose: Base class for transactions that modify a CGCSharedObjectCache and the database
  4. //
  5. //=============================================================================
  6. #ifndef SHAREDOBJECTTRANSACTION_H
  7. #define SHAREDOBJECTTRANSACTION_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. namespace GCSDK
  12. {
  13. class CSharedObjectTransaction
  14. {
  15. public:
  16. /**
  17. * Constructor that will begin a transaction
  18. * @param sqlAccess
  19. * @param pName
  20. */
  21. CSharedObjectTransaction( CSQLAccess &sqlAccess, const char *pName );
  22. /**
  23. * Destructor
  24. */
  25. ~CSharedObjectTransaction();
  26. /**
  27. * Adds an object that exists in the given CGCSharedObjectCache to be managed in this transaction.
  28. * Call this before making any modifications to the object
  29. * @param pSOCache the owner CGCSharedObjectCache
  30. * @param pObject the object that will be modified
  31. */
  32. bool AddManagedObject( CGCSharedObjectCache *pSOCache, CSharedObject *pObject );
  33. /**
  34. * Adds a brand new object to the given CGCSharedObjectCache
  35. * @param pSOCache the owner CGCSharedObjectCache
  36. * @param pObject the newly created object
  37. * @return true if successful, false otherwise
  38. */
  39. bool BAddNewObject( CGCSharedObjectCache *pSOCache, CSharedObject *pObject, bool bAddToDatabase = true );
  40. /**
  41. * Removes an existing object from the CGCSharedObjectCache
  42. * @param pSOCache the owner CGCSharedObjectCache
  43. * @param pObject the object to be removed from the CGCSharedObjectCache
  44. * @param bRemoveFromDatabase whether to remove the item from the database or not
  45. * @return true if successful, false otherwise
  46. */
  47. bool BRemoveObject( CGCSharedObjectCache *pSOCache, CSharedObject *pObject );
  48. /**
  49. * Marks in the transaction that the object was modified. The object must have been previously added via
  50. * the AddManagedObject() call in order for the object to be marked dirty. If the object is new to the
  51. * CGCSharedObjectCache, then calling this will return false (which is not necessarily an error)
  52. *
  53. * @param pObject the object that will be modified
  54. * @param unFieldIdx the field that was changed
  55. * @return true if the field was marked dirty, false otherwise.
  56. */
  57. bool BModifiedObject( CSharedObject *pObject, uint32 unFieldIdx );
  58. /**
  59. * @param pSOCache
  60. * @param soIndex
  61. * @return the CSharedObject that matches either in the CGCSharedObjectCache or to be added
  62. */
  63. CSharedObject *FindSharedObject( CGCSharedObjectCache *pSOCache, const CSharedObject &soIndex );
  64. /**
  65. * Rolls back any changes made to the objects in-memory and in the database
  66. *
  67. * This function should not be made virtual -- it's called from within the destructor.
  68. */
  69. void Rollback();
  70. /**
  71. * Commits any changes to the database and also to memory
  72. * @return true if successful, false otherwise
  73. */
  74. bool BYieldingCommit( bool bAllowEmpty = false );
  75. /**
  76. * @return GCSDK::CSQLAccess associated with this transaction
  77. */
  78. CSQLAccess &GetSQLAccess() { return m_sqlAccess; }
  79. protected:
  80. /**
  81. * Reverts all in-memory modifications and deletes all newly created objects.
  82. *
  83. * This function should not be made virtual -- it's called from within the destructor.
  84. */
  85. void Undo();
  86. struct undoinfo_t
  87. {
  88. CSharedObject *pObject;
  89. CGCSharedObjectCache *pSOCache;
  90. CSharedObject *pOriginalCopy;
  91. bool bAddToDatabase;
  92. };
  93. // variables
  94. CUtlVector< undoinfo_t > m_vecObjects_Added;
  95. CUtlVector< undoinfo_t > m_vecObjects_Removed;
  96. CUtlVector< undoinfo_t > m_vecObjects_Modified;
  97. CSQLAccess &m_sqlAccess;
  98. }; // class CSharedObjectTransaction
  99. }; // namespace GCSDK
  100. #endif // SHAREDOBJECTTRANSACTION_H