nrEngine::IResource Class Reference
[Resource managment]

General interface to hold any kind of resources. More...

Inheritance diagram for nrEngine::IResource:

nrEngine::FileStream nrEngine::IScript nrEngine::Plugin nrEngine::EmptyFileStream nrEngine::EmptyScript nrEngine::Script nrEngine::EmptyPlugin

Public Member Functions

NR_FORCEINLINE std::size_t getResourceDataSize ()
NR_FORCEINLINE const std::string & getResourceType () const
NR_FORCEINLINE bool isResourceLoaded () const
NR_FORCEINLINE ResourceHandle getResourceHandle () const
NR_FORCEINLINE const std::string & getResourceGroup () const
NR_FORCEINLINE const std::string & getResourceName () const
NR_FORCEINLINE const std::list<
std::string > & 
getResourceFilenameList () const
NR_FORCEINLINE bool isResourceEmpty ()
Result unload ()
Result reload (PropertyList *params=NULL)
Result remove ()
void addResourceFilename (const std::string &filename)
void addResourceFilename (const std::list< std::string > &flist)
void setResourceFilename (const std::list< std::string > &flist)
NR_FORCEINLINE void setResourceDirty (bool dirty)
NR_FORCEINLINE bool isResourceDirty ()

Protected Member Functions

 IResource (const std::string &resourceType)
virtual ~IResource ()
virtual Result unloadResource ()=0
virtual Result reloadResource (PropertyList *params=NULL)=0
NR_FORCEINLINE void markResourceLoaded ()
NR_FORCEINLINE void markResourceUnloaded ()
NR_FORCEINLINE SharedPtr<
IResourceLoader
getResourceLoader ()
 Get resource loader assigned with the resource.

Friends

class IResourceLoader
 Only loader can change resource data.
class ResourceManager
 Also resource manager is a friend.

Data Structures

class  _deleter

Detailed Description

General interface to hold any kind of resources.

This is an interface which is describing how resource classes should looks like. Derive your own classes to get resource management system working. Each resource containing only once in the memory if it has the same name. So all names of resources should be unique. Resource is not directly stored by the manager but by the ResourceHolder. This holder just stores the resource and is unique for each resource. Manager just manages such holders and gives the resource pointers access to it.

All derived classes must implement the behavior of resource objects correctly. Correct behavior of resource objects is done if resources do nothing what can change the empty resource behavior if they are empty. For example if you use texture and user is setting some values to it and it is at the moment unloaded, so the method will be called by the empty texture object. Empty texture object should stay empty and therefor there should not be any changes. So texture object has to check whenever it is an empty resource and should not do anything.

The empty flag should be set by the loader, because only the loader does know if and when a resource can be defined as empty. For example you have to load the empty resource texture before you declare this resource as empty.

Definition at line 51 of file Resource.h.


Constructor & Destructor Documentation

nrEngine::IResource::IResource ( const std::string &  resourceType  )  [protected]

Create resource instance.

Definition at line 24 of file Resource.cpp.

nrEngine::IResource::~IResource (  )  [protected, virtual]

As soon as this destructor is called, the resource manager will be notified.

You have to call the unloadResource() method if you have some specific unloading routines for your resource object. We can not call this from here, because calling of virtual functions from constructor/destructor cause in undefined behaviour.

Definition at line 36 of file Resource.cpp.


Member Function Documentation

NR_FORCEINLINE std::size_t nrEngine::IResource::getResourceDataSize (  ) 

Here the class should return count of bytes reserved by the resource. The size must not contain the size of the class itself. For example: 32Bit Texture of 16x16 Pixel has the size: 16*16*4 = 1024 Bytes + some more bytes which does class need to store himself in the memory.

Definition at line 60 of file Resource.h.

NR_FORCEINLINE const std::string& nrEngine::IResource::getResourceType (  )  const

Return here the name of the resource type. This name will be used to assign right empty resources to the resource pointer/holder objects. E.g: "Texture", "Sound", ...

Definition at line 68 of file Resource.h.

NR_FORCEINLINE bool nrEngine::IResource::isResourceLoaded (  )  const

Return true if the resource is marked as loaded. If any resource is loaded so it contains in the memory and has it's full data there.

Definition at line 75 of file Resource.h.

Referenced by reload(), nrEngine::Plugin::reloadResource(), unload(), and nrEngine::Plugin::unloadResource().

NR_FORCEINLINE ResourceHandle nrEngine::IResource::getResourceHandle (  )  const

Return handle of this resource

Definition at line 80 of file Resource.h.

NR_FORCEINLINE const std::string& nrEngine::IResource::getResourceGroup (  )  const

Return group name to which one this resource belongs

Definition at line 85 of file Resource.h.

NR_FORCEINLINE const std::string& nrEngine::IResource::getResourceName (  )  const

Get the name of the resource

Definition at line 90 of file Resource.h.

Referenced by nrEngine::Script::fullRun(), nrEngine::Plugin::initialize(), nrEngine::Script::loadFromString(), nrEngine::Plugin::reloadResource(), and nrEngine::Plugin::unloadResource().

