src/tools/tSafePTRBase.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 *************************************************************************
00004 
00005 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00006 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
00007 
00008 **************************************************************************
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00023   
00024 ***************************************************************************
00025 
00026 */
00027 
00028 #include "tSafePTR.h"
00029 //#include <unistd>
00030 #include "tList.h"
00031 
00032 /*
00033 class tPTRList:public tArray<tCheckedPTRBase *> {
00034   friend tCheckedPTRBase;
00035 
00036   // Array<T *> list;
00037 public:
00038   tPTRList():tArray<tCheckedPTRBase *>(10000){}
00039 
00040   void Add(tCheckedPTRBase *t){
00041     if (t->id<0){    // tEventQueue relies on the fact that we put t in
00042       t->id=Len();   // the last place.
00043       operator[](t->id)=t;
00044       if ((t->id)==710)
00045         st_Breakpoint();
00046     }
00047   }
00048   void Remove(tCheckedPTRBase *t){
00049     // con << "offset=" << offset << '\n';
00050     if (t->id>=0 && Len()>0){
00051 #ifdef DEBUG
00052       if ((t->id)==710)
00053         st_Breakpoint();
00054 
00055       if (t->id>=Len())
00056         tERR_ERROR("Scr. list structure!");
00057       
00058       tCheckedPTRBase *test=operator()(t->id);
00059       if (test!=t)
00060         tERR_ERROR("Screwed list structure!");
00061 #endif
00062     // the check for Len() is done, since this may be
00063       // called on an allready descructed list.
00064       tCheckedPTRBase *other=operator()(Len()-1);
00065       operator()(t->id)=other;
00066 
00067       if ((other->id)==710)
00068         st_Breakpoint();
00069 
00070       other->id=t->id;
00071       SetLen(Len()-1);
00072     }
00073     t->id=-1;
00074   }
00075 };
00076 */
00077 
00078 List<tCheckedPTRBase> tCheckedPTRs;
00079 
00080 tCheckedPTRBase::tCheckedPTRBase(void *x):id(-1),target(x){
00081     Check();
00082     tCheckedPTRs.Add(this,id);
00083 #ifdef DEBUG
00084     //con <<  "Added defined tCheckedPTR " << this << " no " << id << '\n';
00085     Check();
00086 #endif
00087 }
00088 
00089 tCheckedPTRBase::tCheckedPTRBase(const tCheckedPTRBase& x)
00090         :id(-1),target(x.target){
00091     Check();
00092     tCheckedPTRs.Add(this,id);
00093 #ifdef DEBUG
00094     //con <<  "Added defined tCheckedPTR  " << this << "  no " << id << '\n';
00095     Check();
00096 #endif
00097 }
00098 
00099 tCheckedPTRBase::tCheckedPTRBase():id(-1),target(NULL){
00100     Check();
00101     tCheckedPTRs.Add(this,id);
00102 #ifdef DEBUG
00103     //con <<  "Added NULL tCheckedPTR " << this << "  no " << id << '\n';
00104     Check();
00105 #endif
00106 }
00107 
00108 
00109 tCheckedPTRBase::~tCheckedPTRBase(){
00110 #ifdef DEBUG
00111     //con <<  "Removed tCheckedPTR no " << id << '\n';
00112     Check();
00113 #endif
00114     tCheckedPTRs.Remove(this,id);
00115     Check();
00116 };
00117 
00118 
00119 
00120 
00121 
00122 
00123 
00124 class gWallRim;
00125 class gNetPlayerWall;
00126 class eCamera;
00127 class tConfItemBase;
00128 class ePlayer;
00129 class ePlayerNetID;
00130 class uAction;
00131 
00132 /*
00133 
00134 #include "tConfiguration.h"
00135 #include "eGameObject.h"
00136 #include "eTess.h"
00137 
00138 
00139 extern List<gWallRim> se_rimWalls;
00140 extern List<gNetPlayerWall> sg_netPlayerWalls;
00141 extern List<gNetPlayerWall> gridded_sg_netPlayerWalls;
00142 extern List<eCamera> se_cameras;
00143 //extern List<tConfItemBase> tConfItemBase::tConfItems;
00144 //extern List<eGameObject> eGameObject::gameObjects;
00145 extern List<ePlayer> PlayerConfig;
00146 extern List<ePlayerNetID> se_PlayerNetIDs;
00147 extern List<uAction> s_playerActions;
00148 extern List<uAction> s_cameraActions;
00149 extern List<uAction> s_globalActions;
00150 extern List<uAction> su_allActions;
00151 //extern List<ePoint> ePoint::points;
00152 //extern List<eEdge> eEdge::edges;
00153 //extern List<eFace> eFace::faces;
00154 
00155 
00156 template<class T> void suspicious(const List<T> &l,tCheckedPTRBase *adr){
00157   T *best=NULL;
00158   int best_ind=-1;
00159   int i;
00160   for (i=l.Len()-1;i>=0;i--){
00161     T *t=l(i);
00162     int dist=int(adr)-int(t);
00163     if (dist>=0 && dist <1000 && int(t)>int(best)){
00164       best=t;
00165       best_ind=i;
00166     }
00167   }
00168   if (best){
00169     tERR_ERROR("Suspicious object has index " << best_ind << " and is at adress " 
00170           << best << ".");
00171 1  }
00172 }
00173 
00174 */
00175 
00176 
00177 
00178 void tCheckedPTRBase::CheckDestructed(void *test){
00179     for(int i=tCheckedPTRs.Len()-1;i>=0;i--){
00180         tCheckedPTRBase * adr=tCheckedPTRs(i);
00181         if (adr->id!=i)
00182             tERR_ERROR("Checked ptr list screwed! "
00183                        << adr << ":" << adr->id << ":" << adr->target << ":" << i);
00184 
00185         if (adr->target==test){
00186             /*
00187             suspicious(se_rimWalls,adr);
00188             suspicious(sg_netPlayerWalls,adr);
00189             suspicious(gridded_sg_netPlayerWalls,adr);
00190             suspicious(se_cameras,adr);
00191             suspicious(tConfItemBase::tConfItems,adr);
00192             suspicious(eGameObject::gameObjects,adr);
00193             suspicious(eGameObject::gameObjectsInactive,adr);
00194             suspicious(PlayerConfig,adr);
00195             suspicious(se_PlayerNetID,adr);
00196             suspicious(s_playerActions,adr);
00197             suspicious(s_cameraActions,adr);
00198             suspicious(s_globalActions,adr);
00199             suspicious(su_allActions,adr);
00200             suspicious(ePoint::points,adr);
00201             suspicious(eEdge::edges,adr);
00202             suspicious(eFace::faces,adr);
00203             */
00204 
00205             tERR_ERROR("Object destroyed, but pointer at " << adr <<
00206                        " still points to it!");
00207         }
00208     }
00209 
00210 }
00211 
00212 void tCheckedPTRBase::Check(){
00213     for(int i=tCheckedPTRs.Len()-1;i>=0;i--){
00214         tCheckedPTRBase * adr=tCheckedPTRs(i);
00215         if (adr->id!=i)
00216             tERR_ERROR("Checked ptr list screwed! "
00217                        << adr << ":" << adr->id << ":" << adr->target << ":" << i);
00218     }
00219 }
00220 
00221 
00222 
00223 
00224 
00225 

Generated on Sat Mar 15 22:56:02 2008 for Armagetron Advanced by  doxygen 1.5.4