I have a class called SmartMap. (how smart really depends I guess)
The way I originally made it was to use the type as the map element. This worked, but I need a different map for every type. So, I decided to make the map element a void* instead of the type itself, and just allow casting for the type.
This is the class so far. I know it sucks. That is why its here.
Code:
template<class key>
class SmartMap
{
public:
SmartMap(HWND hwnd = 0):Hwnd(hwnd)
{};
void* Push(const key& new_key, void* new_val = 0)
{
std::map<key, void*>::iterator finder = PtrMap.begin();
size_t loc = 0;
for (/**/; finder != PtrMap.end(); ++finder )
{
if ( (*finder).first == new_key )
{
std::stringstream ss1;
ss1 << "Key Found: " << new_key << "\nKey Location: " << loc\
<< "\n\nCannot add new element. Key already exists.";
MessageBoxA(Hwnd,ss1.str().c_str(),"SmartMap<Key,Type> Error.",MB_OK);
return 0;
}
++loc;
}
if ( new_val == 0 )
{
PtrMap[new_key] = new_val;
return PtrMap[new_key];
}
else
{
PtrMap[new_key] = new void*(new_val);
return PtrMap[new_key];
}
return 0;
};
private:
//The actual container
std::map<key, void*> PtrMap;
//The window for the messagebox calls
HWND Hwnd;
//Disable copying
SmartMap(const SmartMap&);
const SmartMap& operator=(const SmartMap&);
};
Here is the only implementation I could get to work. Which is really ugly.
Code:
SmartMap<char> test;
int* t = (int*)test.Push('a',(void*)1001);
std::cout << *t << "\n\n";
I am guessing that this is the only way to do this, but I am really not sure. Looking for input. Any help and I will be greatly appreciative.