Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Namespace Members | Data Fields | Globals

ImagePool.h

Go to the documentation of this file.
00001 /* Copyright (C) 2002 Dominic Letourneau (dominic.letourneau@usherbrooke.ca)
00002 
00003    This library is free software; you can redistribute it and/or
00004    modify it under the terms of the GNU Lesser General Public
00005    License as published by the Free Software Foundation; either
00006    version 2.1 of the License, or (at your option) any later version.
00007    
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Lesser General Public License for more details.
00012    
00013    You should have received a copy of the GNU Lesser General Public
00014    License along with this library; if not, write to the Free Software
00015    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00016 */
00017 #ifndef _IMAGE_POOL_H_
00018 #define _IMAGE_POOL_H_
00019 
00020 #ifdef HAVE_CONFIG_H
00021 #include "config.h"
00022 #endif
00023 
00024 #include <map>
00025 #include <list>
00026 #include "misc.h"
00027 #include "Image.h"
00028 
00029 namespace RobotFlow {
00030 
00031 class ImagePool {
00032 
00033  protected:
00034   
00035   size_t m_max_stored;
00036   std::map<int,std::list<Image*> >  m_image_map;
00037   
00038   public:
00039 
00040   ImagePool(int _max_stored=50) 
00041     : m_max_stored(_max_stored) {
00042 
00043   }
00044   
00045   Image* newImage (int width, int height, int pixelsize) {
00046 
00047     //cerr<<endl<<"Image allocation from pool :"<<width<<" "<<height<<" "<<pixelsize<<endl;    
00048     int size = width * height * pixelsize;
00049 
00050     std::map<int, std::list<Image*> >::iterator iter = m_image_map.find(size);
00051 
00052     if (iter != m_image_map.end()) {
00053 
00054       if ((*iter).second.empty()) {
00055         return new Image(width, height, pixelsize);
00056       }
00057       else {
00058         Image *_image = (*iter).second.front();
00059         (*iter).second.pop_front();
00060         _image->ref();
00061         
00062         //update width & height
00063         _image->update_size(width,height);
00064         
00065         return _image;
00066       }
00067     }
00068     else {
00069       //image of that size unavailable
00070       return new Image(width, height, pixelsize);
00071     }
00072   }
00073 
00074   void release(Image *image) {
00075 
00076     //cerr<<endl<<"Pool release image"<<endl;    
00077     int size = image->get_size();
00078          
00079     std::map<int, std::list<Image*> >::iterator iter = m_image_map.find(size);
00080 
00081     if (iter != m_image_map.end()) {
00082       if ((*iter).second.size() <= m_max_stored) {
00083         (*iter).second.push_back(image);
00084       }
00085       else {
00086         delete image;
00087       }
00088     }
00089     else {
00090       m_image_map.insert(std::make_pair(size,std::list<Image*>(1,image)));
00091     }
00092   }
00093 
00094   ~ImagePool() {
00095 
00096     for (std::map<int, std::list<Image*> >::iterator iter = m_image_map.begin();
00097          iter != m_image_map.end(); iter++) {
00098     
00099       while (!(*iter).second.empty()) {
00100         Image *_image = (*iter).second.front();
00101         (*iter).second.pop_front();
00102         delete _image;
00103       }
00104     }
00105   }
00106 };
00107 
00108 }//namespace RobotFlow
00109 #endif

Generated on Wed Oct 5 14:36:12 2005 for RobotFlow by  doxygen 1.4.4