LibN2L-4 Library Code ReferenceClassesCompounds Files Members Method Index Full Reference cResourceManager.hGo to the documentation of this file.00001 /************************************************************************ 00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library 00003 Copyright (C) 2003 Aaron Cameron 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Lesser General Public License for more details. 00014 00015 You should have received a copy of the GNU Lesser General Public 00016 License along with this library; if not, write to the Free Software 00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 00019 A copy of the GNU Lesser General Public License has been provided with 00020 this library in the file 'COPYING'. 00021 00022 Contact information for the author of this library has been provided 00023 with this library in the file 'AUTHOR'. 00024 ************************************************************************/ 00025 #ifndef _n2l4_cResourceManager_H 00026 #define _n2l4_cResourceManager_H 00027 00028 #include "n2l/n2l.h" 00029 #include "n2l/vfs.h" 00030 #include "n2l/timing.h" 00031 00032 #include <map> 00033 00034 /******************************************************************************/ 00035 namespace n2l 00036 { 00037 class cVfsDirectoryInterface; 00038 class cVfsNodeInterface; 00039 00040 class cGuiElement; 00041 00046 class cResourceManager { 00047 public: 00050 static void dataRoot( 00051 const cAutoPtr<const cVfsDirectoryInterface> &iDataRoot ); 00052 00055 static const cAutoPtr<const cVfsDirectoryInterface> &dataRoot(); 00056 00057 // ------------------------------------ 00058 // Methods to load resources 00059 00060 // This actually has to stay. 00061 static const cAutoPtr<const cGuiElement> &getGuiElement( 00062 const tString &iName ); 00063 00064 static const cAutoPtr<const cVfsNodeInterface> 00065 &getVfsNode( const tString &iName ); 00066 00067 template <class TRes> 00068 static const cAutoPtr<const TRes> &get( const tString &iName ) 00069 { 00070 checkDataRoot(); 00071 n2lDebug( "Loading general resource \"", false ); 00072 n2lDebug( iName, false ); 00073 const tUint STime = n2lGetTicks(); 00074 tBool countMyself = false; 00075 if (!mTiming) { 00076 mTiming = true; 00077 countMyself = true; 00078 } 00079 tResourceContainer::const_iterator rIT( mResources.find(iName) ); 00080 if (rIT==mResources.end()) { 00081 n2lDebug( "\" from filesystem" ); 00082 try { 00083 cResourceSheath<TRes> *temp = 00084 new cResourceSheath<TRes>( 00085 new TRes(*mData->mustFind(iName)) ); 00086 mResources[iName] = temp; 00087 } 00088 catch (const cException &iE) { 00089 mTiming = false; 00090 iE.rethrow(); 00091 } 00092 } else n2lDebug( "\" from cache" ); 00093 if (countMyself) { 00094 mAggLoadTime += n2lGetTicks()-STime; 00095 mTiming = false; 00096 countMyself = false; 00097 } 00098 return cAutoPtr<cResourceSheath<TRes> >( 00099 mResources[iName])->resource(); 00100 } 00101 00102 // ------------------------------------ 00103 // Methods for maintenance 00106 static void refreshTextures(); 00107 00108 // ------------------------------------ 00109 // Methods to do reporting 00112 static const tUint numReferences( const tString &iName ); 00113 00114 00115 static const tString getReferenceReport(); 00116 00117 // ------------------------------------ 00118 // Methods to do housework 00133 static void clearAllCache(); 00134 00141 static void clearUnusedCache(); 00142 00143 00144 static void clearCachedResource( const tString &iName ); 00145 00146 static const tUint aggLoadTime() { return mAggLoadTime; } 00147 static void clearAggLoadTime() { mAggLoadTime = 0; } 00148 00149 private: 00152 class cResource 00153 { 00154 public: 00155 virtual ~cResource() {} 00156 virtual const tUint count() const = 0; 00157 }; 00158 00162 template<class TType> 00163 class cResourceSheath : public cResource 00164 { 00165 public: 00166 cResourceSheath( const cAutoPtr<const TType> iResource ) : 00167 mResource(iResource) {} 00168 virtual ~cResourceSheath() {} 00169 virtual const cAutoPtr<const TType> & resource() const { 00170 return mResource; } 00171 00172 virtual const tUint count() const { return mResource.count(); } 00173 private: 00174 const cAutoPtr<const TType> mResource; 00175 }; 00176 00179 typedef std::map<tString,cAutoPtr<cResource> > tResourceContainer; 00180 00181 // ------------------------------------ 00182 // Members 00183 static cAutoPtr<const cVfsDirectoryInterface> mData; 00184 static tResourceContainer mResources; 00185 static tUint mAggLoadTime; 00186 static tBool mTiming; 00187 00188 00189 // ------------------------------------ 00190 // Private Methods 00191 static void checkDataRoot(); 00192 00193 }; // class 00194 00195 } // namespace 00196 00197 #endif |