From 963152f55dee7ca7ff589387782e8e2555cf1d11 Mon Sep 17 00:00:00 2001 From: James Turk Date: Fri, 10 Jun 2005 05:48:59 +0000 Subject: [PATCH] "ResourceManage"ment base rewritten --- include/ResourceManaged.hpp | 78 ++++++++++++++++++++++++++----------- include/ResourceManager.hpp | 37 +++++++++--------- photon.mm | 8 ++-- todo.txt | 10 ----- 4 files changed, 78 insertions(+), 55 deletions(-) delete mode 100644 todo.txt diff --git a/include/ResourceManaged.hpp b/include/ResourceManaged.hpp index 062ccda..caad403 100644 --- a/include/ResourceManaged.hpp +++ b/include/ResourceManaged.hpp @@ -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 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: // - ResourceManaged(const std::string& path); + ResourceManaged(const std::string& name); // Function: ~ResourceManaged // Destructor, calls . @@ -58,20 +57,40 @@ public: // Opens new resource via the associated . // // 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 -void ResourceManaged::cleanUp() -{ - resMgr_.cleanUp(); -} - template ResourceManaged::ResourceManaged() : - resID_(Resource::Invalid) + resID_(Resource::InvalidID) { } template -ResourceManaged::ResourceManaged(const std::string& path) +ResourceManaged::ResourceManaged(const std::string& name) { - open(path); } template @@ -110,7 +122,7 @@ ResourceManaged::operator=(const ResourceManaged &rhs) { if(this != &rhs) { - if(resID_ != Resource::Invalid) + if(resID_ != Resource::InvalidID) { release(); } @@ -120,16 +132,36 @@ ResourceManaged::operator=(const ResourceManaged &rhs) } template -void ResourceManaged::open(const std::string& path) +void ResourceManaged::open(const std::string& name) { release(); - resID_ = resMgr_.getResID(path); + resID_ = resMgr_.getResID(name); } template void ResourceManaged::release() { resMgr_.delRef(resID_); + resID_ = Resource::InvalidID; +} + +template +void ResourceManaged::cleanUp() +{ + resMgr_.cleanUp(); +} + +template +void ResourceManaged::addResource(const std::string& name, + const std::string& path) +{ + resMgr_.newResource(name,path); +} + +template +void ResourceManaged::addResource(const std::string& path) +{ + resMgr_.newResource(path,path); } } diff --git a/include/ResourceManager.hpp b/include/ResourceManager.hpp index 2b76342..4cbd2de 100644 --- a/include/ResourceManager.hpp +++ b/include/ResourceManager.hpp @@ -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::~ResourceManager() } template -uint ResourceManager::getResID(const std::string& path) +uint ResourceManager::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::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 @@ -123,20 +120,22 @@ void ResourceManager::cleanUp() } template -uint ResourceManager::newResource(const std::string& path) +uint ResourceManager::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 diff --git a/photon.mm b/photon.mm index d89caae..8abc86f 100644 --- a/photon.mm +++ b/photon.mm @@ -11,14 +11,16 @@ - - + + + + - + diff --git a/todo.txt b/todo.txt deleted file mode 100644 index 1a76ae4..0000000 --- a/todo.txt +++ /dev/null @@ -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 -