I have a Engine class which has a member of type std::map, that maps enum to member functions. I'm able to map the enum to all the functions, if the number of arguments of the functions are same.
enum STATE { OFF, ON, HIBERNATE, RESTART };
enum RETCODE { SUCCESS, FAIL, UNKNOWN, OUTOFBOUND };
class Engine
{
Engine();
RETCODE StateOn(double val);
RETCODE StateOff(double val);
RETCODE StateHibernate(double val);
RETCODE StateRestart(double val);
private:
const std::map<STATE, std::function<RETCODE(double)>> Map_State_FnPtr;
};
Engine::Engine() : Map_State_FnPtr {
{ STATE::ON, [=](double val) {return StateOn(val); } },
{ STATE::OFF, [=](double val) {return StateOff(val); } },
{ STATE::HIBERNATE, [=](double val) {return StateHibernate(val); } },
{ STATE::RESTART, [=](double val) {return StateRestart(val); } }
}
{
// c'tor body
}
But I have a scenario where some of the functions can have zero or multiple arguments. How do I declare & construct the map variable in such scenario?
class Engine
{
Engine();
RETCODE StateOn(); // No arguments
RETCODE StateOff(double val); // 1 argument
RETCODE StateHibernate(double val, const std::string & data); // Multiple arguments
RETCODE StateRestart(double val);
private:
const std::unordered_map<STATE, std::function<RETCODE()>> Map_State_FnPtr; // What should be the declaration?
};
Any other suggestions for this scenario? Kindly help.
You can always ignore parameters
{
{ STATE::ON, [=](double, const std::string &) {return StateOn(); } },
{ STATE::OFF, [=](double val, const std::string &) {return StateOff(val); } },
{ STATE::HIBERNATE, [=](double val, const std::string & data) {return StateHibernate(val, data); } },
{ STATE::RESTART, [=](double val, const std::string &) {return StateRestart(val); } }
}