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