Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   Related Pages  

sglObjPool.hpp

00001 /*****************************************************************************
00002  * SGL: A Scene Graph Library
00003  *
00004  * Copyright (C) 1997-2001  Scott McMillan   All Rights Reserved.
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public
00017  * License along with this library; if not, write to the Free
00018  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  *****************************************************************************
00020  *     File: sglObjPool.hpp
00021  *   Author: Tom Stimson
00022  *  Created: 26 Mar 2000
00023  *****************************************************************************/
00024 
00025 #ifndef __SGL_OBJPOOL_HPP
00026 #define __SGL_OBJPOOL_HPP
00027 
00028 #include <sgl.h>
00029 #include <vector>
00030 
00031 
00032 template <class T>
00033 class sglObjPool : public vector<T*>
00034 {
00035 public:
00036    typedef typename vector<T*>::const_iterator base_iterator;
00037 
00038 public:
00039    // start     = initial number of objects to allocate
00040    // increment = number of objects to allocate when pool gets full
00041    sglObjPool(unsigned int start, unsigned int increment);
00042    ~sglObjPool();
00043 
00044    // Gets an unused object from the pool.
00045    T* newObject();
00046 
00047    // Returns an iterator to the current end of the used items in the pool.
00048    base_iterator currEnd() const { return m_curr_end; }
00049 
00050    // Returns the number of currently used objects in the pool.
00051    unsigned int currSize() const { return m_curr_end - begin(); }
00052 
00053    // Marks all items in the pool as unused.
00054    void reset() { m_curr_end = begin(); }
00055 
00056 private: // not implemented
00057    sglObjPool();
00058    sglObjPool(const sglObjPool &);
00059    sglObjPool &operator=(const sglObjPool &);
00060 
00061 private:
00062    unsigned int             m_increment;
00063    base_iterator            m_curr_end;
00064 };
00065 
00066 
00067 //----------------------------------------------------------------------------
00068 template <class T>
00069 inline sglObjPool<T>::sglObjPool(unsigned int start, unsigned int increment)
00070    : m_increment(increment)
00071 {
00072    for (unsigned int i = 0; i < start; ++i)
00073       push_back(new T);
00074 
00075    m_curr_end = begin();
00076 }
00077 
00078 //----------------------------------------------------------------------------
00079 template <class T>
00080 inline sglObjPool<T>::~sglObjPool()
00081 {
00082    for (base_iterator it = begin(); it != end(); ++it)
00083    {
00084       delete *it;
00085    }
00086 }
00087 
00088 //----------------------------------------------------------------------------
00089 template <class T>
00090 inline T* sglObjPool<T>::newObject()
00091 {
00092    if (m_curr_end == end())
00093    {
00094       for (unsigned int i = 0; i < m_increment; ++i)
00095       {
00096          push_back(new T);
00097       }
00098 
00099       // push_back may invalidate iterators
00100       m_curr_end = end() - m_increment;
00101    }
00102 
00103    T* obj = *m_curr_end;
00104    ++m_curr_end;
00105    return obj;
00106 }
00107 
00108 #endif

Generated at Mon Jul 1 18:00:05 2002 for SGL by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001