ModuleFactory.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: ModuleFactory.h //////////////////////////////////////////////////////////////////////////
  24. // Author: Colin Day, September 2001
  25. // Desc: TheModuleFactory is where we actually instance modules for objects
  26. // and drawbles. Those modules are things such as an UpdateModule
  27. // or DamageModule or DrawModule etc.
  28. //
  29. // TheModuleFactory will contain a list of ModuleTemplates, when we
  30. // request a new module, we will look for that template in our
  31. // list and create it
  32. //
  33. ///////////////////////////////////////////////////////////////////////////////////////////////////
  34. #pragma once
  35. #ifndef __MODULEFACTORY_H_
  36. #define __MODULEFACTORY_H_
  37. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  38. #include <stdlib.h>
  39. #include "Common/GameMemory.h"
  40. #include "Common/SubsystemInterface.h"
  41. #include "Common/AsciiString.h"
  42. #include "Common/NameKeyGenerator.h"
  43. #include "Common/Module.h"
  44. #include "Common/STLTypedefs.h"
  45. // FORWARD REFERENCES /////////////////////////////////////////////////////////////////////////////
  46. class Thing;
  47. class Object;
  48. class Drawable;
  49. class INI;
  50. // TYPE DEFINITIONS ///////////////////////////////////////////////////////////////////////////////
  51. typedef Module *(*NewModuleProc)(Thing *thing, const ModuleData* moduleData);
  52. typedef ModuleData* (*NewModuleDataProc)(INI* ini);
  53. //-------------------------------------------------------------------------------------------------
  54. /** We use TheModulyFactory to register classes that will be attached
  55. * to objects and drawables which will be executed or "called back" in the
  56. * correct situations ... such as Die, Damage, Update etc or just as
  57. * a place to store data specific to that type of thing */
  58. //-------------------------------------------------------------------------------------------------
  59. class ModuleFactory : public SubsystemInterface, public Snapshot
  60. {
  61. public:
  62. ModuleFactory( void );
  63. virtual ~ModuleFactory( void );
  64. virtual void init( void );
  65. virtual void reset( void ) { } ///< We don't reset during the lifetime of the app
  66. virtual void update( void ) { } ///< As of now, we don't have a need for an update
  67. Module *newModule( Thing *thing, const AsciiString& name, const ModuleData* data, ModuleType type ); ///< allocate a new module
  68. // module-data
  69. ModuleData* newModuleDataFromINI(INI* ini, const AsciiString& name, ModuleType type, const AsciiString& moduleTag);
  70. Int findModuleInterfaceMask(const AsciiString& name, ModuleType type);
  71. virtual void crc( Xfer *xfer );
  72. virtual void xfer( Xfer *xfer );
  73. virtual void loadPostProcess( void );
  74. protected:
  75. class ModuleTemplate
  76. {
  77. public:
  78. ModuleTemplate() : m_createProc(NULL), m_createDataProc(NULL), m_whichInterfaces(0)
  79. {
  80. }
  81. NewModuleProc m_createProc; ///< creation method
  82. NewModuleDataProc m_createDataProc; ///< creation method
  83. Int m_whichInterfaces;
  84. };
  85. const ModuleTemplate* findModuleTemplate(const AsciiString& name, ModuleType type);
  86. /// adding a new module template to the factory, and assisting macro to make it easier
  87. void addModuleInternal( NewModuleProc proc, NewModuleDataProc dataproc, ModuleType type, const AsciiString& name, Int whichIntf );
  88. #define addModule( classname ) \
  89. addModuleInternal( classname::friend_newModuleInstance, \
  90. classname::friend_newModuleData, \
  91. classname::getModuleType(), \
  92. AsciiString( #classname ), \
  93. classname::getInterfaceMask())
  94. static NameKeyType makeDecoratedNameKey(const AsciiString& name, ModuleType type);
  95. typedef std::map< NameKeyType, ModuleTemplate, std::less<NameKeyType> > ModuleTemplateMap;
  96. typedef std::vector<const ModuleData*> ModuleDataList;
  97. ModuleTemplateMap m_moduleTemplateMap;
  98. ModuleDataList m_moduleDataList;
  99. }; // end class ModuleFactory
  100. // EXTERN /////////////////////////////////////////////////////////////////////////////////////////
  101. extern ModuleFactory *TheModuleFactory; ///< singleton definition
  102. #endif // __MODULEFACTORY_H_