CCPTR.H 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. ** Command & Conquer Red Alert(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. /* $Header: /CounterStrike/CCPTR.H 1 3/03/97 10:24a Joe_bostic $ */
  19. /***********************************************************************************************
  20. *** 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 ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Command & Conquer *
  24. * *
  25. * File Name : CCPTR.H *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : 06/07/96 *
  30. * *
  31. * Last Update : June 7, 1996 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #ifndef CCPTR_H
  37. #define CCPTR_H
  38. /*
  39. ** The CCPtr class is designed for a specific purpose. It functions like a pointer except that
  40. ** it requires no fixups for saving and loading. If pointer fixups are not an issue, than using
  41. ** regular pointers would be more efficient.
  42. */
  43. template<class T>
  44. class CCPtr
  45. {
  46. public:
  47. CCPtr(void) : ID(-1) {};
  48. CCPtr(NoInitClass const & ) {};
  49. CCPtr(T * ptr);
  50. operator T * (void) const {
  51. if (ID == -1) return(NULL);
  52. assert(Heap != NULL && (unsigned)ID < Heap->Length());
  53. return((T*) (*Heap)[ID]);
  54. }
  55. T & operator * (void) const {
  56. assert(Heap != NULL && (unsigned)ID < Heap->Length());
  57. return(*(T*)(*Heap)[ID]);
  58. }
  59. T * operator -> (void) const {
  60. if (ID == -1) return(NULL);
  61. assert(Heap != NULL && (unsigned)ID < Heap->Length());
  62. return((T*) (*Heap)[ID]);
  63. }
  64. bool Is_Valid(void) const {return(ID != -1);}
  65. bool operator == (CCPtr<T> const & rvalue) const {return(ID == rvalue.ID);}
  66. bool operator != (CCPtr<T> const & rvalue) const {return(ID != rvalue.ID);}
  67. bool operator > (CCPtr<T> const & rvalue) const;
  68. bool operator <= (CCPtr<T> const & rvalue) const {return (rvalue > *this);}
  69. bool operator < (CCPtr<T> const & rvalue) const {return (*this != rvalue && rvalue > *this);}
  70. bool operator >= (CCPtr<T> const & rvalue) const {return (*this == rvalue || rvalue > *this);}
  71. long Raw(void) const {return(ID);}
  72. void Set_Raw(long value) {ID = value;}
  73. private:
  74. static FixedIHeapClass * Heap;
  75. /*
  76. ** This is the ID number of the object it refers to. By using an ID number, this class can
  77. ** be saved and loaded without pointer fixups.
  78. */
  79. int ID;
  80. };
  81. /*
  82. ** These template helper functions tell the compiler what to do in the
  83. ** ambiguous case of a CCPtr on one side and a regular type pointer on the
  84. ** other side. In such a case the compiler could create a temp CCPtr object
  85. ** OR call the conversion operator on the existing CCPtr object. Either way
  86. ** is technically valid, but the compiler doesn't know which is better so it
  87. ** generates an error. These routines force the conversion operator rather than
  88. ** creating a temporary object. This presumes that the conversion operator is
  89. ** cheaper than constructing a temporary and that cheaper solutions are desirable.
  90. */
  91. template<class T>
  92. int operator == (CCPtr<T> & lvalue, T * rvalue)
  93. {
  94. return((T*)lvalue == rvalue);
  95. }
  96. template<class T>
  97. int operator == (T * lvalue, CCPtr<T> & rvalue)
  98. {
  99. return(lvalue == (T*)rvalue);
  100. }
  101. #endif