Edit: Well, I guess I went halfway too far since the right definition is probably
MarauderIIC wrote:add parameters to the () operator or one of the constructors; this is just a simple example though.
Moderator: Coders of Rage
MarauderIIC wrote:add parameters to the () operator or one of the constructors; this is just a simple example though.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Code: Select all
#include "stdafx.h"
#include "iostream"
#include "string"
#include "vector"
using namespace std;
class ParameterType
{
public:
ParameterType()
{
isReturn = false;
}
string TypeName;
string Name;
bool isReturn;
};
class ParameterLink
{
public:
void * data;
ParameterLink * Next;
ParameterLink()
{
Next = NULL;
data = NULL;
}
};
class FunctionEntry
{
public:
string myLabel;
void * Addr;
std::vector<ParameterType*> Params;
void AddParam(ParameterType * Type)
{
Params.resize(Params.size()+1);
Params[Params.size()-1] = Type;
}
void AddIntParam(string Name)
{
ParameterType * myType = new ParameterType();
myType->Name = Name;
myType->TypeName = "int";
Params.resize(Params.size()+1);
Params[Params.size()-1] = myType;
}
void AddStringParam(string Name)
{
ParameterType * myType = new ParameterType();
myType->Name = Name;
myType->TypeName = "string";
Params.resize(Params.size()+1);
Params[Params.size()-1] = myType;
}
void AddRefPointer(string Name)
{
ParameterType * myType = new ParameterType();
myType->Name = Name;
myType->TypeName = "reference";
Params.resize(Params.size()+1);
Params[Params.size()-1] = myType;
}
};
class Character
{
public:
string Name;
string Type;
void ChangeName()
{
this->Name = "Jack";
}
};
class FunctionTable
{
public:
std::vector<FunctionEntry*> Functions;
void RegisterVoidFunction(void * Func, string Name)
{
FunctionEntry * myEntry = new FunctionEntry();
myEntry->myLabel = Name;
myEntry->Addr = Func;
Functions.resize(Functions.size()+1);
Functions[Functions.size()-1] = myEntry;
}
FunctionEntry * RegisterFunction(void * Func, string Name)
{
FunctionEntry * myEntry = new FunctionEntry();
myEntry->myLabel = Name;
myEntry->Addr = Func;
Functions.resize(Functions.size()+1);
Functions[Functions.size()-1] = myEntry;
return myEntry;
}
void * ExecuteVoidFunction(string Name)
{
for (int x = 0; x<Functions.size(); x++)
{
if (Functions[x]->myLabel == Name)
{
void * myAddr = Functions[x]->Addr;
void * myreturn = NULL;
_asm
{
call myAddr;
}
return myreturn;
}
}
}
void * ExecuteFunction(string Name, void * params)
{
ParameterLink * myLink = (ParameterLink*) params;
void * myReturn = NULL;
for (int x = 0; x<Functions.size(); x++)
{
if (Functions[x]->myLabel == Name)
{
void * myAddr = Functions[x]->Addr;
for (int r = Functions[x]->Params.size()-1; r>=0; r--)
{
ParameterType * myParam = Functions[x]->Params[r];
if (myParam->TypeName.compare("int") == 0)
{
int * myvalue = (int*) myLink->data;
int mydata = *myvalue;
_asm
{
mov eax, mydata
push eax
}
}
else if (myParam->TypeName.compare("string") == 0)
{
void * myVoid = myLink->data;
string * mydata = (string*) myVoid;
_asm
{
mov eax, mydata;
push eax;
}
}
else if (myParam->TypeName.compare("reference") == 0)
{
void * myVoid = myLink->data;
_asm
{
mov eax, myVoid;
push eax;
}
}
myLink = myLink->Next;
}
_asm
{
call myAddr;
mov myReturn, eax;
}
for (int r = 0; r<Functions[x]->Params.size(); r++)
{
ParameterType * myParam = Functions[x]->Params[r];
_asm
{
pop ebx;
}
}
return myReturn;
}
}
}
};
string * doSomething(int Test, int Test2, string * Data, Character * myChar)
{
cout << "Hello" << Test << Test2 << *Data;
cout << myChar->Name;
myChar->Name = "Sa22m";
return new string("Hello World");
}
int _tmain(int argc, _TCHAR* argv[])
{
FunctionTable * myTable = new FunctionTable();
myTable->RegisterVoidFunction(&doSomething, "DoSomething");
FunctionEntry * myFunc = myTable->RegisterFunction(&doSomething, "DoSomething2");
int mynumber = 10;
int mynumber2 = 20;
string * myData = new string("Test World");
Character * myChar = new Character();
myChar->Name = "John";
myChar->Type = "Bob";
myFunc->AddIntParam("Test");
myFunc->AddIntParam("Test2");
myFunc->AddStringParam("MyString");
myFunc->AddRefPointer("RefTest");
ParameterLink * myLink = new ParameterLink();
myLink->data = (void*) myData;
ParameterLink * myLinkMain = new ParameterLink();
myLinkMain->Next = myLink;
myLinkMain->data = myChar;
ParameterLink * myLink2 = new ParameterLink();
myLink2->data = (void*) &mynumber2;
myLink->Next = myLink2;
ParameterLink * myLink3 = new ParameterLink();
myLink3->data = &mynumber;
myLink2->Next = myLink3;
string myValue = *(string*)myTable->ExecuteFunction("DoSomething2", myLinkMain);
cout << myChar->Name;
cin.get();
return 0;
}