cstool/framedataholder.h
Go to the documentation of this file.00001 /* 00002 Copyright (C) 2004 by Jorrit Tyberghein 00003 (C) 2004 by Frank Richter 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public 00016 License along with this library; if not, write to the Free 00017 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00018 */ 00019 00020 #ifndef __CS_CSTOOL_FRAMEDATAHOLDER_H__ 00021 #define __CS_CSTOOL_FRAMEDATAHOLDER_H__ 00022 00028 #include "csutil/array.h" 00029 00036 template <class T> 00037 class csFrameDataHolder 00038 { 00040 struct FrameData 00041 { 00043 uint lastFrame; 00045 T data; 00046 }; 00048 csArray<FrameData> data; 00050 size_t lastData; 00052 uint nextShrink; 00054 uint lastFrame; 00056 uint clearReq; 00057 00059 void NewFrame() 00060 { 00061 if (clearReq != (uint)~0) 00062 { 00063 data.DeleteAll(); 00064 clearReq = (uint)~0; 00065 } 00066 // Try to shrink the cache to a size that reflects what's needed 00067 if (lastFrame > nextShrink) 00068 { 00069 /* lastData is reset every frame, so it gives a good indicator 00070 * on how much has been used the last frame */ 00071 data.Truncate (lastData+1); 00072 data.ShrinkBestFit (); 00073 nextShrink = (uint)~0; 00074 } 00075 else if (lastData+1 < data.GetSize()) 00076 { 00077 // Last frame we used less cache than available. 00078 // "Schedule" a shrink in the future. 00079 nextShrink = lastFrame + 5; 00080 } 00081 lastData = 0; 00082 } 00083 public: 00084 csFrameDataHolder () : lastData(0), nextShrink((uint)~0), 00085 lastFrame((uint)~0), clearReq((uint)~0) 00086 { } 00087 ~csFrameDataHolder () { } 00088 00096 T& GetUnusedData (bool& created, uint frameNumber) 00097 { 00098 if (lastFrame != frameNumber) 00099 { 00100 NewFrame(); 00101 lastFrame = frameNumber; 00102 } 00103 created = false; 00104 if ((data.GetSize() == 0) || (data[lastData].lastFrame == frameNumber)) 00105 { 00106 size_t startPoint = lastData; 00107 //check the list 00108 if (data.GetSize() > 0) 00109 { 00110 do 00111 { 00112 if (data[lastData].lastFrame != frameNumber) 00113 break; 00114 lastData++; 00115 if (lastData >= data.GetSize()) lastData = 0; 00116 } 00117 while (lastData != startPoint); 00118 } 00119 if (lastData == startPoint) 00120 { 00121 data.SetSize ((lastData = data.GetSize ()) + 1); 00122 created = true; 00123 } 00124 } 00125 00126 FrameData& frameData = data[lastData]; 00127 frameData.lastFrame = frameNumber; 00128 return frameData.data; 00129 } 00130 00140 void Clear (bool instaClear = false) 00141 { 00142 if (instaClear) 00143 data.DeleteAll(); 00144 else 00145 // Don't clear just yet, rather, clear when we enter the next frame. 00146 clearReq = lastFrame; 00147 } 00148 }; 00149 00150 #endif // __CS_CSTOOL_FRAMEDATAHOLDER_H__
Generated for Crystal Space by doxygen 1.4.7