Splitting up my threading changes into smaller pieces, as it's getting too large. This change: pass strings by reference, never by value; some minor formatting cleanup in WResourceManager.

This commit is contained in:
wrenczes@gmail.com
2010-12-01 04:27:56 +00:00
parent f9b4f248aa
commit 609ece329e
2 changed files with 221 additions and 217 deletions
+20 -18
View File
@@ -79,14 +79,15 @@ struct WCacheSort{
}; };
template <class cacheItem, class cacheActual> template <class cacheItem, class cacheActual>
class WCache{ class WCache
{
public: public:
friend class WResourceManager; friend class WResourceManager;
WCache(); WCache();
~WCache(); ~WCache();
cacheItem* Retrieve(int id, string filename, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL); //Primary interface function. cacheItem* Retrieve(int id, const string& filename, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL); //Primary interface function.
bool Release(cacheActual* actual); //Releases an item, and deletes it if unlocked. bool Release(cacheActual* actual); //Releases an item, and deletes it if unlocked.
bool RemoveMiss(int id=0); //Removes a cache miss. bool RemoveMiss(int id=0); //Removes a cache miss.
bool RemoveOldest(); //Remove oldest unlocked item. bool RemoveOldest(); //Remove oldest unlocked item.
@@ -99,10 +100,10 @@ protected:
bool RemoveItem(cacheItem * item, bool force = true); //Removes an item, deleting it. if(force), ignores locks / permanent bool RemoveItem(cacheItem * item, bool force = true); //Removes an item, deleting it. if(force), ignores locks / permanent
bool UnlinkCache(cacheItem * item); //Removes an item from our cache, does not delete it. Use with care. bool UnlinkCache(cacheItem * item); //Removes an item from our cache, does not delete it. Use with care.
bool Delete(cacheItem * item); //SAFE_DELETE and garbage collect. If maxCached == 0, nullify first. (This means you have to free that cacheActual later!) bool Delete(cacheItem * item); //SAFE_DELETE and garbage collect. If maxCached == 0, nullify first. (This means you have to free that cacheActual later!)
cacheItem* Get(int id, string filename, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL); //Subordinate to Retrieve. cacheItem* Get(int id, const string& filename, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL); //Subordinate to Retrieve.
cacheItem* AttemptNew(string filename, int submode); //Attempts a new cache item, progressively clearing cache if it fails. cacheItem* AttemptNew(const string& filename, int submode); //Attempts a new cache item, progressively clearing cache if it fails.
int makeID(int id, string filename, int submode); //Makes an ID appropriate to the submode. int makeID(int id, const string& filename, int submode); //Makes an ID appropriate to the submode.
map<string,int> ids; map<string,int> ids;
map<int,cacheItem*> cache; map<int,cacheItem*> cache;
@@ -119,7 +120,8 @@ protected:
}; };
struct WManagedQuad { struct WManagedQuad
{
WCachedTexture * texture; WCachedTexture * texture;
string resname; string resname;
}; };
@@ -133,11 +135,11 @@ public:
void Unmiss(string filename); void Unmiss(string filename);
JQuad * RetrieveCard(MTGCard * card, int style = RETRIEVE_NORMAL,int submode = CACHE_NORMAL); JQuad * RetrieveCard(MTGCard * card, int style = RETRIEVE_NORMAL,int submode = CACHE_NORMAL);
JSample * RetrieveSample(string filename, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL); JSample * RetrieveSample(const string& filename, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL);
JTexture * RetrieveTexture(string filename, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL); JTexture * RetrieveTexture(const string& filename, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL);
JQuad * RetrieveQuad(string filename, float offX=0.0f, float offY=0.0f, float width=0.0f, float height=0.0f, string resname="", int style = RETRIEVE_LOCK, int submode = CACHE_NORMAL, int id = 0); JQuad * RetrieveQuad(const string& filename, float offX=0.0f, float offY=0.0f, float width=0.0f, float height=0.0f, string resname="", int style = RETRIEVE_LOCK, int submode = CACHE_NORMAL, int id = 0);
JQuad * RetrieveTempQuad(string filename, int submode = CACHE_NORMAL); JQuad * RetrieveTempQuad(const string& filename, int submode = CACHE_NORMAL);
hgeParticleSystemInfo * RetrievePSI(string filename, JQuad * texture, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL); hgeParticleSystemInfo * RetrievePSI(const string& filename, JQuad * texture, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL);
int RetrieveError(); int RetrieveError();
void Release(JTexture * tex); void Release(JTexture * tex);
@@ -165,13 +167,13 @@ public:
int AddQuadToManaged(const WManagedQuad& inManagedQuad); int AddQuadToManaged(const WManagedQuad& inManagedQuad);
//Our file redirect system. //Our file redirect system.
string graphicsFile(const string filename); string graphicsFile(const string& filename);
string avatarFile(const string filename); string avatarFile(const string& filename);
string cardFile(const string filename); string cardFile(const string& filename);
string musicFile(const string filename); string musicFile(const string& filename);
string sfxFile(const string filename); string sfxFile(const string& filename);
int fileOK(string filename, bool relative = false); int fileOK(const string&, bool relative = false);
int dirOK(string dirname); int dirOK(const string& dirname);
//For backwards compatibility with JResourceManager. Avoid using these, they're not optimal. //For backwards compatibility with JResourceManager. Avoid using these, they're not optimal.
int CreateTexture(const string &textureName); int CreateTexture(const string &textureName);
+23 -21
View File
@@ -116,6 +116,7 @@ unsigned long WResourceManager::SizeManaged()
return res; return res;
} }
unsigned int WResourceManager::Count() unsigned int WResourceManager::Count()
{ {
unsigned int count = 0; unsigned int count = 0;
@@ -127,6 +128,7 @@ unsigned int WResourceManager::Count()
count += psiWCache.managed.size(); count += psiWCache.managed.size();
return count; return count;
} }
unsigned int WResourceManager::CountCached() unsigned int WResourceManager::CountCached()
{ {
unsigned int count = 0; unsigned int count = 0;
@@ -185,6 +187,7 @@ WResourceManager::WResourceManager()
bThemedCards = false; bThemedCards = false;
} }
WResourceManager::~WResourceManager() WResourceManager::~WResourceManager()
{ {
LOG("==Destroying WResourceManager=="); LOG("==Destroying WResourceManager==");
@@ -333,12 +336,12 @@ JQuad * WResourceManager::GetQuad(int id)
return result; return result;
} }
JQuad * WResourceManager::RetrieveTempQuad(string filename, int submode) JQuad * WResourceManager::RetrieveTempQuad(const string& filename, int submode)
{ {
return RetrieveQuad(filename, 0, 0, 0, 0, "temporary", RETRIEVE_NORMAL, submode); return RetrieveQuad(filename, 0, 0, 0, 0, "temporary", RETRIEVE_NORMAL, submode);
} }
JQuad * WResourceManager::RetrieveQuad(string filename, float offX, float offY, float width, float height, string resname, JQuad * WResourceManager::RetrieveQuad(const string& filename, float offX, float offY, float width, float height, string resname,
int style, int submode, int id) int style, int submode, int id)
{ {
JQuad * jq = NULL; JQuad * jq = NULL;
@@ -399,6 +402,7 @@ JQuad * WResourceManager::RetrieveQuad(string filename, float offX, float offY,
//Texture doesn't exist, so no quad. //Texture doesn't exist, so no quad.
return NULL; return NULL;
} }
void WResourceManager::Release(JTexture * tex) void WResourceManager::Release(JTexture * tex)
{ {
if (!tex) return; if (!tex) return;
@@ -454,10 +458,8 @@ void WResourceManager::Release(JSample * sample)
sampleWCache.Release(sample); sampleWCache.Release(sample);
} }
JTexture * WResourceManager::RetrieveTexture(string filename, int style, int submode) JTexture * WResourceManager::RetrieveTexture(const string& filename, int style, int submode)
{ {
WCachedTexture * res = NULL;
//Aliases. //Aliases.
if (style == RETRIEVE_THUMB) if (style == RETRIEVE_THUMB)
{ {
@@ -465,7 +467,7 @@ JTexture * WResourceManager::RetrieveTexture(string filename, int style, int sub
style = RETRIEVE_NORMAL; style = RETRIEVE_NORMAL;
} }
res = textureWCache.Retrieve(0, filename, style, submode); WCachedTexture* res = textureWCache.Retrieve(0, filename, style, submode);
lastError = textureWCache.mError; lastError = textureWCache.mError;
if (res) if (res)
@@ -540,9 +542,8 @@ JTexture* WResourceManager::GetTexture(int id)
return jtex; return jtex;
} }
hgeParticleSystemInfo * WResourceManager::RetrievePSI(string filename, JQuad * texture, int style, int submode) hgeParticleSystemInfo * WResourceManager::RetrievePSI(const string& filename, JQuad * texture, int style, int submode)
{ {
if (!texture) return NULL; if (!texture) return NULL;
WCachedParticles * res = psiWCache.Retrieve(0, filename, style, submode); WCachedParticles * res = psiWCache.Retrieve(0, filename, style, submode);
@@ -558,7 +559,7 @@ hgeParticleSystemInfo * WResourceManager::RetrievePSI(string filename, JQuad * t
return NULL; return NULL;
} }
JSample * WResourceManager::RetrieveSample(string filename, int style, int submode) JSample * WResourceManager::RetrieveSample(const string& filename, int style, int submode)
{ {
WCachedSample * tc = NULL; WCachedSample * tc = NULL;
tc = sampleWCache.Retrieve(0, filename, style, submode); tc = sampleWCache.Retrieve(0, filename, style, submode);
@@ -574,7 +575,7 @@ JSample * WResourceManager::RetrieveSample(string filename, int style, int submo
return NULL; return NULL;
} }
string WResourceManager::graphicsFile(const string filename) string WResourceManager::graphicsFile(const string& filename)
{ {
char buf[512]; char buf[512];
@@ -625,7 +626,7 @@ string WResourceManager::graphicsFile(const string filename)
return graphdir; return graphdir;
} }
string WResourceManager::avatarFile(const string filename) string WResourceManager::avatarFile(const string& filename)
{ {
char buf[512]; char buf[512];
@@ -679,7 +680,7 @@ string WResourceManager::avatarFile(const string filename)
return ""; return "";
} }
string WResourceManager::cardFile(const string filename) string WResourceManager::cardFile(const string& filename)
{ {
char buf[512]; char buf[512];
string::size_type i = 0; string::size_type i = 0;
@@ -752,7 +753,7 @@ string WResourceManager::cardFile(const string filename)
return ""; return "";
} }
string WResourceManager::musicFile(const string filename) string WResourceManager::musicFile(const string& filename)
{ {
char buf[512]; char buf[512];
@@ -789,7 +790,7 @@ string WResourceManager::musicFile(const string filename)
return ""; return "";
} }
string WResourceManager::sfxFile(const string filename) string WResourceManager::sfxFile(const string& filename)
{ {
char buf[512]; char buf[512];
@@ -821,7 +822,7 @@ string WResourceManager::sfxFile(const string filename)
return ""; return "";
} }
int WResourceManager::dirOK(string dirname) int WResourceManager::dirOK(const string& dirname)
{ {
char fname[512]; char fname[512];
@@ -839,7 +840,7 @@ int WResourceManager::dirOK(string dirname)
return 0; return 0;
} }
int WResourceManager::fileOK(string filename, bool relative) int WResourceManager::fileOK(const string& filename, bool relative)
{ {
std::ifstream * fp = NULL; std::ifstream * fp = NULL;
if (relative) if (relative)
@@ -1068,7 +1069,7 @@ bool WCache<cacheItem, cacheActual>::RemoveOldest()
{ {
typename map<int, cacheItem*>::iterator oldest = cache.end(); typename map<int, cacheItem*>::iterator oldest = cache.end();
for (typename map<int, cacheItem *>::iterator it = cache.begin(); it != cache.end(); it++) for (typename map<int, cacheItem*>::iterator it = cache.begin(); it != cache.end(); ++it)
{ {
if (it->second && !it->second->isLocked() && (oldest == cache.end() || it->second->lastTime < oldest->second->lastTime)) oldest if (it->second && !it->second->isLocked() && (oldest == cache.end() || it->second->lastTime < oldest->second->lastTime)) oldest
= it; = it;
@@ -1117,7 +1118,7 @@ void WCache<cacheItem, cacheActual>::Resize(unsigned long size, int items)
} }
template<class cacheItem, class cacheActual> template<class cacheItem, class cacheActual>
cacheItem* WCache<cacheItem, cacheActual>::AttemptNew(string filename, int submode) cacheItem* WCache<cacheItem, cacheActual>::AttemptNew(const string& filename, int submode)
{ {
if (submode & CACHE_EXISTING) if (submode & CACHE_EXISTING)
{ //Should never get this far. { //Should never get this far.
@@ -1162,7 +1163,7 @@ cacheItem* WCache<cacheItem, cacheActual>::AttemptNew(string filename, int submo
} }
template<class cacheItem, class cacheActual> template<class cacheItem, class cacheActual>
cacheItem * WCache<cacheItem, cacheActual>::Retrieve(int id, string filename, int style, int submode) cacheItem* WCache<cacheItem, cacheActual>::Retrieve(int id, const string& filename, int style, int submode)
{ {
//Check cache. //Check cache.
cacheItem * tc = NULL; cacheItem * tc = NULL;
@@ -1224,8 +1225,9 @@ cacheItem * WCache<cacheItem, cacheActual>::Retrieve(int id, string filename, in
return NULL; return NULL;
} }
template<class cacheItem, class cacheActual> template<class cacheItem, class cacheActual>
int WCache<cacheItem, cacheActual>::makeID(int id, string filename, int submode) int WCache<cacheItem, cacheActual>::makeID(int id, const string& filename, int submode)
{ {
int mId = id; int mId = id;
if (!mId) if (!mId)
@@ -1250,7 +1252,7 @@ int WCache<cacheItem, cacheActual>::makeID(int id, string filename, int submode)
} }
template<class cacheItem, class cacheActual> template<class cacheItem, class cacheActual>
cacheItem * WCache<cacheItem, cacheActual>::Get(int id, string filename, int style, int submode) cacheItem* WCache<cacheItem, cacheActual>::Get(int id, const string& filename, int style, int submode)
{ {
typename map<int, cacheItem*>::iterator it; typename map<int, cacheItem*>::iterator it;
int lookup = makeID(id, filename, submode); int lookup = makeID(id, filename, submode);