I'm setting up a console command that takes a variable number of arguments, each of which can be basic types (int, float, bool, string) and then pass them to a function that has 8 overloads for supporting a different number of arguments of varying types. How would I parse the command line string into values based on their type and then pass them to the function?
I can retrieve each argument via the function const char* GetArg(int index)
. Converting the char*
to the proper type isn't an issue so don't worry about that part. Storing the values and somehow passing to the template function is the part I'm stuck on.
For example, if the command was executed with the following string: "command 66 true 5 "string value" 0.56"
It would then be broken up into the following args and stored somehow:
int arg1 = GetArg(1); // 66
bool arg2 = GetArg(2); // true
int arg3 = GetArg(3); // 5
char* arg4 = GetArg(4); // "string value"
float arg5 = GetArg(5); // 0.56
And then based on the number of args, call the correct template function:
// The function definition looks something like this:
void SomeFunc();
template<typename T1>
void SomeFunc(const T1& arg1);
template<typename T1, typename T2>
void SomeFunc(const T1& arg1, const T2& arg2);
// etc...
// And then somehow it would be called. This is just an example. I don't
// know how to call it in a way that would work with variable number and
// type of args.
switch (argCount)
{
case 0:
SomeFunc();
break;
case 1:
SomeFunc(arg1);
break;
case 2:
SomeFunc(arg1, arg2);
break;
case 3:
SomeFunc(arg1, arg2, arg3);
break;
case 4:
SomeFunc(arg1, arg2, arg3, arg4);
break;
case 5:
SomeFunc(arg1, arg2, arg3, arg4, arg5);
break;
}
How would you make this possible? Storing the args in some way that can be passed to the template function so that it knows the type of each argument doesn't seem possible, but I feel like I'm just not thinking of something.
I can't change this interface either. This is a third party function that I just have to deal with. So no matter how it is implemented, eventually it has to go through SomeFunc()
.
IMPORTANT: I'm doing this in Visual Studio 2012 so I'm rather limited on newer C++ features. It can do a little bit of C++11 but that's it. Trying to upgrade the project to a newer version but for now that's what I have to deal with.