ResourceHolder.cpp

00001 /***************************************************************************
00002  *                                                                         *
00003  *   (c) Art Tevs, MPI Informatik Saarbruecken                             *
00004  *       mailto: <tevs@mpi-sb.mpg.de>                                      *
00005  *                                                                         *
00006  *   This program is free software; you can redistribute it and/or modify  *
00007  *   it under the terms of the GNU General Public License as published by  *
00008  *   the Free Software Foundation; either version 2 of the License, or     *
00009  *   (at your option) any later version.                                   *
00010  *                                                                         *
00011  ***************************************************************************/
00012 
00013 
00014 //----------------------------------------------------------------------------------
00015 // Includes
00016 //----------------------------------------------------------------------------------
00017 #include "ResourceHolder.h"
00018 #include "Exception.h"
00019 #include "ResourceSystem.h"
00020 #include "Log.h"
00021 
00022 namespace nrEngine{
00023         
00024         //----------------------------------------------------------------------------------
00025         ResourceHolder::~ResourceHolder()
00026         {
00027                 mResource = NULL;
00028         }
00029                 
00030         //----------------------------------------------------------------------------------
00031         ResourceHolder::ResourceHolder(IResource* res, IResource* empty):
00032                         mResource(res), mEmptyResource(empty), countAccess(0)
00033         {
00034                 NR_ASSERT(res != NULL && empty != NULL);
00035                         
00036                 // empty the lock stack
00037                 for (int32 i=0; i < NR_RESOURCE_LOCK_STACK; i++) mLockStack[i] = false;
00038                 mLockStackTop = 0;
00039         
00040                 // empty the lock stack
00041                 for (int32 i=0; i < NR_RESOURCE_LOCK_STACK; i++) mEmptyLockStack[i] = false;
00042                 mEmptyLockStackTop = 0;
00043         }
00044                 
00045         //----------------------------------------------------------------------------------
00046         bool ResourceHolder::lockResource()
00047         {
00048                 // check if resource is already locked
00049                 if (isEmptyLocked() && mResource)
00050                         NR_Log(Log::LOG_ENGINE, Log::LL_WARNING, "You are trying to lock real resource while empty resource is locked. Empty stay locked for %s", mResource->getResourceName().c_str());
00051                 else if (isEmptyLocked() && !mResource)
00052                         NR_Log(Log::LOG_ENGINE, Log::LL_WARNING, "You are trying to lock real resource while empty resource is locked. Empty stay locked");
00053         
00054                 // check whenever we've got the maximum, so do not lock
00055                 if (mLockStackTop >= NR_RESOURCE_LOCK_STACK){
00056                         if (mResource){
00057                                 NR_Log(Log::LOG_ENGINE, Log::LL_DEBUG, 
00058                                         "Can not lock %s anymore. Lock state stack is full!", mResource->getResourceName().c_str());
00059                         }else{
00060                                 NR_Log(Log::LOG_ENGINE, Log::LL_DEBUG, 
00061                                         "Can not lock anymore. Lock state stack is full!");
00062                         }
00063                         
00064                         return false;
00065                 }else{          
00066                         // lock it
00067                         mLockStack[mLockStackTop++] = true;
00068                 }
00069                 
00070                 return true;
00071         }       
00072                 
00073         //----------------------------------------------------------------------------------
00074         void ResourceHolder::unlockResource()
00075         {
00076                 // check whenever we've got right top position
00077                 if (mLockStackTop > 0){
00078                         // unlock it
00079                         mLockStack[--mLockStackTop] = false;
00080                 }
00081         }
00082 
00083         //----------------------------------------------------------------------------------
00084         bool ResourceHolder::lockEmpty()
00085         {
00086                 // check if resource is already locked
00087                 if (isLocked() && mResource)
00088                         NR_Log(Log::LOG_ENGINE, Log::LL_WARNING, "You are trying to lock empty resource while real resource is locked. Empty will be used for %s", mResource->getResourceName().c_str());
00089                 else if (isLocked() && !mResource)
00090                         NR_Log(Log::LOG_ENGINE, Log::LL_WARNING, "You are trying to lock empty resource while real resource is locked. Empty will be used");
00091                  
00092                 // check whenever we've got the maximum, so do not lock
00093                 if (mEmptyLockStackTop >= NR_RESOURCE_LOCK_STACK)
00094                 {
00095                         if (mResource)
00096                         {
00097                                 NR_Log(Log::LOG_ENGINE, Log::LL_DEBUG, 
00098                                         "Can not lock %s anymore. Lock state stack is full!", mResource->getResourceName().c_str());
00099                         }else{
00100                                 NR_Log(Log::LOG_ENGINE, Log::LL_DEBUG, 
00101                                         "Can not lock anymore. Lock state stack is full!");
00102                         }
00103                         return false;
00104                 }else{
00105                         // lock it
00106                         mEmptyLockStack[mEmptyLockStackTop++] = true;
00107                 }
00108                 
00109                 return true;
00110         }       
00111                 
00112         //----------------------------------------------------------------------------------
00113         void ResourceHolder::unlockEmpty()
00114         {
00115                 // check whenever we've got right top position
00116                 if (mEmptyLockStackTop > 0){
00117                         // unlock it
00118                         mEmptyLockStack[--mEmptyLockStackTop] = false;
00119                 }
00120         }
00121 
00122         //----------------------------------------------------------------------------------
00123         void ResourceHolder::resetResource(IResource* res)
00124         {
00125                 mResource = (res);
00126         }
00127         
00128         
00129         //----------------------------------------------------------------------------------
00130         void ResourceHolder::setEmptyResource(IResource* res)
00131         {
00132                 mEmptyResource = (res);
00133         }
00134                 
00135         //----------------------------------------------------------------------------------
00136         IResource* ResourceHolder::getEmpty()
00137         {
00138                 return mEmptyResource;
00139         }
00140         
00141                 
00142         //----------------------------------------------------------------------------------
00143         IResource* ResourceHolder::getResource()
00144         {
00145                 NR_ASSERT(getEmpty() != NULL && "Empty resource must be defined");
00146                 
00147                 // check if empty is locked, then return empty resource
00148                 if (isEmptyLocked()) return getEmpty();
00149                 
00150                 // get resource only if it is exists and loaded or if it exists and locked
00151                 if (mResource!=NULL)
00152                 {
00153                         // check if we have locked to an empty resource
00154                         if (mResource->isResourceLoaded() || isLocked())
00155                         {
00156                                 touchResource();
00157                                 return mResource;
00158                         }
00159                 }
00160 
00161                 return getEmpty();
00162         }
00163 
00164         //----------------------------------------------------------------------------------
00165         void ResourceHolder::touchResource()
00166         {
00167                 // check if resource need to be reloaded
00168                 if (mResource->isResourceDirty()) mResource->reload();
00169                 
00170                 // count up the access count variable
00171                 countAccess ++;
00172         }
00173 
00174 };
00175 

Generated on Wed Sep 12 23:19:42 2007 for nrEngine by  doxygen 1.5.1