diff options
author | Björn Steinbrink <B.Steinbrink@gmx.de> | 2006-01-31 02:51:37 +0000 |
---|---|---|
committer | Björn Steinbrink <B.Steinbrink@gmx.de> | 2006-01-31 02:51:37 +0000 |
commit | 3b1cb45812fe4308073057c27837bc0c69a5d35e (patch) | |
tree | f77657cdc2ae5d333acd47522bf261da1fef9fee /src | |
parent | 093d95683bfcbbff6e0ddd87414e2cd723f17dc9 (diff) | |
download | mana-3b1cb45812fe4308073057c27837bc0c69a5d35e.tar.gz mana-3b1cb45812fe4308073057c27837bc0c69a5d35e.tar.bz2 mana-3b1cb45812fe4308073057c27837bc0c69a5d35e.tar.xz mana-3b1cb45812fe4308073057c27837bc0c69a5d35e.zip |
Made ResourceManager a friend of Resource to avoid looping over decRef, which returns void now.
Diffstat (limited to 'src')
-rw-r--r-- | src/resources/resource.cpp | 6 | ||||
-rw-r--r-- | src/resources/resource.h | 4 | ||||
-rw-r--r-- | src/resources/resourcemanager.cpp | 27 | ||||
-rw-r--r-- | src/resources/resourcemanager.h | 4 |
4 files changed, 19 insertions, 22 deletions
diff --git a/src/resources/resource.cpp b/src/resources/resource.cpp index 5c1df52d..bd8cd437 100644 --- a/src/resources/resource.cpp +++ b/src/resources/resource.cpp @@ -43,7 +43,7 @@ Resource::incRef() mRefCount++; } -bool +void Resource::decRef() { // Reference may not already have reached zero @@ -56,9 +56,5 @@ Resource::decRef() ResourceManager *resman = ResourceManager::getInstance(); resman->release(mIdPath); delete this; - return true; - } - else { - return false; } } diff --git a/src/resources/resource.h b/src/resources/resource.h index fe534606..d8b93e2d 100644 --- a/src/resources/resource.h +++ b/src/resources/resource.h @@ -31,6 +31,8 @@ */ class Resource { + friend class ResourceManager; + public: /** * Constructor @@ -50,7 +52,7 @@ class Resource * @return <code>true</code> if the object was deleted * <code>false</code> otherwise. */ - bool + void decRef(); /** diff --git a/src/resources/resourcemanager.cpp b/src/resources/resourcemanager.cpp index e2839416..09102d2e 100644 --- a/src/resources/resourcemanager.cpp +++ b/src/resources/resourcemanager.cpp @@ -45,19 +45,18 @@ ResourceManager::ResourceManager() ResourceManager::~ResourceManager() { // Create our resource iterator. - std::map<std::string, Resource*>::iterator iter = resources.begin(); + ResourceIterator iter = mResources.begin(); // Iterate through and release references until objects are deleted. - while (!resources.empty()) + while (!mResources.empty()) { - Resource *res = resources.begin()->second; + Resource *res = mResources.begin()->second; std::string id = res->getIdPath(); int references = 0; - do { - references++; - } - while (!res->decRef()); + references += res->mRefCount; + release(res->mIdPath); + delete res; logger->log("ResourceManager::~ResourceManager() cleaned up %d " "references to %s", references, id.c_str()); @@ -126,10 +125,9 @@ Resource* ResourceManager::get(const E_RESOURCE_TYPE &type, const std::string &idPath) { // Check if the id exists, and return the value if it does. - std::map<std::string, Resource*>::iterator resIter = - resources.find(idPath); + ResourceIterator resIter = mResources.find(idPath); - if (resIter != resources.end() && resIter->second) { + if (resIter != mResources.end() && resIter->second) { resIter->second->incRef(); return resIter->second; } @@ -177,7 +175,7 @@ ResourceManager::get(const E_RESOURCE_TYPE &type, const std::string &idPath) if (resource) { resource->incRef(); - resources[idPath] = resource; + mResources[idPath] = resource; } // Return NULL if the object could not be created. @@ -223,13 +221,12 @@ ResourceManager::release(const std::string &idPath) { logger->log("ResourceManager::release(%s)", idPath.c_str()); - std::map<std::string, Resource*>::iterator resIter = - resources.find(idPath); + ResourceIterator resIter = mResources.find(idPath); // The resource has to exist - assert(resIter != resources.end() && resIter->second); + assert(resIter != mResources.end() && resIter->second); - resources.erase(idPath); + mResources.erase(idPath); } ResourceManager* diff --git a/src/resources/resourcemanager.h b/src/resources/resourcemanager.h index 6ebcd590..37fbe213 100644 --- a/src/resources/resourcemanager.h +++ b/src/resources/resourcemanager.h @@ -184,7 +184,9 @@ class ResourceManager private: static ResourceManager *instance; - std::map<std::string, Resource*> resources; + typedef std::map<std::string, Resource*> Resources; + typedef Resources::iterator ResourceIterator; + Resources mResources; }; #endif |