So I thought I'd come back in here after having a tinker and give my outcomes.
#include <functional>
#include <vector>
#include <string>
#include <map>
template
int idType(void)
{
static const int id = typeid(varType).hash_code();
return id;
}
class Variable{
public:
template
Variable(const varType& _in) : type(idType()){
data = new varType(_in);
}
template
const varType& GetValue(void) const{
assert(type == idType());
return *(const varType*)data;
}
int GetType(void) const{
return type;
}
private:
void* data;
int type;
};
int destroyObject(Variable targetID){
if (targetID.GetType() == typeid(std::string).hash_code())
fprintf(stdout, "%s to be destroyed\n", targetID.GetValue().c_str());
else if (targetID.GetType() == typeid(int).hash_code())
fprintf(stdout, "%i to be destroyed\n", targetID.GetValue());
else
fprintf(stdout, "ERROR: EVENTMGT - destroyObject passed invalid variable\n");
return 0;
}
int createObject(Variable targetID){
//will be passed 2 values, both will be strings...
if (targetID.GetType() == typeid(std::string).hash_code())
fprintf(stdout, "%s to be created\n", targetID.GetValue().c_str());
else
fprintf(stdout, "ERROR: EVENTMGT - destroyObject passed invalid variable\n");
return 0;
}
int testUpdate(Variable value){
int newValue = 5;
return newValue;
}
class EVENTMANAGER{
std::map funcMap;
std::map actionMap;
//onCollide callAction doDestroy;
//function* funcArray[1];
public:
EVENTMANAGER(){
funcMap["doDestroy"] = destroyObject;
funcMap["doCreate"] = createObject;
funcMap["testUpdate"] = testUpdate;
actionMap["onCollision"] = "doDestroy";
}
void functionRun(std::string s){
std::string strings[2];
int targetId = 2;
Variable returnVal = funcMap.at("testUpdate")(targetId);
targetId = returnVal.GetValue();
std::string string = "this is a string";
Variable arg = string;
(funcMap.at(actionMap.at(s)))(arg); //could simply pass the string variable, rather than casgint a Variable type to a string.
//example of passing string to function, needs to be explicitly defined and passed as string.
//[Variable arg = "this is a string"] will assume that the variable type is char* and char* is not a valid type.
//std::string string = "this is a string";
//fprintf(stdout, "%s is set to arg\n", arg.GetValue().c_str());
//(funcMap.at(actionMap.at(s)))(string);
//example of passing float to function
//Variable argf = "1.0f";
//(funcMap.at(actionMap.at(s)))(argf)
}
};
As you outlined Juliean, the code you put up was somewhat non-functional, particularly had issues with the type identification in the first block.
Opted to try the typeid inbuilt function to use the hash code to track the variable type, though i've read there are some issues with this, particularly cross platform however its more of an issue using the name member from what i understand.
I've taken on the point of using <functional>, and toyed with the idea of using a Variable return type, for a Variable input.
I had thought about using STD::Vector though thought that this might be redundant, and when i played with it, there was a little bit more effort to pull the variable arguments out, but it completely worked, though its probably better using the vector vs passing a vector.
Its surprising how powerful this kind of templated class and function is, and from the research i've been doing, a lot of people simply say its not possible... which is really surprising that this is as simple as it is.
[edit] Borked formatting [/edit]