Overridable.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // Overridable.h //////////////////////////////////////////////////////////////////////////////////
  24. // Electronic Arts Pacific
  25. // Do Not Distribute
  26. #pragma once
  27. #ifndef _OVERRIDABLE_H_
  28. #define _OVERRIDABLE_H_
  29. /*
  30. In order for something to live in an OVERRIDE<> object, it must be derived from Overridable
  31. (publicly).
  32. This is useful for things like templates, where we want to override the template and make sure
  33. that all instances get the updated values (for instance, via map.ini)
  34. */
  35. class Overridable : public MemoryPoolObject
  36. {
  37. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE( Overridable, "Overridable" )
  38. private:
  39. Overridable *m_nextOverride;
  40. Bool m_isOverride;
  41. public:
  42. Overridable() : m_nextOverride(NULL), m_isOverride(false) {}
  43. // return a constant version of m_nextOverride, which can be NULL if there is no
  44. // override
  45. const Overridable *getNextOverride( void ) const
  46. {
  47. return m_nextOverride;
  48. }
  49. // recursively ask if there is a next override, and if not, return this.
  50. const Overridable *getFinalOverride( void ) const
  51. {
  52. if (m_nextOverride)
  53. return m_nextOverride->getFinalOverride();
  54. return this;
  55. }
  56. // set the next override on this object. This currently makes no attempt to prevent leaks.
  57. // it probably shouldn't because doing so could cause infinite loops
  58. void setNextOverride(Overridable *nextOverridable)
  59. {
  60. m_nextOverride = nextOverridable;
  61. }
  62. // useful for the LocomotorStore to cleanup overrides.
  63. Overridable *friend_getNextOverride( void )
  64. {
  65. return m_nextOverride;
  66. }
  67. // useful for the LocomotorStore to create an override dangling off the final override.
  68. Overridable *friend_getFinalOverride( void )
  69. {
  70. if (m_nextOverride)
  71. return m_nextOverride->friend_getFinalOverride();
  72. return this;
  73. }
  74. // useful for the LocomotorStore to create an override dangling off the final override.
  75. const Overridable *friend_getFinalOverride( void ) const
  76. {
  77. if (m_nextOverride)
  78. return m_nextOverride->friend_getFinalOverride();
  79. return this;
  80. }
  81. // used by ini-parsing functions to mark specific Overridables as overrides
  82. void markAsOverride( void )
  83. {
  84. m_isOverride = true;
  85. }
  86. // used in factory reset() calls at the end of a game to clean up overrides. Can return NULL
  87. // if the first Overridable is itself an override
  88. Overridable *deleteOverrides( void )
  89. {
  90. if ( m_isOverride )
  91. {
  92. deleteInstance();
  93. return NULL;
  94. }
  95. else if ( m_nextOverride )
  96. {
  97. m_nextOverride = m_nextOverride->deleteOverrides();
  98. }
  99. return this;
  100. }
  101. };
  102. // cleans up and dangling overrides.
  103. __inline Overridable::~Overridable()
  104. {
  105. if (m_nextOverride)
  106. m_nextOverride->deleteInstance();
  107. }
  108. #endif /* _OVERRIDABLE_H_ */