I am using a C library that implements a command shell. Custom shell commands are registered by implementing a function with the following call signature:
typedef void(* shellcmd_t)(BaseSequentialStream *chp, int argc, char *argv[])
And then registering that function in a static structure which maps a string command name to that function pointer as follows:
static const ShellCommand commands[] = {
{"myfunction", myfunction},
{NULL, NULL}
}
Where myfunction looks like:
void myfunction(BaseSequentialStream *chp, int argc, char *argv[])
{
// do some stuff
}
I am using C++ and would like to implement some shell commands as class instance member functions (not static member functions). Is there a way to use anything in <functional>
so that I can get a compatible function pointer from a class instance member function and register it with the array of ShellCommand structs? I've been reading about std::bind and std::function and am sort of getting the impression you can give them a pointer to a C-style function, but you can't get a pointer to a C-style function from them so it doesn't seem like it will work, but maybe I'm just missing something.
If anybody has a good approach to solving this problem, I would love to hear about it. It seems like I might have to implement it as a flat C-function that can then call something that will give it a pointer/reference to the instance I want and then once I have that I can forward the call to that instances member function. It just seems kind of messy and was hoping there was a better way.