OGRE  13.6
Object-Oriented Graphics Rendering Engine
Ogre::ResourceManager Class Referenceabstract

Defines a generic resource handler. More...

#include <OgreResourceManager.h>

+ Inheritance diagram for Ogre::ResourceManager:

Classes

class  ResourcePool
 Definition of a pool of resources, which users can use to reuse similar resources many times without destroying and recreating them. More...
 

Public Types

typedef std::pair< ResourcePtr, bool > ResourceCreateOrRetrieveResult
 
typedef std::map< ResourceHandle, ResourcePtrResourceHandleMap
 
typedef std::unordered_map< String, ResourcePtrResourceMap
 
typedef MapIterator< ResourceHandleMapResourceMapIterator
 
typedef std::unordered_map< String, ResourceMapResourceWithGroupMap
 

Public Member Functions

 ResourceManager ()
 
virtual ~ResourceManager ()
 
virtual void _notifyResourceLoaded (Resource *res)
 Notify this manager that a resource which it manages has been loaded. More...
 
virtual void _notifyResourceTouched (Resource *res)
 Notify this manager that a resource which it manages has been 'touched', i.e. More...
 
virtual void _notifyResourceUnloaded (Resource *res)
 Notify this manager that a resource which it manages has been unloaded. More...
 
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. More...
 
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. More...
 
void destroyAllResourcePools ()
 destroy all pools More...
 
void destroyResourcePool (const String &name)
 Destroy a resource pool. More...
 
void destroyResourcePool (ResourcePool *pool)
 Destroy a resource pool. More...
 
virtual ResourcePtr getByHandle (ResourceHandle handle) const
 Retrieves a pointer to a resource by handle, or null if the resource does not exist. More...
 
Real getLoadingOrder (void) const override
 Gets the loading order for scripts of this type. More...
 
size_t getMemoryBudget (void) const
 Get the limit on the amount of memory this resource handler may use. More...
 
size_t getMemoryUsage (void) const
 Gets the current memory usage, in bytes. More...
 
virtual ResourcePtr getResourceByName (const String &name, const String &groupName=RGN_DEFAULT) const
 Retrieves a pointer to a resource by name, or null if the resource does not exist. More...
 
ResourceMapIterator getResourceIterator (void)
 Returns an iterator over all resources in this manager. More...
 
ResourcePoolgetResourcePool (const String &name)
 Create a resource pool, or reuse one that already exists. More...
 
const StringgetResourceType (void) const
 Gets a string identifying the type of resource this manager handles. More...
 
const StringVectorgetScriptPatterns (void) const override
 Gets the file patterns which should be used to find scripts for this class. More...
 
bool getVerbose (void)
 Gets whether this manager and its resources habitually produce log output. More...
 
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 the specialised 'load' methods (containing per-Resource-type parameters). More...
 
void parseScript (DataStreamPtr &stream, const String &groupName) override
 Parse a script file. More...
 
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 of the specialised 'prepare' methods (containing per-Resource-type parameters). More...
 
void reloadAll (bool reloadableOnly=true)
 Caused all currently loaded resources to be reloaded. More...
 
virtual void reloadAll (Resource::LoadingFlags flags)
 Caused all currently loaded resources to be reloaded. More...
 
void reloadUnreferencedResources (bool reloadableOnly=true)
 Caused all currently loaded but not referenced by any other object resources to be reloaded. More...
 
void remove (const ResourcePtr &r)
 Remove a single resource. More...
 
void remove (const String &name, const String &group=RGN_DEFAULT)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
void remove (ResourceHandle handle)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
virtual void removeAll (void)
 Removes all resources. More...
 
virtual void removeUnreferencedResources (bool reloadableOnly=true)
 Remove all resources which are not referenced by any other object. More...
 
bool resourceExists (const String &name, const String &group=RGN_DEFAULT) const
 Returns whether the named resource exists in this manager. More...
 
bool resourceExists (ResourceHandle handle) const
 Returns whether a resource with the given handle exists in this manager. More...
 
