//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.9 2005/11/13 07:59:48 cozman Exp $ #ifndef PHOTON_RESOURCEMANAGED_HPP #define PHOTON_RESOURCEMANAGED_HPP #include #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 . // // Unless extending Photon, generally ResourceManaged does not need to be // directly used. For this reason public documentation is not availble. See // ResourceManaged.hpp for documentation. // // Children: // , // // template class ResourceManaged { // Nondoc-Group: (Con/De)structors public: // Nondoc-Function: ResourceManaged // Default constructor. ResourceManaged(); // Nondoc-Function: ResourceManaged // Initializing constructor, calls with a filename/zipname. // // Parameters: // name - name of resource // // See Also: // ResourceManaged(const std::string& name); // Nondoc-Function: ~ResourceManaged // Destructor, calls . virtual ~ResourceManaged(); ResourceManaged& operator=(const ResourceManaged &rhs); // Nondoc-Group: General public: // Nondoc-Function: open // Opens new resource via the associated . // // Parameters: // name - name of resource virtual void open(const std::string& name); // Nondoc-Function: release // Removes a reference to the resource, releasing if needed. // Generally called by destructor, so should rarely be called. virtual void release(); // Nondoc-Group: Accessors public: // Nondoc-Function: getName // Get the name associated with the resource. // // Returns: // Name of resource, or empty string if no resource is loaded. std::string getName() const; // Nondoc-Group: Resource Manager Access public: static ResMgrT resMgr_; // Nondoc-Function: cleanUp // Cleans up any unused resources of the type. // (Ex. Image::cleanUp() will unload all images.) static void cleanUp(); // Nondoc-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 typename ResMgrT::ResDescT& desc); // Nondoc-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 typename ResMgrT::ResDescT& desc); private: std::string resName_; }; //and he said "the implementation shall follow, as it is written" template ResourceManaged::ResourceManaged() { } template ResourceManaged::ResourceManaged(const std::string& name) : resName_(name) { } template ResourceManaged::~ResourceManaged() { release(); } template ResourceManaged& ResourceManaged::operator=(const ResourceManaged &rhs) { if(this != &rhs) { release(); resName_ = rhs.resName_; } return *this; } template void ResourceManaged::open(const std::string& name) { release(); resName_ = name; } template void ResourceManaged::release() { if(!resName_.empty()) // release is a no-op on an invalid resource { resMgr_.delRef(resName_); // decrement the refcount resName_.clear(); // empty string = invalid resource } } template std::string ResourceManaged::getName() const { return resName_; } template void ResourceManaged::cleanUp() { resMgr_.cleanUp(); } // define the resource manager static instance template T ResourceManaged::resMgr_; } #endif //PHOTON_RESOURCEMANAGED_HPP