CrystalSpace

Public API Reference

Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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

Generated for Crystal Space by doxygen 1.4.4