void setMemoryBudget (size_t bytes)
 Set a limit on the amount of memory this resource handler may use. More...
 
void setVerbose (bool v)
 Sets whether this manager and its resources habitually produce log output. More...
 
void unload (const String &name, const String &group=RGN_DEFAULT)
 Unloads a single resource by name. More...
 
void unload (ResourceHandle handle)
 Unloads a single resource by handle. More...
 
void unloadAll (bool reloadableOnly=true)
 Unloads all resources. More...
 
virtual void unloadAll (Resource::LoadingFlags flags)
 Unloads all resources. More...
 
void unloadUnreferencedResources (bool reloadableOnly=true)
 Unload all resources which are not referenced by any other object. More...
 
- Public Member Functions inherited from Ogre::ScriptLoader
virtual ~ScriptLoader ()
 

Detailed Description

Defines a generic resource handler.

See also
Resource Management
Note
If OGRE_THREAD_SUPPORT is 1, this class is thread-safe.

Member Typedef Documentation

◆ ResourceCreateOrRetrieveResult

◆ ResourceMap

typedef std::unordered_map< String, ResourcePtr > Ogre::ResourceManager::ResourceMap

◆ ResourceWithGroupMap

◆ ResourceHandleMap

◆ ResourceMapIterator

Constructor & Destructor Documentation

◆ ResourceManager()

Ogre::ResourceManager::ResourceManager ( )

◆ ~ResourceManager()

virtual Ogre::ResourceManager::~ResourceManager ( )
virtual

Member Function Documentation

◆ createResource()

ResourcePtr Ogre::ResourceManager::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.

Parameters
nameThe unique name of the Resource
groupThe name of the resource group to attach this new resource to
isManualIs this resource manually loaded? If so, you should really populate the loader parameter in order that the load process can call the loader back when loading is required.
loaderPointer to a ManualLoader implementation which will be called when the Resource wishes to load (should be supplied if you set isManual to true). You can in fact leave this parameter null if you wish, but the Resource will never be able to reload if anything ever causes it to unload. Therefore provision of a proper ManualLoader instance is strongly recommended.
createParamsIf any parameters are required to create an instance, they should be supplied here as name / value pairs

◆ createOrRetrieve()

ResourceCreateOrRetrieveResult Ogre::ResourceManager::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.

This method performs the same task as calling getByName() followed by create() if that returns null. The advantage is that it does it in one call so there are no race conditions if using multiple threads that could cause getByName() to return null, but create() to fail because another thread created a resource in between.

Parameters
nameThe unique name of the Resource
groupThe name of the resource group to attach this new resource to
isManualIs this resource manually loaded? If so, you should really populate the loader parameter in order that the load process can call the loader back when loading is required.
loaderPointer to a ManualLoader implementation which will be called when the Resource wishes to load (should be supplied if you set isManual to true). You can in fact leave this parameter null if you wish, but the Resource will never be able to reload if anything ever causes it to unload. Therefore provision of a proper ManualLoader instance is strongly recommended.
createParamsIf any parameters are required to create an instance, they should be supplied here as name / value pairs
Returns
A pair, the first element being the pointer, and the second being an indicator specifying whether the resource was newly created.

◆ setMemoryBudget()

void Ogre::ResourceManager::setMemoryBudget ( size_t  bytes)

Set a limit on the amount of memory this resource handler may use.

If, when asked to load a new resource, the manager believes it will exceed this memory
budget, it will temporarily unload a resource to make room for the new one. This unloading
is not permanent and the Resource is not destroyed; it simply needs to be reloaded when
next used.

◆ getMemoryBudget()

size_t Ogre::ResourceManager::getMemoryBudget ( void  ) const

Get the limit on the amount of memory this resource handler may use.

◆ getMemoryUsage()

size_t Ogre::ResourceManager::getMemoryUsage ( void  ) const
inline

Gets the current memory usage, in bytes.

◆ unload() [1/2]

