OgreResourceManager.h
Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright (c) 2000-2013 Torus Knot Software Ltd
00008 
00009 Permission is hereby granted, free of charge, to any person obtaining a copy
00010 of this software and associated documentation files (the "Software"), to deal
00011 in the Software without restriction, including without limitation the rights
00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013 copies of the Software, and to permit persons to whom the Software is
00014 furnished to do so, subject to the following conditions:
00015 
00016 The above copyright notice and this permission notice shall be included in
00017 all copies or substantial portions of the Software.
00018 
00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025 THE SOFTWARE.
00026 -----------------------------------------------------------------------------
00027 */
00028 #ifndef _ResourceManager_H__
00029 #define _ResourceManager_H__
00030 
00031 #include "OgrePrerequisites.h"
00032 
00033 #include "OgreResource.h"
00034 #include "OgreResourceGroupManager.h"
00035 #include "OgreIteratorWrappers.h"
00036 #include "OgreCommon.h"
00037 #include "OgreDataStream.h"
00038 #include "OgreStringVector.h"
00039 #include "OgreScriptLoader.h"
00040 #include "OgreHeaderPrefix.h"
00041 
00042 namespace Ogre {
00043 
00046     template <typename T>
00047     class Pool
00048     {
00049     protected:
00050         typedef typename list<T>::type ItemList;
00051         ItemList mItems;
00052         OGRE_AUTO_MUTEX;
00053     public:
00054         Pool() {}
00055         virtual ~Pool() {}
00056 
00060         virtual std::pair<bool, T> removeItem()
00061         {
00062             OGRE_LOCK_AUTO_MUTEX;
00063             std::pair<bool, T> ret;
00064             if (mItems.empty())
00065             {
00066                 ret.first = false;
00067             }
00068             else
00069             {
00070                 ret.first = true;
00071                 ret.second = mItems.front();
00072                 mItems.pop_front();
00073             }
00074             return ret;
00075         }
00076 
00079         virtual void addItem(const T& i)
00080         {
00081             OGRE_LOCK_AUTO_MUTEX;
00082             mItems.push_front(i);
00083         }
00085         virtual void clear()
00086         {
00087             OGRE_LOCK_AUTO_MUTEX;
00088             mItems.clear();
00089         }
00090     };
00091 
00122     class _OgreExport ResourceManager : public ScriptLoader, public ResourceAlloc
00123     {
00124     public:
00125         OGRE_AUTO_MUTEX; // public to allow external locking
00126         ResourceManager();
00127         virtual ~ResourceManager();
00128 
00148         virtual ResourcePtr createResource(const String& name, const String& group,
00149             bool isManual = false, ManualResourceLoader* loader = 0, 
00150             const NameValuePairList* createParams = 0);
00151 
00152         typedef std::pair<ResourcePtr, bool> ResourceCreateOrRetrieveResult;
00166         virtual ResourceCreateOrRetrieveResult createOrRetrieve(const String& name, 
00167             const String& group, bool isManual = false, 
00168             ManualResourceLoader* loader = 0, 
00169             const NameValuePairList* createParams = 0);
00170         
00178         virtual void setMemoryBudget(size_t bytes);
00179 
00182         virtual size_t getMemoryBudget(void) const;
00183 
00185         virtual size_t getMemoryUsage(void) const { return mMemoryUsage.get(); }
00186 
00193         virtual void unload(const String& name);
00194         
00201         virtual void unload(ResourceHandle handle);
00202 
00215         virtual void unloadAll(bool reloadableOnly = true);
00216 
00228         virtual void reloadAll(bool reloadableOnly = true);
00229 
00244         virtual void unloadUnreferencedResources(bool reloadableOnly = true);
00245 
00259         virtual void reloadUnreferencedResources(bool reloadableOnly = true);
00260 
00278         virtual void remove(ResourcePtr& r);
00279 
00297         virtual void remove(const String& name);
00298         
00316         virtual void remove(ResourceHandle handle);
00331         virtual void removeAll(void);
00332 
00347         virtual void removeUnreferencedResources(bool reloadableOnly = true);
00348 
00351         virtual ResourcePtr getResourceByName(const String& name, const String& groupName = ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME);
00354         virtual ResourcePtr getByHandle(ResourceHandle handle);
00355         
00357         virtual bool resourceExists(const String& name)
00358         {
00359             return !getResourceByName(name).isNull();
00360         }
00362         virtual bool resourceExists(ResourceHandle handle)
00363         {
00364             return !getByHandle(handle).isNull();
00365         }
00366 
00370         virtual void _notifyResourceTouched(Resource* res);
00371 
00375         virtual void _notifyResourceLoaded(Resource* res);
00376 
00380         virtual void _notifyResourceUnloaded(Resource* res);
00381 
00397         virtual ResourcePtr prepare(const String& name, 
00398             const String& group, bool isManual = false, 
00399             ManualResourceLoader* loader = 0, const NameValuePairList* loadParams = 0,
00400             bool backgroundThread = false);
00401 
00417         virtual ResourcePtr load(const String& name, 
00418             const String& group, bool isManual = false, 
00419             ManualResourceLoader* loader = 0, const NameValuePairList* loadParams = 0,
00420             bool backgroundThread = false);
00421 
00437         virtual const StringVector& getScriptPatterns(void) const { return mScriptPatterns; }
00438 
00452         virtual void parseScript(DataStreamPtr& stream, const String& groupName)
00453                 { (void)stream; (void)groupName; }
00454 
00461         virtual Real getLoadingOrder(void) const { return mLoadOrder; }
00462 
00464         const String& getResourceType(void) const { return mResourceType; }
00465 
00467         virtual void setVerbose(bool v) { mVerbose = v; }
00468 
00470         virtual bool getVerbose(void) { return mVerbose; }
00471 
00478         class _OgreExport ResourcePool : public Pool<ResourcePtr>, public ResourceAlloc
00479         {
00480         protected:
00481             String mName;
00482         public:
00483             ResourcePool(const String& name);
00484             ~ResourcePool();
00486             const String& getName() const;
00487             void clear();
00488         };
00489         
00491         ResourcePool* getResourcePool(const String& name);
00493         void destroyResourcePool(ResourcePool* pool);
00495         void destroyResourcePool(const String& name);
00497         void destroyAllResourcePools();
00498 
00499 
00500 
00501 
00502     protected:
00503 
00505         ResourceHandle getNextHandle(void);
00506 
00528         virtual Resource* createImpl(const String& name, ResourceHandle handle, 
00529             const String& group, bool isManual, ManualResourceLoader* loader, 
00530             const NameValuePairList* createParams) = 0;
00532         virtual void addImpl( ResourcePtr& res );
00534         virtual void removeImpl( ResourcePtr& res );
00537         virtual void checkUsage(void);
00538 
00539 
00540     public:
00541         typedef HashMap< String, ResourcePtr > ResourceMap;
00542         typedef HashMap< String, ResourceMap > ResourceWithGroupMap;
00543         typedef map<ResourceHandle, ResourcePtr>::type ResourceHandleMap;
00544     protected:
00545         ResourceHandleMap mResourcesByHandle;
00546         ResourceMap mResources;
00547         ResourceWithGroupMap mResourcesWithGroup;
00548         size_t mMemoryBudget; 
00549         AtomicScalar<ResourceHandle> mNextHandle;
00550         AtomicScalar<size_t> mMemoryUsage; 
00551 
00552         bool mVerbose;
00553 
00554         // IMPORTANT - all subclasses must populate the fields below
00555 
00557         StringVector mScriptPatterns; 
00559         Real mLoadOrder; 
00561         String mResourceType; 
00562 
00563     public:
00564         typedef MapIterator<ResourceHandleMap> ResourceMapIterator;
00569         ResourceMapIterator getResourceIterator(void) 
00570         {
00571             return ResourceMapIterator(mResourcesByHandle.begin(), mResourcesByHandle.end());
00572         }
00573 
00574     protected:
00575         typedef map<String, ResourcePool*>::type ResourcePoolMap;
00576         ResourcePoolMap mResourcePoolMap;
00577     };
00578 
00582 }
00583 
00584 #include "OgreHeaderSuffix.h"
00585 
00586 #endif

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Mon Jul 27 2020 13:40:45