Okay, I came up with this. Not sure that it will work, but a little more sensible I think.
It is by no means complete, but figured before i do anything else I would see what you all thought.
Code:
#include <iostream>
#include <string>
#include <map>
#include <windows.h>
#include <sstream>
unsigned ElfHash( const std::string& key )
{
unsigned h = 0, g;
for (unsigned i = 0; i < key.length(); i++ )
{
h = ( h << 4 ) + key.at(i);
g = h & 0xf0000000L;
if ( g != 0 )
h ^= g >> 24;
h &= ~g;
}
return h;
}
class ScriptFunction
{
public:
ScriptFunction(const ScriptFunction& copy)
{
this->Param1ID = copy.Param1ID;
this->Param2ID = copy.Param2ID;
this->Param3ID = copy.Param3ID;
this->ReturnID = copy.ReturnID;
this->Function.Func = copy.Function.Func;
};
//Name in the script
std::string ScriptName;
//The id of the function(same as the index in function list)
UINT FunctionID;
enum ID
{
INT,
FLOAT,
STRING
};
ID ReturnID;
ID Param1ID;
ID Param2ID;
ID Param3ID;
struct func
{
union Var
{
UINT Int;
double Float;
char* String;
};
Var(*Func)(Var,Var,Var);
};
func Function;
};
class ScriptFunctionList
{
private:
std::map<UINT,ScriptFunction*> Functions;
public:
ScriptFunctionList()
{}
~ScriptFunctionList()
{}
//Pushes a function onto stack(copy), and returns ID
UINT PushFunction(ScriptFunction& new_function)
{
ScriptFunction* newFunc = 0;
newFunc = new ScriptFunction(new_function);
if ( newFunc )
{
//IDs are made by hashing the memory location of the ScriptFunction
//Copy new function the add
std::stringstream ss;
ss << &new_function;
UINT tID = ElfHash(ss.str());
newFunc->FunctionID = tID;
Functions[tID] = newFunc;
return tID;
}
else
{
MessageBoxA(0,"ScriptFunctionList Error.\r\nCould not allocate memory.","Error",MB_OK|MB_ICONERROR);
return 0;
}
}
//This will release all functions that were added
void Free()
{
std::map<UINT,ScriptFunction*>::iterator it = Functions.begin();
for ( ; it != Functions.end(); ++it )
{
delete (*it).second;
(*it).second = 0;
}
}
};
I am thinking that this will provide a much more solid function layout. It is the only way I could think to do this. The hash shouldn't replicate values, since the memory address for each function has to be different. Hopefully I am thinking right on this.
edit: modified the above code