Override.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. ** Command & Conquer Generals(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. // Override.h /////////////////////////////////////////////////////////////////////////////////////
  24. // Electronic Arts Pacific
  25. // Do Not Distribute
  26. #pragma once
  27. #ifndef _OVERRIDE_H_
  28. #define _OVERRIDE_H_
  29. #include "Common/Overridable.h"
  30. /*
  31. An OVERRIDE is a replacement for a pointer of its contained type, ie, rather than containing
  32. a LocomotorTemplate*, you would contain an OVERRIDE<LocomotorTemplate>.
  33. OVERRIDE pretends in all ways (dereference via *, -> and casting to type*) to be a type*, so
  34. there should be very little code that needs to be rewritten to work with these.
  35. In order to make something overridable, these are the steps:
  36. 1) Make the desired class derive from Overridable.
  37. 2) Make the container class contain an instance of OVERRIDE<Type>
  38. 3) Make the newOverride function (wherever an override is new'd) request the overridables lastOverride,
  39. to ensure that no leaks are created.
  40. See LocomotorTemplate for an example.
  41. */
  42. template <class T> class OVERRIDE
  43. {
  44. public:
  45. // Provide useful constructores to go from a T* to an OVERRIDE<T>
  46. OVERRIDE(const T *overridable = NULL);
  47. // Copy constructor
  48. OVERRIDE(OVERRIDE<T> &overridable);
  49. // Operator= for copying from another OVERRIDE and T*
  50. __inline OVERRIDE &operator=( const OVERRIDE<T>& override );
  51. __inline OVERRIDE &operator=( const T* overridable );
  52. // these are the methods which we can use to access data in a pointer. (Dereference*, ->, and cast
  53. // to T*). They are all overloaded to recurse to the lowest override and use that.
  54. __inline const T *operator->( void ) const; // overload const ->
  55. __inline const T *operator*( void ) const; // overload const *(dereference operator)
  56. __inline operator const T*( ) const; // overload casting to (const T*)
  57. // this is useful in case you want to get the pointer that this object is actually looking at.
  58. __inline const T *getNonOverloadedPointer( void ) const;
  59. private:
  60. // Because OVERRIDE is meant to live on the object and not in the store, it currently contains
  61. // a constant pointer. We could change this if it seems weird.
  62. const T *m_overridable;
  63. };
  64. //-------------------------------------------------------------------------------------------------
  65. template <class T>
  66. OVERRIDE<T>::OVERRIDE(const T *overridable)
  67. {
  68. m_overridable = overridable;
  69. }
  70. //-------------------------------------------------------------------------------------------------
  71. template <class T>
  72. OVERRIDE<T>::OVERRIDE(OVERRIDE<T> &overridable)
  73. {
  74. m_overridable = overridable.m_overridable;
  75. }
  76. //-------------------------------------------------------------------------------------------------
  77. template <class T>
  78. OVERRIDE<T> &OVERRIDE<T>::operator=( const OVERRIDE<T>& override )
  79. {
  80. m_overridable = override.m_overridable;
  81. return *this;
  82. }
  83. //-------------------------------------------------------------------------------------------------
  84. template <class T>
  85. OVERRIDE<T> &OVERRIDE<T>::operator=(const T* overridable)
  86. {
  87. m_overridable = overridable;
  88. return *this;
  89. }
  90. //-------------------------------------------------------------------------------------------------
  91. template <class T>
  92. const T *OVERRIDE<T>::operator->() const
  93. {
  94. if (!m_overridable)
  95. return NULL;
  96. return (T*) m_overridable->getFinalOverride();
  97. }
  98. //-------------------------------------------------------------------------------------------------
  99. template <class T>
  100. const T *OVERRIDE<T>::operator*() const
  101. {
  102. if (!m_overridable)
  103. return NULL;
  104. return (T*) m_overridable->getFinalOverride();
  105. }
  106. //-------------------------------------------------------------------------------------------------
  107. template <class T>
  108. const T *OVERRIDE<T>::getNonOverloadedPointer( void ) const
  109. {
  110. return (T*) m_overridable;
  111. }
  112. //-------------------------------------------------------------------------------------------------
  113. template <class T>
  114. OVERRIDE<T>::operator const T*( ) const
  115. {
  116. return operator*();
  117. }
  118. #endif /* _OVERRIDE_H_ */