OGRE  1.9
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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-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 _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 
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 
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
virtual void preparingComplete(Resource *)
Called whenever the resource finishes preparing (paging into memory).
Definition: OgreResource.h:120
virtual bool isBackgroundLoaded(void) const
Returns whether this Resource has been earmarked for background loading.
Definition: OgreResource.h:365
#define _OgreExport
Definition: OgrePlatform.h:260
virtual LoadingState getLoadingState() const
Returns the current loading state.
Definition: OgreResource.h:348
virtual bool isReloadable(void) const
Returns true if the Resource is reloadable, false otherwise.
Definition: OgreResource.h:282
#define OGRE_MUTEX(name)
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
LoadingState
Enum identifying the loading state of the resource.
Definition: OgreResource.h:127
virtual void _notifyOrigin(const String &origin)
Notify this resource of it's origin.
Definition: OgreResource.h:420
AtomicScalar< LoadingState > mLoadingState
Is the resource currently loaded?
Definition: OgreResource.h:152
SharedPtr< Resource > ResourcePtr
Shared pointer to a Resource.
Interface describing a manual resource loader.
Definition: OgreResource.h:514
virtual void unloadingComplete(Resource *)
Called whenever the resource has been unloaded.
Definition: OgreResource.h:123
virtual bool isManuallyLoaded(void) const
Is this resource manually loaded?
Definition: OgreResource.h:289
virtual size_t getSize(void) const
Retrieves info about the size of the resource.
Definition: OgreResource.h:301
virtual const String & getName(void) const
Gets resource name.
Definition: OgreResource.h:312
ListenerList mListenerList
Definition: OgreResource.h:167
virtual void preUnloadImpl(void)
Internal hook to perform actions before the unload process.
Definition: OgreResource.h:196
virtual void prepareResource(Resource *resource)
Called when a resource wishes to load.
Definition: OgreResource.h:526
Class defining the common interface which classes can use to present a reflection-style, self-defining parameter set to callers.
virtual bool isPrepared(void) const
Returns true if the Resource has been prepared, false otherwise.
Definition: OgreResource.h:324
String mGroup
The name of the resource group.
Definition: OgreResource.h:148
virtual void unprepareImpl(void)
Internal function for undoing the 'prepare' action.
Definition: OgreResource.h:210
size_t mSize
The size of the resource in bytes.
Definition: OgreResource.h:156
ManualResourceLoader * mLoader
Optional manual loader; if provided, data is loaded from here instead of a file.
Definition: OgreResource.h:162
virtual ResourceManager * getCreator(void)
Gets the manager which created this resource.
Definition: OgreResource.h:411
String mOrigin
Origin of this resource (e.g. script name) - optional.
Definition: OgreResource.h:160
Superclass for all objects that wish to use custom memory allocators when their new / delete operator...
virtual const String & getGroup(void) const
Gets the group which this resource is a member of.
Definition: OgreResource.h:399
virtual bool isLoading() const
Returns whether the resource is currently in the process of background loading.
Definition: OgreResource.h:341
virtual bool isLoaded(void) const
Returns true if the Resource has been loaded, false otherwise.
Definition: OgreResource.h:332
virtual const String & getOrigin(void) const
Get the origin of this resource, e.g.
Definition: OgreResource.h:418
bool mIsManual
Is this file manually loaded?
Definition: OgreResource.h:158
virtual OGRE_DEPRECATED void backgroundPreparingComplete(Resource *)
Callback to indicate that background preparing has completed.
Definition: OgreResource.h:99
String mName
Unique name of the resource.
Definition: OgreResource.h:146
Resource()
Protected unnamed constructor to prevent default construction.
Definition: OgreResource.h:172
virtual void loadingComplete(Resource *)
Called whenever the resource finishes loading.
Definition: OgreResource.h:109
Abstract class representing a loadable resource (e.g.
Definition: OgreResource.h:79
unsigned long long int ResourceHandle
Definition: OgreResource.h:41
virtual void prepareImpl(void)
Internal implementation of the meat of the 'prepare' action.
Definition: OgreResource.h:205
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
virtual void setBackgroundLoaded(bool bl)
Tells the resource whether it is background loaded or not.
Definition: OgreResource.h:375
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
ResourceManager * mCreator
Creator.
Definition: OgreResource.h:144
virtual OGRE_DEPRECATED void backgroundLoadingComplete(Resource *)
Callback to indicate that background loading has completed.
Definition: OgreResource.h:93
ResourceHandle mHandle
Numeric handle for more efficient look up than name.
Definition: OgreResource.h:150
Defines a generic resource handler.
_StringBase String
volatile bool mIsBackgroundLoaded
Is this resource going to be background loaded? Only applicable for multithreaded.
Definition: OgreResource.h:154
#define OGRE_DEPRECATED
Definition: OgrePlatform.h:192
size_t mStateCount
State count, the number of times this resource has changed state.
Definition: OgreResource.h:164
Loading is in progress.
Definition: OgreResource.h:132
virtual ResourceHandle getHandle(void) const
Definition: OgreResource.h:317
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
set< Listener * >::type ListenerList
Definition: OgreResource.h:166