Here:
#include <stack>
#include <list>
#include <map>
#include <string>
using namespace std;
class IndexManager
{
private:
int Indices;
stack<int> UnusedIndexStack;
list<int> UsedIndexList;
list<int>::iterator UsedIndexListIterator;
public:
IndexManager(void)
{
}
IndexManager(int size)
{
Indices = size;
for(size+=1;size>0;size--){
UnusedIndexStack.push(size);
}
}
~IndexManager(void){
}
int Peek(void)
{
int ID;
if(UnusedIndexStack.size() == 1){
ID = Indices + 1;
} else {
ID = UnusedIndexStack.top();
}
return(ID);
}
int Pop(void)
{
int ID;
if(UnusedIndexStack.size() == 1){
ID = ++Indices;
} else {
ID = UnusedIndexStack.top();
UnusedIndexStack.pop();
}
UsedIndexList.push_back(ID);
return(ID);
}
void Push(int ID)
{
UnusedIndexStack.push(ID);
UsedIndexList.remove(ID);
}
int Total(void)
{
return(Indices);
}
int Unused(void)
{
return(UnusedIndexStack.size());
}
int Used(void)
{
return(UsedIndexList.size());
}
};
class ResourceManager
{
private:
IndexManager *Indexer;
map<string,int> NameMap;
map<string,int>::iterator NameMapIterator;
public:
ResourceManager(void){
}
ResourceManager(int size)
{
Indexer = new IndexManager(size);
}
~ResourceManager(void)
{
delete Indexer;
}
int Peek(void)
{
return(Indexer->Peek());
}
int Pop(void)
{
return(Indexer->Pop());
}
void Push(int ID)
{
Indexer->Push(ID);
}
int Add(string name)
{
if(!NameMap[name])
{
NameMap[name] = Indexer->Pop();
}
return(NameMap[name]);
}
void Remove(string name)
{
if(NameMap[name])
{
Indexer->Push(NameMap[name]);
NameMap.erase(name);
}
}
int Get(string name)
{
if(!NameMap[name]){return(-1);}
return(NameMap[name]);
}
void Set(string name, int ID)
{
if(!NameMap[name])
{
NameMap[name] = ID;
}
}
};
Put that code in "ResourceManager.h". Those 2 classes handle all management of ID's for any resource you want.
To use it put this at the top of your main code file after you include "ResourceManager.h"
ResourceManager *ImageResource = new ResourceManager(4096);
And to create a image do this:
//New Image
int IMAGENAME = ImageResource->pop();
dbLoadObject("$filename", IMAGENAME);
And to delete and image do this:
//Delete Image
dbDeleteObject(IMAGENAME);
ImageResource->push(IMAGEENAME);
"IMAGENAME" can be anything you want to call the image.
The classes can also tell you the used, unused, and total ID's in your program. It is a
very useful thing to have if you are making a large game.