void Ogre::ResourceManager::unload ( const String name,
const String group = RGN_DEFAULT 
)

Unloads a single resource by name.

Unloaded resources are not removed, they simply free up their memory as much as they can and wait to be reloaded.

See also
ResourceGroupManager for unloading of resource groups.

◆ unload() [2/2]

void Ogre::ResourceManager::unload ( ResourceHandle  handle)

Unloads a single resource by handle.

Unloaded resources are not removed, they simply free up their memory as much as they can and wait to be reloaded.

See also
ResourceGroupManager for unloading of resource groups.

◆ unloadAll() [1/2]

void Ogre::ResourceManager::unloadAll ( bool  reloadableOnly = true)
inline

Unloads all resources.

Unloaded resources are not removed, they simply free up their memory as much as they can and wait to be reloaded.

See also
ResourceGroupManager for unloading of resource groups.
Parameters
reloadableOnlyIf true (the default), only unload the resource that is reloadable. Because some resources isn't reloadable, they will be unloaded but can't load them later. Thus, you might not want to them unloaded. Or, you might unload all of them, and then populate them manually later.
See also
Resource::isReloadable for resource is reloadable.

References Ogre::Resource::LF_DEFAULT, and Ogre::Resource::LF_INCLUDE_NON_RELOADABLE.

◆ reloadAll() [1/2]

void Ogre::ResourceManager::reloadAll ( bool  reloadableOnly = true)
inline

Caused all currently loaded resources to be reloaded.

All resources currently being held in this manager which are also marked as currently loaded will be unloaded, then loaded again.

Parameters
reloadableOnlyIf true (the default), only reload the resource that is reloadable. Because some resources isn't reloadable, they will be unloaded but can't loaded again. Thus, you might not want to them unloaded. Or, you might unload all of them, and then populate them manually later.
See also
Resource::isReloadable for resource is reloadable.

References Ogre::Resource::LF_DEFAULT, and Ogre::Resource::LF_INCLUDE_NON_RELOADABLE.

◆ unloadUnreferencedResources()

void Ogre::ResourceManager::unloadUnreferencedResources ( bool  reloadableOnly = true)
inline

Unload all resources which are not referenced by any other object.

This method behaves like unloadAll, except that it only unloads resources which are not in use, ie not referenced by other objects. This allows you to free up some memory selectively whilst still keeping the group around (and the resources present, just not using much memory).

Some referenced resource may exists 'weak' pointer to their sub-components (e.g. Entity held pointer to SubMesh), in this case, unload or reload that resource will cause dangerous pointer access. Use this function instead of unloadAll allows you avoid fail in those situations.
Parameters
reloadableOnlyIf true (the default), only unloads resources which can be subsequently automatically reloaded.

References Ogre::Resource::LF_ONLY_UNREFERENCED, and Ogre::Resource::LF_ONLY_UNREFERENCED_INCLUDE_NON_RELOADABLE.

◆ reloadUnreferencedResources()

void Ogre::ResourceManager::reloadUnreferencedResources ( bool  reloadableOnly = true)
inline

Caused all currently loaded but not referenced by any other object resources to be reloaded.

This method behaves like reloadAll, except that it only reloads resources which are not in use, i.e. not referenced by other objects.

Some referenced resource may exists 'weak' pointer to their sub-components (e.g. Entity held pointer to SubMesh), in this case, unload or reload that resource will cause dangerous pointer access. Use this function instead of reloadAll allows you avoid fail in those situations.
Parameters
reloadableOnlyIf true (the default), only reloads resources which can be subsequently automatically reloaded.

References Ogre::Resource::LF_ONLY_UNREFERENCED, and Ogre::Resource::LF_ONLY_UNREFERENCED_INCLUDE_NON_RELOADABLE.

◆ unloadAll() [2/2]

virtual void Ogre::ResourceManager::unloadAll ( Resource::LoadingFlags  flags)
virtual

Unloads all resources.

Unloaded resources are not removed, they simply free up their memory as much as they can and wait to be reloaded.

