"ResourceManage"ment base rewritten

This commit is contained in:
James Turk 2005-06-10 05:48:59 +00:00
parent fb8d39e767
commit 963152f55d
4 changed files with 78 additions and 55 deletions

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // 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 #ifndef PHOTON_RESOURCEMANAGED_HPP
#define PHOTON_RESOURCEMANAGED_HPP #define PHOTON_RESOURCEMANAGED_HPP
@ -39,12 +39,11 @@ public:
// Initializing constructor, calls <open> with a filename/zipname. // Initializing constructor, calls <open> with a filename/zipname.
// //
// Parameters: // Parameters:
// filename - Name of file to open. // name - name of resource
// zipname - Name of zip to open file from, empty string for no zip file.
// //
// See Also: // See Also:
// <open> // <open>
ResourceManaged(const std::string& path); ResourceManaged(const std::string& name);
// Function: ~ResourceManaged // Function: ~ResourceManaged
// Destructor, calls <release>. // Destructor, calls <release>.
@ -58,20 +57,40 @@ public:
// Opens new resource via the associated <ResourceManager>. // Opens new resource via the associated <ResourceManager>.
// //
// Parameters: // Parameters:
// filename - Name of file to open. // name - name of resource
// zipname - Name of zip to open file from, empty string for no zip file. virtual void open(const std::string& name);
virtual void open(const std::string& path);
// Function: release // Function: release
// Removes a reference to the resource, releasing if needed. // Removes a reference to the resource, releasing if needed.
// Generally called by destructor, so should rarely be called. // Generally called by destructor, so should rarely be called.
virtual void release(); virtual void release();
public: // Group: Resource Manager Access
// Function: cleanUp
// Static method which cleans up any resources.
static void cleanUp();
// 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: private:
static ResMgrT resMgr_; static ResMgrT resMgr_;
@ -80,22 +99,15 @@ private:
//and he said "the implementation shall follow, as it is written" //and he said "the implementation shall follow, as it is written"
template<class ResMgrT>
void ResourceManaged<ResMgrT>::cleanUp()
{
resMgr_.cleanUp();
}
template<class ResMgrT> template<class ResMgrT>
ResourceManaged<ResMgrT>::ResourceManaged() : ResourceManaged<ResMgrT>::ResourceManaged() :
resID_(Resource::Invalid) resID_(Resource::InvalidID)
{ {
} }
template<class ResMgrT> template<class ResMgrT>
ResourceManaged<ResMgrT>::ResourceManaged(const std::string& path) ResourceManaged<ResMgrT>::ResourceManaged(const std::string& name)
{ {
open(path);
} }
template<class ResMgrT> template<class ResMgrT>
@ -110,7 +122,7 @@ ResourceManaged<ResMgrT>::operator=(const ResourceManaged<ResMgrT> &rhs)
{ {
if(this != &rhs) if(this != &rhs)
{ {
if(resID_ != Resource::Invalid) if(resID_ != Resource::InvalidID)
{ {
release(); release();
} }
@ -120,16 +132,36 @@ ResourceManaged<ResMgrT>::operator=(const ResourceManaged<ResMgrT> &rhs)
} }
template<class ResMgrT> template<class ResMgrT>
void ResourceManaged<ResMgrT>::open(const std::string& path) void ResourceManaged<ResMgrT>::open(const std::string& name)
{ {
release(); release();
resID_ = resMgr_.getResID(path); resID_ = resMgr_.getResID(name);
} }
template<class ResMgrT> template<class ResMgrT>
void ResourceManaged<ResMgrT>::release() void ResourceManaged<ResMgrT>::release()
{ {
resMgr_.delRef(resID_); 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);
} }
} }

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // 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 #ifndef PHOTON_RESOURCEMANAGER_HPP
#define PHOTON_RESOURCEMANAGER_HPP #define PHOTON_RESOURCEMANAGER_HPP
@ -24,7 +24,7 @@ namespace photon
class Resource class Resource
{ {
public: public:
static const uint Invalid=0xffffffff; static const uint InvalidID=0xffffffff;
Resource() : Resource() :
refCount(0) refCount(0)
@ -32,6 +32,7 @@ public:
} }
uint refCount; uint refCount;
std::string name;
std::string path; std::string path;
}; };
@ -48,15 +49,15 @@ public:
virtual ~ResourceManager(); virtual ~ResourceManager();
uint getResID(const std::string& path); uint getResID(const std::string& name);
void delRef(uint id); void delRef(uint id);
void cleanUp(); void cleanUp();
private: 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; 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); void deleteResource(uint id);
private: private:
@ -76,28 +77,24 @@ ResourceManager<resT>::~ResourceManager()
} }
template<class resT> template<class resT>
uint ResourceManager<resT>::getResID(const std::string& path) uint ResourceManager<resT>::getResID(const std::string& name)
{ {
uint id(0); 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(); for(typename std::vector<resT>::iterator i=resVec_.begin();
i != resVec_.end(); i != resVec_.end() && i->name != name;
++i) ++i)
{ {
++id; // increment id ++id; // increment id
if(i->path == path)
{
return 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> template<class resT>
@ -123,20 +120,22 @@ void ResourceManager<resT>::cleanUp()
} }
template<class resT> 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; resT res;
res.name = name;
res.path = path; res.path = path;
try try
{ {
// attempt to load // attempt to load
loadResource(res,path); loadResource(res, name, path);
} }
catch(ResourceException&) catch(ResourceException&)
{ {
// rethrow any exceptions with specific information // 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 resVec_.push_back(res); // add resource to resVec & return

View File

@ -11,14 +11,16 @@
</node> </node>
<node ID="Freemind_Link_614612335" TEXT="all stable ZEngine/Nova features"> <node ID="Freemind_Link_614612335" TEXT="all stable ZEngine/Nova features">
<font NAME="SansSerif" SIZE="12"/> <font NAME="SansSerif" SIZE="12"/>
<node ID="Freemind_Link_107267630" TEXT="&quot;Engine&quot;"/> <node ID="Freemind_Link_1795651487" TEXT="&quot;ResourceManage&quot;ment">
<node FOLDED="true" ID="Freemind_Link_50716011" TEXT="Texture"> <icon BUILTIN="button_ok"/>
</node>
<node ID="Freemind_Link_50716011" TEXT="Texture">
<node ID="Freemind_Link_561271163" TEXT="Font"/> <node ID="Freemind_Link_561271163" TEXT="Font"/>
<node ID="Freemind_Link_385334177" TEXT="Image"/> <node ID="Freemind_Link_385334177" TEXT="Image"/>
</node> </node>
<node ID="Freemind_Link_1851655735" TEXT="Music"/> <node ID="Freemind_Link_1851655735" TEXT="Music"/>
<node ID="Freemind_Link_1045379727" TEXT="Sound"/> <node ID="Freemind_Link_1045379727" TEXT="Sound"/>
<node ID="Freemind_Link_1795651487" TEXT="ResourceManagers"/> <node ID="Freemind_Link_107267630" TEXT="&quot;Engine&quot;"/>
</node> </node>
<node ID="Freemind_Link_188779968" TEXT="Test Suite"> <node ID="Freemind_Link_188779968" TEXT="Test Suite">
<node ID="Freemind_Link_1570884553" TEXT="port test-suite to Saturn core"> <node ID="Freemind_Link_1570884553" TEXT="port test-suite to Saturn core">

View File

@ -1,10 +0,0 @@
$Id: todo.txt,v 1.4 2005/03/15 19:22:07 cozman Exp $
next:
be nova-photon-complete
month:
-be ZEngine-feature complete
quarter:
-experimental python bindings
-documentation