OGRE  2.0
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 "OgreAtomicScalar.h"
33 #include "OgreStringInterface.h"
34 #include "OgreHeaderPrefix.h"
36 
37 namespace Ogre {
38 
39  typedef unsigned long long int ResourceHandle;
40 
41 
42  // Forward declaration
44 
78  {
79  public:
80  OGRE_AUTO_MUTEX; // public to allow external locking
81  class Listener
82  {
83  public:
84  Listener() {}
85  virtual ~Listener() {}
86 
87 
88 
97  virtual void loadingComplete(Resource*) {}
98 
99 
108  virtual void preparingComplete(Resource*) {}
109 
111  virtual void unloadingComplete(Resource*) {}
112  };
113 
116  {
128  LOADSTATE_PREPARING
129  };
130  protected:
142  volatile bool mIsBackgroundLoaded;
144  size_t mSize;
146  bool mIsManual;
152  size_t mStateCount;
153 
156  OGRE_MUTEX(mListenerListMutex);
157 
161  : mCreator(0), mHandle(0), mLoadingState(LOADSTATE_UNLOADED),
162  mIsBackgroundLoaded(false), mSize(0), mIsManual(0), mLoader(0), mStateCount(0)
163  {
164  }
165 
172  virtual void preLoadImpl(void) {}
179  virtual void postLoadImpl(void) {}
180 
184  virtual void preUnloadImpl(void) {}
189  virtual void postUnloadImpl(void) {}
190 
193  virtual void prepareImpl(void) {}
198  virtual void unprepareImpl(void) {}
202  virtual void loadImpl(void) = 0;
206  virtual void unloadImpl(void) = 0;
207 
208  public:
223  Resource(ResourceManager* creator, const String& name, ResourceHandle handle,
224  const String& group, bool isManual = false, ManualResourceLoader* loader = 0);
225 
231  virtual ~Resource();
232 
247  virtual void prepare(bool backgroundThread = false);
248 
259  virtual void load(bool backgroundThread = false);
260 
266  virtual void reload(void);
267 
270  virtual bool isReloadable(void) const
271  {
272  return !mIsManual || mLoader;
273  }
274 
277  virtual bool isManuallyLoaded(void) const
278  {
279  return mIsManual;
280  }
281 
284  virtual void setManuallyLoaded(bool isManual)
285  {
286  mIsManual = isManual;
287  }
288 
289 
293  virtual void unload(void);
294 
297  virtual size_t getSize(void) const
298  {
299  return mSize;
300  }
301 
304  virtual void touch(void);
305 
308  virtual const String& getName(void) const
309  {
310  return mName;
311  }
312 
313  virtual ResourceHandle getHandle(void) const
314  {
315  return mHandle;
316  }
317 
320  virtual bool isPrepared(void) const
321  {
322  // No lock required to read this state since no modify
323  return (mLoadingState.get() == LOADSTATE_PREPARED);
324  }
325 
328  virtual bool isLoaded(void) const
329  {
330  // No lock required to read this state since no modify
331  return (mLoadingState.get() == LOADSTATE_LOADED);
332  }
333 
336  virtual void setToLoaded(void)
337  {
338  // No lock required to read this state since no modify
339  mLoadingState.set(LOADSTATE_LOADED);
340  }
341 
345  virtual bool isLoading() const
346  {
347  return (mLoadingState.get() == LOADSTATE_LOADING);
348  }
349 
353  {
354  return mLoadingState.get();
355  }
356 
357 
358 
369  virtual bool isBackgroundLoaded(void) const { return mIsBackgroundLoaded; }
370 
379  virtual void setBackgroundLoaded(bool bl) { mIsBackgroundLoaded = bl; }
380 
390  virtual void escalateLoading();
391 
395  virtual void addListener(Listener* lis);
396 
400  virtual void removeListener(Listener* lis);
401 
403  virtual const String& getGroup(void) const { return mGroup; }
404 
412  virtual void changeGroupOwnership(const String& newGroup);
413 
415  virtual ResourceManager* getCreator(void) { return mCreator; }
422  virtual const String& getOrigin(void) const { return mOrigin; }
424  virtual void _notifyOrigin(const String& origin) { mOrigin = origin; }
425 
433  virtual size_t getStateCount() const { return mStateCount; }
434 
440  virtual void _dirtyState();
441 
442 
451  virtual void _fireLoadingComplete(bool wasBackgroundLoaded);
452 
461  virtual void _firePreparingComplete(bool wasBackgroundLoaded);
462 
470  virtual void _fireUnloadingComplete(void);
471 
473  virtual size_t calculateSize(void) const;
474 
475  };
476 
499  {
500  public:
503 
510  virtual void prepareResource(Resource* resource)
511  { (void)resource; }
512 
516  virtual void loadResource(Resource* resource) = 0;
517  };
520 }
521 
522 #include "OgreHeaderSuffix.h"
523 
524 #endif
virtual void preparingComplete(Resource *)
Called whenever the resource finishes preparing (paging into memory).
Definition: OgreResource.h:108
virtual bool isBackgroundLoaded(void) const
Returns whether this Resource has been earmarked for background loading.
Definition: OgreResource.h:369
#define _OgreExport
Definition: OgrePlatform.h:255
virtual LoadingState getLoadingState() const
Returns the current loading state.
Definition: OgreResource.h:352
virtual bool isReloadable(void) const
Returns true if the Resource is reloadable, false otherwise.
Definition: OgreResource.h:270
#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:172
virtual void setManuallyLoaded(bool isManual)
Set "Is this resource manually loaded?".
Definition: OgreResource.h:284
LoadingState
Enum identifying the loading state of the resource.
Definition: OgreResource.h:115
virtual void _notifyOrigin(const String &origin)
Notify this resource of it's origin.
Definition: OgreResource.h:424
AtomicScalar< LoadingState > mLoadingState
Is the resource currently loaded?
Definition: OgreResource.h:140
Interface describing a manual resource loader.
Definition: OgreResource.h:498
virtual void unloadingComplete(Resource *)
Called whenever the resource has been unloaded.
Definition: OgreResource.h:111
virtual bool isManuallyLoaded(void) const
Is this resource manually loaded?
Definition: OgreResource.h:277
virtual size_t getSize(void) const
Retrieves info about the size of the resource.
Definition: OgreResource.h:297
virtual const String & getName(void) const
Gets resource name.
Definition: OgreResource.h:308
ListenerList mListenerList
Definition: OgreResource.h:155
virtual void preUnloadImpl(void)
Internal hook to perform actions before the unload process.
Definition: OgreResource.h:184
virtual void prepareResource(Resource *resource)
Called when a resource wishes to load.
Definition: OgreResource.h:510
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:320
String mGroup
The name of the resource group.
Definition: OgreResource.h:136
virtual void unprepareImpl(void)
Internal function for undoing the 'prepare' action.
Definition: OgreResource.h:198
size_t mSize
The size of the resource in bytes.
Definition: OgreResource.h:144
ManualResourceLoader * mLoader
Optional manual loader; if provided, data is loaded from here instead of a file.
Definition: OgreResource.h:150
virtual ResourceManager * getCreator(void)
Gets the manager which created this resource.
Definition: OgreResource.h:415
String mOrigin
Origin of this resource (e.g. script name) - optional.
Definition: OgreResource.h:148
virtual void setToLoaded(void)
Change the Resource loading state to loaded.
Definition: OgreResource.h:336
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:403
virtual bool isLoading() const
Returns whether the resource is currently in the process of background loading.
Definition: OgreResource.h:345
virtual bool isLoaded(void) const
Returns true if the Resource has been loaded, false otherwise.
Definition: OgreResource.h:328
virtual const String & getOrigin(void) const
Get the origin of this resource, e.g.
Definition: OgreResource.h:422
bool mIsManual
Is this file manually loaded?
Definition: OgreResource.h:146
String mName
Unique name of the resource.
Definition: OgreResource.h:134
Resource()
Protected unnamed constructor to prevent default construction.
Definition: OgreResource.h:160
virtual void loadingComplete(Resource *)
Called whenever the resource finishes loading.
Definition: OgreResource.h:97
Abstract class representing a loadable resource (e.g.
Definition: OgreResource.h:77
unsigned long long int ResourceHandle
Definition: OgreResource.h:39
virtual void prepareImpl(void)
Internal implementation of the meat of the 'prepare' action.
Definition: OgreResource.h:193
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:179
virtual void setBackgroundLoaded(bool bl)
Tells the resource whether it is background loaded or not.
Definition: OgreResource.h:379
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:433
ResourceManager * mCreator
Creator.
Definition: OgreResource.h:132
ResourceHandle mHandle
Numeric handle for more efficient look up than name.
Definition: OgreResource.h:138
Defines a generic resource handler.
_StringBase String
Definition: OgreCommon.h:53
volatile bool mIsBackgroundLoaded
Is this resource going to be background loaded? Only applicable for multithreaded.
Definition: OgreResource.h:142
size_t mStateCount
State count, the number of times this resource has changed state.
Definition: OgreResource.h:152
Loading is in progress.
Definition: OgreResource.h:120
virtual ResourceHandle getHandle(void) const
Definition: OgreResource.h:313
virtual void postUnloadImpl(void)
Internal hook to perform actions after the unload process, but before the resource has been marked as...
Definition: OgreResource.h:189
set< Listener * >::type ListenerList
Definition: OgreResource.h:154