See also
ResourceGroupManager for unloading of resource groups.
Parameters
flagsAllow to restrict processing to only reloadable and/or unreferenced resources.
See also
Resource::LoadingFlags for additional information.

◆ reloadAll() [2/2]

virtual void Ogre::ResourceManager::reloadAll ( Resource::LoadingFlags  flags)
virtual

Caused all currently loaded resources to be reloaded.

All resources currently being held in this manager which are also marked as currently loaded will be unloaded, then loaded again.

Parameters
flagsAllow to restrict processing to only reloadable and/or unreferenced resources. Additionally, reloading could be done with preserving some selected resource states that could be used elsewhere.
See also
Resource::LoadingFlags for additional information.

◆ remove() [1/3]

void Ogre::ResourceManager::remove ( const ResourcePtr r)

Remove a single resource.

Removes a single resource, meaning it will be removed from the list of valid resources in this manager, also causing it to be unloaded.

Note
The word 'Destroy' is not used here, since if any other pointers are referring to this resource, it will persist until they have finished with it; however to all intents and purposes it no longer exists and will likely get destroyed imminently.
If you do have shared pointers to resources hanging around after the ResourceManager is destroyed, you may get problems on destruction of these resources if they were relying on the manager (especially if it is a plugin). If you find you get problems on shutdown in the destruction of resources, try making sure you release all your shared pointers before you shutdown OGRE.

◆ remove() [2/3]

