FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
pool.h
1 /***************************************************************************
2  * Copyright (C) 2005-2008 by the FIFE team *
3  * http://www.fifengine.de *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 #ifndef FIFE_POOL_H
23 #define FIFE_POOL_H
24 
25 // Standard C++ library includes
26 #include <map>
27 #include <vector>
28 #include <string>
29 #include <iostream>
30 #include <cassert>
31 
32 // 3rd party library includes
33 
34 // FIFE includes
35 // These includes are split up in two parts, separated by one empty line
36 // First block: files included from the FIFE root src directory
37 // Second block: files included from the same folder
38 #include "resource.h"
39 #include "resource_location.h"
40 
41 namespace FIFE {
42 
43  struct ResourceLocationComparator {
44  bool operator()(const ResourceLocation* r1, const ResourceLocation* r2) const
45  {
46  return r1->operator<(*r2);
47  }
48  };
49 
50  class IResource;
51 
52  enum { RES_LOADED = 0x01, RES_NON_LOADED = 0x02};
53 
61  class Pool {
62  public:
65  static const int INVALID_ID = -1;
66 
70  Pool(const std::string& name);
71 
74  virtual ~Pool();
75 
78  virtual void addResourceLoader(ResourceLoader* loader);
79 
82  virtual void clearResourceLoaders();
83 
87  virtual int addResourceFromLocation(ResourceLocation* loc);
88 
96  virtual int addResourceFromFile(const std::string& filename);
97 
102  virtual IResource& get(unsigned int index, bool inc = false);
103 
109  virtual void release(unsigned int index, bool dec = false);
110 
116  virtual int purgeLoadedResources();
117 
120  virtual int getResourceCount(int status);
121 
124  virtual void printStatistics();
125 
128  void sanityCheck();
129 
135  virtual void reset();
136 
137  protected:
138  class PoolEntry {
139  public:
140  PoolEntry(): resource(0), location(0), loader(0) {}
141  ~PoolEntry() {
142  delete location;
143  delete resource;
144  }
145 
146  // Pointer to the resource that is loaded.
147  IResource* resource;
148  // Location of the resource.
149  ResourceLocation* location;
150  // Resource loader.
151  ResourceLoader* loader;
152  };
153 
154  void findAndSetProvider(PoolEntry& entry);
155 
156  std::vector<PoolEntry*> m_entries;
157  typedef std::map<ResourceLocation*, int, ResourceLocationComparator> ResourceLocationToEntry;
158  ResourceLocationToEntry m_location_to_entry;
159  std::vector<ResourceLoader*> m_loaders;
160  std::string m_name;
161  };
162 
163 } // FIFE
164 
165 #endif