Base container class question
I made this container, and it seems to work, but I may be missing something.
I was wondering if you all could critique it, and let me know what you think.
All input is greatly appreciated.
Code:
#pragma once
#include "VariaMacros.h"
#include "cSingletonBase.h"
#include <map>
#include <list>
#include <string>
template <typename T>
class cMapBase : public cSingletonBase<cMapBase<T> >
{
EnableSingleton(cMapBase);
DisableClassCopying(cMapBase);
private:
cMapBase():m_CurIndex(0){};
virtual ~cMapBase(){Clear();};
mutable std::map<unsigned long, T*> m_Map;
mutable std::list<unsigned long> m_Indices;
mutable unsigned long m_CurIndex;
virtual bool _Free(T*& p_Element) const
{
if ( !p_Element )
return false;
else
{
delete p_Element;
p_Element = 0;
return true;
}
}
unsigned long _GetIndex() const
{
if ( m_Indices.empty() )
{
++m_CurIndex;
return m_CurIndex;
}
unsigned long Index = m_Indices.back();
m_Indices.pop_back();
return Index;
}
public:
/*
This should add a element, and return its index
- All things that are to be placed in this container have to have a copy constructor(unless unsafe_add is used)
*/
virtual unsigned long Add(const T& p_NewElement) const
{
T* temp = new T(p_NewElement);
unsigned long Index = _GetIndex();
if ( m_Map.find(Index) != m_Map.end() )
{
_Free(temp);
return 0;
}
else
m_Map[Index] = temp;
return Index;
}
/*
This should add an element, and return its index
- This is a unsafe function, and has such warning attached. This function can produce memory leaks if used
incorrectly.
*/
#ifndef CCONTAINER_NO_WARNINGS
virtual unsigned long UNSAFE(cMapBase<T>::Add(const T&))
Add(T* p_NewElement) const
#else
virtual unsigned long
Add(T* p_NewElement) const
#endif
{
unsigned long Index = _GetIndex();
if ( m_Map.find(Index) != m_Map.end() )
{
return 0;
}
else
m_Map[Index] = p_NewElement;
return Index;
}
/*
This should remove an element at the given index.
*/
virtual bool Remove(int p_Index) const
{
std::map<unsigned long, T*>::iterator find_iter = m_Map.find(p_Index);
if ( find_iter == m_Map.end() )
return false;
else
{
m_Indices.push_back(p_Index);
return _Free(m_Map[p_Index]);
}
}
/*
This should delete(free memory) of all elements.
*/
virtual void Clear() const
{
std::map<unsigned long, T*>::iterator free_iter = m_Map.begin();
for ( /**/; free_iter != m_Map.end(); ++free_iter )
{
_Free((*free_iter).second);
}
m_Indices.clear();
m_Map.clear();
m_CurIndex = 0;
}
/*
These should access the element at given index.
*/
virtual T* operator[](unsigned long p_Index) const
{
std::map<unsigned long, T*>::iterator find_iter = m_Map.find(p_Index);
try
{
if ( find_iter == m_Map.end() )
{
throw (std::string("Bad index provided. cContainerBase<")+std::string(typeid(T).name())+std::string(">::operator[](unsigned long)"));
}
}
catch(const std::string& str)
{
std::cout << "\n" << str << "\n";
return NULL;
}
return (*find_iter).second;
}
};
This is the cSingletonBase class as well
Code:
#pragma once
#include "VariaMacros.h"
template< class T >
class cSingletonBase
{
public:
static T* Create()
{
if ( m_Singleton )
{
++m_RefCount;
return m_Singleton;
}
m_RefCount = 1;
m_Singleton = new T;
return m_Singleton;
}
static bool Free()
{
if ( m_Singleton )
{
//Bad ref count check
if ( m_RefCount <= 0 )
_asm int 3
--m_RefCount;
if ( !m_RefCount )
{
delete m_Singleton; m_Singleton = 0;
return true;
}
return false;
}
return true;
}
protected:
cSingletonBase()
{ }
virtual ~cSingletonBase()
{ }
private:
DisableClassCopying(cSingletonBase);
static T* m_Singleton;
static unsigned int m_RefCount;
};
template< class T > T* cSingletonBase< T >::m_Singleton = 0;
template< class T > unsigned int cSingletonBase< T >::m_RefCount = 0;