"Richard Berg" <bi***@mail.com> wrote in message
news:ff**************************@posting.google.c om...
"Samuele Armondi" <sa****************@hotmail.com> wrote in message
news:3ffb36d7_2@mk-nntp- Do you see what I mean?
[SNIP] Thanks!
Ok, I'll go through how i did it...I didn't use polymorphism since I did not
need it, but the bare bones is here.
This is your abstract base class, from which all the argument types will
inherit.
class ArgBase
{
//whatever
}
This is the class that holds all the arguments for a function
class FunctionArguments
{
private:
int NumArgs;
ArgBase *Args; //This could be a std::vector rather than an array
const ArgTypes operator [] (const int) const;
public:
FunctionArguments();
FunctionArguments(int);
FunctionArguments(const FunctionArguments&); // throws
MemCopyError
~FunctionArguments();
FunctionArguments& operator = (const FunctionArguments&); // Need to
be defined because the class allocates memory
const int GetNumArgs() const;
// all the functions to set/retrieve the argument values
};
typedef bool (*FunctionPointer) (const FunctionArguments&);
This class holds the information about a function, such as its name, how
many arguments it takes, ecc. I used a string to represent the argument
sequence, for example a string "ii" meant that a function expected two
integer arguments, "fs" meant float and string, ecc...
class FunctionRecord
{
private:
std::string FunctionName;
std::string ArgumentSequence;
FunctionPointer Function; // Pointer to the actual function
int NumArgs;
bool CheckArgSequence(std::string&);
public:
FunctionRecord(std::string, std::string, int, FunctionPointer);
//throws InvalidArgSequence exception
FunctionRecord();
//This function executes the API call. It inderects FunctionPointer
and passes all the arguments to the actual API call
bool Do(const FunctionArguments&) const;
const std::string& GetArgSequence() const;
const std::string& GetFunctionName() const;
const int GetArgNum() const;
};
Then, you can hold everything in a map:
typedef std::map<std::string, FunctionRecord> FunctionMap;
I added most of the functions at compile time, but I did also make it
possible to add them at runtime. This code comes from the bare bones for an
in-game console I wrote for a friend, but it can very easily be adapted to
your needs.
Once you have the name of the function, you can look it up in the map and
call FunctionMap::second.Do(Arguments) to execute the call.
HTH!!
S. Armondi