Resources Manager
Working 15h per day and updating a blog is not quite compatible. Anyway, the other day I saw tons of Resources manager code and I decided to play a bit with template to tidy things:
template <typename T> class ResourcesManager
{
public:
ResourcesManager() {}
virtual bool Init() = 0;
void DeInit(void(*UnloadImpl) (T*))
{
for (std::map<const char*, T*>::iterator it = m_ResourcesMap.begin(); it != m_ResourcesMap.end(); ++it)
{
UnloadImpl(it->second);
}
m_ResourcesMap.clear();
}
protected:
void Load(T*& pIn, const char * szName, void(*LoadImpl) (T*&, const char *))
{
std::map<const char*, T*>::iterator it = m_ResourcesMap.find(szName);
pIn = NULL;
if (it == m_ResourcesMap.end())
{
LoadImpl(pIn, szName);
if (pIn != NULL)
{
std::pair<const char*, T*> newPair(szName, pIn);
m_ResourcesMap.insert(newPair);
}
}
else
{
pIn = it->second;
}
}
std::map<const char*, T*> m_ResourcesMap;
};
template <typename T> class ResourcesManager
{
public:
ResourcesManager() {}
virtual bool Init() = 0;
void DeInit(void(*UnloadImpl) (T*))
{
for (std::map<const char*, T*>::iterator it = m_ResourcesMap.begin(); it != m_ResourcesMap.end(); ++it)
{
UnloadImpl(it->second);
}
m_ResourcesMap.clear();
}
protected:
void Load(T*& pIn, const char * szName, void(*LoadImpl) (T*&, const char *))
{
std::map<const char*, T*>::iterator it = m_ResourcesMap.find(szName);
pIn = NULL;
if (it == m_ResourcesMap.end())
{
LoadImpl(pIn, szName);
if (pIn != NULL)
{
std::pair<const char*, T*> newPair(szName, pIn);
m_ResourcesMap.insert(newPair);
}
}
else
{
pIn = it->second;
}
}
std::map<const char*, T*> m_ResourcesMap;
};
Implementing LoadImpl and UnloadImpl for your specific resources should be enough to use it.
Comentarios
Publicar un comentario