OgreResourceManager.h
Go to the documentation of this file.
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4  (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2013 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 #ifndef _ResourceManager_H__
29 #define _ResourceManager_H__
30 
31 #include "OgrePrerequisites.h"
32 
33 #include "OgreResource.h"
35 #include "OgreIteratorWrappers.h"
36 #include "OgreCommon.h"
37 #include "OgreDataStream.h"
38 #include "OgreStringVector.h"
39 #include "OgreScriptLoader.h"
40 #include "OgreHeaderPrefix.h"
41 
42 namespace Ogre {
43 
46  template <typename T>
47  class Pool
48  {
49  protected:
50  typedef typename list<T>::type ItemList;
53  public:
54  Pool() {}
55  virtual ~Pool() {}
56 
60  virtual std::pair<bool, T> removeItem()
61  {
63  std::pair<bool, T> ret;
64  if (mItems.empty())
65  {
66  ret.first = false;
67  }
68  else
69  {
70  ret.first = true;
71  ret.second = mItems.front();
72  mItems.pop_front();
73  }
74  return ret;
75  }
76 
79  virtual void addItem(const T& i)
80  {
82  mItems.push_front(i);
83  }
85  virtual void clear()
86  {
88  mItems.clear();
89  }
90  };
91 
123  {
124  public:
125  OGRE_AUTO_MUTEX; // public to allow external locking
127  virtual ~ResourceManager();
128 
148  virtual ResourcePtr createResource(const String& name, const String& group,
149  bool isManual = false, ManualResourceLoader* loader = 0,
150  const NameValuePairList* createParams = 0);
151 
152  typedef std::pair<ResourcePtr, bool> ResourceCreateOrRetrieveResult;
167  const String& group, bool isManual = false,
168  ManualResourceLoader* loader = 0,
169  const NameValuePairList* createParams = 0);
170 
178  virtual void setMemoryBudget(size_t bytes);
179 
182  virtual size_t getMemoryBudget(void) const;
183 
185  virtual size_t getMemoryUsage(void) const { return mMemoryUsage.get(); }
186 
193  virtual void unload(const String& name);
194 
201  virtual void unload(ResourceHandle handle);
202 
215  virtual void unloadAll(bool reloadableOnly = true);
216 
228  virtual void reloadAll(bool reloadableOnly = true);
229 
244  virtual void unloadUnreferencedResources(bool reloadableOnly = true);
245 
259  virtual void reloadUnreferencedResources(bool reloadableOnly = true);
260 
278  virtual void remove(ResourcePtr& r);
279 
297  virtual void remove(const String& name);
298 
316  virtual void remove(ResourceHandle handle);
331  virtual void removeAll(void);
332 
347  virtual void removeUnreferencedResources(bool reloadableOnly = true);
348 
355 
357  virtual bool resourceExists(const String& name)
358  {
359  return !getResourceByName(name).isNull();
360  }
362  virtual bool resourceExists(ResourceHandle handle)
363  {
364  return !getByHandle(handle).isNull();
365  }
366 
370  virtual void _notifyResourceTouched(Resource* res);
371 
375  virtual void _notifyResourceLoaded(Resource* res);
376 
380  virtual void _notifyResourceUnloaded(Resource* res);
381 
397  virtual ResourcePtr prepare(const String& name,
398  const String& group, bool isManual = false,
399  ManualResourceLoader* loader = 0, const NameValuePairList* loadParams = 0,
400  bool backgroundThread = false);
401 
417  virtual ResourcePtr load(const String& name,
418  const String& group, bool isManual = false,
419  ManualResourceLoader* loader = 0, const NameValuePairList* loadParams = 0,
420  bool backgroundThread = false);
421 
437  virtual const StringVector& getScriptPatterns(void) const { return mScriptPatterns; }
438 
452  virtual void parseScript(DataStreamPtr& stream, const String& groupName)
453  { (void)stream; (void)groupName; }
454 
461  virtual Real getLoadingOrder(void) const { return mLoadOrder; }
462 
464  const String& getResourceType(void) const { return mResourceType; }
465 
467  virtual void setVerbose(bool v) { mVerbose = v; }
468 
470  virtual bool getVerbose(void) { return mVerbose; }
471 
478  class _OgreExport ResourcePool : public Pool<ResourcePtr>, public ResourceAlloc
479  {
480  protected:
482  public:
483  ResourcePool(const String& name);
486  const String& getName() const;
487  void clear();
488  };
489 
495  void destroyResourcePool(const String& name);
498 
499 
500 
501 
502  protected:
503 
506 
528  virtual Resource* createImpl(const String& name, ResourceHandle handle,
529  const String& group, bool isManual, ManualResourceLoader* loader,
530  const NameValuePairList* createParams) = 0;
532  virtual void addImpl( ResourcePtr& res );
534  virtual void removeImpl( ResourcePtr& res );
537  virtual void checkUsage(void);
538 
539 
540  public:
541  typedef HashMap< String, ResourcePtr > ResourceMap;
542  typedef HashMap< String, ResourceMap > ResourceWithGroupMap;
544  protected:
548  size_t mMemoryBudget;
551 
552  bool mVerbose;
553 
554  // IMPORTANT - all subclasses must populate the fields below
555 
562 
563  public:
570  {
571  return ResourceMapIterator(mResourcesByHandle.begin(), mResourcesByHandle.end());
572  }
573 
574  protected:
577  };
578 
582 }
583 
584 #include "OgreHeaderSuffix.h"
585 
586 #endif
OgreHeaderSuffix.h
Ogre::ResourceHandle
unsigned long long int ResourceHandle
Definition: OgreResource.h:41
Ogre::ResourceManager::remove
virtual void remove(const String &name)
Remove a single resource by name.
Ogre::ResourceManager::mLoadOrder
Real mLoadOrder
Loading order relative to other managers, higher is later.
Definition: OgreResourceManager.h:559
Ogre::ResourceManager::ResourcePool::clear
void clear()
Clear the pool.
Ogre::ResourceManager::_notifyResourceLoaded
virtual void _notifyResourceLoaded(Resource *res)
Notify this manager that a resource which it manages has been loaded.
Ogre::Pool::addItem
virtual void addItem(const T &i)
Add a new item to the pool.
Definition: OgreResourceManager.h:79
Ogre::ResourceManager::~ResourceManager
virtual ~ResourceManager()
Ogre::ResourceManager::createResource
virtual ResourcePtr createResource(const String &name, const String &group, bool isManual=false, ManualResourceLoader *loader=0, const NameValuePairList *createParams=0)
Creates a new blank resource, but does not immediately load it.
Ogre::ResourceManager::ResourceManager
ResourceManager()
Ogre::AllocatedObject
Superclass for all objects that wish to use custom memory allocators when their new / delete operator...
Definition: OgreMemoryAllocatedObject.h:59
Ogre
Definition: OgreAndroidLogListener.h:35
Ogre::ResourceManager::destroyResourcePool
void destroyResourcePool(const String &name)
Destroy a resource pool.
Ogre::ResourceManager::mVerbose
bool mVerbose
In bytes.
Definition: OgreResourceManager.h:552
Ogre::ResourceManager::destroyAllResourcePools
void destroyAllResourcePools()
destroy all pools
Ogre::map
Definition: OgrePrerequisites.h:534
Ogre::ResourceManager::mResourcesWithGroup
ResourceWithGroupMap mResourcesWithGroup
Definition: OgreResourceManager.h:547
Ogre::ResourceManager::ResourceHandleMap
map< ResourceHandle, ResourcePtr >::type ResourceHandleMap
Definition: OgreResourceManager.h:543
Ogre::ResourceManager::getByHandle
virtual ResourcePtr getByHandle(ResourceHandle handle)
Retrieves a pointer to a resource by handle, or null if the resource does not exist.
Ogre::ResourceManager::getResourcePool
ResourcePool * getResourcePool(const String &name)
Create a resource pool, or reuse one that already exists.
Ogre::ResourceManager::remove
virtual void remove(ResourcePtr &r)
Remove a single resource.
Ogre::ResourceManager::OGRE_AUTO_MUTEX
OGRE_AUTO_MUTEX
Definition: OgreResourceManager.h:125
Ogre::StringVector
vector< String >::type StringVector
Definition: OgreStringVector.h:45
Ogre::Pool::Pool
Pool()
Definition: OgreResourceManager.h:54
Ogre::ResourceManager::removeUnreferencedResources
virtual void removeUnreferencedResources(bool reloadableOnly=true)
Remove all resources which are not referenced by any other object.
Ogre::AtomicScalar< ResourceHandle >
Ogre::ResourceManager::removeImpl
virtual void removeImpl(ResourcePtr &res)
Remove a resource from this manager; remove it from the lists.
Ogre::ResourceManager::load
virtual ResourcePtr load(const String &name, const String &group, bool isManual=false, ManualResourceLoader *loader=0, const NameValuePairList *loadParams=0, bool backgroundThread=false)
Generic load method, used to create a Resource specific to this ResourceManager without using one of ...
Ogre::ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME
static String AUTODETECT_RESOURCE_GROUP_NAME
Special resource group name which causes resource group to be automatically determined based on searc...
Definition: OgreResourceGroupManager.h:270
Ogre::ResourceManager::createImpl
virtual Resource * createImpl(const String &name, ResourceHandle handle, const String &group, bool isManual, ManualResourceLoader *loader, const NameValuePairList *createParams)=0
Create a new resource instance compatible with this manager (no custom parameters are populated at th...
Ogre::ResourceManager::ResourceMap
HashMap< String, ResourcePtr > ResourceMap
Definition: OgreResourceManager.h:541
Ogre::String
_StringBase String
Definition: OgrePrerequisites.h:439
OgreResourceGroupManager.h
Ogre::NameValuePairList
map< String, String >::type NameValuePairList
Name / value parameter pair (first = name, second = value)
Definition: OgreCommon.h:550
Ogre::ResourceManager::mResources
ResourceMap mResources
Definition: OgreResourceManager.h:546
Ogre::ResourceManager::getResourceIterator
ResourceMapIterator getResourceIterator(void)
Returns an iterator over all resources in this manager.
Definition: OgreResourceManager.h:569
Ogre::ResourceManager::parseScript
virtual void parseScript(DataStreamPtr &stream, const String &groupName)
Parse the definition of a set of resources from a script file.
Definition: OgreResourceManager.h:452
OGRE_LOCK_AUTO_MUTEX
#define OGRE_LOCK_AUTO_MUTEX
Definition: OgreThreadDefinesBoost.h:33
Ogre::Pool::mItems
ItemList mItems
Definition: OgreResourceManager.h:51
Ogre::ResourceManager::ResourceMapIterator
MapIterator< ResourceHandleMap > ResourceMapIterator
Definition: OgreResourceManager.h:564
OgreHeaderPrefix.h
Ogre::list
Definition: OgrePrerequisites.h:506
Ogre::ResourceManager::reloadUnreferencedResources
virtual void reloadUnreferencedResources(bool reloadableOnly=true)
Caused all currently loaded but not referenced by any other object resources to be reloaded.
Ogre::ResourceManager::getResourceByName
virtual ResourcePtr getResourceByName(const String &name, const String &groupName=ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME)
Retrieves a pointer to a resource by name, or null if the resource does not exist.
OgrePrerequisites.h
Ogre::ResourceManager::ResourcePool::ResourcePool
ResourcePool(const String &name)
Ogre::ResourceManager::remove
virtual void remove(ResourceHandle handle)
Remove a single resource by handle.
Ogre::ResourceManager::removeAll
virtual void removeAll(void)
Removes all resources.
Ogre::ResourceManager::_notifyResourceUnloaded
virtual void _notifyResourceUnloaded(Resource *res)
Notify this manager that a resource which it manages has been unloaded.
Ogre::ResourceManager::getMemoryBudget
virtual size_t getMemoryBudget(void) const
Get the limit on the amount of memory this resource handler may use.
Ogre::ResourceManager::setMemoryBudget
virtual void setMemoryBudget(size_t bytes)
Set a limit on the amount of memory this resource handler may use.
Ogre::ResourceManager::ResourcePool::mName
String mName
Definition: OgreResourceManager.h:481
OgreResource.h
_OgreExport
#define _OgreExport
Definition: OgrePlatform.h:257
OgreCommon.h
Ogre::ResourceManager::mMemoryBudget
size_t mMemoryBudget
Definition: OgreResourceManager.h:548
Ogre::ResourceManager
Defines a generic resource handler.
Definition: OgreResourceManager.h:123
Ogre::ResourceManager::mResourcesByHandle
ResourceHandleMap mResourcesByHandle
Definition: OgreResourceManager.h:545
Ogre::ResourceManager::unloadAll
virtual void unloadAll(bool reloadableOnly=true)
Unloads all resources.
Ogre::ResourceManager::destroyResourcePool
void destroyResourcePool(ResourcePool *pool)
Destroy a resource pool.
Ogre::ResourceManager::ResourcePoolMap
map< String, ResourcePool * >::type ResourcePoolMap
Definition: OgreResourceManager.h:575
Ogre::ScriptLoader
Abstract class defining the interface used by classes which wish to perform script loading to define ...
Definition: OgreScriptLoader.h:60
OgreScriptLoader.h
Ogre::SharedPtr< Resource >
Ogre::ResourceManager::unload
virtual void unload(const String &name)
Unloads a single resource by name.
Ogre::ResourceManager::mNextHandle
AtomicScalar< ResourceHandle > mNextHandle
In bytes.
Definition: OgreResourceManager.h:549
Ogre::MapIterator
Concrete IteratorWrapper for nonconst access to the underlying key-value container.
Definition: OgreIteratorWrapper.h:319
Ogre::Pool::ItemList
list< T >::type ItemList
Definition: OgreResourceManager.h:50
OgreIteratorWrappers.h
Ogre::Resource
Abstract class representing a loadable resource (e.g.
Definition: OgreResource.h:80
Ogre::ResourceManager::getNextHandle
ResourceHandle getNextHandle(void)
Allocates the next handle.
Ogre::ResourceManager::ResourcePool
Definition of a pool of resources, which users can use to reuse similar resources many times without ...
Definition: OgreResourceManager.h:479
Ogre::ResourceManager::createOrRetrieve
virtual ResourceCreateOrRetrieveResult createOrRetrieve(const String &name, const String &group, bool isManual=false, ManualResourceLoader *loader=0, const NameValuePairList *createParams=0)
Create a new resource, or retrieve an existing one with the same name if it already exists.
Ogre::ResourceManager::mScriptPatterns
StringVector mScriptPatterns
Patterns to use to look for scripts if supported (e.g. *.overlay)
Definition: OgreResourceManager.h:557
Ogre::ResourceManager::unload
virtual void unload(ResourceHandle handle)
Unloads a single resource by handle.
Ogre::ResourceManager::getVerbose
virtual bool getVerbose(void)
Gets whether this manager and its resources habitually produce log output.
Definition: OgreResourceManager.h:470
Ogre::ResourceManager::addImpl
virtual void addImpl(ResourcePtr &res)
Add a newly created resource to the manager (note weak reference)
Ogre::ResourceManager::checkUsage
virtual void checkUsage(void)
Checks memory usage and pages out if required.
Ogre::ResourceManager::ResourceCreateOrRetrieveResult
std::pair< ResourcePtr, bool > ResourceCreateOrRetrieveResult
Definition: OgreResourceManager.h:152
Ogre::Pool::removeItem
virtual std::pair< bool, T > removeItem()
Get the next item from the pool.
Definition: OgreResourceManager.h:60
Ogre::ResourceManager::ResourcePool::getName
const String & getName() const
Get the name of the pool.
Ogre::ResourceManager::getMemoryUsage
virtual size_t getMemoryUsage(void) const
Gets the current memory usage, in bytes.
Definition: OgreResourceManager.h:185
Ogre::ResourceManager::mMemoryUsage
AtomicScalar< size_t > mMemoryUsage
Definition: OgreResourceManager.h:550
Ogre::Real
float Real
Software floating point type.
Definition: OgrePrerequisites.h:70
Ogre::ResourceManager::setVerbose
virtual void setVerbose(bool v)
Sets whether this manager and its resources habitually produce log output.
Definition: OgreResourceManager.h:467
Ogre::ResourceManager::mResourceType
String mResourceType
String identifying the resource type this manager handles.
Definition: OgreResourceManager.h:561
Ogre::Pool::clear
virtual void clear()
Clear the pool.
Definition: OgreResourceManager.h:85
Ogre::ResourceManager::getScriptPatterns
virtual const StringVector & getScriptPatterns(void) const
Gets the file patterns which should be used to find scripts for this ResourceManager.
Definition: OgreResourceManager.h:437
Ogre::ResourceManager::getLoadingOrder
virtual Real getLoadingOrder(void) const
Gets the relative loading order of resources of this type.
Definition: OgreResourceManager.h:461
Ogre::Pool
Template class describing a simple pool of items.
Definition: OgreResourceManager.h:48
OgreStringVector.h
Ogre::ManualResourceLoader
Interface describing a manual resource loader.
Definition: OgreResource.h:515
Ogre::ResourceManager::prepare
virtual ResourcePtr prepare(const String &name, const String &group, bool isManual=false, ManualResourceLoader *loader=0, const NameValuePairList *loadParams=0, bool backgroundThread=false)
Generic prepare method, used to create a Resource specific to this ResourceManager without using one ...
Ogre::ResourceManager::unloadUnreferencedResources
virtual void unloadUnreferencedResources(bool reloadableOnly=true)
Unload all resources which are not referenced by any other object.
Ogre::ResourceManager::_notifyResourceTouched
virtual void _notifyResourceTouched(Resource *res)
Notify this manager that a resource which it manages has been 'touched', i.e.
OgreDataStream.h
Ogre::ResourceManager::ResourcePool::~ResourcePool
~ResourcePool()
Ogre::ResourceManager::resourceExists
virtual bool resourceExists(ResourceHandle handle)
Returns whether a resource with the given handle exists in this manager.
Definition: OgreResourceManager.h:362
Ogre::ResourceManager::ResourceWithGroupMap
HashMap< String, ResourceMap > ResourceWithGroupMap
Definition: OgreResourceManager.h:542
Ogre::ResourceManager::getResourceType
const String & getResourceType(void) const
Gets a string identifying the type of resource this manager handles.
Definition: OgreResourceManager.h:464
Ogre::ResourceManager::reloadAll
virtual void reloadAll(bool reloadableOnly=true)
Caused all currently loaded resources to be reloaded.
Ogre::ResourceManager::mResourcePoolMap
ResourcePoolMap mResourcePoolMap
Definition: OgreResourceManager.h:576
Ogre::ResourceManager::resourceExists
virtual bool resourceExists(const String &name)
Returns whether the named resource exists in this manager.
Definition: OgreResourceManager.h:357
Ogre::Pool::OGRE_AUTO_MUTEX
OGRE_AUTO_MUTEX
Definition: OgreResourceManager.h:52
Ogre::Pool::~Pool
virtual ~Pool()
Definition: OgreResourceManager.h:55

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.