CrystalSpace

Public API Reference

Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | 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 public:
00048   csFrameDataHolder ()
00049   {
00050     lastData = 0;
00051     nextShrink = 0;
00052   }
00053   ~csFrameDataHolder ()
00054   {
00055   }
00056 
00064   T& GetUnusedData (bool& created, uint frameNumber)
00065   {
00066     created = false;
00067     if ((data.Length() == 0) || (data[lastData].lastFrame == frameNumber))
00068     {
00069       lastData = (size_t)-1;
00070       //check the list
00071       size_t i;
00072       for(i = 0; i < data.Length (); i++)
00073       {
00074         if (data[i].lastFrame != frameNumber)
00075         {
00076           lastData = i;
00077           break;
00078         }
00079       }
00080       if (lastData == (size_t)-1)
00081       {
00082         data.SetLength ((lastData = data.Length ()) + 1);
00083         created = true;
00084         nextShrink = frameNumber + 1;
00085       }
00086     }
00087   
00088     // Conserve some memory
00089     if (!created && (frameNumber <= nextShrink))
00090     {
00091       data.ShrinkBestFit ();
00092     }
00093   
00094     FrameData& frameData = data[lastData];
00095     frameData.lastFrame = frameNumber;
00096     return frameData.data;
00097   }
00098   
00104   void Clear ()
00105   {
00106     data.DeleteAll();
00107   }
00108 };
00109 
00110 #endif // __CS_CSTOOL_FRAMEDATAHOLDER_H__

Generated for Crystal Space by doxygen 1.3.9.1