Thank you bubba for that insight. This is the actual code. the m_allocated variable was actually to prevent calling delete on a object that was never allocated in the first place.
Code:
/*
Author: Cody Doughty
Date: March 4, 2008
*/
#ifndef RESOURCE_MANAGER_H
#define RESOURCE_MANAGER_H
//For hgeResourceManager
#include <hgeresource.h>
//For STL map class
#include <map>
//For printf
#include <cstdio>
namespace Varia
{
class ResourceMgr
{
public:
ResourceMgr();
~ResourceMgr();
//This will only work if a Resource has not been allocated at the specified index
void AddResource(int p_index, const char* p_scriptFile);
//This will return NULL if no resource is allocated at the specified index
const hgeResourceManager* GetResource(int p_index);
//Return true if the resource is allocated at the specified index
bool IsAllocated(int p_index);
//This will only work if a Resource has been allocated at the specified index
void RemoveResource(int p_index);
private:
typedef struct m_ResourceStruct
{
bool m_allocated;
hgeResourceManager* m_res;
};
std::map<int, m_ResourceStruct> m_resources;
};
};
#endif
and the implementation
Code:
#include "ResourceManager.h"
Varia::ResourceMgr::ResourceMgr()
{
//nothing to construct
}
Varia::ResourceMgr::~ResourceMgr()
{
std::map<int, m_ResourceStruct>::iterator it;
for ( it = m_resources.begin(); it != m_resources.end(); it++ )
{
if ( (*it).second.m_allocated == true )
{
delete (*it).second.m_res;
(*it).second.m_res = NULL;
}
}
}
void Varia::ResourceMgr::AddResource(int p_index, const char* p_scriptFile)
{
//Check if the index exists
if ( m_resources.find(p_index) != m_resources.end() )
{
//Check if the index is allocated
if ( m_resources[p_index].m_allocated == true )
{
printf("Resource:%d already allocated.\n", p_index);
return;
}
}
//allocate
m_resources[p_index].m_res = new hgeResourceManager(p_scriptFile);
m_resources[p_index].m_allocated = true;
printf("Resource:%d allocated with Script:%s\n",p_index, p_scriptFile);
}
const hgeResourceManager* Varia::ResourceMgr::GetResource(int p_index)
{
//Check if the index exists
if ( m_resources.find(p_index) != m_resources.end() )
{
//Check if the index is allocated
if ( m_resources[p_index].m_allocated == true )
{
printf("Resource:%d accessed.\n", p_index);
return m_resources[p_index].m_res;
}
else
{
printf("Resource:%d not allocated.\n",p_index);
return NULL;
}
}
else
{
printf("Resource:%d does not exist.\n", p_index);
return NULL;
}
}
bool Varia::ResourceMgr::IsAllocated(int p_index)
{
//Check if the index exists
if ( m_resources.find(p_index) != m_resources.end() )
{
if ( m_resources[p_index].m_allocated == true )
{
printf("Resource:%d is allocated.\n",p_index);
return true;
}
else
{
printf("Resource:%d exists but not allocated.\n",p_index);
return false;
}
}
printf("Resource:%d does not exist.\n",p_index);
return false;
}
void Varia::ResourceMgr::RemoveResource(int p_index)
{
//Check if the index exists
if ( m_resources.find(p_index) != m_resources.end() )
{
//Check if the index is allocated
if ( m_resources[p_index].m_allocated == true )
{
delete m_resources[p_index].m_res;
m_resources[p_index].m_res = NULL;
m_resources[p_index].m_allocated = false;
printf("Resource:%d Freed.\n", p_index);
return;
}
else
{
printf("Resource:%d exists but not allocated.\n",p_index);
return;
}
}
else
{
printf("Resource:%d does not exist.\n", p_index);
return;
}
}
I don't really know if this does what I want it to, but I am hoping it does.
I know it doesn't make much sense to have a ResourceManager for a ResourceManager, but its the only way I could think of to make it work right.