CrystalSpace

Public API Reference

csutil/fifo.h

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2005 by Jorrit Tyberghein
00003               (C) 2005 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_CSUTIL_FIFO_H__
00021 #define __CS_CSUTIL_FIFO_H__
00022 
00027 #include "csutil/array.h"
00028 
00033 template <class T, class ElementHandler = csArrayElementHandler<T>,
00034   class MemoryAllocator = CS::Memory::AllocatorMalloc>
00035 class csFIFO
00036 {
00037 public:
00038   typedef csFIFO<T, ElementHandler, MemoryAllocator> ThisType;
00039   typedef T ValueType;
00040   typedef ElementHandler ElementHandlerType;
00041   typedef MemoryAllocator AllocatorType;
00042 
00043 private:
00044   csArray<T, ElementHandler, MemoryAllocator> a1, a2;
00045 public:
00050   csFIFO (size_t icapacity = 0, size_t ithreshold = 0) 
00051     :  a1 (icapacity, ithreshold), a2 (icapacity, ithreshold) { }
00052 
00056   T PopTop ()
00057   {
00058     CS_ASSERT ((a1.Length() > 0) || (a2.Length() > 0));
00059     if (a2.Length() == 0)
00060     {
00061       size_t n = a1.Length();
00062       while (n-- > 0)
00063       {
00064         a2.Push (a1[n]);
00065       }
00066       a1.Empty();
00067     }
00068     return a2.Pop();
00069   }
00070 
00074   void Push (T const& what)
00075   {
00076     a1.Push (what);
00077   }
00078 
00080   size_t Length()
00081   {
00082     return a1.Length() + a2.Length();
00083   }
00084 
00089   bool Delete (T const& what)
00090   {
00091     return (a1.Delete (what) || a2.Delete (what));
00092   }
00093 
00095   void DeleteAll ()
00096   {
00097     a1.DeleteAll();
00098     a2.DeleteAll();
00099   }
00100 };
00101 
00102 #endif // __CS_CSUTIL_FIFO_H__

Generated for Crystal Space 1.0.2 by doxygen 1.4.7