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-06-27 04:24:08 +00:00
|
|
|
// $Id: ResourceManager.hpp,v 1.7 2005/06/27 04:24:16 cozman Exp $
|
2005-03-02 08:37:40 +00:00
|
|
|
|
|
|
|
#ifndef PHOTON_RESOURCEMANAGER_HPP
|
|
|
|
#define PHOTON_RESOURCEMANAGER_HPP
|
|
|
|
|
2005-06-14 00:28:36 +00:00
|
|
|
#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
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
uint refCount;
|
2005-06-10 05:48:59 +00:00
|
|
|
std::string name;
|
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 ResourceManager : public boost::noncopyable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ResourceManager();
|
|
|
|
|
|
|
|
virtual ~ResourceManager();
|
|
|
|
|
2005-06-14 00:28:36 +00:00
|
|
|
void delRef(const std::string& name);
|
2005-03-02 08:37:40 +00:00
|
|
|
void cleanUp();
|
|
|
|
|
2005-06-14 00:28:36 +00:00
|
|
|
void newResource(const std::string& name, const std::string& path);
|
|
|
|
|
|
|
|
resT& getResource(const std::string& name);
|
2005-06-11 05:28:41 +00:00
|
|
|
|
2005-03-02 08:37:40 +00:00
|
|
|
private:
|
2005-06-10 07:06:06 +00:00
|
|
|
virtual void loadResource(resT &res, const std::string& path)=0;
|
2005-03-02 08:37:40 +00:00
|
|
|
virtual void freeResource(resT &res)=0;
|
|
|
|
|
2005-06-14 00:28:36 +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
|
|
|
|
2005-06-14 00:28:36 +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>
|
|
|
|
ResourceManager<resT>::ResourceManager()
|
2005-06-14 00:28:36 +00:00
|
|
|
{ }
|
2005-03-02 08:37:40 +00:00
|
|
|
|
|
|
|
template<class resT>
|
|
|
|
ResourceManager<resT>::~ResourceManager()
|
2005-06-14 00:28:36 +00:00
|
|
|
{ }
|
2005-03-02 08:37:40 +00:00
|
|
|
|
|
|
|
template<class resT>
|
2005-06-14 00:28:36 +00:00
|
|
|
void ResourceManager<resT>::delRef(const std::string& name)
|
2005-03-02 08:37:40 +00:00
|
|
|
{
|
2005-06-14 00:28:36 +00:00
|
|
|
MapIterator resource( resourceMap_.find(name) );
|
2005-03-02 08:37:40 +00:00
|
|
|
|
2005-06-14 00:28:36 +00:00
|
|
|
// if the resource was found
|
|
|
|
if(resource != resourceMap_.end())
|
2005-03-02 08:37:40 +00:00
|
|
|
{
|
2005-06-14 00:28:36 +00:00
|
|
|
if(--resource->second.refCount <= 0)
|
|
|
|
{
|
|
|
|
deleteResource(name);
|
|
|
|
}
|
2005-03-02 08:37:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class resT>
|
|
|
|
void ResourceManager<resT>::cleanUp()
|
|
|
|
{
|
|
|
|
// delete resources, until none are left
|
2005-06-14 00:28:36 +00:00
|
|
|
while(!resourceMap_.empty())
|
2005-03-02 08:37:40 +00:00
|
|
|
{
|
2005-06-14 00:28:36 +00:00
|
|
|
freeResource(resourceMap_.begin()->second);
|
2005-03-02 08:37:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class resT>
|
2005-06-14 00:28:36 +00:00
|
|
|
void ResourceManager<resT>::newResource(const std::string& name,
|
2005-06-10 05:48:59 +00:00
|
|
|
const std::string& path)
|
2005-03-02 08:37:40 +00:00
|
|
|
{
|
2005-06-14 00:28:36 +00:00
|
|
|
resT resource;
|
|
|
|
resource.name = name;
|
|
|
|
resource.path = path;
|
2005-03-02 08:37:40 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// attempt to load
|
2005-06-14 00:28:36 +00:00
|
|
|
loadResource(resource, path);
|
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-06-13 05:38:06 +00:00
|
|
|
throw ResourceException("Could not load " + path + " as " + name +
|
2005-06-27 04:24:08 +00:00
|
|
|
": " + e.getDesc());
|
2005-03-02 08:37:40 +00:00
|
|
|
}
|
|
|
|
|
2005-06-14 00:28:36 +00:00
|
|
|
resourceMap_[name] = resource; // add the resource to resourceMap
|
2005-03-02 08:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class resT>
|
2005-06-14 00:28:36 +00:00
|
|
|
resT& ResourceManager<resT>::getResource(const std::string& name)
|
2005-03-02 08:37:40 +00:00
|
|
|
{
|
2005-06-14 00:28:36 +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;
|
2005-06-14 00:28:36 +00:00
|
|
|
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-06-14 00:28:36 +00:00
|
|
|
}
|
2005-03-02 08:37:40 +00:00
|
|
|
|
2005-06-14 00:28:36 +00:00
|
|
|
template<class resT>
|
|
|
|
void ResourceManager<resT>::deleteResource(const std::string& name)
|
|
|
|
{
|
|
|
|
MapIterator resource( resourceMap_.find(name) );
|
|
|
|
|
|
|
|
// if the resource was found
|
|
|
|
if(resource != resourceMap_.end())
|
|
|
|
{
|
|
|
|
freeResource(resource->second); // free resource and remove it from the map
|
|
|
|
resourceMap_.erase(name);
|
|
|
|
}
|
2005-03-02 08:37:40 +00:00
|
|
|
}
|
|
|
|
|
2005-06-27 04:24:08 +00:00
|
|
|
template<class resT>
|
|
|
|
void ResourceManager<resT>::printReport(std::ostream& os)
|
|
|
|
{
|
|
|
|
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
|