void Ogre::ResourceManager::remove ( const String name,
const String group = RGN_DEFAULT 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ remove() [3/3]

void Ogre::ResourceManager::remove ( ResourceHandle  handle)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ removeAll()

virtual void Ogre::ResourceManager::removeAll ( void  )
virtual

Removes all resources.

Note
The word 'Destroy' is not used here, since if any other pointers are referring to these resources, they will persist until they have been finished with; however to all intents and purposes the resources no longer exist and will get destroyed imminently.
If you do have shared pointers to resources hanging around after the ResourceManager is destroyed, you may get problems on destruction of these resources if they were relying on the manager (especially if it is a plugin). If you find you get problems on shutdown in the destruction of resources, try making sure you release all your shared pointers before you shutdown OGRE.

Reimplemented in Ogre::CompositorManager.

◆ removeUnreferencedResources()

virtual void Ogre::ResourceManager::removeUnreferencedResources ( bool  reloadableOnly = true)
virtual

Remove all resources which are not referenced by any other object.

This method behaves like removeAll, except that it only removes resources which are not in use, ie not referenced by other objects. This allows you to free up some memory selectively whilst still keeping the group around (and the resources present, just not using much memory).

Some referenced resource may exists 'weak' pointer to their sub-components (e.g. Entity held pointer to SubMesh), in this case, remove or reload that resource will cause dangerous pointer access. Use this function instead of removeAll allows you avoid fail in those situations.
Parameters
reloadableOnlyIf true (the default), only removes resources which can be subsequently automatically reloaded.

◆ getResourceByName()

virtual ResourcePtr Ogre::ResourceManager::getResourceByName ( const String name,
const String groupName = RGN_DEFAULT 
) const
virtual

Retrieves a pointer to a resource by name, or null if the resource does not exist.

◆ getByHandle()

virtual ResourcePtr Ogre::ResourceManager::getByHandle ( ResourceHandle  handle) const
virtual

Retrieves a pointer to a resource by handle, or null if the resource does not exist.

◆ resourceExists() [1/2]

bool Ogre::ResourceManager::resourceExists ( const String name,
const String group = RGN_DEFAULT 
) const
inline

Returns whether the named resource exists in this manager.

◆ resourceExists() [2/2]

bool Ogre::ResourceManager::resourceExists ( ResourceHandle  handle) const
inline

Returns whether a resource with the given handle exists in this manager.

◆ _notifyResourceTouched()

virtual void Ogre::ResourceManager::_notifyResourceTouched ( Resource res)
virtual

Notify this manager that a resource which it manages has been 'touched', i.e.

used.

◆ _notifyResourceLoaded()

virtual void Ogre::ResourceManager::_notifyResourceLoaded ( Resource res)
virtual

Notify this manager that a resource which it manages has been loaded.

◆ _notifyResourceUnloaded()

virtual void Ogre::ResourceManager::_notifyResourceUnloaded ( Resource res)
virtual

Notify this manager that a resource which it manages has been unloaded.

◆ prepare()

ResourcePtr Ogre::ResourceManager::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 of the specialised 'prepare' methods (containing per-Resource-type parameters).

Parameters
nameThe name of the Resource
groupThe resource group to which this resource will belong
isManualIs the resource to be manually loaded? If so, you should provide a value for the loader parameter
loaderThe manual loader which is to perform the required actions when this resource is loaded; only applicable when you specify true for the previous parameter
loadParamsOptional pointer to a list of name/value pairs containing loading parameters for this type of resource.
backgroundThreadOptional boolean which lets the load routine know if it is being run on the background resource loading thread

◆ load()

ResourcePtr Ogre::ResourceManager::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 the specialised 'load' methods (containing per-Resource-type parameters).

Parameters
nameThe name of the Resource
groupThe resource group to which this resource will belong
isManualIs the resource to be manually loaded? If so, you should provide a value for the loader parameter
loaderThe manual loader which is to perform the required actions when this resource is loaded; only applicable when you specify true for the previous parameter
loadParamsOptional pointer to a list of name/value pairs containing loading parameters for this type of resource.
backgroundThreadOptional boolean which lets the load routine know if it is being run on the background resource loading thread

◆ getScriptPatterns()

const StringVector& Ogre::ResourceManager::getScriptPatterns ( void  ) const
inlineoverridevirtual

Gets the file patterns which should be used to find scripts for this class.

This method is called when a resource group is loaded if you use ResourceGroupManager::_registerScriptLoader.

Returns
A list of file patterns, in the order they should be searched in.

Implements Ogre::ScriptLoader.

◆ parseScript()

void Ogre::ResourceManager::parseScript ( DataStreamPtr stream,
const String groupName 
)
overridevirtual

Parse a script file.

Parameters
streamWeak reference to a data stream which is the source of the script
groupNameThe name of a resource group which should be used if any resources are created during the parse of this script.

Implements Ogre::ScriptLoader.

◆ getLoadingOrder()

Real Ogre::ResourceManager::getLoadingOrder ( void  ) const
inlineoverridevirtual

Gets the loading order for scripts of this type.

There are dependencies between some kinds of scripts, and this value enumerates that. Higher values load later during bulk loading tasks.

Returns
The loading order

Implements Ogre::ScriptLoader.

◆ getResourceType()

const String& Ogre::ResourceManager::getResourceType ( void  ) const
inline

Gets a string identifying the type of resource this manager handles.

◆ setVerbose()

void Ogre::ResourceManager::setVerbose ( bool  v)
inline

Sets whether this manager and its resources habitually produce log output.

◆ getVerbose()

bool Ogre::ResourceManager::getVerbose ( void  )
inline

Gets whether this manager and its resources habitually produce log output.

◆ getResourcePool()

ResourcePool* Ogre::ResourceManager::getResourcePool ( const String name)

Create a resource pool, or reuse one that already exists.

◆ destroyResourcePool() [1/2]

void Ogre::ResourceManager::destroyResourcePool ( ResourcePool pool)

Destroy a resource pool.

◆ destroyResourcePool() [2/2]

void Ogre::ResourceManager::destroyResourcePool ( const String name)

Destroy a resource pool.

◆ destroyAllResourcePools()

void Ogre::ResourceManager::destroyAllResourcePools ( )

destroy all pools

◆ getResourceIterator()

ResourceMapIterator Ogre::ResourceManager::getResourceIterator ( void  )
inline

Returns an iterator over all resources in this manager.

Note
Use of this iterator is NOT thread safe!

The documentation for this class was generated from the following file: