CrystalSpace

Public API Reference

Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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 {
00039   struct FrameData
00040   {
00041     uint lastFrame;
00042     T data;
00043   };
00044   csArray<FrameData> data;
00045   size_t lastData;
00046   uint nextShrink;
00047   uint lastFrame, clearReq;
00048 public:
00049   csFrameDataHolder () : lastData(0), nextShrink(0), lastFrame((uint)~0), clearReq((uint)~0)
00050   { }
00051   ~csFrameDataHolder () { }
00052 
00060   T& GetUnusedData (bool& created, uint frameNumber)
00061   {
00062     if ((clearReq != (uint)~0) && (clearReq != frameNumber))
00063       data.DeleteAll();
00064     lastFrame = frameNumber;
00065     created = false;
00066     if ((data.GetSize() == 0) || (data[lastData].lastFrame == frameNumber))
00067     {
00068       lastData = (size_t)-1;
00069       //check the list
00070       size_t i;
00071       for(i = 0; i < data.GetSize (); i++)
00072       {
00073         if (data[i].lastFrame != frameNumber)
00074         {
00075           lastData = i;
00076           break;
00077         }
00078       }
00079       if (lastData == (size_t)-1)
00080       {
00081         data.SetSize ((lastData = data.GetSize ()) + 1);
00082         created = true;
00083         nextShrink = frameNumber + 1;
00084       }
00085     }
00086   
00087     // Conserve some memory
00088     if (!created && (frameNumber <= nextShrink))
00089     {
00090       data.ShrinkBestFit ();
00091     }
00092   
00093     FrameData& frameData = data[lastData];
00094     frameData.lastFrame = frameNumber;
00095     return frameData.data;
00096   }
00097   
00107   void Clear (bool instaClear = false)
00108   {
00109     if (instaClear)
00110       data.DeleteAll();
00111     else
00112       // Don't clear just yet, rather, clear when we enter the next frame.
00113       clearReq = lastFrame;
00114   }
00115 };
00116 
00117 #endif // __CS_CSTOOL_FRAMEDATAHOLDER_H__

Generated for Crystal Space by doxygen 1.4.4