NR_FORCEINLINE const std::list<std::string>& nrEngine::IResource::getResourceFilenameList (  )  const

Get the file name from which one this resource can be restored or loaded

Definition at line 95 of file Resource.h.

Referenced by nrEngine::Plugin::reloadResource().

NR_FORCEINLINE bool nrEngine::IResource::isResourceEmpty (  ) 

Returns true if this resource is a empty resource. Empty resources are used to replace normal resources if they are unloaded.

Definition at line 101 of file Resource.h.

Referenced by nrEngine::IResourcePtr::operator==().

Result nrEngine::IResource::unload (  ) 

Unload resource. The resource should overwrite the IResource::unloadResource() method which will be called. The resource should know how to unload itself. The resource owner (in this case loader) will be notified about unloading on success.

Definition at line 42 of file Resource.cpp.

References isResourceLoaded(), nrEngine::ResourceManager::notifyUnloaded(), nrEngine::OK, nrEngine::Engine::sResourceManager(), and unloadResource().

Referenced by reload(), and remove().

Result nrEngine::IResource::reload ( PropertyList params = NULL  ) 

Reload resource if this was previously unloaded. Each resource should overwrite IResource::reloadResource() method. Resource should know how to reload itself if it was previously unloaded. Resource owner (in this case loader) will be notified on success.

Note:
If resource was not unloaded before, so the IResource::unload() method will be called before reloading.

Definition at line 60 of file Resource.cpp.

References isResourceLoaded(), nrEngine::ResourceManager::notifyLoaded(), nrEngine::OK, reloadResource(), nrEngine::Engine::sResourceManager(), and unload().

Result nrEngine::IResource::remove (  ) 

Remove resource. Removing resource through this method, meanse to remove the resource from the loader and from the manager. After this method call all resource pointers associated within this resource will point to the empty resource. All shared instaces would also be removed.

If you call this method through resource pointer, then nothing bad happens. If you call it directly (i.e. YourResource* res; res->remove()), hten you are responsible for deleting the pointer by yourself

Definition at line 86 of file Resource.cpp.

References unload().

Referenced by nrEngine::ResourceManager::remove().

void nrEngine::IResource::addResourceFilename ( const std::string &  filename  ) 

Add resource file name. The filename is used to associated the resource with. If a resource is build upon of a multiple files, then all the files should be added through this method.

Parameters:
filename Filename of a file associated with the resource

Definition at line 96 of file Resource.cpp.

Referenced by addResourceFilename().

void nrEngine::IResource::addResourceFilename ( const std::list< std::string > &  flist  ) 

See also:
addResourceFilename(const std::string& filename)
Parameters:
flist List of filenames to add

Definition at line 105 of file Resource.cpp.

References addResourceFilename().

void nrEngine::IResource::setResourceFilename ( const std::list< std::string > &  flist  ) 

Set resource file name list. This method will replace the current filename list with the given one.

Parameters:
flist List of filenames

Definition at line 113 of file Resource.cpp.

NR_FORCEINLINE void nrEngine::IResource::setResourceDirty ( bool  dirty  ) 

Set the resource as dirty. If resource is marked as dirty, then it will be reloaded on next access.

Parameters:
dirty True to mark resource as dirty otherwise false.
Note:
After the resource is reloaded it will be unmarked

Definition at line 159 of file Resource.h.

NR_FORCEINLINE bool nrEngine::IResource::isResourceDirty (  ) 

Check whenever a resource is dirty. Dirty resources require reloading as soon as possible (i.e. on next access)

Definition at line 165 of file Resource.h.

virtual Result nrEngine::IResource::unloadResource (  )  [protected, pure virtual]

Unload the resource. Each resource should know how to unload it from the memory. Unloading does not remove the resource it just unload used data from memory. To remove the resource from memory you have either to use ResourceManager or the ResourceLoader.

Implemented in nrEngine::IScript, nrEngine::Plugin, and nrEngine::EmptyPlugin.

Referenced by unload().

virtual Result nrEngine::IResource::reloadResource ( PropertyList params = NULL  )  [protected, pure virtual]

Reload resource. Each resource object should be able to reload itself from the disk or other media. It can use the assigned loader to load files or to make it by itself. At the end the resource should be marked as loaded.

Implemented in nrEngine::IScript, and nrEngine::Plugin.

Referenced by reload().

NR_FORCEINLINE void nrEngine::IResource::markResourceLoaded (  )  [protected]

Mark a resource as loaded. This method will not call the reloadResource() method. Instead it will just mark this resource as it were loaded, so you get access to real resource object through the pointers instead of emtpy resource. Call this method if you create a resource by your own without loading by loader.

Definition at line 209 of file Resource.h.

Referenced by nrEngine::IScript::notifyLoaded().

NR_FORCEINLINE void nrEngine::IResource::markResourceUnloaded (  )  [protected]

Mark the resource as unloaded. When you mark it, then empty resource will be used instead.

Definition at line 215 of file Resource.h.

Referenced by nrEngine::Plugin::unloadResource().


The documentation for this class was generated from the following files:
Generated on Wed Sep 12 23:19:43 2007 for nrEngine by  doxygen 1.5.1