ResourceManaged/Manager import
This commit is contained in:
parent
1a7f0d4c28
commit
4d0168d08c
137
include/ResourceManaged.hpp
Normal file
137
include/ResourceManaged.hpp
Normal file
@ -0,0 +1,137 @@
|
|||||||
|
//This file is part of Photon (http://photon.sourceforge.net)
|
||||||
|
//Copyright (C) 2004-2005 James Turk
|
||||||
|
//
|
||||||
|
// Author:
|
||||||
|
// James Turk (jpt2433@rit.edu)
|
||||||
|
//
|
||||||
|
// Version:
|
||||||
|
// $Id: ResourceManaged.hpp,v 1.1 2005/03/02 08:37:40 cozman Exp $
|
||||||
|
|
||||||
|
#ifndef PHOTON_RESOURCEMANAGED_HPP
|
||||||
|
#define PHOTON_RESOURCEMANAGED_HPP
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "ResourceManager.hpp"
|
||||||
|
|
||||||
|
namespace photon
|
||||||
|
{
|
||||||
|
|
||||||
|
// Class: ResourceManaged
|
||||||
|
// Base template class, to be used as a base from which classes which have
|
||||||
|
// resources that can be controlled, such as textures and music, can be
|
||||||
|
// derived. Resource managed classes rely on a <ResourceManager>.
|
||||||
|
//
|
||||||
|
// Children:
|
||||||
|
// <Texture>, <Image>
|
||||||
|
//
|
||||||
|
// <Audio>, <Music>, <Sound>
|
||||||
|
template<class ResMgrT>
|
||||||
|
class ResourceManaged
|
||||||
|
{
|
||||||
|
// Group: (Con/De)structors
|
||||||
|
public:
|
||||||
|
// Function: ResourceManaged
|
||||||
|
// Default constructor.
|
||||||
|
ResourceManaged();
|
||||||
|
|
||||||
|
// Function: ResourceManaged
|
||||||
|
// 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.
|
||||||
|
//
|
||||||
|
// See Also:
|
||||||
|
// <open>
|
||||||
|
ResourceManaged(const std::string& path);
|
||||||
|
|
||||||
|
// Function: ~ResourceManaged
|
||||||
|
// Destructor, calls <release>.
|
||||||
|
virtual ~ResourceManaged();
|
||||||
|
|
||||||
|
ResourceManaged& operator=(const ResourceManaged &rhs);
|
||||||
|
|
||||||
|
// Group: General
|
||||||
|
public:
|
||||||
|
// Function: open
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
// 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();
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
static ResMgrT resMgr_;
|
||||||
|
uint resID_;
|
||||||
|
};
|
||||||
|
|
||||||
|
//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)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class ResMgrT>
|
||||||
|
ResourceManaged<ResMgrT>::ResourceManaged(const std::string& path)
|
||||||
|
{
|
||||||
|
open(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class ResMgrT>
|
||||||
|
ResourceManaged<ResMgrT>::~ResourceManaged()
|
||||||
|
{
|
||||||
|
release();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class ResMgrT>
|
||||||
|
ResourceManaged<ResMgrT>&
|
||||||
|
ResourceManaged<ResMgrT>::operator=(const ResourceManaged<ResMgrT> &rhs)
|
||||||
|
{
|
||||||
|
if(this != &rhs)
|
||||||
|
{
|
||||||
|
if(resID_ != Resource::Invalid)
|
||||||
|
{
|
||||||
|
release();
|
||||||
|
}
|
||||||
|
resID_ = rhs.resID_;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class ResMgrT>
|
||||||
|
void ResourceManaged<ResMgrT>::open(const std::string& path)
|
||||||
|
{
|
||||||
|
release();
|
||||||
|
resID_ = resMgr_.getResID(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class ResMgrT>
|
||||||
|
void ResourceManaged<ResMgrT>::release()
|
||||||
|
{
|
||||||
|
resMgr_.delRef(resID_);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //PHOTON_RESOURCEMANAGED_HPP
|
161
include/ResourceManager.hpp
Normal file
161
include/ResourceManager.hpp
Normal file
@ -0,0 +1,161 @@
|
|||||||
|
//This file is part of Photon (http://photon.sourceforge.net)
|
||||||
|
//Copyright (C) 2004-2005 James Turk
|
||||||
|
//
|
||||||
|
// Author:
|
||||||
|
// James Turk (jpt2433@rit.edu)
|
||||||
|
//
|
||||||
|
// Version:
|
||||||
|
// $Id: ResourceManager.hpp,v 1.1 2005/03/02 08:37:40 cozman Exp $
|
||||||
|
|
||||||
|
#ifndef PHOTON_RESOURCEMANAGER_HPP
|
||||||
|
#define PHOTON_RESOURCEMANAGER_HPP
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <boost/utility.hpp>
|
||||||
|
|
||||||
|
#include "types.hpp"
|
||||||
|
#include "exceptions.hpp"
|
||||||
|
|
||||||
|
namespace photon
|
||||||
|
{
|
||||||
|
|
||||||
|
class Resource
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static const uint Invalid=0xffffffff;
|
||||||
|
|
||||||
|
Resource() :
|
||||||
|
refCount(0)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
uint refCount;
|
||||||
|
std::string path;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Class: ResourceManager
|
||||||
|
// Templated base class for managing resources like textures and music.
|
||||||
|
//
|
||||||
|
// All ResourceManager work is done behind the scenes, it and all classes
|
||||||
|
// derived from it are therefore left without public documentation.
|
||||||
|
template<class resT>
|
||||||
|
class ResourceManager : public boost::noncopyable
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ResourceManager();
|
||||||
|
|
||||||
|
virtual ~ResourceManager();
|
||||||
|
|
||||||
|
uint getResID(const std::string& path);
|
||||||
|
void delRef(uint id);
|
||||||
|
void cleanUp();
|
||||||
|
|
||||||
|
private:
|
||||||
|
virtual void loadResource(resT &res, const std::string& path)=0;
|
||||||
|
virtual void freeResource(resT &res)=0;
|
||||||
|
|
||||||
|
uint newResource(const std::string& path);
|
||||||
|
void deleteResource(uint id);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<resT> resVec_;
|
||||||
|
};
|
||||||
|
|
||||||
|
// implementation (damn you templor, cruel god of templates!)
|
||||||
|
|
||||||
|
template<class resT>
|
||||||
|
ResourceManager<resT>::ResourceManager()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class resT>
|
||||||
|
ResourceManager<resT>::~ResourceManager()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class resT>
|
||||||
|
uint ResourceManager<resT>::getResID(const std::string& path)
|
||||||
|
{
|
||||||
|
uint id(0);
|
||||||
|
|
||||||
|
// loop through resources and find id
|
||||||
|
for(typename std::vector<resT>::iterator i=resVec_.begin();
|
||||||
|
i != resVec_.end();
|
||||||
|
++i)
|
||||||
|
{
|
||||||
|
++id; // increment id
|
||||||
|
if(i->path == path)
|
||||||
|
{
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(id == resVec_.size())
|
||||||
|
{
|
||||||
|
id = newResource(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return id; //not already in vector, add resource
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class resT>
|
||||||
|
void ResourceManager<resT>::delRef(uint id)
|
||||||
|
{
|
||||||
|
// if decremented count is <= 0, delete resource
|
||||||
|
if(id < resVec_.size() && --resVec_[id].refcount <= 0)
|
||||||
|
{
|
||||||
|
deleteResource(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class resT>
|
||||||
|
void ResourceManager<resT>::cleanUp()
|
||||||
|
{
|
||||||
|
// delete resources, until none are left
|
||||||
|
for(typename std::vector<resT>::iterator i=resVec_.begin();
|
||||||
|
i != resVec_.end();
|
||||||
|
++i)
|
||||||
|
{
|
||||||
|
freeResource(*i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class resT>
|
||||||
|
uint ResourceManager<resT>::newResource(const std::string& path)
|
||||||
|
{
|
||||||
|
resT res;
|
||||||
|
res.path = path;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// attempt to load
|
||||||
|
loadResource(res,path);
|
||||||
|
}
|
||||||
|
catch(ResourceException&)
|
||||||
|
{
|
||||||
|
// rethrow any exceptions with specific information
|
||||||
|
throw ResourceException("Could not load " + path);
|
||||||
|
}
|
||||||
|
|
||||||
|
resVec_.push_back(res); // add resource to resVec & return
|
||||||
|
return static_cast<uint>(resVec_.size()-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class resT>
|
||||||
|
void ResourceManager<resT>::deleteResource(uint id)
|
||||||
|
{
|
||||||
|
// check boundaries
|
||||||
|
if(id >= resVec_.size())
|
||||||
|
{
|
||||||
|
throw RangeException("Attempt to delete invalid resource.");
|
||||||
|
}
|
||||||
|
|
||||||
|
freeResource(resVec_[id]); // free the resource and erase it from the vec
|
||||||
|
resVec_.erase(resVec_.begin()+id);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //PHOTON_RESOURCEMANAGER_HPP
|
Loading…
Reference in New Issue
Block a user