OgreResource.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 _Resource_H__
29 #define _Resource_H__
30 
31 #include "OgrePrerequisites.h"
32 #include "OgreString.h"
33 #include "OgreSharedPtr.h"
34 #include "OgreStringInterface.h"
35 #include "OgreAtomicScalar.h"
37 #include "OgreHeaderPrefix.h"
38 
39 namespace Ogre {
40 
41  typedef unsigned long long int ResourceHandle;
42 
43 
44  // Forward declaration
46 
79  class _OgreExport Resource : public StringInterface, public ResourceAlloc
80  {
81  public:
82  OGRE_AUTO_MUTEX; // public to allow external locking
83  class Listener
84  {
85  public:
86  Listener() {}
87  virtual ~Listener() {}
88 
94 
100 
109  virtual void loadingComplete(Resource*) {}
110 
111 
120  virtual void preparingComplete(Resource*) {}
121 
123  virtual void unloadingComplete(Resource*) {}
124  };
125 
128  {
140  LOADSTATE_PREPARING
141  };
142  protected:
154  volatile bool mIsBackgroundLoaded;
156  size_t mSize;
158  bool mIsManual;
164  size_t mStateCount;
165 
168  OGRE_MUTEX(mListenerListMutex);
169 
173  : mCreator(0), mHandle(0), mLoadingState(LOADSTATE_UNLOADED),
174  mIsBackgroundLoaded(false), mSize(0), mIsManual(0), mLoader(0)
175  {
176  }
177 
184  virtual void preLoadImpl(void) {}
191  virtual void postLoadImpl(void) {}
192 
196  virtual void preUnloadImpl(void) {}
201  virtual void postUnloadImpl(void) {}
202 
205  virtual void prepareImpl(void) {}
210  virtual void unprepareImpl(void) {}
214  virtual void loadImpl(void) = 0;
218  virtual void unloadImpl(void) = 0;
219 
220  public:
235  Resource(ResourceManager* creator, const String& name, ResourceHandle handle,
236  const String& group, bool isManual = false, ManualResourceLoader* loader = 0);
237 
243  virtual ~Resource();
244 
259  virtual void prepare(bool backgroundThread = false);
260 
271  virtual void load(bool backgroundThread = false);
272 
278  virtual void reload(void);
279 
282  virtual bool isReloadable(void) const
283  {
284  return !mIsManual || mLoader;
285  }
286 
289  virtual bool isManuallyLoaded(void) const
290  {
291  return mIsManual;
292  }
293 
297  virtual void unload(void);
298 
301  virtual size_t getSize(void) const
302  {
303  return mSize;
304  }
305 
308  virtual void touch(void);
309 
312  virtual const String& getName(void) const
313  {
314  return mName;
315  }
316 
317  virtual ResourceHandle getHandle(void) const
318  {
319  return mHandle;
320  }
321 
324  virtual bool isPrepared(void) const
325  {
326  // No lock required to read this state since no modify
327  return (mLoadingState.get() == LOADSTATE_PREPARED);
328  }
329 
332  virtual bool isLoaded(void) const
333  {
334  // No lock required to read this state since no modify
335  return (mLoadingState.get() == LOADSTATE_LOADED);
336  }
337 
341  virtual bool isLoading() const
342  {
343  return (mLoadingState.get() == LOADSTATE_LOADING);
344  }
345 
349  {
350  return mLoadingState.get();
351  }
352 
353 
354 
365  virtual bool isBackgroundLoaded(void) const { return mIsBackgroundLoaded; }
366 
375  virtual void setBackgroundLoaded(bool bl) { mIsBackgroundLoaded = bl; }
376 
386  virtual void escalateLoading();
387 
391  virtual void addListener(Listener* lis);
392 
396  virtual void removeListener(Listener* lis);
397 
399  virtual const String& getGroup(void) const { return mGroup; }
400 
408  virtual void changeGroupOwnership(const String& newGroup);
409 
411  virtual ResourceManager* getCreator(void) { return mCreator; }
418  virtual const String& getOrigin(void) const { return mOrigin; }
420  virtual void _notifyOrigin(const String& origin) { mOrigin = origin; }
421 
429  virtual size_t getStateCount() const { return mStateCount; }
430 
436  virtual void _dirtyState();
437 
438 
447  virtual void _fireLoadingComplete(bool wasBackgroundLoaded);
448 
457  virtual void _firePreparingComplete(bool wasBackgroundLoaded);
458 
466  virtual void _fireUnloadingComplete(void);
467 
469  virtual size_t calculateSize(void) const;
470 
471  };
472 
491  typedef SharedPtr<Resource> ResourcePtr;
492 
514  class _OgreExport ManualResourceLoader
515  {
516  public:
519 
526  virtual void prepareResource(Resource* resource)
527  { (void)resource; }
528 
532  virtual void loadResource(Resource* resource) = 0;
533  };
536 }
537 
538 #include "OgreHeaderSuffix.h"
539 
540 #endif
OgreSharedPtr.h
OgreHeaderSuffix.h
Ogre::ResourceHandle
unsigned long long int ResourceHandle
Definition: OgreResource.h:41
Ogre::ManualResourceLoader::prepareResource
virtual void prepareResource(Resource *resource)
Called when a resource wishes to load.
Definition: OgreResource.h:526
Ogre::Resource::getName
virtual const String & getName(void) const
Gets resource name.
Definition: OgreResource.h:312
Ogre::AllocatedObject
Superclass for all objects that wish to use custom memory allocators when their new / delete operator...
Definition: OgreMemoryAllocatedObject.h:58
Ogre
Definition: OgreAndroidLogListener.h:34
Ogre::Resource::OGRE_AUTO_MUTEX
OGRE_AUTO_MUTEX
Definition: OgreResource.h:82
Ogre::Resource::Listener::backgroundPreparingComplete
virtual OGRE_DEPRECATED void backgroundPreparingComplete(Resource *)
Callback to indicate that background preparing has completed.
Definition: OgreResource.h:99
Ogre::Resource::postUnloadImpl
virtual void postUnloadImpl(void)
Internal hook to perform actions after the unload process, but before the resource has been marked as...
Definition: OgreResource.h:201
Ogre::set
Definition: OgrePrerequisites.h:519
Ogre::Resource::isReloadable
virtual bool isReloadable(void) const
Returns true if the Resource is reloadable, false otherwise.
Definition: OgreResource.h:282
Ogre::ManualResourceLoader::ManualResourceLoader
ManualResourceLoader()
Definition: OgreResource.h:517
Ogre::Resource::Listener::preparingComplete
virtual void preparingComplete(Resource *)
Called whenever the resource finishes preparing (paging into memory).
Definition: OgreResource.h:120
Ogre::AtomicScalar< LoadingState >
Ogre::Resource::isBackgroundLoaded
virtual bool isBackgroundLoaded(void) const
Returns whether this Resource has been earmarked for background loading.
Definition: OgreResource.h:365
Ogre::Resource::mIsManual
bool mIsManual
Is this file manually loaded?
Definition: OgreResource.h:158
Ogre::Resource::prepareImpl
virtual void prepareImpl(void)
Internal implementation of the meat of the 'prepare' action.
Definition: OgreResource.h:205
Ogre::Resource::mName
String mName
Unique name of the resource.
Definition: OgreResource.h:146
Ogre::String
_StringBase String
Definition: OgrePrerequisites.h:439
Ogre::ResourcePtr
SharedPtr< Resource > ResourcePtr
Shared pointer to a Resource.
Definition: OgrePrerequisites.h:317
Ogre::Resource::getOrigin
virtual const String & getOrigin(void) const
Get the origin of this resource, e.g.
Definition: OgreResource.h:418
Ogre::Resource::LOADSTATE_PREPARED
@ LOADSTATE_PREPARED
Fully prepared.
Definition: OgreResource.h:138
Ogre::Resource::isLoading
virtual bool isLoading() const
Returns whether the resource is currently in the process of background loading.
Definition: OgreResource.h:341
Ogre::Resource::Resource
Resource()
Protected unnamed constructor to prevent default construction.
Definition: OgreResource.h:172
Ogre::Resource::mListenerList
ListenerList mListenerList
Definition: OgreResource.h:167
Ogre::Resource::preUnloadImpl
virtual void preUnloadImpl(void)
Internal hook to perform actions before the unload process.
Definition: OgreResource.h:196
Ogre::Resource::LOADSTATE_UNLOADING
@ LOADSTATE_UNLOADING
Currently unloading.
Definition: OgreResource.h:136
Ogre::Resource::LOADSTATE_LOADING
@ LOADSTATE_LOADING
Loading is in progress.
Definition: OgreResource.h:132
OGRE_MUTEX
#define OGRE_MUTEX(name)
Definition: OgreThreadDefinesBoost.h:47
OgreStringInterface.h
OgreHeaderPrefix.h
Ogre::Resource::mSize
size_t mSize
The size of the resource in bytes.
Definition: OgreResource.h:156
Ogre::Resource::setBackgroundLoaded
virtual void setBackgroundLoaded(bool bl)
Tells the resource whether it is background loaded or not.
Definition: OgreResource.h:375
OgrePrerequisites.h
Ogre::Resource::mLoader
ManualResourceLoader * mLoader
Optional manual loader; if provided, data is loaded from here instead of a file.
Definition: OgreResource.h:162
Ogre::Resource::getGroup
virtual const String & getGroup(void) const
Gets the group which this resource is a member of.
Definition: OgreResource.h:399
Ogre::Resource::getStateCount
virtual size_t getStateCount() const
Returns the number of times this resource has changed state, which generally means the number of time...
Definition: OgreResource.h:429
OgreThreadHeaders.h
Ogre::Resource::getHandle
virtual ResourceHandle getHandle(void) const
Definition: OgreResource.h:317
Ogre::Resource::isLoaded
virtual bool isLoaded(void) const
Returns true if the Resource has been loaded, false otherwise.
Definition: OgreResource.h:332
Ogre::Resource::mIsBackgroundLoaded
volatile bool mIsBackgroundLoaded
Is this resource going to be background loaded? Only applicable for multithreaded.
Definition: OgreResource.h:154
Ogre::ManualResourceLoader::~ManualResourceLoader
virtual ~ManualResourceLoader()
Definition: OgreResource.h:518
Ogre::Resource::LOADSTATE_LOADED
@ LOADSTATE_LOADED
Fully loaded.
Definition: OgreResource.h:134
Ogre::Resource::LoadingState
LoadingState
Enum identifying the loading state of the resource.
Definition: OgreResource.h:127
Ogre::ResourceManager
Defines a generic resource handler.
Definition: OgreResourceManager.h:122
Ogre::AtomicScalar::get
T get(void) const
Definition: OgreAtomicScalar.h:417
Ogre::Resource::mStateCount
size_t mStateCount
State count, the number of times this resource has changed state.
Definition: OgreResource.h:164
Ogre::Resource::mOrigin
String mOrigin
Origin of this resource (e.g. script name) - optional.
Definition: OgreResource.h:160
Ogre::StringInterface
Class defining the common interface which classes can use to present a reflection-style,...
Definition: OgreStringInterface.h:163
Ogre::Resource
Abstract class representing a loadable resource (e.g.
Definition: OgreResource.h:79
Ogre::Resource::LOADSTATE_UNLOADED
@ LOADSTATE_UNLOADED
Not loaded.
Definition: OgreResource.h:130
Ogre::Resource::preLoadImpl
virtual void preLoadImpl(void)
Internal hook to perform actions before the load process, but after the resource has been marked as '...
Definition: OgreResource.h:184
Ogre::Resource::ListenerList
set< Listener * >::type ListenerList
Definition: OgreResource.h:166
OGRE_DEPRECATED
#define OGRE_DEPRECATED
Definition: OgrePlatform.h:189
Ogre::Resource::Listener::Listener
Listener()
Definition: OgreResource.h:86
Ogre::Resource::getSize
virtual size_t getSize(void) const
Retrieves info about the size of the resource.
Definition: OgreResource.h:301
Ogre::Resource::isPrepared
virtual bool isPrepared(void) const
Returns true if the Resource has been prepared, false otherwise.
Definition: OgreResource.h:324
Ogre::Resource::Listener
Definition: OgreResource.h:83
Ogre::Resource::Listener::loadingComplete
virtual void loadingComplete(Resource *)
Called whenever the resource finishes loading.
Definition: OgreResource.h:109
Ogre::Resource::mGroup
String mGroup
The name of the resource group.
Definition: OgreResource.h:148
Ogre::Resource::mLoadingState
AtomicScalar< LoadingState > mLoadingState
Is the resource currently loaded?
Definition: OgreResource.h:152
Ogre::Resource::postLoadImpl
virtual void postLoadImpl(void)
Internal hook to perform actions after the load process, but before the resource has been marked as f...
Definition: OgreResource.h:191
Ogre::Resource::Listener::backgroundLoadingComplete
virtual OGRE_DEPRECATED void backgroundLoadingComplete(Resource *)
Callback to indicate that background loading has completed.
Definition: OgreResource.h:93
Ogre::Resource::_notifyOrigin
virtual void _notifyOrigin(const String &origin)
Notify this resource of it's origin.
Definition: OgreResource.h:420
Ogre::Resource::isManuallyLoaded
virtual bool isManuallyLoaded(void) const
Is this resource manually loaded?
Definition: OgreResource.h:289
Ogre::Resource::getLoadingState
virtual LoadingState getLoadingState() const
Returns the current loading state.
Definition: OgreResource.h:348
Ogre::Resource::Listener::~Listener
virtual ~Listener()
Definition: OgreResource.h:87
Ogre::Resource::getCreator
virtual ResourceManager * getCreator(void)
Gets the manager which created this resource.
Definition: OgreResource.h:411
OgreString.h
Ogre::Resource::unprepareImpl
virtual void unprepareImpl(void)
Internal function for undoing the 'prepare' action.
Definition: OgreResource.h:210
Ogre::Resource::mCreator
ResourceManager * mCreator
Creator.
Definition: OgreResource.h:144
Ogre::Resource::Listener::unloadingComplete
virtual void unloadingComplete(Resource *)
Called whenever the resource has been unloaded.
Definition: OgreResource.h:123
Ogre::ManualResourceLoader
Interface describing a manual resource loader.
Definition: OgreResource.h:514
Ogre::Resource::mHandle
ResourceHandle mHandle
Numeric handle for more efficient look up than name.
Definition: OgreResource.h:150
OgreAtomicScalar.h

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 Tue Apr 13 2021 08:53:15