From 4b0172d1f666a12cffbdd6bd331e95a92d8f652f Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Thu, 23 Jun 2005 23:33:34 +0000 Subject: Finishing resource manager ability to clean up resources. Still work to do on resources other than images. --- src/resources/resource.cpp | 46 +++++++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 25 deletions(-) (limited to 'src/resources/resource.cpp') diff --git a/src/resources/resource.cpp b/src/resources/resource.cpp index b3aa80f6..3ff8064d 100644 --- a/src/resources/resource.cpp +++ b/src/resources/resource.cpp @@ -22,49 +22,45 @@ */ #include "resource.h" +#include "resourcemanager.h" Resource::Resource(): - referenceCount(0) + mRefCount(0) { } Resource::~Resource() { - // TODO: Notify resource manager about this resource being deleted } -bool Resource::isLoaded() const +void +Resource::setIdPath(const std::string &idPath) { - return loaded; + mIdPath = idPath; } -void Resource::incRef() +void +Resource::incRef() { - referenceCount++; + mRefCount++; } -bool Resource::decRef() +bool +Resource::decRef() { - /* Warning: There is still a serious problem with the object deleting - * itself and that is that the resource manager doesn't know about it - * currently, causing it to crash while trying to clean up. Don't use - * this function until that is solved. Probably we'll have to make it - * so that decrementing count goes through resource manager too. - */ - if (referenceCount > 0) - { - referenceCount--; + // Reference may not already have reached zero + assert(mRefCount != 0); - if (referenceCount == 0) { - //delete this; - return true; - } - else { - return false; - } + mRefCount--; + + if (mRefCount == 0) { + // Make sure resource manager won't refer to deleted resource + ResourceManager *resman = ResourceManager::getInstance(); + resman->release(mIdPath); + delete this; + return true; } else { - // Warning: Shouldn't get here! - return true; + return false; } } -- cgit v1.2.3-70-g09d2