CrystalSpace

Public API Reference

Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

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

Generated for Crystal Space by doxygen 1.2.14