00001 // 00002 // /home/ms/sidplay/include/RCS/smart.h,v 00003 // 00004 00005 #ifndef SMART__H //ALFRED 00006 #define SMART__H //ALFRED 00007 00008 //#include <iostream.h> //ALFRED 00009 //#include <iomanip.h> //ALFRED 00010 #include <ctype.h> 00011 #include "mytypes.h" 00012 00013 00014 template <class T> 00015 class smartPtrBase 00016 { 00017 public: // ----------------------------------------------------------------- 00018 00019 // --- constructors --- 00020 00021 smartPtrBase(T* pBuffer, udword bufferLen) 00022 { 00023 doFree = false; 00024 if ( bufferLen >= 1 ) 00025 { 00026 pBufCurrent = ( pBufBegin = pBuffer ); 00027 pBufEnd = pBufBegin + bufferLen; 00028 bufLen = bufferLen; 00029 status = true; 00030 } 00031 else 00032 { 00033 pBufCurrent = ( pBufBegin = ( pBufEnd = 0 )); 00034 bufLen = 0; 00035 status = false; 00036 } 00037 } 00038 00039 smartPtrBase(udword bufferLen) 00040 { 00041 doFree = false; 00042 if ( bufferLen >= 1 ) 00043 { 00044 pBufCurrent = ( pBufBegin = new T[bufferLen] ); 00045 if ( pBufBegin == 0 ) 00046 { 00047 status = false; 00048 } 00049 doFree = true; 00050 pBufEnd = pBufBegin + bufferLen; 00051 bufLen = bufferLen; 00052 status = true; 00053 } 00054 else 00055 { 00056 pBufCurrent = ( pBufBegin = ( pBufEnd = 0 )); 00057 bufLen = 0; 00058 status = false; 00059 } 00060 } 00061 00062 // --- destructor --- 00063 00064 virtual ~smartPtrBase() 00065 { 00066 if ( doFree && (pBufBegin != 0) ) 00067 { 00068 delete[] pBufBegin; 00069 } 00070 } 00071 00072 // --- public member functions --- 00073 00074 T* tellBegin() { return pBufBegin; } //ALFRED - removed 'virtual' keyword 00075 udword tellLength() { return bufLen; } //ALFRED - removed 'virtual' keyword 00076 udword tellPos() { return (udword)(pBufCurrent-pBufBegin); } //ALFRED - removed 'virtual' keyword 00077 00078 bool reset(T element) //ALFRED - removed 'virtual' keyword 00079 { 00080 if ( bufLen >= 1 ) 00081 { 00082 pBufCurrent = pBufBegin; 00083 return (status = true); 00084 } 00085 else 00086 { 00087 return (status = false); 00088 } 00089 } 00090 00091 bool good() //ALFRED - removed 'virtual' keyword 00092 { 00093 if ( pBufCurrent < pBufEnd ) 00094 return true; 00095 else 00096 return false; 00097 } 00098 00099 bool fail() //ALFRED - removed 'virtual' keyword 00100 { 00101 if ( pBufCurrent == pBufEnd ) 00102 return true; 00103 else 00104 return false; 00105 } 00106 00107 void operator ++() //ALFRED - removed 'virtual' keyword 00108 { 00109 if ( status && !fail() ) 00110 { 00111 pBufCurrent++; 00112 } 00113 else 00114 { 00115 status = false; 00116 } 00117 } 00118 00119 void operator ++(int) //ALFRED - removed 'virtual' keyword 00120 { 00121 if ( status && !fail() ) 00122 { 00123 pBufCurrent++; 00124 } 00125 else 00126 { 00127 status = false; 00128 } 00129 } 00130 00131 void operator --() //ALFRED - removed 'virtual' keyword 00132 { 00133 if ( status && (pBufCurrent > pBufBegin) ) 00134 { 00135 pBufCurrent--; 00136 } 00137 else 00138 { 00139 status = false; 00140 } 00141 } 00142 00143 void operator --(int) //ALFRED - removed 'virtual' keyword 00144 { 00145 if ( status && (pBufCurrent > pBufBegin) ) 00146 { 00147 pBufCurrent--; 00148 } 00149 else 00150 { 00151 status = false; 00152 } 00153 } 00154 00155 void operator +=(udword offset) //ALFRED - removed 'virtual' keyword 00156 { 00157 if ( status && ((pBufCurrent + offset) < pBufEnd) ) 00158 { 00159 pBufCurrent += offset; 00160 } 00161 else 00162 { 00163 status = false; 00164 } 00165 } 00166 00167 void operator -=(udword offset) //ALFRED - removed 'virtual' keyword 00168 { 00169 if ( status && ((pBufCurrent - offset) >= pBufBegin) ) 00170 { 00171 pBufCurrent -= offset; 00172 } 00173 else 00174 { 00175 status = false; 00176 } 00177 } 00178 00179 T operator*() 00180 { 00181 if ( status && good() ) 00182 { 00183 return *pBufCurrent; 00184 } 00185 else 00186 { 00187 status = false; 00188 return dummy; 00189 } 00190 } 00191 00192 T& operator [](udword index) 00193 { 00194 if ( status && ((pBufCurrent + index) < pBufEnd) ) 00195 { 00196 return pBufCurrent[index]; 00197 } 00198 else 00199 { 00200 status = false; 00201 return dummy; 00202 } 00203 } 00204 00205 #if !defined(__BORLANDC__) 00206 // operator int() { return status; } //ALFRED - removed 'virtual' keyword 00207 #endif 00208 operator bool() { return status; } //ALFRED - removed 'virtual' keyword 00209 00210 protected: // -------------------------------------------------------------- 00211 00212 T* pBufBegin; 00213 T* pBufEnd; 00214 T* pBufCurrent; 00215 udword bufLen; 00216 bool status; 00217 bool doFree; 00218 T dummy; 00219 }; 00220 00221 00222 template <class TP> 00223 class smartPtr : public smartPtrBase<TP> 00224 { 00225 public: // ----------------------------------------------------------------- 00226 00227 // --- constructors --- 00228 00229 smartPtr(TP* pBuffer, udword bufferLen) : smartPtrBase<TP>(pBuffer, bufferLen) 00230 { 00231 } 00232 00233 smartPtr(udword bufferLen) : smartPtrBase<TP>(bufferLen) 00234 { 00235 } 00236 00237 // --- public member functions --- 00238 00239 }; 00240 00241 00242 #endif // SMART__H //ALFRED