I have a command router that takes evaluates JSON data from the web into C/C++ application functions. I always like to try new things so I wanted to convert the router from C to C++ because I enjoy OOP. I was wondering if you can use std::map
to map strings to function calls.
Instead of doing this
enum myCommands {
cmdGetUserName,
cmdGetUserId
};
struct cmdRoutes cmdList[] = {
{"getUserName", cmdGetUserName},
{"getUserId", cmdGetUserId}
}
void processCmd(json jsonObject)
{
int cmd = getCmd(jsonObject.cmd, cmdList);
switch(cmd){
case cmdGetUserName:
case cmdGetUserId:
...etc
}
}
Can I instead use map to avoid all that?
std:map<string, AppStatus> CmdMap;
CmdMap["getUserName"] = MyClass.GetUserName;
// now simply..
CmdMap[jsonObject.cmd](...arguments...);