CrystalSpace

Public API Reference

csgeom/polypool.h

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 1998 by Jorrit Tyberghein
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public
00015     License along with this library; if not, write to the Free
00016     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 */
00018 
00019 #ifndef __CS_POLYPOOL_H__
00020 #define __CS_POLYPOOL_H__
00021 
00029 #include "csextern.h"
00030 
00031 #include "csgeom/poly2d.h"
00032 
00039 class csPoly2DPool
00040 {
00041 private:
00042   struct PoolObj
00043   {
00044     PoolObj* next;
00045     csPoly2D* pol2d;
00046   };
00048   PoolObj* alloced;
00050   PoolObj* freed;
00051 
00052   // Factory for creating new polygons.
00053   csPoly2DFactory* factory;
00054 
00055 public:
00057   csPoly2DPool (csPoly2DFactory* fact) : alloced (0), freed (0),
00058         factory (fact) { }
00059 
00061   ~csPoly2DPool ()
00062   {
00063     while (alloced)
00064     {
00065       PoolObj* n = alloced->next;
00066       //delete alloced->pol2d; @@@ This free is not valid!
00067       // We should use a ref count on the pool itself so that we
00068       // now when all objects in the pool are freed and the
00069       // 'alloced' list will be empty.
00070       delete alloced;
00071       alloced = n;
00072     }
00073     while (freed)
00074     {
00075       PoolObj* n = freed->next;
00076       delete freed->pol2d;
00077       delete freed;
00078       freed = n;
00079     }
00080   }
00081 
00083   csPoly2D* Alloc ()
00084   {
00085     PoolObj* pnew;
00086     if (freed)
00087     {
00088       pnew = freed;
00089       freed = freed->next;
00090     }
00091     else
00092     {
00093       pnew = new PoolObj ();
00094       pnew->pol2d = factory->Create ();
00095     }
00096     pnew->next = alloced;
00097     alloced = pnew;
00098     return pnew->pol2d;
00099   }
00100 
00106   void Free (csPoly2D* pol)
00107   {
00108     if (alloced)
00109     {
00110       PoolObj* po = alloced;
00111       alloced = alloced->next;
00112       po->pol2d = pol;
00113       po->next = freed;
00114       freed = po;
00115     }
00116     else
00117     {
00118       // Cannot happen!
00119       CS_ASSERT (false);
00120     }
00121   }
00122 };
00123 
00126 #endif // __CS_POLYPOOL_H__

Generated for Crystal Space 1.2.1 by doxygen 1.5.3