The first was with just the textures. Once I had textures loading with the correct interface, I realised that I actually needed a templated resource counting class. So I implemented this instead and re-implemented the texture table using it. This core functionality is repeated so much that a templated class seemed the only way to go. I considered using inheritance but decided to keep this simple.
#pragma once#include #include #include #include #include #include #include "../Logging/Logging.h"/* Templated storage of a set of objects along with a modifiable usage count.*/template <class VALUE> class UseCountTable{ public: UseCountTable() { } ~UseCountTable() { } bool addMember(const std::string& name, int use_count, VALUE &object) {//return false if action cannot be performed std::mapint
if(it == objects.end())
{
objects[name]=std::pair<int,VALUE>(use_count,object);
return true;
}
else
{
return false;
}
}
bool updateMember(const std::string &name, int change)
{//return false if action cannot be performed.
std::mapint,VALUE> >::iterator it = objects.find(name);
if(it != objects.end())
{
it->second.first += change;
return true;
}
else
{
return false;
}
}
bool deleteMember(const std::string& name)
{//return false if action cannot be performed
std::mapint,VALUE> >::iterator it = objects.find(name);
if(it != objects.end())
{
objects.erase(it);
return true;
}
else
{
return false;
}
}
bool exists(const std::string& name)
{//return false if action cannot be performed
std::mapint,VALUE> >::iterator it = objects.find(name);
if(it != objects.end())
{
return true;
}
else
{
return false;
}
}
bool getMember(const std::string& name, int& use_count, VALUE& object)
{//return false if action cannot be performed
std::mapint,VALUE> >::iterator it = objects.find(name);
if(it != objects.end())
{
use_count = it->second.first;
object = it->second.second;
return true;
}
else
{
return false;
}
}
bool getUseCount(const std::string& name, int& use_count)
{//return false if action cannot be performed
std::mapint,VALUE> >::iterator it = objects.find(name);
if(it != objects.end())
{
use_count = it->second.first;
return true;
}
else
{
return false;
}
}
void consoleDump()
{
std::mapint,VALUE> >::iterator
it = objects.begin(),
end=objects.end();
for(it; it != end; ++it)
{
std::cout<<"NAME:" <first <<" USE COUNT:" <second.first< }
}
void getAllObjects(std::vector &table)
{
std::mapint,VALUE> >::iterator
it = objects.begin(),
end=objects.end();
for(it; it != end; ++it)
{
table.push_back(it->second);
}
}
private:
std::mapint,VALUE> > objects;
};
The other classes in the system will each be a wrapper for this; they will handle the shopping list they are given and load / update the resources under their control accordingly. I've dabbled with different ways of ensuring deletion of the contents, but after looking at one hack or another, decided to let the wrapper classes do it. any suggestions are welcome though. I've started a thread discussing this class and the unit test I did for it.