30 #include "util/base/exception.h"
31 #include "util/log/logger.h"
36 static Logger _log(LM_POOL);
40 m_location_to_entry(),
47 FL_LOG(_log,
LMsg(
"Pool destroyed: ") << m_name);
52 std::vector<ResourceLoader*>::iterator loader;
53 for (loader = m_loaders.begin(); loader != m_loaders.end(); loader++) {
59 std::vector<PoolEntry*>::iterator entry;
60 for (entry = m_entries.begin(); entry != m_entries.end(); entry++) {
63 if( (*entry)->resource && (*entry)->resource->getRefCount() > 0 ) {
64 FL_WARN(_log,
LMsg(m_name +
" leak: ") << (*entry)->location->getFilename());
65 (*entry)->resource = 0;
70 m_location_to_entry.clear();
75 std::vector<PoolEntry*>::iterator it;
76 for (it = m_entries.begin(); it != m_entries.end(); it++) {
77 PoolEntry* entry = *it;
78 if( entry->resource && entry->resource->getRefCount() == 0 ) {
79 delete entry->resource;
88 m_loaders.push_back(loader);
96 ResourceLocationToEntry::const_iterator it = m_location_to_entry.find(loc);
97 if (it != m_location_to_entry.end()) {
101 PoolEntry* entry =
new PoolEntry();
102 entry->location = loc->
clone();
103 m_entries.push_back(entry);
104 size_t index = m_entries.size() - 1;
105 m_location_to_entry[entry->location] = index;
115 if (index >= m_entries.size()) {
116 FL_ERR(_log,
LMsg(
"Tried to get with index ") << index <<
", only " << m_entries.size() <<
" items in pool " + m_name);
117 throw IndexOverflow( __FUNCTION__ );
120 PoolEntry* entry = m_entries[index];
121 if (entry->resource) {
122 res = entry->resource;
124 if (!entry->loader) {
125 findAndSetProvider(*entry);
127 entry->resource = entry->loader->loadResource(*entry->location);
130 if (!entry->loader) {
131 LMsg msg(
"No suitable loader was found for resource ");
132 msg <<
"#" << index <<
"<" << entry->location->getFilename()
133 <<
"> in pool " << m_name;
136 throw NotFound(msg.str);
143 if (!entry->resource) {
144 LMsg msg(
"The loader was unable to load the resource ");
145 msg <<
"#" << index <<
"<" << entry->location->getFilename()
146 <<
"> in pool " << m_name;
148 throw NotFound(msg.str);
150 res = entry->resource;
155 res->setPoolId(index);
160 if (index >= m_entries.size()) {
161 throw IndexOverflow( __FUNCTION__ );
165 PoolEntry* entry = m_entries[index];
166 if (entry->resource) {
167 res = entry->resource;
171 if(res->getRefCount() == 0) {
172 delete entry->resource;
180 std::vector<PoolEntry*>::iterator entry;
181 for (entry = m_entries.begin(); entry != m_entries.end(); entry++) {
182 if (status & RES_LOADED) {
183 if ((*entry)->resource) {
187 if (status & RES_NON_LOADED) {
188 if (!(*entry)->resource) {
196 void Pool::findAndSetProvider(PoolEntry& entry) {
197 std::vector<ResourceLoader*>::iterator it = m_loaders.begin();
198 std::vector<ResourceLoader*>::iterator end = m_loaders.end();
200 FL_PANIC(_log,
"no loader constructors given for resource pool");
202 for(; it != end; ++it) {
203 IResource* res = (*it)->loadResource(*entry.location);
205 entry.resource = res;
216 std::vector<PoolEntry*>::iterator entry;
217 for (entry = m_entries.begin(); entry != m_entries.end(); entry++) {
218 if ((*entry)->resource) {
219 if ((*entry)->resource->getRefCount() > 0) {
224 FL_LOG(_log,
LMsg(
"Pool locked =") << amount);
225 FL_LOG(_log,
LMsg(
"Pool total size =") << m_entries.size());
234 for(
unsigned i = 0; i != m_entries.size(); ++i) {
236 for(
unsigned j = i+1; j < m_entries.size(); ++j) {
237 if( *m_entries[i]->location == *m_entries[j]->location )
242 FL_WARN(_log,
LMsg(
"Multiple entries: ") << m_entries[i]->location->getFilename()
243 <<
" #entries = " << (count+1) );