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

Fl_Image_Cache.h

00001 /*
00002  * $Id: Fl_Image_Cache.h,v 1.4 2003/03/15 16:09:55 laza2000 Exp $
00003  *
00004  * Extended Fast Light Toolkit (EFLTK)
00005  * Copyright (C) 2002-2003 by EDE-Team
00006  * WWW: http://www.sourceforge.net/projects/ede
00007  *
00008  * Fast Light Toolkit (FLTK)
00009  * Copyright (C) 1998-2003 by Bill Spitzak and others.
00010  * WWW: http://www.fltk.org
00011  *
00012  * This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
00013  * version 2. See COPYING for details.
00014  *
00015  * Author : Mikko Lahteenmaki
00016  * Email  : mikko@fltk.net
00017  *
00018  * Please report all bugs and problems to "efltk-bugs@fltk.net"
00019  *
00020  */
00021 
00022 #ifndef _FL_IMAGE_CACHE_H_
00023 #define _FL_IMAGE_CACHE_H_
00024 
00025 #include "Fl_Image.h"
00026 #include "Fl_Ptr_List.h"
00027 
00029 class Fl_Image_Cache
00030 {
00031     typedef struct {
00032         Fl_Image *image;
00033         char *identify;
00034     } CachedImage;
00035 
00036     Fl_Ptr_List images;
00037     uint size_;
00038     bool autodelete_;
00039 
00040 public:
00041     // Size of cache as a param, 0 is unlimited
00042     Fl_Image_Cache(int size=0) { size_ = size; autodelete_ = false; }
00043     ~Fl_Image_Cache() { clear(); }
00044 
00045     // Clears cache, deletes images if auto delete is set true
00046     void clear() {
00047         for(uint n=0; n<images.size(); n++) {
00048             CachedImage *i = (CachedImage *)images[n];
00049             if(i->identify) delete []i->identify;
00050             if(i->image && autodelete_) delete i->image;
00051             delete i;
00052         }
00053         images.clear();
00054     }
00055     Fl_Image *get(const char *identify) {
00056         for(uint n=0; n<images.size(); n++) {
00057             CachedImage *i = (CachedImage *)images[n];
00058             if(!strcmp(identify, i->identify)) return i->image;
00059         }
00060         return 0;
00061     }
00062 
00063     // Removes image from cache, returns removed image
00064     Fl_Image *remove(const char *identify) {
00065         for(uint n=0; n<images.size(); n++) {
00066             CachedImage *i = (CachedImage *)images[n];
00067             if(!strcmp(identify, i->identify)) {
00068                 images.remove(i);
00069                 if(i->identify) delete []i->identify;
00070                 Fl_Image *tmp = i->image;
00071                 delete i;
00072                 return tmp;
00073             }
00074         }
00075         return 0;
00076     }
00077 
00078     // Returns replaced image, if any
00079     Fl_Image *add(Fl_Image *i, const char *identify) {
00080         if(!i) return 0;
00081 
00082         CachedImage *c;
00083         // Remove previously cached with same identify
00084         for(uint n=0; n<images.size(); n++) {
00085             c = (CachedImage *)images[n];
00086             if(!strcmp(identify, c->identify)) {
00087                 if(c->identify) delete []c->identify;
00088                 c->identify = strdup(identify);
00089                 Fl_Image *tmp = c->image;
00090                 c->image = i;
00091                 return tmp;
00092             }
00093         }
00094 
00095         c = new CachedImage;
00096         c->image = i;
00097         c->identify = strdup(identify);
00098 
00099         images.append(c);
00100         check_size();
00101 
00102         return 0;
00103     }
00104 
00105     // Check cache size and removes images first, if needed
00106     void check_size() {
00107         if(size_>0 && images.count() > size_) {
00108             CachedImage *i = (CachedImage *)images[0];
00109             images.remove(i);
00110             if(i->identify) delete []i->identify;
00111             if(i->image && autodelete_) delete i->image;
00112             delete i;
00113         }
00114     }
00115 
00116     bool autodelete() { return autodelete_; }
00117     void autodelete(bool val) { autodelete_ = val; }
00118     int size() { return size_; }
00119     void size(int s) { size_ = s; check_size(); }
00120     int cached() { return images.count(); }
00121 };
00122 
00123 #endif

Generated on Thu Jul 31 15:33:44 2003 for eFLTK by doxygen1.2.15