CCPTR.H 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. /* $Header: /CounterStrike/CCPTR.H 1 3/03/97 10:24a Joe_bostic $ */
  15. /***********************************************************************************************
  16. *** 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 ***
  17. ***********************************************************************************************
  18. * *
  19. * Project Name : Command & Conquer *
  20. * *
  21. * File Name : CCPTR.H *
  22. * *
  23. * Programmer : Joe L. Bostic *
  24. * *
  25. * Start Date : 06/07/96 *
  26. * *
  27. * Last Update : June 7, 1996 [JLB] *
  28. * *
  29. *---------------------------------------------------------------------------------------------*
  30. * Functions: *
  31. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  32. #ifndef CCPTR_H
  33. #define CCPTR_H
  34. /*
  35. ** The CCPtr class is designed for a specific purpose. It functions like a pointer except that
  36. ** it requires no fixups for saving and loading. If pointer fixups are not an issue, than using
  37. ** regular pointers would be more efficient.
  38. */
  39. template<class T>
  40. class CCPtr
  41. {
  42. public:
  43. CCPtr(void) : ID(-1) {};
  44. CCPtr(NoInitClass const & ) {};
  45. CCPtr(T * ptr);
  46. operator T * (void) const {
  47. if (ID == -1) return(NULL);
  48. assert(Heap != NULL && ID < Heap->Length());
  49. return((T*) (*Heap)[ID]);
  50. }
  51. T & operator * (void) const {
  52. assert(Heap != NULL && ID < Heap->Length());
  53. return(*(T*)(*Heap)[ID]);
  54. }
  55. T * operator -> (void) const {
  56. if (ID == -1) return(NULL);
  57. assert(Heap != NULL && ID < Heap->Length());
  58. return((T*) (*Heap)[ID]);
  59. }
  60. bool Is_Valid(void) const {return(ID != -1);}
  61. bool operator == (CCPtr<T> const & rvalue) const {return(ID == rvalue.ID);}
  62. bool operator != (CCPtr<T> const & rvalue) const {return(ID != rvalue.ID);}
  63. bool operator > (CCPtr<T> const & rvalue) const;
  64. bool operator <= (CCPtr<T> const & rvalue) const {return (rvalue > *this);}
  65. bool operator < (CCPtr<T> const & rvalue) const {return (*this != rvalue && rvalue > *this);}
  66. bool operator >= (CCPtr<T> const & rvalue) const {return (*this == rvalue || rvalue > *this);}
  67. long Raw(void) const {return(ID);}
  68. void Set_Raw(long value) {ID = value;}
  69. static void Set_Heap(FixedIHeapClass *heap) {Heap = heap;}
  70. private:
  71. static FixedIHeapClass * Heap;
  72. /*
  73. ** This is the ID number of the object it refers to. By using an ID number, this class can
  74. ** be saved and loaded without pointer fixups.
  75. */
  76. int ID;
  77. };
  78. /*
  79. ** These template helper functions tell the compiler what to do in the
  80. ** ambiguous case of a CCPtr on one side and a regular type pointer on the
  81. ** other side. In such a case the compiler could create a temp CCPtr object
  82. ** OR call the conversion operator on the existing CCPtr object. Either way
  83. ** is technically valid, but the compiler doesn't know which is better so it
  84. ** generates an error. These routines force the conversion operator rather than
  85. ** creating a temporary object. This presumes that the conversion operator is
  86. ** cheaper than constructing a temporary and that cheaper solutions are desirable.
  87. */
  88. template<class T>
  89. int operator == (CCPtr<T> & lvalue, T * rvalue)
  90. {
  91. return((T*)lvalue == rvalue);
  92. }
  93. template<class T>
  94. int operator == (T * lvalue, CCPtr<T> & rvalue)
  95. {
  96. return(lvalue == (T*)rvalue);
  97. }
  98. #endif