Mercurial > fife-parpg
comparison engine/core/util/resource/pool.h @ 0:4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
author | mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Sun, 29 Jun 2008 18:44:17 +0000 |
parents | |
children | 90005975cdbb |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4a0efb7baf70 |
---|---|
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 modify * | |
7 * it under the terms of the GNU General Public License as published by * | |
8 * the Free Software Foundation; either version 2 of the License, or * | |
9 * (at your option) any later version. * | |
10 * * | |
11 * This program 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 * | |
14 * GNU General Public License for more details. * | |
15 * * | |
16 * You should have received a copy of the GNU General Public License * | |
17 * along with this program; if not, write to the * | |
18 * Free Software Foundation, Inc., * | |
19 * 51 Franklin St, 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 class IResource; | |
44 | |
45 /** Clients of pool get notifications about pool events through this interface | |
46 */ | |
47 class IPoolListener { | |
48 public: | |
49 virtual void poolCleared() = 0; | |
50 virtual ~IPoolListener() {}; | |
51 }; | |
52 | |
53 enum { RES_LOADED = 0x01, RES_NON_LOADED = 0x02}; | |
54 | |
55 /** Pool is used to optimize memory usage for resources | |
56 * | |
57 * Pool guarantees that there is minimal amount of resources | |
58 * used in cases when it is would possible that multiple | |
59 * instances of the same data would be loaded into the memory. | |
60 * Pool is the owner for resources taking care of their deletion. | |
61 */ | |
62 class Pool { | |
63 public: | |
64 /** Indicates invalid index for pool | |
65 */ | |
66 static const int INVALID_ID = -1; | |
67 | |
68 /** Default constructor. | |
69 */ | |
70 Pool(); | |
71 | |
72 /** Destructor. | |
73 */ | |
74 virtual ~Pool(); | |
75 | |
76 /** Adds new resource provider. Transfers provider ownership to the pool | |
77 */ | |
78 virtual void addResourceLoader(ResourceLoader* loader); | |
79 | |
80 /** Adds new resource into the pool using the given location. | |
81 * @return The index of the resource in the pool. | |
82 */ | |
83 virtual int addResourceFromLocation(const ResourceLocation& loc); | |
84 | |
85 /** This is a convenience version of addResourceFromLocation(). | |
86 * It converts the filename into a ResourceLocation and then | |
87 * calls addResourceFromLocation. | |
88 * | |
89 * @param filename The file to be loaded. | |
90 * @return The index of the resource in the pool. | |
91 */ | |
92 virtual int addResourceFromFile(const std::string& filename); | |
93 | |
94 /** Gets resource from pool with given index | |
95 * | |
96 * @param inc Specifies weither this call will increase the ref counter | |
97 */ | |
98 virtual IResource& get(unsigned int index, bool inc = false); | |
99 | |
100 /** Gets resource index from pool with given filename | |
101 * The resource will be created if it is not in the pool | |
102 */ | |
103 virtual unsigned int getIndex(const std::string& filename); | |
104 | |
105 /** Removes the resource from pool if reference counter is null | |
106 * | |
107 * @param dec Specifies weither the ref counter will be decreased | |
108 * before checking | |
109 */ | |
110 virtual void release(unsigned int index, bool dec = false); | |
111 | |
112 /** Gets amount of resources in the pool with given status | |
113 */ | |
114 virtual int getResourceCount(int status); | |
115 | |
116 /** Clears pool from resources. Frees associated memory | |
117 */ | |
118 virtual void clear(); | |
119 | |
120 /** Adds pool listener. | |
121 * Pool listeners get indications e.g. when ownerships of pooled | |
122 * resources change. | |
123 */ | |
124 virtual void addPoolListener(IPoolListener* listener); | |
125 | |
126 /** Removes pool listener | |
127 */ | |
128 virtual void removePoolListener(IPoolListener* listener); | |
129 | |
130 /** Prints the cache statistics to the standard output | |
131 */ | |
132 virtual void printStatistics(); | |
133 | |
134 protected: | |
135 private: | |
136 class PoolEntry { | |
137 public: | |
138 PoolEntry(): resource(0), location(0), loader(0) {} | |
139 ~PoolEntry() { | |
140 delete location; | |
141 delete resource; | |
142 } | |
143 | |
144 // Pointer to the resource that is loaded. | |
145 IResource* resource; | |
146 // Location of the resource. | |
147 ResourceLocation* location; | |
148 // Resource loader. | |
149 ResourceLoader* loader; | |
150 }; | |
151 | |
152 void findAndSetProvider(PoolEntry& entry); | |
153 std::vector<PoolEntry*> m_entries; | |
154 std::vector<IPoolListener*> m_listeners; | |
155 std::vector<ResourceLoader*> m_loaders; | |
156 int m_curind; | |
157 }; | |
158 | |
159 } // FIFE | |
160 | |
161 #endif |