persistfactory.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WWSaveLoad *
  23. * *
  24. * $Archive:: /Commando/Code/wwsaveload/persistfactory.h $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 5/04/01 8:42p $*
  29. * *
  30. * $Revision:: 11 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if defined(_MSC_VER)
  36. #pragma once
  37. #endif
  38. #ifndef PERSISTFACTORY_H
  39. #define PERSISTFACTORY_H
  40. #include "always.h"
  41. #include "bittype.h"
  42. #include "chunkio.h"
  43. #include "wwdebug.h"
  44. #include "saveload.h"
  45. class PersistClass;
  46. /*
  47. ** PersistFactoryClass
  48. ** Create a PersistFactoryClass for each concrete derived PersistClass. These
  49. ** factories automatically register with the SaveLoadSystem in their constructors
  50. ** and should be accessible through the virtual Get_Factory method of any
  51. ** derived PersistClass.
  52. */
  53. class PersistFactoryClass
  54. {
  55. public:
  56. PersistFactoryClass(void);
  57. virtual ~PersistFactoryClass(void);
  58. virtual uint32 Chunk_ID(void) const = 0;
  59. virtual PersistClass * Load(ChunkLoadClass & cload) const = 0;
  60. virtual void Save(ChunkSaveClass & csave,PersistClass * obj) const = 0;
  61. private:
  62. PersistFactoryClass * NextFactory;
  63. friend class SaveLoadSystemClass;
  64. };
  65. /*
  66. ** SimplePersistFactoryClass
  67. ** This template automates the creation of a PersistFactory for any type of Persist
  68. ** object. Simply instantiate a single static instance of this template with the
  69. ** type and chunkid in the .cpp file of your class.
  70. */
  71. template <class T,int CHUNKID> class SimplePersistFactoryClass : public PersistFactoryClass
  72. {
  73. public:
  74. virtual uint32 Chunk_ID(void) const { return CHUNKID; }
  75. virtual PersistClass * Load(ChunkLoadClass & cload) const;
  76. virtual void Save(ChunkSaveClass & csave,PersistClass * obj) const;
  77. /*
  78. ** Internal chunk id's
  79. */
  80. enum
  81. {
  82. SIMPLEFACTORY_CHUNKID_OBJPOINTER = 0x00100100,
  83. SIMPLEFACTORY_CHUNKID_OBJDATA
  84. };
  85. };
  86. template<class T, int CHUNKID> PersistClass *
  87. SimplePersistFactoryClass<T,CHUNKID>::Load(ChunkLoadClass & cload) const
  88. {
  89. T * new_obj = new T;
  90. T * old_obj = NULL;
  91. cload.Open_Chunk();
  92. WWASSERT(cload.Cur_Chunk_ID() == SIMPLEFACTORY_CHUNKID_OBJPOINTER);
  93. cload.Read(&old_obj,sizeof(T *));
  94. cload.Close_Chunk();
  95. cload.Open_Chunk();
  96. WWASSERT(cload.Cur_Chunk_ID() == SIMPLEFACTORY_CHUNKID_OBJDATA);
  97. new_obj->Load(cload);
  98. cload.Close_Chunk();
  99. SaveLoadSystemClass::Register_Pointer(old_obj,new_obj);
  100. return new_obj;
  101. }
  102. template<class T, int CHUNKID> void
  103. SimplePersistFactoryClass<T,CHUNKID>::Save(ChunkSaveClass & csave,PersistClass * obj) const
  104. {
  105. uint32 objptr = (uint32)obj;
  106. csave.Begin_Chunk(SIMPLEFACTORY_CHUNKID_OBJPOINTER);
  107. csave.Write(&objptr,sizeof(uint32));
  108. csave.End_Chunk();
  109. csave.Begin_Chunk(SIMPLEFACTORY_CHUNKID_OBJDATA);
  110. obj->Save(csave);
  111. csave.End_Chunk();
  112. }
  113. #endif