cpp_photon/include/ResourceManager.hpp

188 lines
4.3 KiB
C++
Raw Normal View History

2005-03-02 08:37:40 +00:00
//This file is part of Photon (http://photon.sourceforge.net)
//Copyright (C) 2004-2005 James Turk
//
// Author:
// James Turk (jpt2433@rit.edu)
//
// Version:
2005-07-04 03:06:48 +00:00
// $Id: ResourceManager.hpp,v 1.9 2005/07/04 03:06:48 cozman Exp $
2005-03-02 08:37:40 +00:00
#ifndef PHOTON_RESOURCEMANAGER_HPP
#define PHOTON_RESOURCEMANAGER_HPP
#include <map>
2005-03-02 08:37:40 +00:00
#include <string>
#include <boost/utility.hpp>
#include "types.hpp"
#include "exceptions.hpp"
namespace photon
{
class Resource
{
2005-07-04 03:06:48 +00:00
public:
Resource() :
refCount(0)
{ }
2005-03-02 08:37:40 +00:00
public:
uint refCount;
};
class ResourceDescriptor
{
public:
ResourceDescriptor() { }
ResourceDescriptor(const std::string& p) :
path(p)
{ }
2005-07-04 03:06:48 +00:00
public:
2005-03-02 08:37:40 +00:00
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 ResDescT_=ResourceDescriptor>
2005-03-02 08:37:40 +00:00
class ResourceManager : public boost::noncopyable
{
public:
typedef ResDescT_ ResDescT;
2005-03-02 08:37:40 +00:00
public:
ResourceManager();
virtual ~ResourceManager();
void newResource(const std::string& name, const ResDescT& path);
resT& getResource(const std::string& name);
2005-07-04 03:06:48 +00:00
void delRef(const std::string& name);
void cleanUp();
2005-03-02 08:37:40 +00:00
private:
virtual void loadResourceData(resT &res, const ResDescT& path)=0;
virtual void freeResourceData(resT &res)=0;
2005-03-02 08:37:40 +00:00
void deleteResource(const std::string& name);
2005-06-27 04:24:08 +00:00
public:
void printReport(std::ostream& os);
2005-03-02 08:37:40 +00:00
private:
typedef std::map<std::string,resT> MapT;
typedef typename MapT::iterator MapIterator;
MapT resourceMap_;
2005-03-02 08:37:40 +00:00
};
// implementation (damn you templor, cruel god of templates!)
template<class resT, class ResDescT>
ResourceManager<resT, ResDescT>::ResourceManager()
{ }
2005-03-02 08:37:40 +00:00
template<class resT, class ResDescT>
ResourceManager<resT, ResDescT>::~ResourceManager()
{ }
2005-03-02 08:37:40 +00:00
template<class resT, class ResDescT>
void ResourceManager<resT, ResDescT>::newResource(const std::string& name,
const ResDescT& desc)
2005-03-02 08:37:40 +00:00
{
resT resource;
2005-03-02 08:37:40 +00:00
try
{
// attempt to load
loadResourceData(resource, desc);
2005-03-02 08:37:40 +00:00
}
2005-06-13 05:38:06 +00:00
catch(ResourceException& e)
2005-03-02 08:37:40 +00:00
{
// rethrow any exceptions with specific information
2005-07-04 03:06:48 +00:00
throw ResourceException("Could not load " + desc.path + " as " + name +
2005-06-27 04:24:08 +00:00
": " + e.getDesc());
2005-03-02 08:37:40 +00:00
}
resourceMap_[name] = resource; // add the resource to resourceMap
2005-03-02 08:37:40 +00:00
}
template<class resT, class ResDescT>
resT& ResourceManager<resT, ResDescT>::getResource(const std::string& name)
2005-03-02 08:37:40 +00:00
{
MapIterator resource( resourceMap_.find(name) );
if(resource != resourceMap_.end())
{
2005-06-27 04:24:08 +00:00
// increment the refCount and return the resource for use
++resource->second.refCount;
return resource->second;
}
else
2005-03-02 08:37:40 +00:00
{
2005-06-27 04:24:08 +00:00
throw ResourceException("No resource named \"" + name + "\" exists.");
2005-03-02 08:37:40 +00:00
}
}
2005-03-02 08:37:40 +00:00
template<class resT, class ResDescT>
void ResourceManager<resT, ResDescT>::deleteResource(const std::string& name)
{
MapIterator resource( resourceMap_.find(name) );
// if the resource was found
if(resource != resourceMap_.end())
{
// free resource and remove it from the map
freeResourceData(resource->second);
resourceMap_.erase(name);
}
2005-03-02 08:37:40 +00:00
}
2005-07-04 03:06:48 +00:00
template<class resT, class ResDescT>
void ResourceManager<resT, ResDescT>::delRef(const std::string& name)
{
MapIterator resource( resourceMap_.find(name) );
// if the resource was found
if(resource != resourceMap_.end())
{
if(--resource->second.refCount <= 0)
{
deleteResource(name);
}
}
}
template<class resT, class ResDescT>
void ResourceManager<resT, ResDescT>::cleanUp()
{
// delete resources, until none are left
while(!resourceMap_.empty())
{
deleteResource(resourceMap_.begin()->first);
}
}
template<class resT, class ResDescT>
void ResourceManager<resT, ResDescT>::printReport(std::ostream& os)
2005-06-27 04:24:08 +00:00
{
MapIterator resource( resourceMap_.begin() );
for(MapIterator i = resourceMap_.begin(); i != resourceMap_.end(); ++i)
{
os << i->second.name << "\t" << i->second.path << "\t"
<< i->second.refCount << "\n";
}
}
2005-03-02 08:37:40 +00:00
}
#endif //PHOTON_RESOURCEMANAGER_HPP