Google

Main Page   Class Hierarchy   Compound List   File List   Compound Members  

polypool.h

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 
00022 #include "csgeom/poly2d.h"
00023 
00030 class csPoly2DPool
00031 {
00032 private:
00033   struct PoolObj
00034   {
00035     PoolObj* next;
00036     csPoly2D* pol2d;
00037   };
00039   PoolObj* alloced;
00041   PoolObj* freed;
00042 
00043   // Factory for creating new polygons.
00044   csPoly2DFactory* factory;
00045 
00046 public:
00048   csPoly2DPool (csPoly2DFactory* fact) : alloced (NULL), freed (NULL),
00049         factory (fact) { }
00050 
00052   ~csPoly2DPool ()
00053   {
00054     while (alloced)
00055     {
00056       PoolObj* n = alloced->next;
00057       //delete alloced->pol2d; @@@ This free is not valid!
00058       // We should use a ref count on the pool itself so that we
00059       // now when all objects in the pool are freed and the
00060       // 'alloced' list will be empty.
00061       delete alloced;
00062       alloced = n;
00063     }
00064     while (freed)
00065     {
00066       PoolObj* n = freed->next;
00067       delete freed->pol2d;
00068       delete freed;
00069       freed = n;
00070     }
00071   }
00072 
00074   csPoly2D* Alloc ()
00075   {
00076     PoolObj* pnew;
00077     if (freed)
00078     {
00079       pnew = freed;
00080       freed = freed->next;
00081     }
00082     else
00083     {
00084       pnew = new PoolObj ();
00085       pnew->pol2d = factory->Create ();
00086     }
00087     pnew->next = alloced;
00088     alloced = pnew;
00089     return pnew->pol2d;
00090   }
00091 
00097   void Free (csPoly2D* pol)
00098   {
00099     if (alloced)
00100     {
00101       PoolObj* po = alloced;
00102       alloced = alloced->next;
00103       po->pol2d = pol;
00104       po->next = freed;
00105       freed = po;
00106     }
00107     else
00108     {
00109       // Cannot happen!
00110       CS_ASSERT (false);
00111     }
00112   }
00113 };
00114 
00115 
00116 #endif // __CS_POLYPOOL_H__

Generated for Crystal Space by doxygen 1.2.5 written by Dimitri van Heesch, ©1997-2000