OGRE  2.0
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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-2014 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 "OgreCommon.h"
36 #include "OgreStringVector.h"
37 #include "OgreScriptLoader.h"
38 #include "OgreHeaderPrefix.h"
39 
40 namespace Ogre {
41 
44  template <typename T>
45  class Pool
46  {
47  protected:
48  typedef typename list<T>::type ItemList;
51  public:
52  Pool() {}
53  virtual ~Pool() {}
54 
58  virtual std::pair<bool, T> removeItem()
59  {
61  std::pair<bool, T> ret;
62  if (mItems.empty())
63  {
64  ret.first = false;
65  }
66  else
67  {
68  ret.first = true;
69  ret.second = mItems.front();
70  mItems.pop_front();
71  }
72  return ret;
73  }
74 
77  virtual void addItem(const T& i)
78  {
80  mItems.push_front(i);
81  }
83  virtual void clear()
84  {
86  mItems.clear();
87  }
88  };
89 
121  {
122  public:
123  OGRE_AUTO_MUTEX; // public to allow external locking
124  ResourceManager();
125  virtual ~ResourceManager();
126 
146  virtual ResourcePtr createResource(const String& name, const String& group,
147  bool isManual = false, ManualResourceLoader* loader = 0,
148  const NameValuePairList* createParams = 0);
149 
150  typedef std::pair<ResourcePtr, bool> ResourceCreateOrRetrieveResult;
164  virtual ResourceCreateOrRetrieveResult createOrRetrieve(const String& name,
165  const String& group, bool isManual = false,
166  ManualResourceLoader* loader = 0,
167  const NameValuePairList* createParams = 0);
168 
176  virtual void setMemoryBudget(size_t bytes);
177 
180  virtual size_t getMemoryBudget(void) const;
181 
183  virtual size_t getMemoryUsage(void) const { return mMemoryUsage.get(); }
184 
191  virtual void unload(const String& name);
192 
199  virtual void unload(ResourceHandle handle);
200 
213  virtual void unloadAll(bool reloadableOnly = true);
214 
226  virtual void reloadAll(bool reloadableOnly = true);
227 
242  virtual void unloadUnreferencedResources(bool reloadableOnly = true);
243 
257  virtual void reloadUnreferencedResources(bool reloadableOnly = true);
258 
276  virtual void remove(ResourcePtr& r);
277 
295  virtual void remove(const String& name);
296 
314  virtual void remove(ResourceHandle handle);
329  virtual void removeAll(void);
330 
345  virtual void removeUnreferencedResources(bool reloadableOnly = true);
346 
349  virtual ResourcePtr getResourceByName(const String& name, const String& groupName = ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME);
352  virtual ResourcePtr getByHandle(ResourceHandle handle);
353 
355  virtual bool resourceExists(const String& name)
356  {
357  return !getResourceByName(name).isNull();
358  }
360  virtual bool resourceExists(ResourceHandle handle)
361  {
362  return !getByHandle(handle).isNull();
363  }
364 
368  virtual void _notifyResourceTouched(Resource* res);
369 
373  virtual void _notifyResourceLoaded(Resource* res);
374 
378  virtual void _notifyResourceUnloaded(Resource* res);
379 
395  virtual ResourcePtr prepare(const String& name,
396  const String& group, bool isManual = false,
397  ManualResourceLoader* loader = 0, const NameValuePairList* loadParams = 0,
398  bool backgroundThread = false);
399 
415  virtual ResourcePtr load(const String& name,
416  const String& group, bool isManual = false,
417  ManualResourceLoader* loader = 0, const NameValuePairList* loadParams = 0,
418  bool backgroundThread = false);
419 
435  virtual const StringVector& getScriptPatterns(void) const { return mScriptPatterns; }
436 
450  virtual void parseScript(DataStreamPtr& stream, const String& groupName)
451  { (void)stream; (void)groupName; }
452 
459  virtual Real getLoadingOrder(void) const { return mLoadOrder; }
460 
462  const String& getResourceType(void) const { return mResourceType; }
463 
465  virtual void setVerbose(bool v) { mVerbose = v; }
466 
468  virtual bool getVerbose(void) { return mVerbose; }
469 
476  class _OgreExport ResourcePool : public Pool<ResourcePtr>, public ResourceAlloc
477  {
478  protected:
480  public:
481  ResourcePool(const String& name);
482  ~ResourcePool();
484  const String& getName() const;
485  void clear();
486  };
487 
489  ResourcePool* getResourcePool(const String& name);
491  void destroyResourcePool(ResourcePool* pool);
493  void destroyResourcePool(const String& name);
495  void destroyAllResourcePools();
496 
497 
498 
499 
500  protected:
501 
503  ResourceHandle getNextHandle(void);
504 
526  virtual Resource* createImpl(const String& name, ResourceHandle handle,
527  const String& group, bool isManual, ManualResourceLoader* loader,
528  const NameValuePairList* createParams) = 0;
530  virtual void addImpl( ResourcePtr& res );
532  virtual void removeImpl( ResourcePtr& res );
535  virtual void checkUsage(void);
536 
537 
538  public:
539  typedef HashMap< String, ResourcePtr > ResourceMap;
540  typedef HashMap< String, ResourceMap > ResourceWithGroupMap;
542  protected:
546  size_t mMemoryBudget;
549 
550  bool mVerbose;
551 
552  // IMPORTANT - all subclasses must populate the fields below
553 
560 
561  public:
568  {
569  return ResourceMapIterator(mResourcesByHandle.begin(), mResourcesByHandle.end());
570  }
571 
572  protected:
575  };
576 
580 }
581 
582 #include "OgreHeaderSuffix.h"
583 
584 #endif
AtomicScalar< ResourceHandle > mNextHandle
In bytes.
virtual bool getVerbose(void)
Gets whether this manager and its resources habitually produce log output.
String mResourceType
String identifying the resource type this manager handles.
float Real
Software floating point type.
#define _OgreExport
Definition: OgrePlatform.h:255
HashMap< String, ResourcePtr > ResourceMap
map< String, String >::type NameValuePairList
Name / value parameter pair (first = name, second = value)
Definition: OgreCommon.h:580
virtual const StringVector & getScriptPatterns(void) const
Gets the file patterns which should be used to find scripts for this ResourceManager.
StringVector mScriptPatterns
Patterns to use to look for scripts if supported (e.g. *.overlay)
const String & getResourceType(void) const
Gets a string identifying the type of resource this manager handles.
static String AUTODETECT_RESOURCE_GROUP_NAME
Special resource group name which causes resource group to be automatically determined based on searc...
ResourceMapIterator getResourceIterator(void)
Returns an iterator over all resources in this manager.
HashMap< String, ResourceMap > ResourceWithGroupMap
Interface describing a manual resource loader.
Definition: OgreResource.h:498
virtual Real getLoadingOrder(void) const
Gets the relative loading order of resources of this type.
virtual void parseScript(DataStreamPtr &stream, const String &groupName)
Parse the definition of a set of resources from a script file.
virtual void clear()
Clear the pool.
ResourceHandleMap mResourcesByHandle
Concrete IteratorWrapper for nonconst access to the underlying key-value container.
map< ResourceHandle, ResourcePtr >::type ResourceHandleMap
#define OGRE_LOCK_AUTO_MUTEX
Abstract class defining the interface used by classes which wish to perform script loading to define ...
Template class describing a simple pool of items.
virtual std::pair< bool, T > removeItem()
Get the next item from the pool.
virtual void setVerbose(bool v)
Sets whether this manager and its resources habitually produce log output.
Superclass for all objects that wish to use custom memory allocators when their new / delete operator...
std::pair< ResourcePtr, bool > ResourceCreateOrRetrieveResult
ResourcePoolMap mResourcePoolMap
vector< String >::type StringVector
Abstract class representing a loadable resource (e.g.
Definition: OgreResource.h:77
unsigned long long int ResourceHandle
Definition: OgreResource.h:39
Definition of a pool of resources, which users can use to reuse similar resources many times without ...
virtual bool resourceExists(ResourceHandle handle)
Returns whether a resource with the given handle exists in this manager.
ResourceWithGroupMap mResourcesWithGroup
virtual size_t getMemoryUsage(void) const
Gets the current memory usage, in bytes.
list< T >::type ItemList
Real mLoadOrder
Loading order relative to other managers, higher is later.
Defines a generic resource handler.
_StringBase String
Definition: OgreCommon.h:53
MapIterator< ResourceHandleMap > ResourceMapIterator
map< String, ResourcePool * >::type ResourcePoolMap
virtual void addItem(const T &i)
Add a new item to the pool.
AtomicScalar< size_t > mMemoryUsage
virtual bool resourceExists(const String &name)
Returns whether the named resource exists in this manager.