FIFE
2008.0
|
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 00082 virtual void clearResourceLoaders(); 00083 00087 virtual int addResourceFromLocation(ResourceLocation* loc); 00088 00096 virtual int addResourceFromFile(const std::string& filename); 00097 00102 virtual IResource& get(unsigned int index, bool inc = false); 00103 00109 virtual void release(unsigned int index, bool dec = false); 00110 00116 virtual int purgeLoadedResources(); 00117 00120 virtual int getResourceCount(int status); 00121 00124 virtual void printStatistics(); 00125 00128 void sanityCheck(); 00129 00135 virtual void reset(); 00136 00137 protected: 00138 class PoolEntry { 00139 public: 00140 PoolEntry(): resource(0), location(0), loader(0) {} 00141 ~PoolEntry() { 00142 delete location; 00143 delete resource; 00144 } 00145 00146 // Pointer to the resource that is loaded. 00147 IResource* resource; 00148 // Location of the resource. 00149 ResourceLocation* location; 00150 // Resource loader. 00151 ResourceLoader* loader; 00152 }; 00153 00154 void findAndSetProvider(PoolEntry& entry); 00155 00156 std::vector<PoolEntry*> m_entries; 00157 typedef std::map<ResourceLocation*, int, ResourceLocationComparator> ResourceLocationToEntry; 00158 ResourceLocationToEntry m_location_to_entry; 00159 std::vector<ResourceLoader*> m_loaders; 00160 std::string m_name; 00161 }; 00162 00163 } // FIFE 00164 00165 #endif