"ResourceManage"ment base rewritten
This commit is contained in:
parent
fb8d39e767
commit
963152f55d
@ -5,7 +5,7 @@
|
||||
// James Turk (jpt2433@rit.edu)
|
||||
//
|
||||
// Version:
|
||||
// $Id: ResourceManaged.hpp,v 1.1 2005/03/02 08:37:40 cozman Exp $
|
||||
// $Id: ResourceManaged.hpp,v 1.2 2005/06/10 05:48:59 cozman Exp $
|
||||
|
||||
#ifndef PHOTON_RESOURCEMANAGED_HPP
|
||||
#define PHOTON_RESOURCEMANAGED_HPP
|
||||
@ -39,12 +39,11 @@ public:
|
||||
// Initializing constructor, calls <open> with a filename/zipname.
|
||||
//
|
||||
// Parameters:
|
||||
// filename - Name of file to open.
|
||||
// zipname - Name of zip to open file from, empty string for no zip file.
|
||||
// name - name of resource
|
||||
//
|
||||
// See Also:
|
||||
// <open>
|
||||
ResourceManaged(const std::string& path);
|
||||
ResourceManaged(const std::string& name);
|
||||
|
||||
// Function: ~ResourceManaged
|
||||
// Destructor, calls <release>.
|
||||
@ -58,20 +57,40 @@ public:
|
||||
// Opens new resource via the associated <ResourceManager>.
|
||||
//
|
||||
// Parameters:
|
||||
// filename - Name of file to open.
|
||||
// zipname - Name of zip to open file from, empty string for no zip file.
|
||||
virtual void open(const std::string& path);
|
||||
// name - name of resource
|
||||
virtual void open(const std::string& name);
|
||||
|
||||
// Function: release
|
||||
// Removes a reference to the resource, releasing if needed.
|
||||
// Generally called by destructor, so should rarely be called.
|
||||
virtual void release();
|
||||
|
||||
public:
|
||||
// Function: cleanUp
|
||||
// Static method which cleans up any resources.
|
||||
static void cleanUp();
|
||||
// Group: Resource Manager Access
|
||||
|
||||
// Function: cleanUp
|
||||
// Cleans up any unused resources of the type.
|
||||
// (Ex. Image::cleanUp() will unload all images.)
|
||||
static virtual void cleanUp();
|
||||
|
||||
// Function: addResource
|
||||
// Define a new named resource.
|
||||
// (Ex. Image::addResource("monkey","images/monkey.png") would
|
||||
// make it so that any attempts to load "monkey" would load the image
|
||||
// images/monkey.png)
|
||||
//
|
||||
// Parameters:
|
||||
// name - Name to give to resource.
|
||||
// path - Path of resource data file.
|
||||
static void addResource(const std::string& name, const std::string& path);
|
||||
|
||||
// Function: addResource
|
||||
// Define a new unaliased resource. (name == path).
|
||||
// (Ex. Image::addResource("images/monkey.png") is essentially the same as
|
||||
// Image::addResource("images/monkey.png","images/monkey.png")
|
||||
//
|
||||
// Parameters:.
|
||||
// path - Path of resource data file.
|
||||
static void addResource(const std::string& path);
|
||||
|
||||
private:
|
||||
static ResMgrT resMgr_;
|
||||
@ -80,22 +99,15 @@ private:
|
||||
|
||||
//and he said "the implementation shall follow, as it is written"
|
||||
|
||||
template<class ResMgrT>
|
||||
void ResourceManaged<ResMgrT>::cleanUp()
|
||||
{
|
||||
resMgr_.cleanUp();
|
||||
}
|
||||
|
||||
template<class ResMgrT>
|
||||
ResourceManaged<ResMgrT>::ResourceManaged() :
|
||||
resID_(Resource::Invalid)
|
||||
resID_(Resource::InvalidID)
|
||||
{
|
||||
}
|
||||
|
||||
template<class ResMgrT>
|
||||
ResourceManaged<ResMgrT>::ResourceManaged(const std::string& path)
|
||||
ResourceManaged<ResMgrT>::ResourceManaged(const std::string& name)
|
||||
{
|
||||
open(path);
|
||||
}
|
||||
|
||||
template<class ResMgrT>
|
||||
@ -110,7 +122,7 @@ ResourceManaged<ResMgrT>::operator=(const ResourceManaged<ResMgrT> &rhs)
|
||||
{
|
||||
if(this != &rhs)
|
||||
{
|
||||
if(resID_ != Resource::Invalid)
|
||||
if(resID_ != Resource::InvalidID)
|
||||
{
|
||||
release();
|
||||
}
|
||||
@ -120,16 +132,36 @@ ResourceManaged<ResMgrT>::operator=(const ResourceManaged<ResMgrT> &rhs)
|
||||
}
|
||||
|
||||
template<class ResMgrT>
|
||||
void ResourceManaged<ResMgrT>::open(const std::string& path)
|
||||
void ResourceManaged<ResMgrT>::open(const std::string& name)
|
||||
{
|
||||
release();
|
||||
resID_ = resMgr_.getResID(path);
|
||||
resID_ = resMgr_.getResID(name);
|
||||
}
|
||||
|
||||
template<class ResMgrT>
|
||||
void ResourceManaged<ResMgrT>::release()
|
||||
{
|
||||
resMgr_.delRef(resID_);
|
||||
resID_ = Resource::InvalidID;
|
||||
}
|
||||
|
||||
template<class ResMgrT>
|
||||
void ResourceManaged<ResMgrT>::cleanUp()
|
||||
{
|
||||
resMgr_.cleanUp();
|
||||
}
|
||||
|
||||
template<class ResMgrT>
|
||||
void ResourceManaged<ResMgrT>::addResource(const std::string& name,
|
||||
const std::string& path)
|
||||
{
|
||||
resMgr_.newResource(name,path);
|
||||
}
|
||||
|
||||
template<class ResMgrT>
|
||||
void ResourceManaged<ResMgrT>::addResource(const std::string& path)
|
||||
{
|
||||
resMgr_.newResource(path,path);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,7 +5,7 @@
|
||||
// James Turk (jpt2433@rit.edu)
|
||||
//
|
||||
// Version:
|
||||
// $Id: ResourceManager.hpp,v 1.1 2005/03/02 08:37:40 cozman Exp $
|
||||
// $Id: ResourceManager.hpp,v 1.2 2005/06/10 05:48:59 cozman Exp $
|
||||
|
||||
#ifndef PHOTON_RESOURCEMANAGER_HPP
|
||||
#define PHOTON_RESOURCEMANAGER_HPP
|
||||
@ -24,7 +24,7 @@ namespace photon
|
||||
class Resource
|
||||
{
|
||||
public:
|
||||
static const uint Invalid=0xffffffff;
|
||||
static const uint InvalidID=0xffffffff;
|
||||
|
||||
Resource() :
|
||||
refCount(0)
|
||||
@ -32,6 +32,7 @@ public:
|
||||
}
|
||||
|
||||
uint refCount;
|
||||
std::string name;
|
||||
std::string path;
|
||||
};
|
||||
|
||||
@ -48,15 +49,15 @@ public:
|
||||
|
||||
virtual ~ResourceManager();
|
||||
|
||||
uint getResID(const std::string& path);
|
||||
uint getResID(const std::string& name);
|
||||
void delRef(uint id);
|
||||
void cleanUp();
|
||||
|
||||
private:
|
||||
virtual void loadResource(resT &res, const std::string& path)=0;
|
||||
virtual void loadResource(resT &res, const std::string& name)=0;
|
||||
virtual void freeResource(resT &res)=0;
|
||||
|
||||
uint newResource(const std::string& path);
|
||||
uint newResource(const std::string& name, const std::string& path);
|
||||
void deleteResource(uint id);
|
||||
|
||||
private:
|
||||
@ -76,28 +77,24 @@ ResourceManager<resT>::~ResourceManager()
|
||||
}
|
||||
|
||||
template<class resT>
|
||||
uint ResourceManager<resT>::getResID(const std::string& path)
|
||||
uint ResourceManager<resT>::getResID(const std::string& name)
|
||||
{
|
||||
uint id(0);
|
||||
|
||||
// loop through resources and find id
|
||||
// loop through resources until the resource name in question is found
|
||||
for(typename std::vector<resT>::iterator i=resVec_.begin();
|
||||
i != resVec_.end();
|
||||
i != resVec_.end() && i->name != name;
|
||||
++i)
|
||||
{
|
||||
++id; // increment id
|
||||
if(i->path == path)
|
||||
{
|
||||
return id;
|
||||
}
|
||||
++id; // increment id
|
||||
}
|
||||
|
||||
if(id == resVec_.size())
|
||||
if(id == resVec_.size()) // not found -> throw a ResourceException
|
||||
{
|
||||
id = newResource(path);
|
||||
throw ResourceException("Failed to find resource \"" + name + "\"");
|
||||
}
|
||||
|
||||
return id; //not already in vector, add resource
|
||||
return id;
|
||||
}
|
||||
|
||||
template<class resT>
|
||||
@ -123,20 +120,22 @@ void ResourceManager<resT>::cleanUp()
|
||||
}
|
||||
|
||||
template<class resT>
|
||||
uint ResourceManager<resT>::newResource(const std::string& path)
|
||||
uint ResourceManager<resT>::newResource(const std::string& name,
|
||||
const std::string& path)
|
||||
{
|
||||
resT res;
|
||||
res.name = name;
|
||||
res.path = path;
|
||||
|
||||
try
|
||||
{
|
||||
// attempt to load
|
||||
loadResource(res,path);
|
||||
loadResource(res, name, path);
|
||||
}
|
||||
catch(ResourceException&)
|
||||
{
|
||||
// rethrow any exceptions with specific information
|
||||
throw ResourceException("Could not load " + path);
|
||||
throw ResourceException("Could not load " + path + " as " + name);
|
||||
}
|
||||
|
||||
resVec_.push_back(res); // add resource to resVec & return
|
||||
|
@ -11,14 +11,16 @@
|
||||
</node>
|
||||
<node ID="Freemind_Link_614612335" TEXT="all stable ZEngine/Nova features">
|
||||
<font NAME="SansSerif" SIZE="12"/>
|
||||
<node ID="Freemind_Link_107267630" TEXT=""Engine""/>
|
||||
<node FOLDED="true" ID="Freemind_Link_50716011" TEXT="Texture">
|
||||
<node ID="Freemind_Link_1795651487" TEXT=""ResourceManage"ment">
|
||||
<icon BUILTIN="button_ok"/>
|
||||
</node>
|
||||
<node ID="Freemind_Link_50716011" TEXT="Texture">
|
||||
<node ID="Freemind_Link_561271163" TEXT="Font"/>
|
||||
<node ID="Freemind_Link_385334177" TEXT="Image"/>
|
||||
</node>
|
||||
<node ID="Freemind_Link_1851655735" TEXT="Music"/>
|
||||
<node ID="Freemind_Link_1045379727" TEXT="Sound"/>
|
||||
<node ID="Freemind_Link_1795651487" TEXT="ResourceManagers"/>
|
||||
<node ID="Freemind_Link_107267630" TEXT=""Engine""/>
|
||||
</node>
|
||||
<node ID="Freemind_Link_188779968" TEXT="Test Suite">
|
||||
<node ID="Freemind_Link_1570884553" TEXT="port test-suite to Saturn core">
|
||||
|
Loading…
Reference in New Issue
Block a user