pool.h

00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 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  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 #ifndef FIFE_POOL_H
00023 #define FIFE_POOL_H
00024 
00025 // Standard C++ library includes
00026 #include <map>
00027 #include <vector>
00028 #include <string>
00029 #include <iostream>
00030 #include <cassert>
00031 
00032 // 3rd party library includes
00033 
00034 // FIFE includes
00035 // These includes are split up in two parts, separated by one empty line
00036 // First block: files included from the FIFE root src directory
00037 // Second block: files included from the same folder
00038 #include "resource.h"
00039 #include "resource_location.h"
00040 
00041 namespace FIFE {
00042 
00043     struct ResourceLocationComparator {
00044         bool operator()(const ResourceLocation* r1, const ResourceLocation* r2) const
00045         {
00046             return r1->operator<(*r2);
00047         }
00048     };
00049 
00050     class IResource;
00051 
00052     enum { RES_LOADED = 0x01, RES_NON_LOADED  = 0x02};
00053 
00061     class Pool {
00062     public:
00065         static const int INVALID_ID = -1;
00066 
00070         Pool(const std::string& name);
00071 
00074         virtual ~Pool();
00075 
00078         virtual void addResourceLoader(ResourceLoader* loader);
00079 
00083         virtual int addResourceFromLocation(ResourceLocation* loc);
00084         
00092         virtual int addResourceFromFile(const std::string& filename);
00093 
00098         virtual IResource& get(unsigned int index, bool inc = false);
00099         
00105         virtual void release(unsigned int index, bool dec = false);
00106 
00112         virtual int purgeLoadedResources();
00113 
00116         virtual int getResourceCount(int status);
00117 
00120         virtual void printStatistics();
00121 
00124         void sanityCheck();
00125 
00131         virtual void reset();
00132 
00133     private:
00134         class PoolEntry {
00135         public:
00136             PoolEntry(): resource(0), location(0), loader(0) {}
00137             ~PoolEntry() {
00138                 delete location;
00139                 delete resource;
00140             }
00141 
00142             // Pointer to the resource that is loaded.
00143             IResource* resource;
00144             // Location of the resource.
00145             ResourceLocation* location;
00146             // Resource loader.
00147             ResourceLoader* loader;
00148         };
00149 
00150         void findAndSetProvider(PoolEntry& entry);
00151 
00152         std::vector<PoolEntry*> m_entries;
00153         typedef std::map<ResourceLocation*, int, ResourceLocationComparator> ResourceLocationToEntry;
00154         ResourceLocationToEntry m_location_to_entry;
00155         std::vector<ResourceLoader*> m_loaders;
00156         std::string m_name;
00157     };
00158 
00159 } // FIFE
00160 
00161 #endif