"Richard Berg" <bi***@mail.com > wrote in message
news:ff******** *************** ***@posting.goo gle.com...
"Samuele Armondi" <sa************ ****@hotmail.co m> 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 FunctionArgumen ts
{
private:
int NumArgs;
ArgBase *Args; //This could be a std::vector rather than an array
const ArgTypes operator [] (const int) const;
public:
FunctionArgumen ts();
FunctionArgumen ts(int);
FunctionArgumen ts(const FunctionArgumen ts&); // throws
MemCopyError
~FunctionArgume nts();
FunctionArgumen ts& operator = (const FunctionArgumen ts&); // Need to
be defined because the class allocates memory
const int GetNumArgs() const;
// all the functions to set/retrieve the argument values
};
typedef bool (*FunctionPoint er) (const FunctionArgumen ts&);
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 ArgumentSequenc e;
FunctionPointer Function; // Pointer to the actual function
int NumArgs;
bool CheckArgSequenc e(std::string&) ;
public:
FunctionRecord( std::string, std::string, int, FunctionPointer );
//throws InvalidArgSeque nce exception
FunctionRecord( );
//This function executes the API call. It inderects FunctionPointer
and passes all the arguments to the actual API call
bool Do(const FunctionArgumen ts&) 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::s tring, 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::se cond.Do(Argumen ts) to execute the call.
HTH!!
S. Armondi